From 9fa1d92290cdbecf4a288e352964dce725f692ab Mon Sep 17 00:00:00 2001 From: Ghost_chu Date: Wed, 1 Jan 2025 19:42:38 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A8=8D=E5=BE=AE=E4=BC=98=E5=8C=96=E4=B8=80?= =?UTF-8?q?=E7=82=B9=E7=82=B9=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../module/analyse/AnalyseService.java | 19 ++++++++++++++++--- .../ghostchu/btn/sparkle/util/IPMerger.java | 7 +++---- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/ghostchu/btn/sparkle/module/analyse/AnalyseService.java b/src/main/java/com/ghostchu/btn/sparkle/module/analyse/AnalyseService.java index adfba34..4177040 100644 --- a/src/main/java/com/ghostchu/btn/sparkle/module/analyse/AnalyseService.java +++ b/src/main/java/com/ghostchu/btn/sparkle/module/analyse/AnalyseService.java @@ -291,7 +291,18 @@ public void cronUpdateTrunkerFile() { ((peerInfo.getUserAgent().contains("Transmission") == peerId.startsWith("-TR"))) || ((peerInfo.getUserAgent().contains("aria2") == peerId.startsWith("A2"))) ) { - ipTries.add(IPUtil.toIPAddress(peerInfo.getIp().getClientIp().toByteArray())); + var ip = IPUtil.toIPAddress(peerInfo.getIp().getClientIp().toByteArray()); + if (!ip.isLocal() && !ip.isLoopback()) { + try { + if (ip.getPrefixLength() == null && ip.isIPv6()) { + ip = ip.toPrefixBlock(ipv6ConvertToPrefixLength); + } + ipTries.add(ip); + } catch (Exception e) { + log.error("Unable to convert {} with prefix block {}.", ip, ipv6ConvertToPrefixLength, e); + } + } + } success.incrementAndGet(); } catch (Exception e) { @@ -307,8 +318,10 @@ public void cronUpdateTrunkerFile() { }, service); } - var filtered = filterIP(ipTries); - var ips = ipMerger.merge(filtered).stream().distinct().map(ip -> new AnalysedRule(null, ip, TRACKER_HIGH_RISK, "Auto Generated by Sparkle")).collect(Collectors.toSet()); + // var filtered = filterIP(ipTries); // too slow + + + var ips = ipMerger.merge(ipTries).stream().distinct().map(ip -> new AnalysedRule(null, ip, TRACKER_HIGH_RISK, "Auto Generated by Sparkle")).collect(Collectors.toSet()); analysedRuleRepository.replaceAll(TRACKER_HIGH_RISK, ips); meterRegistry.gauge("sparkle_analyse_tracker_high_risk_identity", Collections.emptyList(), ips.size()); log.info("Tracker HighRisk identity: {}, tooked {} ms; success: {}/{}.", ips.size(), System.currentTimeMillis() - startAt, success.get(), count.get()); diff --git a/src/main/java/com/ghostchu/btn/sparkle/util/IPMerger.java b/src/main/java/com/ghostchu/btn/sparkle/util/IPMerger.java index bfc8430..a838ba3 100644 --- a/src/main/java/com/ghostchu/btn/sparkle/util/IPMerger.java +++ b/src/main/java/com/ghostchu/btn/sparkle/util/IPMerger.java @@ -33,8 +33,8 @@ public List merge(Collection sorted) { Set createdCIDR = new TreeSet<>(); IPAddress current = null; int counter = 0; - list.removeIf(str -> str.startsWith("#")); for (String rule : list) { + if (rule.startsWith("#")) continue; if (current == null) { current = toCIDR(rule); continue; @@ -66,10 +66,9 @@ public List merge(Collection sorted) { } } } - List finalSorted = list; createdCIDR.forEach(cidr -> { var base = toCIDR(cidr); - finalSorted.removeIf(ip -> { + ((List) list).removeIf(ip -> { IPAddress address = toIP(ip); if (address == null) { System.out.println("(Unresolved data) " + ip); @@ -81,7 +80,7 @@ public List merge(Collection sorted) { }); return new ArrayList<>() {{ this.addAll(createdCIDR); - this.addAll(finalSorted); + this.addAll(list); }}; }