Advertisement
Guest User

SearchEEPSitesI2P

a guest
Dec 8th, 2011
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.99 KB | None | 0 0
  1. package net.i2p.hack.scripts;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.DataInputStream;
  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.io.FileNotFoundException;
  9. import java.io.FileWriter;
  10. import java.io.IOException;
  11. import java.io.InputStreamReader;
  12. import java.net.HttpURLConnection;
  13. import java.net.URL;
  14. import java.util.Properties;
  15. import java.util.Scanner;
  16. import java.util.StringTokenizer;
  17.  
  18. import org.apache.commons.lang3.StringUtils;
  19.  
  20. /**
  21.  * Checker of EEPSites status.
  22.  * @author Adastra.
  23.  * */
  24. public class SearchEEPSitesI2P implements Runnable {
  25.    
  26.     /**
  27.      * The path where the file "hosts.txt" is located.
  28.      * */
  29.     private FileInputStream hostsI2P;
  30.    
  31.     /**
  32.      * The path of the results.
  33.      * */
  34.     private File storeResultsPath;
  35.    
  36.     /**
  37.      * HTML File of Results by EEPSITE.
  38.      * */
  39.     private FileWriter iframeFile;
  40.  
  41.     /**
  42.      * CSV File of Results separed with comma.
  43.      * */
  44.     private FileWriter resultsCSVFile;
  45.    
  46.     /**
  47.      * CSV File of Results separed with comma.
  48.      * */
  49.     private FileWriter eepsitesFailed;
  50.  
  51.     /**
  52.      * Main Point
  53.      * */
  54.     public static void main(String ... params){
  55.         SearchEEPSitesI2P spider = new SearchEEPSitesI2P();
  56.         spider.readHostsI2P();
  57.         spider.readScanningDir(spider);
  58.         if(spider.hostsI2P == null) {
  59.             return;
  60.         }
  61.         spider.crawlEepsites();    
  62.     }
  63.  
  64.  
  65.     /**
  66.      * Read the hosts.txt file to parse the EEPSITES registered in the I2P Instance.
  67.      * */
  68.     private void readHostsI2P() {
  69.         String i2pHome="";
  70.         System.out.println("Enter the location of hosts.txt file\n");
  71.         ///home/adastra/.i2p/hosts.txt
  72.         Scanner scanner = new Scanner(System.in);
  73.         i2pHome = scanner.next();
  74.         System.out.println("Hosts File: "+i2pHome);
  75.         try {
  76.             hostsI2P = new FileInputStream(new File(i2pHome));
  77.         } catch(FileNotFoundException file_ex) {
  78.             file_ex.printStackTrace();
  79.             System.out.println("Exception: "+file_ex.getMessage());
  80.         }
  81.     }
  82.    
  83.     /**
  84.      * Read the Scanning Directory to store all result files.
  85.      * */
  86.     private void readScanningDir(SearchEEPSitesI2P spider) {
  87.         System.out.println("Enter the Directory to Store Files \n");
  88.         Scanner scanner = new Scanner(System.in);
  89.         String dirStore = scanner.next();
  90.         System.out.println("Dir. to store: "+dirStore);
  91.         spider.storeResultsPath = new File(dirStore);
  92.         // /media/Adastra/ScanningResultsI2P
  93.         if(!spider.storeResultsPath.exists()) {
  94.             spider.storeResultsPath.mkdir();  
  95.         }
  96.     }
  97.    
  98.     /**
  99.      * Search the EEPSITES.
  100.      * */
  101.     private void crawlEepsites() {
  102.         try {
  103.             DataInputStream in = new DataInputStream(hostsI2P);
  104.             BufferedReader br = new BufferedReader(new InputStreamReader(in));
  105.             String eepsite;
  106.             while ((eepsite = br.readLine()) != null) {
  107.                 Thread crawler = new Thread(this);
  108.                 crawler.setName(eepsite);
  109.                 crawler.start();
  110.             }
  111.             in.close();
  112.             br.close();
  113.         } catch (IOException e) {
  114.             e.printStackTrace();
  115.         }
  116.        
  117.     }
  118.  
  119.  
  120.     /**
  121.      * Read every line in the hosts.txt file an performs the connection with the site.
  122.      * */
  123.     @Override
  124.     public void run() {
  125.         connectEepsite(Thread.currentThread().getName());
  126.     }
  127.    
  128.     /**
  129.      * Performs the connection with the EEPSITE.
  130.      * @param eepsite EEPSITE to connect.
  131.      * */
  132.     private void connectEepsite(final String eepsite) {
  133.         HttpURLConnection connection = null;   
  134.         try {
  135.             StringTokenizer tokenizer = new StringTokenizer(eepsite, "=");
  136.             while(tokenizer.hasMoreElements()) {
  137.                 String eepsiteHttp = "http://"+tokenizer.nextToken();
  138.                 URL rutaPeticion = new URL(eepsiteHttp);   
  139.                 Properties sysProps = System.getProperties();
  140.                 sysProps.put( "http.proxyHost", "127.0.0.1");
  141.                 sysProps.put( "http.proxyPort", "8080" );
  142.                 connection = (HttpURLConnection)rutaPeticion.openConnection();
  143.                 connection.setRequestProperty("Content-type", "html/text");  
  144.                 connection.connect();
  145.                 if(Math.round((connection.getResponseCode()/100)) == 2) {
  146.                     BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  147.                     String content = "";
  148.                     while(br.readLine() != null) {
  149.                         content = content+"\n"+br.readLine();              
  150.                     }
  151.                     String iframeName = storeResultsPath+System.getProperty("file.separator")+StringUtils.replace(StringUtils.remove(eepsiteHttp, "http://"), ".i2p", ".html");
  152.                     iframeFile = new FileWriter(new File(iframeName));     
  153.                     BufferedWriter iframeWriter = new BufferedWriter(iframeFile);
  154.                     iframeWriter.write(content);
  155.                     iframeWriter.close();
  156.                     storeInformationEepsites(eepsiteHttp+","+connection.getResponseCode()+","+connection.getResponseMessage()+","+connection.getHeaderField("Server")+","+connection.getHeaderField("Host")+","+connection.getLastModified()+"\n");                    
  157.                 } else {
  158.                     storeFailedEepsites(eepsiteHttp+","+connection.getResponseCode()+","+connection.getResponseMessage()+","+connection.getHeaderField("Server")+","+connection.getHeaderField("Host")+","+connection.getLastModified()+"\n");
  159.                 }
  160.                    
  161.             }  
  162.            
  163.         }catch(IOException ioException){
  164.         } finally {
  165.             connection.disconnect();
  166.         }
  167.        
  168.     }
  169.    
  170.     /**
  171.      * Store Banned information about EEPSITES scanned in the network.
  172.      * */
  173.     private void storeInformationEepsites(String eepsite) {
  174.         try {
  175.             resultsCSVFile = new FileWriter(new File(storeResultsPath+System.getProperty("file.separator")+"resultsCSV.txt"), true);
  176.             BufferedWriter csvWriter = new BufferedWriter(resultsCSVFile);
  177.             csvWriter.write(eepsite);
  178.             csvWriter.close();             
  179.         } catch (IOException e) {
  180.             // TODO Auto-generated catch block
  181.             e.printStackTrace();
  182.         }  
  183.     }
  184.    
  185.     /**
  186.      * Store the failed addresses to find EEPSITES.
  187.      * */
  188.     private void storeFailedEepsites(String failed) {
  189.         try {
  190.             eepsitesFailed = new FileWriter(new File(storeResultsPath+System.getProperty("file.separator")+"eepsitesFailed.txt"), true);
  191.             BufferedWriter eepsitesFailedWriter = new BufferedWriter(eepsitesFailed);
  192.             eepsitesFailedWriter.write(failed);
  193.             eepsitesFailedWriter.close();
  194.         } catch (IOException e) {
  195.             // TODO Auto-generated catch block
  196.             e.printStackTrace();
  197.         }      
  198.     }  
  199. }
  200.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement