|
|
|
@ -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<ProxyAccountEntity>().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<ProxyEntity> proxyList = proxyMapper.selectList(new LambdaQueryWrapper<ProxyEntity>().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<ProxyEntity> proxyList = proxyMapper.selectList(new LambdaQueryWrapper<ProxyEntity>().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());
|
|
|
|
|