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