From d19eedd330387d5f8217186a19fceabe3f65bedc Mon Sep 17 00:00:00 2001 From: fanyuhui <852221040@qq.com> Date: Mon, 1 Apr 2024 19:00:38 +0800 Subject: [PATCH] update web and bug fix --- common/pom.xml | 8 + .../proxyip/select/common/bean/ProxyIp.java | 60 +----- .../select/common/service/IApiService.java | 10 + .../common/service/impl/ApiServiceImpl.java | 53 +++-- foreign-server/resources/table.sql | 12 +- .../bean/params/AddDnsRecordsBatchParams.java | 11 +- .../bean/params/AddProxyIpToDbParams.java | 19 ++ .../bean/params/AddSingleDnsRecordParams.java | 11 +- .../select/bean/params/ProxyIpPageParams.java | 45 +---- .../select/bean/params/RmFromDbParams.java | 11 +- .../bean/params/RmSingleDnsRecordParams.java | 11 +- .../params/RmSingleIpDnsRecordParams.java | 17 ++ .../select/business/IProxyIpBusiness.java | 8 +- .../business/impl/DnsRecordBusinessImpl.java | 36 ++-- .../business/impl/ProxyIpBusinessImpl.java | 70 ++++++- .../select/config/LocalDateTimeHandler.java | 67 +++++++ .../select/controller/ProxyIpController.java | 14 +- .../src/main/resources/static/proxyIp.html | 188 +++++++++++++++--- foreign-server/src/test/java/DbTest.java | 10 +- 19 files changed, 448 insertions(+), 213 deletions(-) create mode 100644 foreign-server/src/main/java/com/proxyip/select/bean/params/AddProxyIpToDbParams.java create mode 100644 foreign-server/src/main/java/com/proxyip/select/bean/params/RmSingleIpDnsRecordParams.java create mode 100644 foreign-server/src/main/java/com/proxyip/select/config/LocalDateTimeHandler.java diff --git a/common/pom.xml b/common/pom.xml index 5e89cea..854edd4 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -22,6 +22,14 @@ org.springframework.boot spring-boot-starter-web + + org.slf4j + slf4j-api + + + org.projectlombok + lombok + diff --git a/common/src/main/java/com/proxyip/select/common/bean/ProxyIp.java b/common/src/main/java/com/proxyip/select/common/bean/ProxyIp.java index baadf83..65d470e 100644 --- a/common/src/main/java/com/proxyip/select/common/bean/ProxyIp.java +++ b/common/src/main/java/com/proxyip/select/common/bean/ProxyIp.java @@ -1,12 +1,17 @@ package com.proxyip.select.common.bean; +import com.fasterxml.jackson.annotation.JsonFormat; +import lombok.Data; + import java.io.Serializable; +import java.time.LocalDateTime; /** * 代理ip表 * * @author Yohann */ +@Data public class ProxyIp implements Serializable { private String id; @@ -16,57 +21,8 @@ public class ProxyIp implements Serializable { private Integer pingValue; - private static final long serialVersionUID = 1L; - - @Override - public String toString() { - return "ProxyIp{" + - "id='" + id + '\'' + - ", country='" + country + '\'' + - ", ip='" + ip + '\'' + - ", pingValue=" + pingValue + - '}'; - } - - public ProxyIp() { - } - - public ProxyIp(String id, String country, String ip, Integer pingValue) { - this.id = id; - this.country = country; - this.ip = ip; - this.pingValue = pingValue; - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8") + private LocalDateTime createTime; - public String getCountry() { - return country; - } - - public void setCountry(String country) { - this.country = country; - } - - public String getIp() { - return ip; - } - - public void setIp(String ip) { - this.ip = ip; - } - - public Integer getPingValue() { - return pingValue; - } - - public void setPingValue(Integer pingValue) { - this.pingValue = pingValue; - } + private static final long serialVersionUID = 1L; } \ No newline at end of file diff --git a/common/src/main/java/com/proxyip/select/common/service/IApiService.java b/common/src/main/java/com/proxyip/select/common/service/IApiService.java index daba15a..9886d4c 100644 --- a/common/src/main/java/com/proxyip/select/common/service/IApiService.java +++ b/common/src/main/java/com/proxyip/select/common/service/IApiService.java @@ -64,4 +64,14 @@ public interface IApiService { * @param apiToken apiToken */ void removeCfDnsRecords(List proxyDomainList, String zoneId, String apiToken); + + /** + * 清除cf指定代理域名的某个ip地址的dns记录 + * + * @param proxyDomain 代理域名 + * @param ip ip地址 + * @param zoneId zoneId + * @param apiToken apiToken + */ + void removeCfSingleDnsRecords(String proxyDomain, String ip, String zoneId, String apiToken); } diff --git a/common/src/main/java/com/proxyip/select/common/service/impl/ApiServiceImpl.java b/common/src/main/java/com/proxyip/select/common/service/impl/ApiServiceImpl.java index cc41cce..1204185 100644 --- a/common/src/main/java/com/proxyip/select/common/service/impl/ApiServiceImpl.java +++ b/common/src/main/java/com/proxyip/select/common/service/impl/ApiServiceImpl.java @@ -2,6 +2,7 @@ import com.proxyip.select.common.exception.BusinessException; import com.proxyip.select.common.service.IApiService; +import lombok.extern.slf4j.Slf4j; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; @@ -23,6 +24,7 @@ * @since 2024/3/29 13:57 */ @Service +@Slf4j public class ApiServiceImpl implements IApiService { /** @@ -44,6 +46,15 @@ public class ApiServiceImpl implements IApiService { " -H \"Authorization: Bearer %s\" " + " -H \"Content-Type: application/json\""; + /** + * cf删除指定代理域名的某个ip地址的dns记录api + */ + private static String CF_REMOVE_SINGLE_DNS_RECORDS_API = "curl -X GET \"https://api.cloudflare.com/client/v4/zones/%s/dns_records?type=A&name=%s\" " + + "-H \"Authorization: Bearer %s\" " + + "-H \"Content-Type: application/json\" | jq -c '.result[] | select(.content == \"%s\") | .id' | xargs -n 1 -I {} curl -X DELETE \"https://api.cloudflare.com/client/v4/zones/%s/dns_records/{}\" " + + "-H \"Authorization: Bearer %s\" " + + "-H \"Content-Type: application/json\""; + /** * 获取ip归属国家api */ @@ -67,7 +78,7 @@ public void uploadFileToNetDisc(String filePath, String apiAddress) { String line; while ((line = reader.readLine()) != null) { if (line.contains("\"code\":1")) { - System.out.println("√√√ 文件:" + filePath + " 已发送至个人网盘 √√√"); + log.info("√√√ 文件:{} 已发送至个人网盘 √√√", filePath); break; } } @@ -99,7 +110,7 @@ public List resolveDomain(List domainList, String dnsServer) { // Wait for the process to finish int exitCode = process.waitFor(); if (exitCode != 0) { - System.out.println("Error executing nslookup command"); + log.error("Error executing nslookup command"); // System.exit(1); } } catch (Exception e) { @@ -175,19 +186,19 @@ public void addCfDnsRecords(String domainPrefix, String ipAddress, String zoneId // try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) { // String line; -// System.out.println("add ------------------" + domainPrefix + "-------------------"); +// log.info("add ------------------" + domainPrefix + "-------------------"); // while ((line = reader.readLine()) != null) { -// System.out.println(line); +// log.info(line); // } -// System.out.println("add ------------------" + domainPrefix + "-------------------"); +// log.info("add ------------------" + domainPrefix + "-------------------"); // } // Wait for the process to finish int exitCode = process.waitFor(); if (exitCode != 0) { - System.out.println("Error executing addCfDnsRecords task"); + log.error("Error executing addCfDnsRecords task"); } -// System.out.println("域名:" + domainPrefix + "." + dnsCfg.getRootDomain() + " 的dns记录添加成功!ip地址:" + ipAddress); +// log.info("域名:" + domainPrefix + "." + dnsCfg.getRootDomain() + " 的dns记录添加成功!ip地址:" + ipAddress); } catch (Exception e) { e.printStackTrace(); throw new BusinessException(-1, "添加cf记录失败:" + e.getLocalizedMessage()); @@ -204,15 +215,15 @@ public void removeCfDnsRecords(List proxyDomainList, String zoneId, Stri // Wait for the process to finish int exitCode = process.waitFor(); if (exitCode != 0) { - System.out.println("Error executing addCfDnsRecords task"); + log.error("Error executing removeCfDnsRecords task"); } // try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) { // String line; -// System.out.println("rm ------------------" + proxyDomain + "-------------------"); +// log.info("rm ------------------" + proxyDomain + "-------------------"); // while ((line = reader.readLine()) != null) { -// System.out.println(line); +// log.info(line); // } -// System.out.println("rm ------------------" + proxyDomain + "-------------------"); +// log.info("rm ------------------" + proxyDomain + "-------------------"); // } catch (Exception e) { // e.printStackTrace(); // } @@ -220,7 +231,25 @@ public void removeCfDnsRecords(List proxyDomainList, String zoneId, Stri e.printStackTrace(); throw new BusinessException(-1, "清除DNS记录失败:" + e.getLocalizedMessage()); } - System.out.println("√√√ 域名:" + proxyDomain + "的dns记录已清除! √√√"); + log.info("√√√ 域名:{} 的dns记录已清除! √√√", proxyDomain); }); } + + @Override + public void removeCfSingleDnsRecords(String proxyDomain, String ip, String zoneId, String apiToken) { + String curlCommand = String.format(CF_REMOVE_SINGLE_DNS_RECORDS_API, zoneId, proxyDomain, apiToken, ip, zoneId, apiToken); + ProcessBuilder processBuilder = new ProcessBuilder("bash", "-c", curlCommand); + try { + Process process = processBuilder.start(); + // Wait for the process to finish + int exitCode = process.waitFor(); + if (exitCode != 0) { + log.error("Error executing removeCfSingleDnsRecords task"); + } + } catch (IOException | InterruptedException e) { + e.printStackTrace(); + throw new BusinessException(-1, "清除DNS记录失败:" + e.getLocalizedMessage()); + } + log.info("√√√ 域名:{} 的A记录ip:{} 的dns记录已清除! √√√", proxyDomain, ip); + } } diff --git a/foreign-server/resources/table.sql b/foreign-server/resources/table.sql index 78ca557..83d553a 100644 --- a/foreign-server/resources/table.sql +++ b/foreign-server/resources/table.sql @@ -1,12 +1,14 @@ create table if not exists `proxy_ip` ( - id varchar(256) not null, --'id' - country varchar(64) null, --'国家代码' - ip varchar(64) not null, --'ip地址' - ping_value INT null, --'ping值' + id varchar(256) not null, --'id' + country varchar(64) null, --'国家代码' + ip varchar(64) not null, --'ip地址' + ping_value INT null, --'ping值' + create_time datetime default (datetime('now', 'localtime')) not null,-- '记录创建时间' primary key ("id") );--代理ip表 CREATE INDEX idx_country ON proxy_ip (country); CREATE INDEX idx_ip ON proxy_ip (ip); -CREATE INDEX idx_ping_value ON proxy_ip (ping_value); \ No newline at end of file +CREATE INDEX idx_ping_value ON proxy_ip (ping_value); +CREATE INDEX idx_create_time ON proxy_ip (create_time DESC); \ No newline at end of file diff --git a/foreign-server/src/main/java/com/proxyip/select/bean/params/AddDnsRecordsBatchParams.java b/foreign-server/src/main/java/com/proxyip/select/bean/params/AddDnsRecordsBatchParams.java index 84ece21..6e48c42 100644 --- a/foreign-server/src/main/java/com/proxyip/select/bean/params/AddDnsRecordsBatchParams.java +++ b/foreign-server/src/main/java/com/proxyip/select/bean/params/AddDnsRecordsBatchParams.java @@ -1,5 +1,7 @@ package com.proxyip.select.bean.params; +import lombok.Data; + import java.util.List; /** @@ -9,15 +11,8 @@ * @author: Yohann * @date: 2024/3/30 20:47 */ +@Data public class AddDnsRecordsBatchParams { List ids; - - public List getIds() { - return ids; - } - - public void setIds(List ids) { - this.ids = ids; - } } diff --git a/foreign-server/src/main/java/com/proxyip/select/bean/params/AddProxyIpToDbParams.java b/foreign-server/src/main/java/com/proxyip/select/bean/params/AddProxyIpToDbParams.java new file mode 100644 index 0000000..996b539 --- /dev/null +++ b/foreign-server/src/main/java/com/proxyip/select/bean/params/AddProxyIpToDbParams.java @@ -0,0 +1,19 @@ +package com.proxyip.select.bean.params; + +import lombok.Data; + +import java.util.List; + +/** + *

+ * AddProxyIpToDbParams + *

+ * + * @author yuhui.fan + * @since 2024/4/1 16:51 + */ +@Data +public class AddProxyIpToDbParams { + + List ipList; +} diff --git a/foreign-server/src/main/java/com/proxyip/select/bean/params/AddSingleDnsRecordParams.java b/foreign-server/src/main/java/com/proxyip/select/bean/params/AddSingleDnsRecordParams.java index 51b5fd8..a7faf1b 100644 --- a/foreign-server/src/main/java/com/proxyip/select/bean/params/AddSingleDnsRecordParams.java +++ b/foreign-server/src/main/java/com/proxyip/select/bean/params/AddSingleDnsRecordParams.java @@ -1,5 +1,7 @@ package com.proxyip.select.bean.params; +import lombok.Data; + /** * @projectName: scan-proxyip * @package: com.proxyip.select.bean.params @@ -7,15 +9,8 @@ * @author: Yohann * @date: 2024/3/30 17:54 */ +@Data public class AddSingleDnsRecordParams { private String id; - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } } diff --git a/foreign-server/src/main/java/com/proxyip/select/bean/params/ProxyIpPageParams.java b/foreign-server/src/main/java/com/proxyip/select/bean/params/ProxyIpPageParams.java index 43313cc..1c2515b 100644 --- a/foreign-server/src/main/java/com/proxyip/select/bean/params/ProxyIpPageParams.java +++ b/foreign-server/src/main/java/com/proxyip/select/bean/params/ProxyIpPageParams.java @@ -1,5 +1,7 @@ package com.proxyip.select.bean.params; +import lombok.Data; + /** * @projectName: scan-proxyip * @package: com.proxyip.select.bean.params @@ -7,51 +9,10 @@ * @author: Yohann * @date: 2024/3/30 14:37 */ +@Data public class ProxyIpPageParams { private String keyword; private long currentPage; private long pageSize; - - public ProxyIpPageParams() { - } - - public ProxyIpPageParams(String keyword, long currentPage, long pageSize) { - this.keyword = keyword; - this.currentPage = currentPage; - this.pageSize = pageSize; - } - - public String getKeyword() { - return keyword; - } - - public void setKeyword(String keyword) { - this.keyword = keyword; - } - - public long getCurrentPage() { - return currentPage; - } - - public void setCurrentPage(long currentPage) { - this.currentPage = currentPage; - } - - public long getPageSize() { - return pageSize; - } - - public void setPageSize(long pageSize) { - this.pageSize = pageSize; - } - - @Override - public String toString() { - return "ProxyIpPageParams{" + - "keyword='" + keyword + '\'' + - ", currentPage=" + currentPage + - ", pageSize=" + pageSize + - '}'; - } } diff --git a/foreign-server/src/main/java/com/proxyip/select/bean/params/RmFromDbParams.java b/foreign-server/src/main/java/com/proxyip/select/bean/params/RmFromDbParams.java index f7b9d2b..6edba12 100644 --- a/foreign-server/src/main/java/com/proxyip/select/bean/params/RmFromDbParams.java +++ b/foreign-server/src/main/java/com/proxyip/select/bean/params/RmFromDbParams.java @@ -1,5 +1,7 @@ package com.proxyip.select.bean.params; +import lombok.Data; + import java.util.List; /** @@ -9,15 +11,8 @@ * @author: Yohann * @date: 2024/3/30 18:01 */ +@Data public class RmFromDbParams { List ids; - - public List getIds() { - return ids; - } - - public void setIds(List ids) { - this.ids = ids; - } } diff --git a/foreign-server/src/main/java/com/proxyip/select/bean/params/RmSingleDnsRecordParams.java b/foreign-server/src/main/java/com/proxyip/select/bean/params/RmSingleDnsRecordParams.java index b7f1dfc..be8fa24 100644 --- a/foreign-server/src/main/java/com/proxyip/select/bean/params/RmSingleDnsRecordParams.java +++ b/foreign-server/src/main/java/com/proxyip/select/bean/params/RmSingleDnsRecordParams.java @@ -1,5 +1,7 @@ package com.proxyip.select.bean.params; +import lombok.Data; + /** * @projectName: scan-proxyip * @package: com.proxyip.select.bean.params @@ -7,15 +9,8 @@ * @author: Yohann * @date: 2024/3/30 17:59 */ +@Data public class RmSingleDnsRecordParams { private String proxyDomain; - - public String getProxyDomain() { - return proxyDomain; - } - - public void setProxyDomain(String proxyDomain) { - this.proxyDomain = proxyDomain; - } } diff --git a/foreign-server/src/main/java/com/proxyip/select/bean/params/RmSingleIpDnsRecordParams.java b/foreign-server/src/main/java/com/proxyip/select/bean/params/RmSingleIpDnsRecordParams.java new file mode 100644 index 0000000..67c1917 --- /dev/null +++ b/foreign-server/src/main/java/com/proxyip/select/bean/params/RmSingleIpDnsRecordParams.java @@ -0,0 +1,17 @@ +package com.proxyip.select.bean.params; + +import lombok.Data; + +/** + * @projectName: scan-proxyip + * @package: com.proxyip.select.bean.params + * @className: RmSingleDnsRecordParams + * @author: Yohann + * @date: 2024/3/30 17:59 + */ +@Data +public class RmSingleIpDnsRecordParams { + + private String proxyDomain; + private String ip; +} diff --git a/foreign-server/src/main/java/com/proxyip/select/business/IProxyIpBusiness.java b/foreign-server/src/main/java/com/proxyip/select/business/IProxyIpBusiness.java index c83cdc2..8eeac7d 100644 --- a/foreign-server/src/main/java/com/proxyip/select/business/IProxyIpBusiness.java +++ b/foreign-server/src/main/java/com/proxyip/select/business/IProxyIpBusiness.java @@ -13,11 +13,15 @@ public interface IProxyIpBusiness { void rmAllDnsRecords(); - void rmSingleDnsRecord(RmSingleDnsRecordParams params); + void rmSingleIpDnsRecord(RmSingleIpDnsRecordParams params); - void addSingleDnsRecord(AddSingleDnsRecordParams params); + void rmSingleDnsRecord(RmSingleDnsRecordParams params); void rmMoveFromDb(RmFromDbParams params); + void addSingleDnsRecord(AddSingleDnsRecordParams params); + void addDnsRecordsBatch(AddDnsRecordsBatchParams params); + + void addProxyIpToDbBatch(AddProxyIpToDbParams params); } diff --git a/foreign-server/src/main/java/com/proxyip/select/business/impl/DnsRecordBusinessImpl.java b/foreign-server/src/main/java/com/proxyip/select/business/impl/DnsRecordBusinessImpl.java index e4848ff..4e312ca 100644 --- a/foreign-server/src/main/java/com/proxyip/select/business/impl/DnsRecordBusinessImpl.java +++ b/foreign-server/src/main/java/com/proxyip/select/business/impl/DnsRecordBusinessImpl.java @@ -12,6 +12,7 @@ import com.proxyip.select.config.CloudflareCfg; import com.proxyip.select.config.DnsCfg; import com.proxyip.select.business.IDnsRecordBusiness; +import lombok.extern.slf4j.Slf4j; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; @@ -34,6 +35,7 @@ * @since 2024/3/21 14:26 */ @Service +@Slf4j public class DnsRecordBusinessImpl implements IDnsRecordBusiness { @Resource @@ -47,8 +49,6 @@ public class DnsRecordBusinessImpl implements IDnsRecordBusiness { @Resource private IdGen idGen; - private static final Logger logger = LoggerFactory.getLogger(DnsRecordBusinessImpl.class); - @Override public void addLimitDnsRecordAndWriteToFile(List ipAddresses, String outputFile) { List countryCodeList = Arrays.stream(CountryEnum.values()).map(CountryEnum::getCode).collect(Collectors.toList()); @@ -75,15 +75,13 @@ public void addLimitDnsRecordAndWriteToFile(List ipAddresses, String out proxyIp.setIp(ip); Integer pingValue = NetUtils.getPingValue(ip); proxyIp.setPingValue(pingValue == null ? 999999 : pingValue); + proxyIp.setCreateTime(LocalDateTime.now()); return proxyIp; }).collect(Collectors.toList()); // 持久化到数据库 - List ipInDbList = proxyIpService.listObjs(new LambdaQueryWrapper() - .select(ProxyIp::getIp), String::valueOf); - if (CommonUtils.isNotEmpty(ipInDbList)) { - list.removeIf(x -> ipInDbList.contains(x.getIp())); - } + list.removeIf(x -> Optional.ofNullable(proxyIpService.getOne(new LambdaQueryWrapper() + .eq(ProxyIp::getIp, x.getIp()))).isPresent()); proxyIpService.saveBatch(list); // 分组并保留五条记录 @@ -123,7 +121,7 @@ public void addLimitDnsRecordAndWriteToFile(List ipAddresses, String out apiService.addCfDnsRecords(prefix, x.getIp(), cloudflareCfg.getZoneId(), cloudflareCfg.getApiToken()); } }); - logger.info("√√√ 所有DNS记录添加完成!!! √√√"); + log.info("√√√ 所有DNS记录添加完成!!! √√√"); // 写入文件 CompletableFuture.runAsync(() -> { @@ -140,7 +138,7 @@ public void addLimitDnsRecordAndWriteToFile(List ipAddresses, String out } catch (Exception e) { throw new RuntimeException("写入文件:" + outputFile + "失败"); } - logger.info("√√√ 获取proxyIps任务完成,文件位置:{} √√√", outputFile); + log.info("√√√ 获取proxyIps任务完成,文件位置:{} √√√", outputFile); // 发送到网盘api if (!"".equals(dnsCfg.getUploadApi())) { @@ -180,12 +178,12 @@ public void addDnsRecordAndWriteToFile(List ipAddresses, String outputFi e.printStackTrace(); } } else { - logger.info("IP:{} 无法ping通,已跳过", ipAddress); + log.info("IP:{} 无法ping通,已跳过", ipAddress); } }); - logger.info("√√√ 所有DNS记录添加完成!!! √√√"); - logger.info("√√√ 获取proxyIps任务完成,文件位置:{} √√√", outputFile); + log.info("√√√ 所有DNS记录添加完成!!! √√√"); + log.info("√√√ 获取proxyIps任务完成,文件位置:{} √√√", outputFile); // 发送到网盘api if (!"".equals(dnsCfg.getUploadApi())) { @@ -203,12 +201,12 @@ public void rmCfDnsRecords() { .map(x -> x.getLowCode() + "." + cloudflareCfg.getProxyDomainPrefix() + "." + cloudflareCfg.getRootDomain()) .collect(Collectors.toList()); apiService.removeCfDnsRecords(proxyDomainList, cloudflareCfg.getZoneId(), cloudflareCfg.getApiToken()); - logger.info("√√√ 所有DNS记录已清除成功,开始添加DNS记录... √√√"); + log.info("√√√ 所有DNS记录已清除成功,开始添加DNS记录... √√√"); } @Override public void rmIpInDb() { - logger.info("当前时间:{},开始清除数据库中无效的ip...", getNowDateTime()); + log.info("当前时间:{},开始清除数据库中无效的ip...", getNowDateTime()); List voidIdList = Optional.ofNullable(proxyIpService.list()) .filter(CommonUtils::isNotEmpty).orElseGet(Collections::emptyList).parallelStream() .filter(x -> !dnsCfg.getReleaseIps().contains(x.getIp())) @@ -218,12 +216,12 @@ public void rmIpInDb() { if (CommonUtils.isNotEmpty(voidIdList)) { proxyIpService.removeByIds(voidIdList); } - logger.info("√√√ 清除数据库中无效ip任务已完成 √√√"); + log.info("√√√ 清除数据库中无效ip任务已完成 √√√"); } @Override public void updateProxyIpTask() { - logger.info("当前时间:{},开始更新DNS记录...", getNowDateTime()); + log.info("当前时间:{},开始更新DNS记录...", getNowDateTime()); long begin = System.currentTimeMillis(); // 获取proxyIps List ipAddresses = apiService.resolveDomain(dnsCfg.getProxyDomain(), dnsCfg.getDnsServer()); @@ -238,7 +236,7 @@ public void updateProxyIpTask() { long end = System.currentTimeMillis(); - logger.info("√√√ 更新DNS记录任务完成!!!总耗时:{} ms √√√", (end - begin)); + log.info("√√√ 更新DNS记录任务完成!!!总耗时:{} ms √√√", (end - begin)); } @Override @@ -249,7 +247,7 @@ public void updateDbTask() { @Override public void updateIpPingValueInDb() { - logger.info("当前时间:{},开始更新数据库中ip的ping值...", getNowDateTime()); + log.info("当前时间:{},开始更新数据库中ip的ping值...", getNowDateTime()); List proxyIpList = Optional.ofNullable(proxyIpService.list()) .filter(CommonUtils::isNotEmpty).orElseGet(Collections::emptyList).parallelStream() .peek(x -> { @@ -260,7 +258,7 @@ public void updateIpPingValueInDb() { if (CommonUtils.isNotEmpty(proxyIpList)) { proxyIpService.updateBatchById(proxyIpList); } - logger.info("√√√ 更新数据库中ip的ping值任务已完成 √√√"); + log.info("√√√ 更新数据库中ip的ping值任务已完成 √√√"); } /** diff --git a/foreign-server/src/main/java/com/proxyip/select/business/impl/ProxyIpBusinessImpl.java b/foreign-server/src/main/java/com/proxyip/select/business/impl/ProxyIpBusinessImpl.java index 135f31b..ca59a6a 100644 --- a/foreign-server/src/main/java/com/proxyip/select/business/impl/ProxyIpBusinessImpl.java +++ b/foreign-server/src/main/java/com/proxyip/select/business/impl/ProxyIpBusinessImpl.java @@ -12,12 +12,19 @@ import com.proxyip.select.common.service.IApiService; import com.proxyip.select.common.service.IProxyIpService; import com.proxyip.select.common.utils.CommonUtils; +import com.proxyip.select.common.utils.IdGen; +import com.proxyip.select.common.utils.NetUtils; import com.proxyip.select.config.CloudflareCfg; +import com.proxyip.select.config.DnsCfg; +import org.springframework.cglib.core.Local; import org.springframework.stereotype.Service; import javax.annotation.Resource; +import java.time.LocalDateTime; import java.util.Collections; +import java.util.List; import java.util.Optional; +import java.util.stream.Collectors; /** * @projectName: scan-proxyip @@ -29,6 +36,8 @@ @Service public class ProxyIpBusinessImpl implements IProxyIpBusiness { + @Resource + private DnsCfg dnsCfg; @Resource private CloudflareCfg cloudflareCfg; @Resource @@ -37,6 +46,8 @@ public class ProxyIpBusinessImpl implements IProxyIpBusiness { private IDnsRecordBusiness dnsRecordService; @Resource private IApiService apiService; + @Resource + private IdGen idGen; @Override public IPage pageList(ProxyIpPageParams params) { @@ -44,7 +55,7 @@ public IPage pageList(ProxyIpPageParams params) { new LambdaQueryWrapper() .likeRight(ProxyIp::getCountry, params.getKeyword()).or() .likeRight(ProxyIp::getIp, params.getKeyword()) - .orderByAsc(ProxyIp::getPingValue)); + .orderByDesc(ProxyIp::getCreateTime)); } @Override @@ -52,6 +63,15 @@ public void rmAllDnsRecords() { dnsRecordService.rmCfDnsRecords(); } + @Override + public void rmSingleIpDnsRecord(RmSingleIpDnsRecordParams params) { + apiService.removeCfSingleDnsRecords( + params.getProxyDomain() + "." + cloudflareCfg.getProxyDomainPrefix() + "." + cloudflareCfg.getRootDomain(), + params.getIp(), + cloudflareCfg.getZoneId(), + cloudflareCfg.getApiToken()); + } + @Override public void rmSingleDnsRecord(RmSingleDnsRecordParams params) { apiService.removeCfDnsRecords( @@ -82,15 +102,43 @@ public void rmMoveFromDb(RmFromDbParams params) { public void addDnsRecordsBatch(AddDnsRecordsBatchParams params) { Optional.ofNullable(proxyIpService.listByIds(params.getIds())) .filter(CommonUtils::isNotEmpty).ifPresent(list -> { - list.parallelStream().forEach(proxyIp -> { - String prefix = EnumUtils.getEnumByCode(CountryEnum.class, proxyIp.getCountry()).getLowCode() + "." - + cloudflareCfg.getProxyDomainPrefix(); - apiService.addCfDnsRecords( - prefix, - proxyIp.getIp(), - cloudflareCfg.getZoneId(), - cloudflareCfg.getApiToken()); - }); - }); + list.parallelStream().forEach(proxyIp -> { + String prefix = EnumUtils.getEnumByCode(CountryEnum.class, proxyIp.getCountry()).getLowCode() + "." + + cloudflareCfg.getProxyDomainPrefix(); + apiService.addCfDnsRecords( + prefix, + proxyIp.getIp(), + cloudflareCfg.getZoneId(), + cloudflareCfg.getApiToken()); + }); + }); + } + + @Override + public void addProxyIpToDbBatch(AddProxyIpToDbParams params) { + Optional.ofNullable(params.getIpList()) + .filter(CommonUtils::isNotEmpty).ifPresent(ipList -> { + List list = ipList.parallelStream() + .map(String::trim) + .map(ip -> { + // 获取国家代码 + String countryCode = apiService.getIpCountry(ip, dnsCfg.getGeoipAuth()); + if (countryCode == null || EnumUtils.getEnumByCode(CountryEnum.class, countryCode) == null) { + countryCode = apiService.getIpCountry(ip); + } + + ProxyIp proxyIp = new ProxyIp(); + proxyIp.setId(String.valueOf(idGen.nextId())); + proxyIp.setCountry(countryCode); + proxyIp.setIp(ip); + Integer pingValue = NetUtils.getPingValue(ip); + proxyIp.setPingValue(pingValue == null ? 999999 : pingValue); + proxyIp.setCreateTime(LocalDateTime.now()); + return proxyIp; + }).collect(Collectors.toList()); + list.removeIf(x -> Optional.ofNullable(proxyIpService.getOne(new LambdaQueryWrapper() + .eq(ProxyIp::getIp, x.getIp()))).isPresent()); + proxyIpService.saveBatch(list); + }); } } diff --git a/foreign-server/src/main/java/com/proxyip/select/config/LocalDateTimeHandler.java b/foreign-server/src/main/java/com/proxyip/select/config/LocalDateTimeHandler.java new file mode 100644 index 0000000..95a206c --- /dev/null +++ b/foreign-server/src/main/java/com/proxyip/select/config/LocalDateTimeHandler.java @@ -0,0 +1,67 @@ +package com.proxyip.select.config; + +import org.apache.ibatis.type.BaseTypeHandler; +import org.apache.ibatis.type.JdbcType; +import org.springframework.stereotype.Component; + +import java.sql.CallableStatement; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; + +/** + * sqlite localDateTime 处理 + * @author libo + */ +@Component +public class LocalDateTimeHandler extends BaseTypeHandler { + + private static final DateTimeFormatter DEFAULT_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + + private static final DateTimeFormatter[] FORMATTERS = new DateTimeFormatter[]{ + DateTimeFormatter.ofPattern("yyyy-MM-dd"), + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"), + DEFAULT_FORMATTER, + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"), + DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm"), + DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"), + DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS")}; + + @Override + public void setNonNullParameter(PreparedStatement ps, int i, LocalDateTime parameter, JdbcType jdbcType) throws SQLException { + ps.setObject(i, parameter.format(DEFAULT_FORMATTER)); + } + + @Override + public LocalDateTime getNullableResult(ResultSet rs, String columnName) throws SQLException { + return ts2LocalDateTime(rs.getString(columnName)); + } + + @Override + public LocalDateTime getNullableResult(ResultSet rs, int columnIndex) throws SQLException { + return ts2LocalDateTime(rs.getString(columnIndex)); + } + + @Override + public LocalDateTime getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { + return ts2LocalDateTime(cs.getString(columnIndex)); + } + + private LocalDateTime ts2LocalDateTime(String timeStr) { + if (timeStr == null || "".equals(timeStr) || "null".equals(timeStr)){ + return null; + } + + for (DateTimeFormatter df:FORMATTERS) { + try{ + return LocalDateTime.parse(timeStr, df); + }catch (DateTimeParseException e){ + } + } + throw new RuntimeException("not supported time str:"+timeStr); + } + +} diff --git a/foreign-server/src/main/java/com/proxyip/select/controller/ProxyIpController.java b/foreign-server/src/main/java/com/proxyip/select/controller/ProxyIpController.java index a35df4e..7c22a01 100644 --- a/foreign-server/src/main/java/com/proxyip/select/controller/ProxyIpController.java +++ b/foreign-server/src/main/java/com/proxyip/select/controller/ProxyIpController.java @@ -40,10 +40,16 @@ public ResponseData rmAllDnsRecords() { return ResponseData.successData("删除所有dns记录成功"); } + @PostMapping(path = "/rmSingleIpDnsRecord") + public ResponseData rmSingleIpDnsRecord(@RequestBody RmSingleIpDnsRecordParams params) { + proxyIpBusiness.rmSingleIpDnsRecord(params); + return ResponseData.successData("删除该代理域名的某个ip地址的dns记录成功"); + } + @PostMapping(path = "/rmSingleDnsRecord") public ResponseData rmSingleDnsRecord(@RequestBody RmSingleDnsRecordParams params) { proxyIpBusiness.rmSingleDnsRecord(params); - return ResponseData.successData("删除改代理域名所有dns记录成功"); + return ResponseData.successData("删除该代理域名所有dns记录成功"); } @PostMapping(path = "/addSingleDnsRecord") @@ -57,4 +63,10 @@ public ResponseData addDnsRecordsBatch(@RequestBody AddDnsRecordsBatchPara proxyIpBusiness.addDnsRecordsBatch(params); return ResponseData.successData("添加多条dns记录成功"); } + + @PostMapping(path = "/addProxyIpToDbBatch") + public ResponseData addProxyIpToDbBatch(@RequestBody AddProxyIpToDbParams params) { + proxyIpBusiness.addProxyIpToDbBatch(params); + return ResponseData.successData("添加多个proxyIp到数据库成功"); + } } diff --git a/foreign-server/src/main/resources/static/proxyIp.html b/foreign-server/src/main/resources/static/proxyIp.html index 7e80ec9..441b67a 100644 --- a/foreign-server/src/main/resources/static/proxyIp.html +++ b/foreign-server/src/main/resources/static/proxyIp.html @@ -87,6 +87,24 @@ .git-contact-float:hover { background-color: #0056b3; /* 鼠标悬停时的背景颜色 */ } + + .el-tag + .el-tag { + margin-left: 10px; + } + + .button-new-tag { + margin-left: 10px; + height: 32px; + line-height: 30px; + padding-top: 0; + padding-bottom: 0; + } + + .input-new-tag { + width: 200px; + margin-left: 10px; + vertical-align: bottom; + } @@ -100,28 +118,70 @@

代理IP列表

@input="search"> - - - - - - - - - - - - - - - - 批量删除数据库中数据 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 批量删除数据库中数据 删除所有代理域名DNS记录 + + 批量解析所选ip地址到CF的DNS记录 + + + {{ tag }} + + + + + New ProxyIp + 添加proxyIp到数据库 + +
@@ -148,11 +208,16 @@

代理IP列表

{{ scope.row.ip }} - + + + +