qq_41184589 2017-12-02 12:22 采纳率: 66.7%
浏览 1559
已结题

请教各位会JAVA的大神

请问各位JAVA大神,这源代码出了什么问题?
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
/*** @author Robbin Fan ***/
public class IPBanner {
public static final String NETSTAT = "netstat -nt";
public static final String IP_INSERT = "iptables -I INPUT -i eth0 -j DROP -p tcp --dport 80 -s ";
public static final String IP_DEL = "iptables -D INPUT -i eth0 -j DROP -p tcp --dport 80 -s ";
public static final String HOST_IP = "61.129.70.239:80";
public static final long BAN_TIMEOUT = 30 * 60 * 1000L;
public static final long BAN_INTERVAL = 30 * 1000L;
public static final int CONCURRENT = 80;
public static final int SYN_CONCURRENT = 8;
public static final Map banMap = new HashMap();
public static void ban()//throwsException
{
Set banList = dynamicBanIP();
System.out.println();
System.out.println("Time: " + new Date());
Runtime runtime = Runtime.getRuntime();
List expiredIPList = new ArrayList();

for(Iterator iter = banMap.entrySet().iterator(); iter.hasNext();){
Map.Entry entry = (Map.Entry) iter.next();
if((System.currentTimeMillis() - ((Long) entry.getValue()).longValue()) > BAN_TIMEOUT){
expiredIPList.add(entry.getKey());
}
}
for(int i = 0; i try {
runtime.exec(IP_DEL + expiredIPList.get(i));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("DEL IP: " + expiredIPList.get(i));
banMap.remove(expiredIPList.get(i));
}
for(Iterator iter = banList.iterator(); iter.hasNext();){
String ip = (String) iter.next();
if(!banMap.containsKey(ip)){
try {
runtime.exec(IP_INSERT + ip);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
banMap.put(ip, new Long(System.currentTimeMillis()));
System.out.println("BAN IP:" + ip);
}
}
System.out.println("---ban ip list---");
for(Iterator iter = banMap.keySet().iterator(); iter.hasNext();){
String ip = (String) iter.next();
System.out.println(ip);
}
}
public static Set dynamicBanIP()//throwsException
{
String ipstat = null;
Set banList = new HashSet();
List ipList = new ArrayList();
List countList = new ArrayList();
List synCountList = new ArrayList();
List finCountList = new ArrayList();
Runtime runtime = Runtime.getRuntime();
Process process = null;
try {
process = runtime.exec(NETSTAT);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputStream input = process.getInputStream();
InputStreamReader inputReader = new InputStreamReader(input);
BufferedReader reader = new BufferedReader(inputReader);
try {
reader.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
reader.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
while((ipstat = reader.readLine()) != null){
StringTokenizer token = new StringTokenizer(ipstat);
while(token.hasMoreTokens()){
token.nextToken();
token.nextToken();
token.nextToken();
String originalIP = token.nextToken();
String ip = token.nextToken().split(":")[0];
String status = token.nextToken();
if(HOST_IP.equals(originalIP)){
if(!ipList.contains(ip)){
ipList.add(ip);
countList.add(new Integer(1));
if("SYN_RECV".equals(status)){
synCountList.add(new Integer(1));
}else{
synCountList.add(new Integer(0));
}
if("FIN_WAIT1".equals(status)){
finCountList.add(new Integer(1));
}else{
finCountList.add(new Integer(0));
}
}else{
int index = ipList.indexOf(ip);
countList.set(index, new Integer(((Integer) countList.get(index)).intValue() + 1));
if("SYN_RECV".equals(status)){
synCountList.set(index, new Integer(((Integer) synCountList.get(index)).intValue() + 1));
}
if("FIN_WAIT1".equals(status)){
finCountList.set(index, new Integer(((Integer) finCountList.get(index)).intValue() + 1));
}
}
}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
inputReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
input.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
process.destroy();
for(int i = 0; i if(((Integer) countList.get(i)).intValue() > CONCURRENT)
banList.add(ipList.get(i));
if(((Integer) synCountList.get(i)).intValue() > SYN_CONCURRENT)
banList.add(ipList.get(i));
if(((Integer) finCountList.get(i)).intValue() > SYN_CONCURRENT)
banList.add(ipList.get(i));
}
return banList;
}
public static void main(String[] args)//throwsException
{while(true){
ban();
try {
Thread.sleep(BAN_INTERVAL);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
图片说明

  • 写回答

13条回答 默认 最新

  • qq_41252409 2017-12-02 12:31
    关注

    import java.util.HashMap;还有他如何还挺热乎还挺热乎投入而后热火分别被风忽然发表回复和你发表回复合法发布会烦恼放不放的

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条