Become a fan of Slashdot on Facebook

 



Forgot your password?
typodupeerror
×
User Journal

Journal fishdan's Journal: AccessMonitor.java

import java.io.*;
import java.util.*;
import java.net.*;
import java.text.SimpleDateFormat;

public class AccessMonitor {

  public static void main(String argv[]){
    try{
      File localLog=new File ("AccessMonitor.log");
      if(localLog.length()>100000L){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
        localLog.renameTo(new File(sdf.format(new Date())+"_AccessMonitor.log"));
      }
      FileWriter fw=new FileWriter("AccessMonitor.log",true);
      PrintWriter log=new PrintWriter(fw);
      Properties props=new Properties();
      props.load(new FileInputStream("AccessMonitor.cf"));
      System.out.println("Props loaded");
      FileReader reader=new FileReader(props.getProperty("LOGFILE"));
      BufferedReader in=new BufferedReader(reader);
      String bannedWords=props.getProperty("BANNED");
      StringTokenizer st=new StringTokenizer(bannedWords,",");
      Vector words=new Vector();
      while(st.hasMoreTokens()){
        words.addElement(st.nextToken());
      }
      Hashtable banned=new Hashtable();
      String line;
      //System.out.println("READ:"+line);
      while((line=in.readLine())!=null){
        for(int x=0;x<words.size();x++){
          if(line.indexOf((String)words.elementAt(x))>0){
            StringTokenizer str=new StringTokenizer(line," ");
            String iAddress=str.nextToken();
            try{
                InetAddress inet=InetAddress.getByName(iAddress);
                String ip=inet.getHostAddress().trim();

                if(banned.containsKey(ip)){
                    Integer count=(Integer)banned.get(ip);
                    count=new Integer(count.intValue()+1);
                    banned.put(ip,count);
                }
                else{
                    banned.put(ip,new Integer(1));
                }
            }
            catch(Exception e){
                System.out.println("Choked on "+line);
            }

            break;
          }
        }
      }
      ban(banned,props);

    }
    catch(Exception e){
      e.printStackTrace();
    }

  }
  public static void ban(Hashtable banned,Properties props){
    try{
      Socket s=new Socket(props.getProperty("FIREWALL"), 7328);
      PrintWriter pw=new PrintWriter(s.getOutputStream());
      Enumeration e=banned.keys();
      while(e.hasMoreElements()){
        String add=(String)e.nextElement();
        System.out.println("BADDY at "+add+" accessed "+((Integer)banned.get(add)).toString());
        pw.println(add);
        pw.flush();
      }

    }
    catch(Exception e){
      e.printStackTrace();
    }
  }

}

"Most of us, when all is said and done, like what we like and make up reasons for it afterwards." -- Soren F. Petersen

Working...