diff --git a/src/main/java/com/ghostchu/peerbanhelper/btn/BtnRuleParsed.java b/src/main/java/com/ghostchu/peerbanhelper/btn/BtnRuleParsed.java index 9d5e158596..a133a7062e 100644 --- a/src/main/java/com/ghostchu/peerbanhelper/btn/BtnRuleParsed.java +++ b/src/main/java/com/ghostchu/peerbanhelper/btn/BtnRuleParsed.java @@ -10,7 +10,7 @@ import com.ghostchu.peerbanhelper.util.rule.Rule; import com.ghostchu.peerbanhelper.util.rule.RuleParser; import com.ghostchu.peerbanhelper.util.rule.matcher.IPMatcher; -import inet.ipaddr.IPAddress; +import inet.ipaddr.format.util.DualIPv4v6Tries; import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.jetbrains.annotations.NotNull; @@ -94,10 +94,13 @@ public String matcherIdentifier() { return rules; } - public Map parseIPRule(Map> raw) { Map rules = new HashMap<>(); - raw.forEach((k, v) -> rules.put(k,new IPMatcher(version, k, v.stream().map(IPAddressUtil::getIPAddress).toList()))); + raw.forEach((k, v) -> { + DualIPv4v6Tries tries = new DualIPv4v6Tries(); + v.stream().map(IPAddressUtil::getIPAddress).forEach(tries::add); + rules.put(k,new IPMatcher(version, k, List.of(tries))); + }); return rules; } @@ -120,7 +123,7 @@ public static class BtnRuleIpMatcher extends IPMatcher { private final String version; - public BtnRuleIpMatcher(String version, String ruleId, String ruleName, List ruleData) { + public BtnRuleIpMatcher(String version, String ruleId, String ruleName, List ruleData) { super(ruleId, ruleName, ruleData); this.version = version; } diff --git a/src/main/java/com/ghostchu/peerbanhelper/module/impl/rule/IPBlackRuleList.java b/src/main/java/com/ghostchu/peerbanhelper/module/impl/rule/IPBlackRuleList.java index 90c914500a..550c73c492 100644 --- a/src/main/java/com/ghostchu/peerbanhelper/module/impl/rule/IPBlackRuleList.java +++ b/src/main/java/com/ghostchu/peerbanhelper/module/impl/rule/IPBlackRuleList.java @@ -32,6 +32,7 @@ import com.google.common.io.Files; import com.j256.ormlite.stmt.SelectArg; import inet.ipaddr.IPAddress; +import inet.ipaddr.format.util.DualIPv4v6Tries; import io.ipfs.cid.Cid; import lombok.Getter; import lombok.extern.slf4j.Slf4j; @@ -202,7 +203,7 @@ public StdResp updateRule(String locale, @NotNull ConfigurationSection rule, IPB File dir = new File(Main.getDataDirectory(), "/sub"); dir.mkdirs(); File ruleFile = new File(dir, ruleFileName); - List ipAddresses = new ArrayList<>(); + DualIPv4v6Tries ipAddresses = new DualIPv4v6Tries(); getResource(url) .whenComplete((dataUpdateResult, throwable) -> { if (throwable != null) { @@ -212,7 +213,7 @@ public StdResp updateRule(String locale, @NotNull ConfigurationSection rule, IPB if (ipBanMatchers.stream().noneMatch(ele -> ele.getRuleId().equals(ruleId))) { try { fileToIPList(ruleFile, ipAddresses); - ipBanMatchers.add(new IPMatcher(ruleId, name, ipAddresses)); + ipBanMatchers.add(new IPMatcher(ruleId, name, List.of(ipAddresses))); log.warn(tlUI(Lang.IP_BAN_RULE_USE_CACHE, name)); result.set(new StdResp(false, tl(locale, Lang.IP_BAN_RULE_USE_CACHE, name), null)); } catch (IOException ex) { @@ -252,12 +253,12 @@ public StdResp updateRule(String locale, @NotNull ConfigurationSection rule, IPB if (ent_count > 0) { // 如果已经存在则更新,否则添加 ipBanMatchers.stream().filter(ele -> ele.getRuleId().equals(ruleId)).findFirst().ifPresentOrElse(ele -> { - ele.setData(name, ipAddresses); + ele.setData(name, List.of(ipAddresses)); moduleMatchCache.invalidateAll(); log.info(tlUI(Lang.IP_BAN_RULE_UPDATE_SUCCESS, name)); result.set(new StdResp(true, tl(locale, Lang.IP_BAN_RULE_UPDATE_SUCCESS, name), null)); }, () -> { - ipBanMatchers.add(new IPMatcher(ruleId, name, ipAddresses)); + ipBanMatchers.add(new IPMatcher(ruleId, name, List.of(ipAddresses))); log.info(tlUI(Lang.IP_BAN_RULE_LOAD_SUCCESS, name)); result.set(new StdResp(true, tl(locale, Lang.IP_BAN_RULE_LOAD_SUCCESS, name), null)); }); @@ -338,7 +339,7 @@ private CompletableFuture getResource(String url) { * @param ips ip列表 * @return 加载的行数 */ - private int fileToIPList(File ruleFile, List ips) throws IOException { + private int fileToIPList(File ruleFile, DualIPv4v6Tries ips) throws IOException { AtomicInteger count = new AtomicInteger(); Files.readLines(ruleFile, StandardCharsets.UTF_8).stream().filter(s -> !s.isBlank()).forEach(ele -> { if (ele.startsWith("#")) { @@ -364,7 +365,7 @@ private int fileToIPList(File ruleFile, List ips) throws IOException * @param ips ip列表 * @return 加载的行数 */ - private int stringToIPList(String data, List ips) throws IOException { + private int stringToIPList(String data, DualIPv4v6Tries ips) throws IOException { AtomicInteger count = new AtomicInteger(); Arrays.stream(data.split("\n")).filter(s -> !s.isBlank()).forEach(ele -> { if (ele.startsWith("#")) { diff --git a/src/main/java/com/ghostchu/peerbanhelper/util/rule/matcher/IPMatcher.java b/src/main/java/com/ghostchu/peerbanhelper/util/rule/matcher/IPMatcher.java index bf6cc3c0f7..8e11a190d9 100644 --- a/src/main/java/com/ghostchu/peerbanhelper/util/rule/matcher/IPMatcher.java +++ b/src/main/java/com/ghostchu/peerbanhelper/util/rule/matcher/IPMatcher.java @@ -17,12 +17,12 @@ @Slf4j @EqualsAndHashCode(callSuper = true) @ToString(callSuper = true) -public class IPMatcher extends RuleMatcher { - private DualIPv4v6Tries ips = new DualIPv4v6Tries(); +public class IPMatcher extends RuleMatcher { + private DualIPv4v6Tries ips; - public IPMatcher(String ruleId, String ruleName, List ruleData) { + public IPMatcher(String ruleId, String ruleName, List ruleData) { super(ruleId, ruleName, ruleData); - ruleData.forEach(ips::add); + this.ips = ruleData.getFirst();; } /** @@ -31,11 +31,9 @@ public IPMatcher(String ruleId, String ruleName, List ruleData) { * @param ruleName 规则名 * @param ruleData 规则数据 */ - public void setData(String ruleName, List ruleData) { + public void setData(String ruleName, List ruleData) { setRuleName(ruleName); - var tmp = new DualIPv4v6Tries(); - ruleData.forEach(tmp::add); - this.ips = tmp; + this.ips = ruleData.getFirst(); } public long size() {