Advertisement
Guest User

NetDBFilterIP

a guest
Dec 8th, 2011
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.24 KB | None | 0 0
  1. package net.i2p.hack.scripts;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.BufferedReader;
  5. import java.io.BufferedWriter;
  6. import java.io.DataInputStream;
  7. import java.io.File;
  8. import java.io.FileInputStream;
  9. import java.io.FileWriter;
  10. import java.io.IOException;
  11. import java.io.InputStreamReader;
  12. import java.io.Serializable;
  13. import java.util.ArrayList;
  14. import java.util.HashSet;
  15. import java.util.List;
  16. import java.util.Scanner;
  17. import java.util.Set;
  18. import java.util.StringTokenizer;
  19.  
  20. import net.i2p.data.DataFormatException;
  21. import net.i2p.data.RouterAddress;
  22. import net.i2p.data.RouterInfo;
  23.  
  24. import org.apache.commons.lang3.StringUtils;
  25.  
  26. /**
  27.  * Search in every "router-info" file under "netDB" directory.
  28.  * Search for IP Addresses for each readed file.
  29.  * @author Adastra
  30.  * */
  31. public class NetDBFilterIP implements Serializable {
  32.  
  33.     /**
  34.      * Serial ID.
  35.      */
  36.     private static final long serialVersionUID = 1L;
  37.  
  38.    
  39.     /**
  40.      * The path of the results.
  41.      * */
  42.     private File storeResultsPath;
  43.    
  44.     /**
  45.      * List of IP Addresses to exclude.
  46.      * */
  47.     private static List<String> excludeAddresses = new ArrayList<String>();
  48.    
  49.     /**
  50.      * CSV File of Results separed with comma.
  51.      * */
  52.     private FileWriter addressesInsideNetDb;
  53.    
  54.     /**
  55.      * CSV File of Results separed with comma.
  56.      * */
  57.     private FileWriter addressNotFound;
  58.    
  59.     /**
  60.      * Main execution flow.
  61.      * @param args Parameters.
  62.      * */
  63.     public static void main(String ... args) {
  64.         excludeAddresses.add("192.168.1.33");
  65.         excludeAddresses.add("192.168.1.1");
  66.         NetDBFilterIP netFilter = new NetDBFilterIP();
  67.         netFilter.readNetDBDirectory();
  68.     }
  69.    
  70.     /**
  71.      * Reads the NetDB directory.
  72.      * */
  73.     private void readNetDBDirectory() {
  74.         String i2pNetDB="";
  75.         System.out.println("Enter the location NetDb Directory\n");
  76.         Scanner scanner = new Scanner(System.in);
  77.         i2pNetDB = scanner.next();
  78.         System.out.println("NetDb Directory: "+i2pNetDB);
  79.         searchIPAddresses(i2pNetDB);
  80.    
  81.     }
  82.    
  83.     /**
  84.      * Search the IP Addresses registered in every "Route-info" file of the Local NetDB.
  85.      * */
  86.     private void searchIPAddresses(String i2pNetDB) {
  87.         File netDBRoot = new File(i2pNetDB);
  88.         List<RouterInfo> routersNetDB = new ArrayList<RouterInfo>();
  89.         readScanningDir();
  90.         try {
  91.             File[] files = netDBRoot.listFiles();
  92.             for(File file : files) {
  93.                 System.out.println("File "+file.getName());
  94.                 BufferedInputStream binput = new BufferedInputStream(new FileInputStream(file));
  95.                 RouterInfo router = new RouterInfo();
  96.                 try {
  97.                     router.readBytes(binput);
  98.                 } catch (DataFormatException e) {
  99.                     e.printStackTrace();
  100.                 }
  101.                 routersNetDB.add(router);
  102.             }
  103.             System.out.println("Enter the Path of the CVS generated from PCAP file");
  104.             Scanner scanner = new Scanner(System.in);
  105.             String rutaCSVCap = scanner.next();
  106.             DataInputStream in = new DataInputStream(new FileInputStream(new File(rutaCSVCap)));
  107.             BufferedReader br = new BufferedReader(new InputStreamReader(in));
  108.             String line="";
  109.             List<String> addressesCSV = new ArrayList<String>();
  110.             while((line = br.readLine()) != null) {
  111.                 StringTokenizer tokenizer = new StringTokenizer(line, ",");
  112.                 if(tokenizer.hasMoreTokens()) {
  113.                     String ipAddress = tokenizer.nextToken();
  114.                     if(StringUtils.isNotEmpty(ipAddress) && ipAddress.contains(".")) {
  115.                         addressesCSV.add(ipAddress);
  116.                     }  
  117.                 }
  118.             }
  119.             addressesCSV.removeAll(excludeAddresses);
  120.             Set<String> uniqueAddresses = new HashSet<String>(addressesCSV);
  121.            
  122.             Set<String> addressesFound = new HashSet<String>();
  123.             Set<String> addressesNotFound = new HashSet<String>();
  124.            
  125.             for(RouterInfo router : routersNetDB) {
  126.                 for(RouterAddress routerAddress : router.getAddresses()) {
  127.                     if(uniqueAddresses.contains(routerAddress.getOptions().get("host"))) {
  128.                         addressesFound.add(routerAddress.getOptions().get("host").toString());
  129.                     } else {
  130.                         if(routerAddress.getOptions().get("host") == null) {
  131.                             for(int i = 0; i <=5; i++){
  132.                                 if(routerAddress.getOptions().get("ihost"+i) != null) {
  133.                                     if(uniqueAddresses.contains(routerAddress.getOptions().get("ihost"+i)))
  134.                                         addressesFound.add(routerAddress.getOptions().get("ihost"+i).toString());
  135.                                     else
  136.                                         addressesNotFound.add(routerAddress.getOptions().get("ihost"+i).toString());    
  137.                                 }  
  138.                             }
  139.                             continue;
  140.                         }
  141.                         addressesNotFound.add(routerAddress.getOptions().get("host").toString());
  142.                     }
  143.                 }  
  144.             }
  145.             for(String address : addressesFound) {
  146.                 storeAddress(address+"\n");
  147.             }
  148.            
  149.             for(String address : addressesNotFound) {
  150.                 storeNotFound(address+"\n");
  151.             }
  152.         } catch (Exception e) {
  153.             // TODO Auto-generated catch block
  154.             e.printStackTrace();
  155.         }
  156.     }
  157.    
  158.     /**
  159.      * Open and appends the file with addresses found.
  160.      * */
  161.     private void storeAddress(String content) {
  162.         try {
  163.             addressesInsideNetDb = new FileWriter(new File(storeResultsPath+System.getProperty("file.separator")+"addressesInsideNetDb.txt"), true);
  164.             BufferedWriter csvAddressWriter = new BufferedWriter(addressesInsideNetDb);
  165.             csvAddressWriter.write(content);
  166.             csvAddressWriter.close();          
  167.         } catch (IOException e) {
  168.             // TODO Auto-generated catch block
  169.             e.printStackTrace();
  170.         }
  171.     }
  172.    
  173.     /**
  174.      * Open and appends the file with addresses not found.
  175.      * */
  176.     private void storeNotFound(String content) {
  177.         try {
  178.             addressNotFound = new FileWriter(new File(storeResultsPath+System.getProperty("file.separator")+"addressesNotFound.txt"), true);
  179.             BufferedWriter csvAddressNotFoundWriter = new BufferedWriter(addressNotFound);
  180.             csvAddressNotFoundWriter.write(content);
  181.             csvAddressNotFoundWriter.close();                  
  182.         } catch (IOException e) {
  183.             // TODO Auto-generated catch block
  184.             e.printStackTrace();
  185.         }
  186.     }
  187.    
  188.     /**
  189.      * Read the Scanning Directory to store all result files.
  190.      * */
  191.     private void readScanningDir() {
  192.         System.out.println("Enter the Directory to Store Files \n");
  193.         Scanner scanner = new Scanner(System.in);
  194.         String dirStore = scanner.next();
  195.         System.out.println("Dir. to store: "+dirStore);
  196.         storeResultsPath = new File(dirStore);
  197.         // /media/Adastra/ScanningResultsI2P
  198.         if(!storeResultsPath.exists()) {
  199.             storeResultsPath.mkdir();  
  200.         } else {
  201.             File resultsCSV = new File(dirStore+System.getProperty("file.separator")+"resultsCSV.txt");
  202.             if(resultsCSV.exists()) {
  203.                 try {
  204.                     DataInputStream in = new DataInputStream(new FileInputStream(resultsCSV));
  205.                     BufferedReader br = new BufferedReader(new InputStreamReader(in));
  206.                     String informationEepsite;
  207.                     List<String> informationEepsites = new ArrayList<String>();
  208.                     while ((informationEepsite = br.readLine()) != null) {
  209.                         informationEepsites.add(informationEepsite);
  210.                     }
  211.                     in.close();
  212.                     br.close();
  213.                    
  214.                     Set<String> uniqueInformation = new HashSet<String>(informationEepsites);
  215.                    
  216.                     FileWriter resultsCSVUnique = new FileWriter(resultsCSV, false);
  217.                     BufferedWriter csvAddressNotFoundWriter = new BufferedWriter(resultsCSVUnique);
  218.                     for(String eepsiteInfo : uniqueInformation)
  219.                         csvAddressNotFoundWriter.write(eepsiteInfo);
  220.                     csvAddressNotFoundWriter.close();
  221.                 }catch (IOException e) {
  222.                     System.out.println("Cleanning of results Failed...");
  223.                     e.printStackTrace();
  224.                 }  
  225.             }  
  226.            
  227.         }
  228.     }
  229. }
  230.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement