Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: Freenet/contrib/fproxy/ProxyServer.java


1   package Freenet.contrib.fproxy;
2   import Freenet.*;
3   import Freenet.support.*;
4   import java.io.*;
5   import java.net.*;
6   
7   
8   /*
9     This code is part of fproxy, an HTTP proxy server for Freenet.
10    It is distributed under the GNU Public Licence (GPL) version 2.  See
11    http://www.gnu.org/ for further details of the GPL.
12  */
13  
14  
15  /**
16   * The main ProxyServer class.  It listens for incoming HTTP connections on 
17   * -listenPort (default 80), extracts the Freenet key, and issues a proxy 
18   * request on the web client's behalf to the Freenet node at -serverAddress 
19   * (default tcp/localhost:19114).  The reply is streamed directly over the
20   * HTTP connection back to the web client, with the appropriate MIME type
21   * (currently, always text/plain).  Any errors are also reported to the 
22   * web client.
23   *
24   * @author <a href="http://www.doc.ic.ac.uk/~twh1/">Theodore Hong</a>
25   **/
26  
27  public class ProxyServer
28  {
29      // server parameters
30      public int listenPort;
31      public String serverAddress;
32      public long hopsToLive;
33      public PrintStream Lout = Logger.Lout;
34  
35      // defaults
36      static private int defaultListenPort = 80;
37      static private String defaultServerAddress = "tcp/localhost:19114";
38      static private long defaultHopsToLive = 5;
39      static private int defaultLogging = Logger.NORMAL;
40      static private int defaultVerbosity = 1;
41  
42      static public void main(String[] args) {
43    // parse command line
44    Logger.threshold = Logger.ERROR;
45    Params params = new Params(args);
46  
47    // check for help
48    if (params.getParam("help") != null) {
49        usage();
50        return;
51    }
52  
53    // process options
54    int port = params.getint("listenPort", defaultListenPort);
55    String addr = params.getParam("serverAddress", defaultServerAddress);
56    long htl = params.getlong("htl", defaultHopsToLive);
57    ProxyServer ps = new ProxyServer(port, addr, htl);
58  
59    // set logging
60          if (params.getParam("logging")==null)
61              Logger.threshold = defaultLogging;
62    else if (params.getParam("logging").equalsIgnoreCase("error"))
63        Logger.threshold = Logger.ERROR;
64    else if (params.getParam("logging").equalsIgnoreCase("normal"))
65        Logger.threshold = Logger.NORMAL;
66    else if (params.getParam("logging").equalsIgnoreCase("minor"))
67        Logger.threshold = Logger.MINOR;
68    else if (params.getParam("logging").equalsIgnoreCase("debugging"))
69        Logger.threshold = Logger.DEBUGGING;
70          Logger.verbosity = params.getint("verbosity",defaultVerbosity);
71  
72    String fname = params.getParam("logFile","NO");
73    if (!fname.equalsIgnoreCase("NO")) {
74        try {
75      Logger.logto(fname);
76        } catch (Exception e) {
77      System.out.println("Writing to log failed");
78        }
79    }
80    ps.Lout = Logger.Lout;
81    
82    // bind local port
83    ServerSocket sock;
84    try {
85        Logger.log("ProxyServer.java", "Proxy server started on port "+port, Logger.NORMAL);
86        sock = new ServerSocket(port);
87    }
88    catch (IOException e) {
89        Logger.log("ProxyServer.java", "Error binding local port: "+e, Logger.ERROR);
90        return;
91    }
92  
93    // listen for connections
94    while(true) {
95        try {
96      Socket a = sock.accept();
97      Logger.log("ProxyServer.java", "Got new connection: "+a,
98           Logger.MINOR);
99      HttpHandler h = new HttpHandler(a, ps);
100     h.start();
101             }
102       catch (IOException e) {
103     throw new RuntimeException("Error accepting next connection:"+e);
104             }
105         }
106     }
107 
108 
109     /**
110      * Constructor which stores the invocation parameters of this ProxyServer.
111      **/
112     public ProxyServer(int port,String addr, long htl) {
113   this.listenPort = port;
114   this.serverAddress = addr;
115   this.hopsToLive = htl;
116     }
117 
118 
119     /**
120      * Print usage information
121      **/
122     public static void usage() {
123   System.out.println("fproxy version 0.1");
124   System.out.println("Usage: fproxy [options]");
125   System.out.println("");
126   System.out.println("  -listenPort port           Local port to listen on");
127   System.out.println("  -serverAddress address     Server node to connect to");
128   System.out.println("  -htl hops-to-live          Hops-to-live for request");
129   System.out.println("  -logFile file              Name of log file (`no' to log to console)");
130   System.out.println("  -logging error|normal|minor|debugging");
131   System.out.println("                             Logging level");
132 
133   System.out.println("  -help                      Display this help and exit");
134   System.out.println("");
135   System.out.println("Send bug reports to Theodore Hong <twh1@doc.ic.ac.uk>.");
136     }
137 }
138