1
|
package com.monitor.other.maliciousip.util;
|
1
|
package com.monitor.other.maliciousip.util;
|
2
|
|
2
|
|
|
|
3
|
+import org.slf4j.Logger;
|
|
|
4
|
+import org.slf4j.LoggerFactory;
|
|
|
5
|
+
|
3
|
import redis.clients.jedis.Jedis;
|
6
|
import redis.clients.jedis.Jedis;
|
4
|
import redis.clients.jedis.JedisPool;
|
7
|
import redis.clients.jedis.JedisPool;
|
5
|
import redis.clients.jedis.JedisPoolConfig;
|
8
|
import redis.clients.jedis.JedisPoolConfig;
|
|
@@ -10,11 +13,20 @@ import redis.clients.jedis.JedisPoolConfig; |
|
@@ -10,11 +13,20 @@ import redis.clients.jedis.JedisPoolConfig; |
10
|
*
|
13
|
*
|
11
|
*/
|
14
|
*/
|
12
|
public class RedisWriteUtil {
|
15
|
public class RedisWriteUtil {
|
|
|
16
|
+ /**
|
|
|
17
|
+ * 日志接口
|
|
|
18
|
+ */
|
|
|
19
|
+ private Logger logger = LoggerFactory.getLogger(getClass());
|
13
|
|
20
|
|
14
|
/**
|
21
|
/**
|
15
|
* Redis set 集合,里面放的都是ip
|
22
|
* Redis set 集合,里面放的都是ip
|
16
|
*/
|
23
|
*/
|
17
|
- private static final String MALICIOUS_IP = "yh:malicious:ips";
|
24
|
+ private static final String MALICIOUS_IP_HASH = "yh:malicious:ips";
|
|
|
25
|
+
|
|
|
26
|
+ /**
|
|
|
27
|
+ * Redis String 类型
|
|
|
28
|
+ */
|
|
|
29
|
+ private static final String MALICIOUS_IP = "yh:mip:";
|
18
|
|
30
|
|
19
|
/**
|
31
|
/**
|
20
|
* 黑名单失效时间24小时
|
32
|
* 黑名单失效时间24小时
|
|
@@ -101,44 +113,69 @@ public class RedisWriteUtil { |
|
@@ -101,44 +113,69 @@ public class RedisWriteUtil { |
101
|
* @return
|
113
|
* @return
|
102
|
*/
|
114
|
*/
|
103
|
public void setIps(String []ips) {
|
115
|
public void setIps(String []ips) {
|
|
|
116
|
+ try{
|
|
|
117
|
+ if((null == ips) || (0 == ips.length)){
|
|
|
118
|
+ return;
|
|
|
119
|
+ }
|
104
|
Jedis jedis_qq1 = getJedis_qq1();
|
120
|
Jedis jedis_qq1 = getJedis_qq1();
|
105
|
Jedis jedis_qq2 = getJedis_qq2();
|
121
|
Jedis jedis_qq2 = getJedis_qq2();
|
106
|
Jedis jedis_aws1 = getJedis_aws1();
|
122
|
Jedis jedis_aws1 = getJedis_aws1();
|
107
|
Jedis jedis_aws2 = getJedis_aws2();
|
123
|
Jedis jedis_aws2 = getJedis_aws2();
|
108
|
|
124
|
|
109
|
- jedis_qq1.sadd(MALICIOUS_IP, ips);
|
|
|
110
|
- jedis_qq1.expire(MALICIOUS_IP, MALICIOUS_IP_EXPIRE);
|
|
|
111
|
- jedis_qq2.sadd(MALICIOUS_IP, ips);
|
|
|
112
|
- jedis_qq2.expire(MALICIOUS_IP, MALICIOUS_IP_EXPIRE);
|
|
|
113
|
- jedis_aws1.sadd(MALICIOUS_IP, ips);
|
|
|
114
|
- jedis_aws1.expire(MALICIOUS_IP, MALICIOUS_IP_EXPIRE);
|
|
|
115
|
- jedis_aws2.sadd(MALICIOUS_IP, ips);
|
|
|
116
|
- jedis_aws2.expire(MALICIOUS_IP, MALICIOUS_IP_EXPIRE);
|
125
|
+ //此处用于查询
|
|
|
126
|
+ /*jedis_aws1.sadd(MALICIOUS_IP_HASH, ips);
|
|
|
127
|
+ jedis_aws1.expire(MALICIOUS_IP_HASH, MALICIOUS_IP_EXPIRE);*/
|
|
|
128
|
+
|
|
|
129
|
+ //此处用于拦截
|
|
|
130
|
+ for (String ip : ips) {
|
|
|
131
|
+ jedis_qq1.set(MALICIOUS_IP + ip, "0");
|
|
|
132
|
+ jedis_qq1.expire(MALICIOUS_IP + ip, MALICIOUS_IP_EXPIRE);
|
|
|
133
|
+ jedis_qq2.set(MALICIOUS_IP + ip, "0");
|
|
|
134
|
+ jedis_qq2.expire(MALICIOUS_IP + ip, MALICIOUS_IP_EXPIRE);
|
|
|
135
|
+ jedis_aws1.set(MALICIOUS_IP + ip, "0");
|
|
|
136
|
+ jedis_aws1.expire(MALICIOUS_IP + ip, MALICIOUS_IP_EXPIRE);
|
|
|
137
|
+ jedis_aws2.set(MALICIOUS_IP + ip, "0");
|
|
|
138
|
+ jedis_aws2.expire(MALICIOUS_IP + ip, MALICIOUS_IP_EXPIRE);
|
|
|
139
|
+ }
|
117
|
|
140
|
|
118
|
returnResource(jedis_qq1);
|
141
|
returnResource(jedis_qq1);
|
119
|
returnResource(jedis_qq2);
|
142
|
returnResource(jedis_qq2);
|
120
|
returnResource(jedis_aws1);
|
143
|
returnResource(jedis_aws1);
|
121
|
returnResource(jedis_aws2);
|
144
|
returnResource(jedis_aws2);
|
|
|
145
|
+ }catch(Exception e){
|
|
|
146
|
+ logger.error(" - RedisWriteUtil - setIps - error", e);
|
|
|
147
|
+ }
|
122
|
}
|
148
|
}
|
123
|
|
149
|
|
124
|
public void removeIp(String[] ips){
|
150
|
public void removeIp(String[] ips){
|
|
|
151
|
+ try{
|
|
|
152
|
+ if((null == ips) || (0 == ips.length)){
|
|
|
153
|
+ return;
|
|
|
154
|
+ }
|
125
|
Jedis jedis_qq1 = getJedis_qq1();
|
155
|
Jedis jedis_qq1 = getJedis_qq1();
|
126
|
Jedis jedis_qq2 = getJedis_qq2();
|
156
|
Jedis jedis_qq2 = getJedis_qq2();
|
127
|
Jedis jedis_aws1 = getJedis_aws1();
|
157
|
Jedis jedis_aws1 = getJedis_aws1();
|
128
|
Jedis jedis_aws2 = getJedis_aws2();
|
158
|
Jedis jedis_aws2 = getJedis_aws2();
|
129
|
|
159
|
|
130
|
- jedis_qq1.srem(MALICIOUS_IP, ips);
|
|
|
131
|
- jedis_qq1.expire(MALICIOUS_IP, MALICIOUS_IP_EXPIRE);
|
|
|
132
|
- jedis_qq2.srem(MALICIOUS_IP, ips);
|
|
|
133
|
- jedis_qq2.expire(MALICIOUS_IP, MALICIOUS_IP_EXPIRE);
|
|
|
134
|
- jedis_aws1.srem(MALICIOUS_IP, ips);
|
|
|
135
|
- jedis_aws1.expire(MALICIOUS_IP, MALICIOUS_IP_EXPIRE);
|
|
|
136
|
- jedis_aws2.srem(MALICIOUS_IP, ips);
|
|
|
137
|
- jedis_aws2.expire(MALICIOUS_IP, MALICIOUS_IP_EXPIRE);
|
160
|
+ //此处用于查询
|
|
|
161
|
+ /*jedis_aws1.srem(MALICIOUS_IP_HASH, ips);
|
|
|
162
|
+ jedis_aws1.expire(MALICIOUS_IP_HASH, MALICIOUS_IP_EXPIRE);*/
|
|
|
163
|
+
|
|
|
164
|
+ for (int index = 0; index < ips.length; index ++){
|
|
|
165
|
+ ips[index] = MALICIOUS_IP + ips[index];
|
|
|
166
|
+ }
|
|
|
167
|
+ //此处用于拦截
|
|
|
168
|
+ jedis_qq1.del(ips);
|
|
|
169
|
+ jedis_qq2.del(ips);
|
|
|
170
|
+ jedis_aws1.del(ips);
|
|
|
171
|
+ jedis_aws2.del(ips);
|
138
|
|
172
|
|
139
|
returnResource(jedis_qq1);
|
173
|
returnResource(jedis_qq1);
|
140
|
returnResource(jedis_qq2);
|
174
|
returnResource(jedis_qq2);
|
141
|
returnResource(jedis_aws1);
|
175
|
returnResource(jedis_aws1);
|
142
|
returnResource(jedis_aws2);
|
176
|
returnResource(jedis_aws2);
|
|
|
177
|
+ }catch(Exception e){
|
|
|
178
|
+ logger.error(" - RedisWriteUtil - setIps - error", e);
|
|
|
179
|
+ }
|
143
|
}
|
180
|
}
|
144
|
} |
181
|
} |