From 8ad7d5f857119ffd2775b40f9d9df4784093b183 Mon Sep 17 00:00:00 2001 From: yqy Date: Tue, 18 Apr 2023 16:39:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=8E=B7=E5=8F=96=E4=BB=A3?= =?UTF-8?q?=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../baiye/service/impl/ProxyServiceImpl.java | 141 ++++++++++++------ 1 file changed, 94 insertions(+), 47 deletions(-) diff --git a/cdp-tools/cdp-tool-xhs/cdp-tool-xhs-service/src/main/java/com/baiye/service/impl/ProxyServiceImpl.java b/cdp-tools/cdp-tool-xhs/cdp-tool-xhs-service/src/main/java/com/baiye/service/impl/ProxyServiceImpl.java index 1ff35a1..78ed4e5 100644 --- a/cdp-tools/cdp-tool-xhs/cdp-tool-xhs-service/src/main/java/com/baiye/service/impl/ProxyServiceImpl.java +++ b/cdp-tools/cdp-tool-xhs/cdp-tool-xhs-service/src/main/java/com/baiye/service/impl/ProxyServiceImpl.java @@ -2,6 +2,7 @@ package com.baiye.service.impl; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.io.FileUtil; +import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.RandomUtil; import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpResponse; @@ -45,7 +46,8 @@ public class ProxyServiceImpl implements ProxyService { private final ProxyMapper proxyMapper; private final ProxyAccountMapper proxyAccountMapper; - private final String QUILT_PROXY_URL = "https://tool.lu/ip"; + private final String QUILT_PROXY_URL = "https://www.baidu.com"; +// private final String QUILT_PROXY_URL = "https://tool.lu/ip"; @Override @Transactional(rollbackFor = Exception.class) @@ -55,54 +57,60 @@ public class ProxyServiceImpl implements ProxyService { if (!MobileUtil.checkPhone(phone)) throw new BadRequestException("请输入正确手机号!!!"); // 2、有绑定关系直接返回 ProxyAccountEntity entity = proxyAccountMapper.selectOne(new LambdaQueryWrapper().eq(ProxyAccountEntity::getNid, phone)); - if (entity != null && entity.getId() != null) { - return proxyMapper.selectById(entity.getProxyId()); + if (ObjectUtil.isNull(entity)) { + throw new BadRequestException("未绑定代理Ip,请绑定ip后重试"); } + ProxyEntity proxyEntity = proxyMapper.selectById(entity.getProxyId()); + return verifyProxy(proxyEntity); +// if (entity != null && entity.getId() != null) { +// return proxyMapper.selectById(entity.getProxyId()); +// } - List proxyList = proxyMapper.selectList(new LambdaQueryWrapper().eq(ProxyEntity::getStatus, 0)); - if (CollUtil.isNotEmpty(proxyList)) { - //3、获取一个可用代理地址 - ProxyEntity proxyEntity = proxyList.get(RandomUtil.randomInt(0, proxyList.size())); - try { - String account = proxyEntity.getAccount(); - String password = proxyEntity.getPassword(); - /** - * 代理配置 以下配置要在项目启动之前{@link JDKConfig}配置 - * System.setProperty("jdk.http.auth.tunneling.disabledSchemes", ""); - */ - Authenticator.setDefault( - new Authenticator() { - @Override - public PasswordAuthentication getPasswordAuthentication() { - return new PasswordAuthentication(account, password.toCharArray()); - } - } - ); - HttpResponse execute = HttpRequest.get(QUILT_PROXY_URL) - .setHttpProxy(proxyEntity.getIp(), proxyEntity.getPort()) - .basicProxyAuth(account, password) - .execute(); - if (execute.getStatus() != 200) { - log.info("代理请求失败: {}", proxyEntity.getIp()); - proxyEntity = askProxy(proxyList, proxyEntity); - } - } catch (Exception e) { - log.error("无效的代理地址: {}, 错误信息:{}", proxyEntity.getIp(), e.getMessage()); - proxyEntity = askProxy(proxyList, proxyEntity); - } - - //4、添加代理地址-手机号映射关系 - if (proxyEntity.getId() != null) { - ProxyAccountEntity proxyAccountEntity = new ProxyAccountEntity(); - proxyAccountEntity.setNid(phone); - proxyAccountEntity.setProxyId(proxyEntity.getId()); - proxyAccountMapper.insert(proxyAccountEntity); - return proxyEntity; - } else { - throw new BadRequestException("无可用代理,请联系管理员!!!"); - } - } - throw new BadRequestException("无可用代理,请联系管理员!!!"); +// List proxyList = proxyMapper.selectList(new LambdaQueryWrapper().eq(ProxyEntity::getStatus, 0)); +// if (CollUtil.isNotEmpty(proxyList)) { +// //3、获取一个可用代理地址 +// ProxyEntity proxyEntity = proxyList.get(RandomUtil.randomInt(0, proxyList.size())); +// try { +// String account = proxyEntity.getAccount(); +// String password = proxyEntity.getPassword(); +// /** +// * 代理配置 以下配置要在项目启动之前{@link JDKConfig}配置 +// * System.setProperty("jdk.http.auth.tunneling.disabledSchemes", ""); +// */ +// Authenticator.setDefault( +// new Authenticator() { +// @Override +// public PasswordAuthentication getPasswordAuthentication() { +// return new PasswordAuthentication(account, password.toCharArray()); +// } +// } +// ); +// HttpResponse execute = HttpRequest.get(QUILT_PROXY_URL) +// .setHttpProxy(proxyEntity.getIp(), proxyEntity.getPort()) +// .basicProxyAuth(account, password) +// .timeout(2_000) +// .execute(); +// if (execute.getStatus() != 200) { +// log.info("代理请求失败: {}", proxyEntity.getIp()); +// proxyEntity = askProxy(proxyList, proxyEntity); +// } +// } catch (Exception e) { +// log.error("无效的代理地址: {}, 错误信息:{}", proxyEntity.getIp(), e.getMessage()); +// proxyEntity = askProxy(proxyList, proxyEntity); +// } +// +// //4、添加代理地址-手机号映射关系 +// if (proxyEntity.getId() != null) { +// ProxyAccountEntity proxyAccountEntity = new ProxyAccountEntity(); +// proxyAccountEntity.setNid(phone); +// proxyAccountEntity.setProxyId(proxyEntity.getId()); +// proxyAccountMapper.insert(proxyAccountEntity); +// return proxyEntity; +// } else { +// throw new BadRequestException("无可用代理,请联系管理员!!!"); +// } +// } +// throw new BadRequestException("无可用代理,请联系管理员!!!"); } @Override @@ -254,6 +262,44 @@ public class ProxyServiceImpl implements ProxyService { } } + /** + * 验证代理ip是否可用 + * + * @param proxyEntity + * @return + */ + private ProxyEntity verifyProxy(ProxyEntity proxyEntity) { + try { + String account = proxyEntity.getAccount(); + String password = proxyEntity.getPassword(); + /** + * 代理配置 以下配置要在项目启动之前{@link JDKConfig}配置 + * System.setProperty("jdk.http.auth.tunneling.disabledSchemes", ""); + */ + Authenticator.setDefault( + new Authenticator() { + @Override + public PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(account, password.toCharArray()); + } + } + ); + HttpResponse execute = HttpRequest.get(QUILT_PROXY_URL) + .setHttpProxy(proxyEntity.getIp(), proxyEntity.getPort()) + .basicProxyAuth(account, password) + .timeout(2_000) + .execute(); + if (execute.getStatus() != 200) { + log.info("代理请求失败: {}", proxyEntity.getIp()); + throw new BadRequestException("代理Ip不可用,请重新绑定"); + } + } catch (Exception e) { + log.error("无效的代理地址: {}, 错误信息:{}", proxyEntity.getIp(), e.getMessage()); + throw new BadRequestException("代理Ip不可用,请重新绑定"); + } + return proxyEntity; + } + /** * 随机代理失败后,循环获取可用代理 */ @@ -272,6 +318,7 @@ public class ProxyServiceImpl implements ProxyService { ); HttpResponse execute = HttpRequest.get(QUILT_PROXY_URL) .setHttpProxy(entity.getIp(), entity.getPort()) + .timeout(2_000) .basicProxyAuth(entity.getAccount(), entity.getPassword()) .execute(); if (execute.getStatus() != 200) log.info("无效的代理地址: {}", entity.getIp());