Java browser: Proxy.java

Content of Proxy.java extracted from proxy.jar

// PROXY HTTP 1.0
// 01-11-2006
// version 0.4: filter corrected
// version 0.5: downloaded files are put in separate directories
// version 0.6: changed file naming, add command line options
// version 0.7: package introduced
 
// idee di miglioramento
// -- introdurre gestione tramite GUI
// -- estendere filtri
//
// le richieste del client hanno il formato: METHOD host[:port][path]
// il proxy replica le richieste verso il server host[:port] nel seguente formato: METHOD [path]
 
// questo programma può funzionare come un normale proxy o come un tunnel
package proxy;
 
import java.net.*;
import java.io.*;
import java.util.*;
import parser.*;
 
//
//  Proxy
//
 
public class Proxy {
static final String cache = "cache";  // directory used to contain files
 
static int clientCount = 0;
static PrintWriter out = null;
 
static Set<String> wordSet = null; // definito per filterProxy
static boolean tunnel = false;  // specifica se si comporta da tunnel o da proxy
static String tunnel_host; // host remoto su cui fare tunnel
static int tunnel_port; // port remoto
 
public void run(int localport) {
 try {
	ServerSocket sSocket = new ServerSocket(localport);
	System.out.println("proxy started...");
	Runtime.getRuntime().addShutdownHook(new shutdown());
	while(true) {
		ProxyConnection c = new ProxyConnection(sSocket.accept());
	}
 } catch(Throwable t) {
	t.printStackTrace(System.err);
 } finally {
	System.out.println("exiting...");
 }
}
 
public static void main(String[] args) throws IOException, ParserException {
 Proxy self = new Proxy();
 Parser cli = new Parser();
 Option filter = cli.addOption("-filter", true, "<filename>","list of words to filter");
 Option log = cli.addOption("-log", false, null,"create the log file proxy.log");
 cli.addOption("-help", false, null,"print this help message");
 String [] result = cli.parse(args); 
 if (result.length >= 1) {
  if (cli.hasOption(filter))  
  {     wordSet = new HashSet<String>();
		LineNumberReader in = new LineNumberReader(new FileReader(cli.getValue(filter)));
		String s;
		while ((s = in.readLine()) != null)
		{
			if (s.trim().length() != 0)
			{  wordSet.add(s.trim().toUpperCase());
			}
 
		}
		in.close();
  }
  File dir =  new File(cache);
  dir.mkdir();
  try
  {
	  int localport = Integer.parseInt(result[0]);	
	  try
	  {
		tunnel_host = result[1];
		tunnel_port = Integer.parseInt(result[2]);
		tunnel = true;
		System.out.println("tunnel mode");
	  }
	  catch (RuntimeException e)  
	  { System.out.println("starting as proxy"); }
	  if (cli.hasOption(log)) out = new PrintWriter(new FileWriter("proxy.log"));
	  self.run(localport);
  }
  catch (RuntimeException e)
  {  System.out.println("localport has to be an integer");
  }
 
 
 } else {
	System.err.println("Usage: java proxy"+cli.getUsage("<localport> [<remotehost> <remoteport>]"));
	System.err.println();
	System.err.println("In case <remotehost> and <remoteport> are specified the program will act as a tunnel");
 }
}
 
} // proxy
 
 
 
 class shutdown extends Thread {
	 public void run() {
		System.out.println("Number of open connections: "+Proxy.clientCount);
	 }
 }
 
Share Share on Facebook Share on Twitter Bookmark on Reddit Share via mail
Privacy Policy Creative Commons Attribution-Share Alike Trovami