|
|
1
|
+package com.monitor.other.maliciousip.job;
|
|
|
2
|
+
|
|
|
3
|
+import java.util.ArrayList;
|
|
|
4
|
+import java.util.List;
|
|
|
5
|
+import java.util.Timer;
|
|
|
6
|
+import java.util.TimerTask;
|
|
|
7
|
+
|
|
|
8
|
+import org.apache.commons.collections.CollectionUtils;
|
|
|
9
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
10
|
+import org.slf4j.Logger;
|
|
|
11
|
+import org.slf4j.LoggerFactory;
|
|
|
12
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
13
|
+
|
|
|
14
|
+import com.alibaba.fastjson.JSON;
|
|
|
15
|
+import com.monitor.other.maliciousip.MaliciousIpBL;
|
|
|
16
|
+import com.monitor.other.maliciousip.util.RedisReadUtil;
|
|
|
17
|
+import com.monitor.other.maliciousip.util.RedisWriteUtil;
|
|
|
18
|
+
|
|
|
19
|
+public class MaliciousIpJob {
|
|
|
20
|
+
|
|
|
21
|
+ /**
|
|
|
22
|
+ * 日志接口
|
|
|
23
|
+ */
|
|
|
24
|
+ private Logger logger = LoggerFactory.getLogger("banIPLogger");
|
|
|
25
|
+
|
|
|
26
|
+ @Autowired
|
|
|
27
|
+ private RedisReadUtil redisRead;
|
|
|
28
|
+
|
|
|
29
|
+ @Autowired
|
|
|
30
|
+ private RedisWriteUtil redisWrite;
|
|
|
31
|
+
|
|
|
32
|
+ /**
|
|
|
33
|
+ * 公司内部ip,需要过滤掉
|
|
|
34
|
+ */
|
|
|
35
|
+ private final static String YOHO_INTERNAL_IP = "172.31|10.66|10.67|54.222";
|
|
|
36
|
+
|
|
|
37
|
+ /**
|
|
|
38
|
+ * 比例大于等于99
|
|
|
39
|
+ */
|
|
|
40
|
+ private final static int PERCENT_MAX = 99;
|
|
|
41
|
+
|
|
|
42
|
+ /**
|
|
|
43
|
+ * 次数大于等于100
|
|
|
44
|
+ */
|
|
|
45
|
+ private final static int COUNT_MAX = 50;
|
|
|
46
|
+
|
|
|
47
|
+ /**
|
|
|
48
|
+ * 敏感接口比例大于等于99%,且敏感接口QPS大于等于100的值,暂时默认放到黑名单
|
|
|
49
|
+ */
|
|
|
50
|
+ public void init(){
|
|
|
51
|
+ logger.info("-init job!");
|
|
|
52
|
+ Timer timer = new Timer();
|
|
|
53
|
+ TimerTask timerTask = new TimerTask() {
|
|
|
54
|
+ @Override
|
|
|
55
|
+ public void run() {
|
|
|
56
|
+ //详情
|
|
|
57
|
+ logger.info("-init run job!");
|
|
|
58
|
+ uploadMaliciousIp();
|
|
|
59
|
+ }
|
|
|
60
|
+ }; //0:代表是延迟0
|
|
|
61
|
+ timer.scheduleAtFixedRate(timerTask, 0, 1* 30);
|
|
|
62
|
+ }
|
|
|
63
|
+
|
|
|
64
|
+ private void uploadMaliciousIp(){
|
|
|
65
|
+ try{
|
|
|
66
|
+ String ips_json = redisRead.getIpsBL();
|
|
|
67
|
+ if(StringUtils.isBlank(ips_json)){
|
|
|
68
|
+ return;
|
|
|
69
|
+ }
|
|
|
70
|
+ List<MaliciousIpBL> listMaliciousIp;
|
|
|
71
|
+ List<MaliciousIpBL> listBlackListIp;
|
|
|
72
|
+ String yoho_ip = YOHO_INTERNAL_IP;
|
|
|
73
|
+ listMaliciousIp = (List<MaliciousIpBL>) JSON.parseArray(ips_json, MaliciousIpBL.class);
|
|
|
74
|
+ if(CollectionUtils.isNotEmpty(listMaliciousIp)){
|
|
|
75
|
+ listBlackListIp = new ArrayList<>();
|
|
|
76
|
+ for (MaliciousIpBL maliciousIp : listMaliciousIp) {
|
|
|
77
|
+ //过滤yoho内外网
|
|
|
78
|
+ if(StringUtils.isNotBlank(yoho_ip) && StringUtils.isNoneBlank(maliciousIp.getUserIP())){
|
|
|
79
|
+ String []arrIp = StringUtils.split(maliciousIp.getUserIP(),'.');
|
|
|
80
|
+ String inIp = arrIp[0] + "." + arrIp[1];
|
|
|
81
|
+ if(yoho_ip.indexOf(inIp) >= 0){
|
|
|
82
|
+ continue;
|
|
|
83
|
+ }
|
|
|
84
|
+ }
|
|
|
85
|
+
|
|
|
86
|
+ //敏感接口比例大于等于99%,且敏感接口QPS大于等于100的值,暂时默认放到黑名单
|
|
|
87
|
+ if((maliciousIp.getPercent() >= PERCENT_MAX) && (maliciousIp.getCount() >= COUNT_MAX)){
|
|
|
88
|
+ listBlackListIp.add(maliciousIp);
|
|
|
89
|
+ }
|
|
|
90
|
+ }
|
|
|
91
|
+ //加入黑名单
|
|
|
92
|
+ if(CollectionUtils.isEmpty(listBlackListIp)){
|
|
|
93
|
+ return;
|
|
|
94
|
+ }
|
|
|
95
|
+ String[] arr_ip = new String[listBlackListIp.size()];
|
|
|
96
|
+ for (int index = 0; index < listBlackListIp.size(); index++) {
|
|
|
97
|
+ arr_ip[index] = listBlackListIp.get(index).getUserIP();
|
|
|
98
|
+ logger.info("-- Ban IP add to list: IP:{}, QPS:{}, Per:{}.", listBlackListIp.get(index).getUserIP(), listBlackListIp.get(index).getCount(), listBlackListIp.get(index).getPercent());
|
|
|
99
|
+ }
|
|
|
100
|
+
|
|
|
101
|
+ redisWrite.setIps(arr_ip);
|
|
|
102
|
+ }
|
|
|
103
|
+ }catch(Exception e){
|
|
|
104
|
+ logger.error("- MaliciousIpJob - uploadMaliciousIp,", e);
|
|
|
105
|
+ }
|
|
|
106
|
+ }
|
|
|
107
|
+} |