diff --git a/src/main/java/com/ghostchu/btn/sparkle/config/SpringRedisConfig.java b/src/main/java/com/ghostchu/btn/sparkle/config/SpringRedisConfig.java index 4143979a..a7aac689 100644 --- a/src/main/java/com/ghostchu/btn/sparkle/config/SpringRedisConfig.java +++ b/src/main/java/com/ghostchu/btn/sparkle/config/SpringRedisConfig.java @@ -22,8 +22,8 @@ public LettuceConnectionFactory redisConnectionFactory(@Value("${sparkle.redis.u @Bean @Primary - public RedisTemplate redisTemplate(LettuceConnectionFactory redisConnectionFactory, ObjectMapper mapper) { - RedisTemplate redisTemplate = new RedisTemplate<>(); + public RedisTemplate redisTemplate(LettuceConnectionFactory redisConnectionFactory, ObjectMapper mapper) { + RedisTemplate redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer(mapper)); 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 971ba8d1..746f4874 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 @@ -137,21 +137,23 @@ HAVING SUM(app_count) > ? ipMerger.merge(ipTries); List untrustedIps = new ArrayList<>(); filterIP(ipTries).forEach(ip -> untrustedIps.add(new AnalysedRule(null, ip.toString(), UNTRUSTED_IP, "Generated at " + MsgUtil.getNowDateTimeString()))); + analysedRuleRepository.deleteAllByModule(UNTRUSTED_IP); meterRegistry.gauge("sparkle_analyse_untrusted_ip_address", Collections.emptyList(), untrustedIps.size()); - analysedRuleRepository.replaceAll(UNTRUSTED_IP, untrustedIps); + analysedRuleRepository.saveAll(untrustedIps); log.info("Untrusted IPs: {}, tooked {} ms", untrustedIps.size(), System.currentTimeMillis() - startAt); } - public Set getAnalysedRules() { + @Transactional + public List getAnalysedRules() { return analysedRuleRepository.findAll(); } - public Set getUntrustedIPAddresses() { - return analysedRuleRepository.findAllByModule(UNTRUSTED_IP); + public List getUntrustedIPAddresses() { + return analysedRuleRepository.findByModuleOrderByIpAsc(UNTRUSTED_IP); } - public Set getOverDownloadIPAddresses() { - return analysedRuleRepository.findAllByModule(OVER_DOWNLOAD); + public List getOverDownloadIPAddresses() { + return analysedRuleRepository.findByModuleOrderByIpAsc(OVER_DOWNLOAD); } @Transactional @@ -183,8 +185,8 @@ public void cronHighRiskIps() throws InterruptedException { log.info("High risk IPs: {}, tooked {} ms", highRiskIps.size(), System.currentTimeMillis() - startAt); } - public Set getHighRiskIps() { - return analysedRuleRepository.findAllByModule(HIGH_RISK_IP); + public List getHighRiskIps() { + return analysedRuleRepository.findByModuleOrderByIpAsc(HIGH_RISK_IP); } // // @Transactional @@ -224,8 +226,8 @@ public Set getHighRiskIps() { // } // } - public Set getHighRiskIPV6Identity() { - return analysedRuleRepository.findAllByModule(HIGH_RISK_IPV6_IDENTITY); + public List getHighRiskIPV6Identity() { + return analysedRuleRepository.findByModuleOrderByIpAsc(HIGH_RISK_IPV6_IDENTITY); } @Transactional @@ -291,8 +293,8 @@ private void scanFile(Consumer predicate) { } } - public Set getTrackerHighRisk() { - return analysedRuleRepository.findAllByModule(TRACKER_HIGH_RISK); + public List getTrackerHighRisk() { + return analysedRuleRepository.findByModuleOrderByIpAsc(TRACKER_HIGH_RISK); } diff --git a/src/main/java/com/ghostchu/btn/sparkle/module/analyse/impl/AnalysedRule.java b/src/main/java/com/ghostchu/btn/sparkle/module/analyse/impl/AnalysedRule.java index 85ca1c05..32aebac9 100644 --- a/src/main/java/com/ghostchu/btn/sparkle/module/analyse/impl/AnalysedRule.java +++ b/src/main/java/com/ghostchu/btn/sparkle/module/analyse/impl/AnalysedRule.java @@ -1,14 +1,30 @@ package com.ghostchu.btn.sparkle.module.analyse.impl; +import jakarta.persistence.*; import lombok.AllArgsConstructor; -import lombok.Data; +import lombok.Getter; import lombok.NoArgsConstructor; +import lombok.Setter; +import org.hibernate.annotations.DynamicUpdate; + +@Entity +@Table(name = "analyse_rules", + indexes = {@Index(columnList = "module"), @Index(columnList = "ip")} +) @AllArgsConstructor @NoArgsConstructor -@Data +@Getter +@Setter +@DynamicUpdate public class AnalysedRule { + @Id + @GeneratedValue + @Column(nullable = false, unique = true) private Long id; + @Column(nullable = false) private String ip; + @Column(nullable = false) private String module; + @Column(nullable = false) private String comment; } diff --git a/src/main/java/com/ghostchu/btn/sparkle/module/analyse/impl/AnalysedRuleRepository.java b/src/main/java/com/ghostchu/btn/sparkle/module/analyse/impl/AnalysedRuleRepository.java index 37921320..f0588eee 100644 --- a/src/main/java/com/ghostchu/btn/sparkle/module/analyse/impl/AnalysedRuleRepository.java +++ b/src/main/java/com/ghostchu/btn/sparkle/module/analyse/impl/AnalysedRuleRepository.java @@ -1,41 +1,21 @@ package com.ghostchu.btn.sparkle.module.analyse.impl; -import lombok.Cleanup; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.data.redis.core.ScanOptions; -import org.springframework.stereotype.Service; +import com.ghostchu.btn.sparkle.module.repository.SparkleCommonRepository; +import org.springframework.stereotype.Repository; -import java.util.HashSet; import java.util.List; -import java.util.Set; -@Service -public class AnalysedRuleRepository { +@Repository +public interface AnalysedRuleRepository extends SparkleCommonRepository { //List findByModule(String module); - @Autowired - private RedisTemplate redisTemplate; + List findByModuleOrderByIpAsc(String module); - public void deleteAllByModule(String module) { - redisTemplate.unlink("analysed_rules:" + module); - } - - public Set findAll() { - Set analysedRules = new HashSet<>(); - @Cleanup - var it = redisTemplate.opsForSet().scan("analysed_rules:*", ScanOptions.scanOptions().build()); - while (it.hasNext()) { - analysedRules.add(it.next()); - } - return analysedRules; - } + long deleteAllByModule(String module); - public Set findAllByModule(String module) { - return redisTemplate.opsForSet().members("analysed_rules:" + module); - } + List findAll(); - public void replaceAll(String module, List rules) { + default void replaceAll(String module, List rules) { deleteAllByModule(module); - redisTemplate.opsForSet().add("analysed_rules:" + module, rules.toArray(new AnalysedRule[0])); + saveAll(rules); } }