Skip to content

Commit

Permalink
过滤 IP
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghost-chu committed Oct 7, 2024
1 parent 31a981a commit 1c525cb
Showing 1 changed file with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@

import java.net.InetAddress;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;

@Service
Expand Down Expand Up @@ -63,8 +60,10 @@ public void cronUntrustedIPAddresses() {
.generateUntrustedIPAddresses(new Timestamp(System.currentTimeMillis() - untrustedIpAddressGenerateOffset), new Timestamp(System.currentTimeMillis()), untrustedIpAddressGenerateThreshold)
.stream()
.map(IPUtil::toString)
.collect(Collectors.toList()));
var untrustedIps = list.stream().map(ip -> new AnalysedRule(null, ip, UNTRUSTED_IP, "Generated at " + MsgUtil.getNowDateTimeString())).toList();
.collect(Collectors.toList()))
.stream().map(IPUtil::toIPAddress)
.toList();
var untrustedIps = filterIP(list).stream().map(ip -> new AnalysedRule(null, ip.toString(), UNTRUSTED_IP, "Generated at " + MsgUtil.getNowDateTimeString())).toList();
analysedRuleRepository.deleteAllByModule(UNTRUSTED_IP);
analysedRuleRepository.saveAll(untrustedIps);
}
Expand Down Expand Up @@ -98,7 +97,7 @@ public void cronHighRiskIps() {
.map(ban -> IPUtil.toIPAddress(ban.getPeerIp().getHostAddress()))
.distinct()
.toList());
var highRiskIps = list.stream().map(ip -> new AnalysedRule(null, ip.toString(), HIGH_RISK_IP, "Generated at " + MsgUtil.getNowDateTimeString())).toList();
var highRiskIps = filterIP(list).stream().map(ip -> new AnalysedRule(null, ip.toString(), HIGH_RISK_IP, "Generated at " + MsgUtil.getNowDateTimeString())).toList();
analysedRuleRepository.deleteAllByModule(HIGH_RISK_IP);
analysedRuleRepository.saveAll(highRiskIps);
}
Expand Down Expand Up @@ -133,7 +132,7 @@ public void cronHighRiskIPV6Identity() {
.distinct()
.sorted()
.forEach(list::add);
var ips = list.stream().map(ip -> new AnalysedRule(null, ip.toString(), HIGH_RISK_IPV6_IDENTITY, "Generated at " + MsgUtil.getNowDateTimeString())).toList();
var ips = filterIP(list).stream().map(ip -> new AnalysedRule(null, ip.toString(), HIGH_RISK_IPV6_IDENTITY, "Generated at " + MsgUtil.getNowDateTimeString())).toList();
analysedRuleRepository.deleteAllByModule(HIGH_RISK_IPV6_IDENTITY);
analysedRuleRepository.saveAll(ips);
}
Expand Down Expand Up @@ -198,7 +197,7 @@ AggregatedUploads AS (
List<AnalysedRule> rules = new ArrayList<>();
for (String ip : ips) {
try {
if (new IPAddressString(ip).getAddress().isLocal()) {
if (new IPAddressString(ip).getAddress().isAnyLocal()) {
continue;
}
rules.add(new AnalysedRule(
Expand All @@ -215,6 +214,10 @@ AggregatedUploads AS (
analysedRuleRepository.saveAll(rules);
}

public Collection<IPAddress> filterIP(Collection<IPAddress> ips) {
ips.removeIf(IPAddress::isAnyLocal);
return ips;
}

private Timestamp nowTimestamp() {
return new Timestamp(System.currentTimeMillis());
Expand Down

0 comments on commit 1c525cb

Please sign in to comment.