source: branches/2.2/jabberit_messenger/java_source/src/nu/fw/jeti/plugins/filetransfer/socks5/jsocks/SocksServerSocket.java @ 3102

Revision 3102, 5.9 KB checked in by amuller, 14 years ago (diff)

Ticket #986 - Efetuado merge para o Branch 2.2( atualizacao do modulo)

  • Property svn:executable set to *
Line 
1package nu.fw.jeti.plugins.filetransfer.socks5.jsocks;
2
3import java.net.*;
4import java.io.*;
5//TODO remove if bind not necessary
6/**
7   SocksServerSocket allows to accept connections from one particular
8   host through the SOCKS4 or SOCKS5 proxy.
9*/
10public class SocksServerSocket extends ServerSocket{
11   //Data members
12   protected Proxy proxy;
13   protected String localHost;
14   protected InetAddress localIP;
15   protected int localPort;
16
17   boolean doing_direct = false;
18   InetAddress  remoteAddr;
19
20   /**
21    * Creates ServerSocket capable of accepting one connection
22    * through the firewall, uses default Proxy.
23    *@param host Host from which the connection should be recieved.
24    *@param port Port number of the primary connection.
25    */
26   public SocksServerSocket(String host,int port)
27          throws SocksException,UnknownHostException,IOException{
28      this(Proxy.defaultProxy,host,port);
29   }
30   /**
31    *Creates ServerSocket capable of accepting one connection
32    *through the firewall, uses given proxy.
33    *@param p    Proxy object to use.
34    *@param host Host from which the connection should be recieved.
35    *@param port Port number of the primary connection.
36    */
37   public SocksServerSocket(Proxy p,String host,int port)
38          throws SocksException,UnknownHostException,IOException{
39
40
41      super(0);
42      if(p == null) throw new SocksException(Proxy.SOCKS_NO_PROXY);
43      //proxy=p;
44      proxy = p.copy();
45      if(proxy.isDirect(host)){
46         remoteAddr = InetAddress.getByName(host);
47         proxy = null;
48         doDirect();
49      }else{
50         processReply(proxy.bind(host,port));
51      }
52   }
53
54   /**
55    * Creates ServerSocket capable of accepting one connection
56    * through the firewall, uses default Proxy.
57    *@param ip Host from which the connection should be recieved.
58    *@param port Port number of the primary connection.
59    */
60   public SocksServerSocket(InetAddress ip, int port) throws SocksException,
61                                                             IOException{
62      this(Proxy.defaultProxy,ip,port);
63   }
64
65   /**
66    *Creates ServerSocket capable of accepting one connection
67    *through the firewall, uses given proxy.
68    *@param p    Proxy object to use.
69    *@param ip   Host from which the connection should be recieved.
70    *@param port Port number of the primary connection.
71    */
72   public SocksServerSocket(Proxy p,InetAddress ip, int port)
73          throws SocksException,IOException{
74      super(0);
75
76      if(p == null) throw new SocksException(Proxy.SOCKS_NO_PROXY);
77      this.proxy = p.copy();
78
79      if(proxy.isDirect(ip)){
80         remoteAddr = ip;
81         doDirect();
82      }else{
83         processReply(proxy.bind(ip,port));
84      }
85   }
86
87
88   /**
89    * Accepts the incoming connection.
90    */
91   public Socket accept() throws IOException{
92      Socket s;
93
94      if(!doing_direct){
95         if(proxy == null) return null;
96
97         ProxyMessage msg = proxy.accept();
98         s = msg.ip == null? new SocksSocket(msg.host,msg.port,proxy)
99                                  : new SocksSocket(msg.ip,msg.port,proxy);
100         //Set timeout back to 0
101         proxy.proxySocket.setSoTimeout(0);
102      }else{ //Direct Connection
103
104          //Mimic the proxy behaviour,
105          //only accept connections from the speciefed host.
106          while(true){
107            s = super.accept();
108            if(s.getInetAddress().equals(remoteAddr)){
109               //got the connection from the right host
110               //Close listenning socket.
111               break;
112            }else
113               s.close(); //Drop all connections from other hosts
114          }
115
116      }
117      proxy = null;
118      //Return accepted socket
119      return s;
120   }
121
122   /**
123    * Closes the connection to proxy if socket have not been accepted, if
124    * the direct connection is used, closes direct ServerSocket. If the
125    * client socket have been allready accepted, does nothing.
126    */
127   public void close() throws IOException{
128      super.close();
129      if(proxy != null) proxy.endSession();
130      proxy = null;
131   }
132
133   /**
134     Get the name of the host proxy is using to listen for incoming
135     connection.
136     <P>
137     Usefull when address is returned by proxy as the hostname.
138     @return the hostname of the address proxy is using to listen
139     for incoming connection.
140    */
141   public String getHost(){
142      return localHost;
143   }
144
145   /**
146    * Get address assigned by proxy to listen for incomming
147    * connections, or the local machine address if doing direct
148    * connection.
149    */
150   public InetAddress getInetAddress(){
151      if(localIP == null){
152         try{
153           localIP = InetAddress.getByName(localHost);
154         }catch(UnknownHostException e){
155           return null;
156         }
157      }
158      return localIP;
159   }
160
161   /**
162    *  Get port assigned by proxy to listen for incoming connections, or
163       the port chosen by local system, if accepting directly.
164    */
165   public int getLocalPort(){
166      return localPort;
167   }
168
169   /**
170    Set Timeout.
171
172    @param timeout Amount of time in milliseconds, accept should wait for
173                   incoming connection before failing with exception.
174                   Zero timeout implies infinity.
175   */
176   public void setSoTimeout(int timeout) throws SocketException{
177      super.setSoTimeout(timeout);
178      if(!doing_direct) proxy.proxySocket.setSoTimeout(timeout);
179   }
180
181
182//Private Methods
183//////////////////
184
185   private void processReply(ProxyMessage reply)throws SocksException{
186      localPort = reply.port;
187      /*
188       * If the server have assigned same host as it was contacted on
189       * it might return an address of all zeros
190       */
191      if(reply.host.equals("0.0.0.0")){
192         localIP = proxy.proxyIP;
193         localHost = localIP.getHostName();
194      }else{
195         localHost = reply.host;
196         localIP = reply.ip;
197      }
198   }
199
200   private void doDirect(){
201      doing_direct = true;
202      localPort = super.getLocalPort();
203      localIP = super.getInetAddress();
204      localHost = localIP.getHostName();
205   }
206
207}
Note: See TracBrowser for help on using the repository browser.