Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghost-chu committed Dec 21, 2024
1 parent b357abc commit 9019bd4
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public LettuceConnectionFactory redisConnectionFactory(@Value("${sparkle.redis.u

@Bean
@Primary
public RedisTemplate<Object, Object> redisTemplate(LettuceConnectionFactory redisConnectionFactory, ObjectMapper mapper) {
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
public RedisTemplate<String, String> redisTemplate(LettuceConnectionFactory redisConnectionFactory, ObjectMapper mapper) {
RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer(mapper));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,21 +137,23 @@ HAVING SUM(app_count) > ?
ipMerger.merge(ipTries);
List<AnalysedRule> 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<AnalysedRule> getAnalysedRules() {
@Transactional
public List<AnalysedRule> getAnalysedRules() {
return analysedRuleRepository.findAll();
}

public Set<AnalysedRule> getUntrustedIPAddresses() {
return analysedRuleRepository.findAllByModule(UNTRUSTED_IP);
public List<AnalysedRule> getUntrustedIPAddresses() {
return analysedRuleRepository.findByModuleOrderByIpAsc(UNTRUSTED_IP);
}

public Set<AnalysedRule> getOverDownloadIPAddresses() {
return analysedRuleRepository.findAllByModule(OVER_DOWNLOAD);
public List<AnalysedRule> getOverDownloadIPAddresses() {
return analysedRuleRepository.findByModuleOrderByIpAsc(OVER_DOWNLOAD);
}

@Transactional
Expand Down Expand Up @@ -183,8 +185,8 @@ public void cronHighRiskIps() throws InterruptedException {
log.info("High risk IPs: {}, tooked {} ms", highRiskIps.size(), System.currentTimeMillis() - startAt);
}

public Set<AnalysedRule> getHighRiskIps() {
return analysedRuleRepository.findAllByModule(HIGH_RISK_IP);
public List<AnalysedRule> getHighRiskIps() {
return analysedRuleRepository.findByModuleOrderByIpAsc(HIGH_RISK_IP);
}
//
// @Transactional
Expand Down Expand Up @@ -224,8 +226,8 @@ public Set<AnalysedRule> getHighRiskIps() {
// }
// }

public Set<AnalysedRule> getHighRiskIPV6Identity() {
return analysedRuleRepository.findAllByModule(HIGH_RISK_IPV6_IDENTITY);
public List<AnalysedRule> getHighRiskIPV6Identity() {
return analysedRuleRepository.findByModuleOrderByIpAsc(HIGH_RISK_IPV6_IDENTITY);
}

@Transactional
Expand Down Expand Up @@ -291,8 +293,8 @@ private void scanFile(Consumer<Peer.PeerInfo> predicate) {
}
}

public Set<AnalysedRule> getTrackerHighRisk() {
return analysedRuleRepository.findAllByModule(TRACKER_HIGH_RISK);
public List<AnalysedRule> getTrackerHighRisk() {
return analysedRuleRepository.findByModuleOrderByIpAsc(TRACKER_HIGH_RISK);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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<AnalysedRule, Long> {
//List<AnalysedRule> findByModule(String module);
@Autowired
private RedisTemplate<String, AnalysedRule> redisTemplate;
List<AnalysedRule> findByModuleOrderByIpAsc(String module);

public void deleteAllByModule(String module) {
redisTemplate.unlink("analysed_rules:" + module);
}

public Set<AnalysedRule> findAll() {
Set<AnalysedRule> 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<AnalysedRule> findAllByModule(String module) {
return redisTemplate.opsForSet().members("analysed_rules:" + module);
}
List<AnalysedRule> findAll();

public void replaceAll(String module, List<AnalysedRule> rules) {
default void replaceAll(String module, List<AnalysedRule> rules) {
deleteAllByModule(module);
redisTemplate.opsForSet().add("analysed_rules:" + module, rules.toArray(new AnalysedRule[0]));
saveAll(rules);
}
}

0 comments on commit 9019bd4

Please sign in to comment.