Authored by Xu

已经上传的恶意ip查询

package com.monitor.other.maliciousip;
/**
* 恶意ip内容集合
* @author hui.xu
*
*/
public class MaliciousIpLook {
/**
* 恶意ip
*/
private String ip;
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
}
\ No newline at end of file
... ...
... ... @@ -12,6 +12,7 @@ import org.springframework.web.bind.annotation.ResponseBody;
import com.monitor.model.response.BaseResponse;
import com.monitor.other.maliciousip.MaliciousDetailIp;
import com.monitor.other.maliciousip.MaliciousIp;
import com.monitor.other.maliciousip.MaliciousIpLook;
import com.monitor.other.maliciousip.UpLoadError;
import com.monitor.other.maliciousip.UpLoadMaliciousIp;
import com.monitor.other.maliciousip.service.impl.MaliciousIpServiceImpl;
... ... @@ -65,4 +66,32 @@ public class MaliciousIpController {
String []arr = StringUtils.split(ips.getIps(), '|');
return ipImpl.upLoad(arr);
}
/**
* 查看已经上传过的恶意ip
* @return
*/
@RequestMapping("/lookIps")
@ResponseBody
public BaseResponse<List<MaliciousIpLook>> lookIps(){
return ipImpl.lookIps();
}
/**
* 移除恶意代码
* @return
*/
@RequestMapping("/removeIp")
@ResponseBody
public BaseResponse<UpLoadError> removeIp(@RequestBody UpLoadMaliciousIp ips) {
BaseResponse<UpLoadError> response = new BaseResponse<>();
if(null == ips){
return response;
}
if(StringUtils.isBlank(ips.getIps())){
return response;
}
String []arr = StringUtils.split(ips.getIps(), '|');
return ipImpl.removeIp(arr);
}
}
\ No newline at end of file
... ...
... ... @@ -5,6 +5,7 @@ import java.util.List;
import com.monitor.model.response.BaseResponse;
import com.monitor.other.maliciousip.MaliciousDetailIp;
import com.monitor.other.maliciousip.MaliciousIp;
import com.monitor.other.maliciousip.MaliciousIpLook;
import com.monitor.other.maliciousip.UpLoadError;
/**
... ... @@ -33,4 +34,17 @@ public interface MaliciousIpService {
* @return
*/
BaseResponse<UpLoadError> upLoad(String[] ips);
/**
* 查看已经保存过得恶意ip
* @return
*/
BaseResponse<List<MaliciousIpLook>> lookIps();
/**
* 移除恶意ip
* @param ip
* @return
*/
BaseResponse<UpLoadError> removeIp(String[] ip);
}
\ No newline at end of file
... ...
package com.monitor.other.maliciousip.service.impl;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.math.NumberUtils;
... ... @@ -16,8 +18,10 @@ import com.alibaba.fastjson.JSON;
import com.monitor.model.response.BaseResponse;
import com.monitor.other.maliciousip.MaliciousDetailIp;
import com.monitor.other.maliciousip.MaliciousIp;
import com.monitor.other.maliciousip.MaliciousIpLook;
import com.monitor.other.maliciousip.UpLoadError;
import com.monitor.other.maliciousip.service.MaliciousIpService;
import com.monitor.other.maliciousip.util.RedisReadMaliciousUtil;
import com.monitor.other.maliciousip.util.RedisReadUtil;
import com.monitor.other.maliciousip.util.RedisWriteUtil;
... ... @@ -41,6 +45,9 @@ public class MaliciousIpServiceImpl implements MaliciousIpService {
@Autowired
private RedisWriteUtil redisWrite;
@Autowired
private RedisReadMaliciousUtil redisReadMalicious;
/* (non-Javadoc)
* @see com.monitor.other.maliciousip.service.MaliciousIpService#getIps()
* 从Redis中获取恶意ip集合
... ... @@ -111,6 +118,32 @@ public class MaliciousIpServiceImpl implements MaliciousIpService {
return response;
}
/* (non-Javadoc)
* @see com.monitor.other.maliciousip.service.MaliciousIpService#lookIps()
* 查看已经上传过的恶意ip
*/
@Override
public BaseResponse<List<MaliciousIpLook>> lookIps() {
BaseResponse<List<MaliciousIpLook>> response = new BaseResponse<>();
List<MaliciousIpLook> looks = new ArrayList<>();
MaliciousIpLook look;
try{
Set<String> values = redisReadMalicious.getIps();
if(CollectionUtils.isNotEmpty(values)){
for (String string : values) {
look = new MaliciousIpLook();
look.setIp(string);
looks.add(look);
}
response.setData(looks);
}
}catch(Exception e){
logger.error(" - MaliciousIpServiceImpl - getDetail - error", e);
}
return response;
}
class ComparatorIp implements Comparator<MaliciousIp> {
public int compare(MaliciousIp e1, MaliciousIp e2) {
int count1 = e1.getCount();
... ... @@ -138,4 +171,24 @@ public class MaliciousIpServiceImpl implements MaliciousIpService {
}
}
}
/**
* 移除恶意ip
* @param ips
* @return
*/
@Override
public BaseResponse<UpLoadError> removeIp(String[] ips) {
BaseResponse<UpLoadError> response = new BaseResponse<>();
UpLoadError error = new UpLoadError();
try{
redisWrite.removeIp(ips);
error.setStatus("0");
}catch(Exception e){
error.setStatus("1");
error.setMessage(e.getMessage());
}
response.setData(error);
return response;
}
}
\ No newline at end of file
... ...
package com.monitor.other.maliciousip.util;
import java.util.Set;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
* Redis 读 基本操作类
* @author hui.xu
*
*/
@Service
public class RedisReadMaliciousUtil {
/**
* Redis set 集合,里面放的都是ip
*/
private static final String MALICIOUS_IP = "yh:malicious:ips";
private JedisPool pool = null;
private String redis_address;
public void setRedis_address(String redis_address) {
this.redis_address = redis_address;
}
private int redis_port;
public void setRedis_port(int redis_port) {
this.redis_port = redis_port;
}
public void init(){
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(20);
config.setMaxIdle(10);
config.setMaxWaitMillis(1000 * 100);
pool = new JedisPool(config, redis_address, redis_port);
}
private Jedis getJedis() {
Jedis jedis = pool.getResource();
return jedis;
}
private void returnResource(Jedis redis) {
try {
if (redis != null) {
redis.close();
}
} catch (Exception e) {
}
}
/**
* 获取所有恶意ip集合
* @return
*/
public Set<String> getIps() {
Jedis jedis = getJedis();
Set<String> value = jedis.smembers(MALICIOUS_IP);
returnResource(jedis);
return value;
}
}
\ No newline at end of file
... ...
... ... @@ -17,9 +17,9 @@ public class RedisWriteUtil {
private static final String MALICIOUS_IP = "yh:malicious:ips";
/**
* 黑名单失效时间10分钟,顺时延长10分钟
* 黑名单失效时间24小时
*/
private static final int MALICIOUS_IP_EXPIRE = 10 * 60;
private static final int MALICIOUS_IP_EXPIRE = 24 * 60 * 60;
private JedisPool pool_qq1 = null;
private JedisPool pool_qq2 = null;
... ... @@ -120,4 +120,25 @@ public class RedisWriteUtil {
returnResource(jedis_aws1);
returnResource(jedis_aws2);
}
public void removeIp(String[] ips){
Jedis jedis_qq1 = getJedis_qq1();
Jedis jedis_qq2 = getJedis_qq2();
Jedis jedis_aws1 = getJedis_aws1();
Jedis jedis_aws2 = getJedis_aws2();
jedis_qq1.srem(MALICIOUS_IP, ips);
jedis_qq1.expire(MALICIOUS_IP, MALICIOUS_IP_EXPIRE);
jedis_qq2.srem(MALICIOUS_IP, ips);
jedis_qq2.expire(MALICIOUS_IP, MALICIOUS_IP_EXPIRE);
jedis_aws1.srem(MALICIOUS_IP, ips);
jedis_aws1.expire(MALICIOUS_IP, MALICIOUS_IP_EXPIRE);
jedis_aws2.srem(MALICIOUS_IP, ips);
jedis_aws2.expire(MALICIOUS_IP, MALICIOUS_IP_EXPIRE);
returnResource(jedis_qq1);
returnResource(jedis_qq2);
returnResource(jedis_aws1);
returnResource(jedis_aws2);
}
}
\ No newline at end of file
... ...
... ... @@ -22,6 +22,11 @@
<property name = "redis_port" value="${malicious.ip.redis.port}" />
</bean>
<bean id="redisReadMaliciousUtil" class="com.monitor.other.maliciousip.util.RedisReadMaliciousUtil" init-method="init">
<property name = "redis_address" value="${malicious.ip.write.aws.host1}" />
<property name = "redis_port" value="${malicious.ip.redis.port}" />
</bean>
<bean id="sheduletask" class="com.monitor.other.task.SheduleTask">
<property name="taskScheduler" ref="scheduler"/>
... ...