Merge remote-tracking branch 'origin/master'

master
bynt 1 year ago
commit a8740d3a89

@ -2,6 +2,7 @@ package com.baiye.service.impl;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil; import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse; import cn.hutool.http.HttpResponse;
@ -45,7 +46,8 @@ public class ProxyServiceImpl implements ProxyService {
private final ProxyMapper proxyMapper; private final ProxyMapper proxyMapper;
private final ProxyAccountMapper proxyAccountMapper; 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 @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
@ -55,54 +57,60 @@ public class ProxyServiceImpl implements ProxyService {
if (!MobileUtil.checkPhone(phone)) throw new BadRequestException("请输入正确手机号!!!"); if (!MobileUtil.checkPhone(phone)) throw new BadRequestException("请输入正确手机号!!!");
// 2、有绑定关系直接返回 // 2、有绑定关系直接返回
ProxyAccountEntity entity = proxyAccountMapper.selectOne(new LambdaQueryWrapper<ProxyAccountEntity>().eq(ProxyAccountEntity::getNid, phone)); ProxyAccountEntity entity = proxyAccountMapper.selectOne(new LambdaQueryWrapper<ProxyAccountEntity>().eq(ProxyAccountEntity::getNid, phone));
if (entity != null && entity.getId() != null) { if (ObjectUtil.isNull(entity)) {
return proxyMapper.selectById(entity.getProxyId()); 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)); // List<ProxyEntity> proxyList = proxyMapper.selectList(new LambdaQueryWrapper<ProxyEntity>().eq(ProxyEntity::getStatus, 0));
if (CollUtil.isNotEmpty(proxyList)) { // if (CollUtil.isNotEmpty(proxyList)) {
//3、获取一个可用代理地址 // //3、获取一个可用代理地址
ProxyEntity proxyEntity = proxyList.get(RandomUtil.randomInt(0, proxyList.size())); // ProxyEntity proxyEntity = proxyList.get(RandomUtil.randomInt(0, proxyList.size()));
try { // try {
String account = proxyEntity.getAccount(); // String account = proxyEntity.getAccount();
String password = proxyEntity.getPassword(); // String password = proxyEntity.getPassword();
/** // /**
* {@link JDKConfig} // * 代理配置 以下配置要在项目启动之前{@link JDKConfig}配置
* System.setProperty("jdk.http.auth.tunneling.disabledSchemes", ""); // * System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
*/ // */
Authenticator.setDefault( // Authenticator.setDefault(
new Authenticator() { // new Authenticator() {
@Override // @Override
public PasswordAuthentication getPasswordAuthentication() { // public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(account, password.toCharArray()); // return new PasswordAuthentication(account, password.toCharArray());
} // }
} // }
); // );
HttpResponse execute = HttpRequest.get(QUILT_PROXY_URL) // HttpResponse execute = HttpRequest.get(QUILT_PROXY_URL)
.setHttpProxy(proxyEntity.getIp(), proxyEntity.getPort()) // .setHttpProxy(proxyEntity.getIp(), proxyEntity.getPort())
.basicProxyAuth(account, password) // .basicProxyAuth(account, password)
.execute(); // .timeout(2_000)
if (execute.getStatus() != 200) { // .execute();
log.info("代理请求失败: {}", proxyEntity.getIp()); // if (execute.getStatus() != 200) {
proxyEntity = askProxy(proxyList, proxyEntity); // log.info("代理请求失败: {}", proxyEntity.getIp());
} // proxyEntity = askProxy(proxyList, proxyEntity);
} catch (Exception e) { // }
log.error("无效的代理地址: {}, 错误信息:{}", proxyEntity.getIp(), e.getMessage()); // } catch (Exception e) {
proxyEntity = askProxy(proxyList, proxyEntity); // log.error("无效的代理地址: {}, 错误信息:{}", proxyEntity.getIp(), e.getMessage());
} // proxyEntity = askProxy(proxyList, proxyEntity);
// }
//4、添加代理地址-手机号映射关系 //
if (proxyEntity.getId() != null) { // //4、添加代理地址-手机号映射关系
ProxyAccountEntity proxyAccountEntity = new ProxyAccountEntity(); // if (proxyEntity.getId() != null) {
proxyAccountEntity.setNid(phone); // ProxyAccountEntity proxyAccountEntity = new ProxyAccountEntity();
proxyAccountEntity.setProxyId(proxyEntity.getId()); // proxyAccountEntity.setNid(phone);
proxyAccountMapper.insert(proxyAccountEntity); // proxyAccountEntity.setProxyId(proxyEntity.getId());
return proxyEntity; // proxyAccountMapper.insert(proxyAccountEntity);
} else { // return proxyEntity;
throw new BadRequestException("无可用代理,请联系管理员!!!"); // } else {
} // throw new BadRequestException("无可用代理,请联系管理员!!!");
} // }
throw new BadRequestException("无可用代理,请联系管理员!!!"); // }
// throw new BadRequestException("无可用代理,请联系管理员!!!");
} }
@Override @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) HttpResponse execute = HttpRequest.get(QUILT_PROXY_URL)
.setHttpProxy(entity.getIp(), entity.getPort()) .setHttpProxy(entity.getIp(), entity.getPort())
.timeout(2_000)
.basicProxyAuth(entity.getAccount(), entity.getPassword()) .basicProxyAuth(entity.getAccount(), entity.getPassword())
.execute(); .execute();
if (execute.getStatus() != 200) log.info("无效的代理地址: {}", entity.getIp()); if (execute.getStatus() != 200) log.info("无效的代理地址: {}", entity.getIp());

Loading…
Cancel
Save