Mercurial > 510Connectbot
comparison src/net/sourceforge/jsocks/SocksSocket.java @ 349:205ee2873330
update jsocks to 2011-03-19
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Fri, 01 Aug 2014 11:23:10 -0700 |
parents | 0ce5cc452d02 |
children |
comparison
equal
deleted
inserted
replaced
348:29076621bab0 | 349:205ee2873330 |
---|---|
5 | 5 |
6 /** | 6 /** |
7 * SocksSocket tryies to look very similar to normal Socket, | 7 * SocksSocket tryies to look very similar to normal Socket, |
8 * while allowing connections through the SOCKS4 or 5 proxy. | 8 * while allowing connections through the SOCKS4 or 5 proxy. |
9 * To use this class you will have to identify proxy you need | 9 * To use this class you will have to identify proxy you need |
10 * to use, Proxy class allows you to set default proxy, which | 10 * to use, CProxy class allows you to set default proxy, which |
11 * will be used by all Socks aware sockets. You can also create | 11 * will be used by all Socks aware sockets. You can also create |
12 * either Socks4Proxy or Socks5Proxy, and use them by passing to the | 12 * either Socks4Proxy or Socks5Proxy, and use them by passing to the |
13 * appropriate constructors. | 13 * appropriate constructors. |
14 * <P> | 14 * <P> |
15 * Using Socks package can be as easy as that: | 15 * Using Socks package can be as easy as that: |
16 * | 16 * |
17 * <pre><tt> | 17 * <pre><tt> |
19 * import Socks.*; | 19 * import Socks.*; |
20 * .... | 20 * .... |
21 * | 21 * |
22 * try{ | 22 * try{ |
23 * //Specify SOCKS5 proxy | 23 * //Specify SOCKS5 proxy |
24 * Proxy.setDefaultProxy("socks-proxy",1080); | 24 * CProxy.setDefaultProxy("socks-proxy",1080); |
25 * | 25 * |
26 * //OR you still use SOCKS4 | 26 * //OR you still use SOCKS4 |
27 * //Code below uses SOCKS4 proxy | 27 * //Code below uses SOCKS4 proxy |
28 * //Proxy.setDefaultProxy("socks-proxy",1080,userName); | 28 * //CProxy.setDefaultProxy("socks-proxy",1080,userName); |
29 * | 29 * |
30 * Socket s = SocksSocket("some.host.of.mine",13); | 30 * Socket s = SocksSocket("some.host.of.mine",13); |
31 * readTimeFromSock(s); | 31 * readTimeFromSock(s); |
32 * }catch(SocksException sock_ex){ | 32 * }catch(SocksException sock_ex){ |
33 * //Usually it will turn in more or less meaningfull message | 33 * //Usually it will turn in more or less meaningfull message |
38 *<P> | 38 *<P> |
39 * However if the need exist for more control, like resolving addresses | 39 * However if the need exist for more control, like resolving addresses |
40 * remotely, or using some non-trivial authentication schemes, it can be done. | 40 * remotely, or using some non-trivial authentication schemes, it can be done. |
41 */ | 41 */ |
42 | 42 |
43 public class SocksSocket extends Socket { | 43 public class SocksSocket extends Socket{ |
44 //Data members | 44 //Data members |
45 protected Proxy proxy; | 45 protected CProxy proxy; |
46 protected String localHost, remoteHost; | 46 protected String localHost, remoteHost; |
47 protected InetAddress localIP, remoteIP; | 47 protected InetAddress localIP, remoteIP; |
48 protected int localPort, remotePort; | 48 protected int localPort,remotePort; |
49 | 49 |
50 private Socket directSock = null; | 50 private Socket directSock = null; |
51 | 51 |
52 /** | 52 /** |
53 * Tryies to connect to given host and port | 53 * Tryies to connect to given host and port |
54 * using default proxy. If no default proxy speciefied | 54 * using default proxy. If no default proxy speciefied |
55 * it throws SocksException with error code SOCKS_NO_PROXY. | 55 * it throws SocksException with error code SOCKS_NO_PROXY. |
56 @param host Machine to connect to. | 56 @param host Machine to connect to. |
57 @param port Port to which to connect. | 57 @param port Port to which to connect. |
58 * @see SocksSocket#SocksSocket(Proxy,String,int) | 58 * @see SocksSocket#SocksSocket(CProxy,String,int) |
59 * @see Socks5Proxy#resolveAddrLocally | 59 * @see Socks5Proxy#resolveAddrLocally |
60 */ | 60 */ |
61 public SocksSocket(String host, int port) | 61 public SocksSocket(String host,int port) |
62 throws SocksException, UnknownHostException { | 62 throws SocksException,UnknownHostException{ |
63 this(Proxy.defaultProxy, host, port); | 63 this(CProxy.defaultProxy,host,port); |
64 } | 64 } |
65 /** | 65 /** |
66 * Connects to host port using given proxy server. | 66 * Connects to host port using given proxy server. |
67 @param p Proxy to use. | 67 @param p CProxy to use. |
68 @param host Machine to connect to. | 68 @param host Machine to connect to. |
69 @param port Port to which to connect. | 69 @param port Port to which to connect. |
70 @throws UnknownHostException | 70 @throws UnknownHostException |
71 If one of the following happens: | 71 If one of the following happens: |
72 <ol> | 72 <ol> |
73 | 73 |
74 <li> Proxy settings say that address should be resolved locally, but | 74 <li> CProxy settings say that address should be resolved locally, but |
75 this fails. | |
76 <li> CProxy settings say that the host should be contacted directly but | |
77 host name can't be resolved. | |
78 </ol> | |
79 @throws SocksException | |
80 If one of the following happens: | |
81 <ul> | |
82 <li> CProxy is is null. | |
83 <li> CProxy settings say that the host should be contacted directly but | |
75 this fails. | 84 this fails. |
76 <li> Proxy settings say that the host should be contacted directly but | 85 <li> Socks Server can't be contacted. |
77 host name can't be resolved. | 86 <li> Authentication fails. |
78 </ol> | 87 <li> Connection is not allowed by the SOCKS proxy. |
79 @throws SocksException | 88 <li> SOCKS proxy can't establish the connection. |
80 If one of the following happens: | 89 <li> Any IO error occured. |
81 <ul> | 90 <li> Any protocol error occured. |
82 <li> Proxy is is null. | 91 </ul> |
83 <li> Proxy settings say that the host should be contacted directly but | 92 @throws IOexception if anything is wrong with I/O. |
84 this fails. | 93 @see Socks5Proxy#resolveAddrLocally |
85 <li> Socks Server can't be contacted. | 94 */ |
86 <li> Authentication fails. | 95 public SocksSocket(CProxy p,String host,int port) |
87 <li> Connection is not allowed by the SOCKS proxy. | 96 throws SocksException,UnknownHostException{ |
88 <li> SOCKS proxy can't establish the connection. | 97 |
89 <li> Any IO error occured. | 98 |
90 <li> Any protocol error occured. | 99 if(p == null) throw new SocksException(CProxy.SOCKS_NO_PROXY); |
91 </ul> | 100 //proxy=p; |
92 @throws IOexception if anything is wrong with I/O. | 101 proxy = p.copy(); |
93 @see Socks5Proxy#resolveAddrLocally | 102 remoteHost = host; |
94 */ | 103 remotePort = port; |
95 public SocksSocket(Proxy p, String host, int port) throws SocksException, | 104 if(proxy.isDirect(host)){ |
96 UnknownHostException { | 105 remoteIP = InetAddress.getByName(host); |
97 remoteHost = host; | 106 doDirect(); |
98 remotePort = port; | 107 } |
99 remoteIP = InetAddress.getByName(host); | 108 else |
109 processReply(proxy.connect(host,port)); | |
110 } | |
111 | |
112 | |
113 /** | |
114 * Tryies to connect to given ip and port | |
115 * using default proxy. If no default proxy speciefied | |
116 * it throws SocksException with error code SOCKS_NO_PROXY. | |
117 @param ip Machine to connect to. | |
118 @param port Port to which to connect. | |
119 * @see SocksSocket#SocksSocket(CProxy,String,int) | |
120 */ | |
121 public SocksSocket(InetAddress ip, int port) throws SocksException{ | |
122 this(CProxy.defaultProxy,ip,port); | |
123 } | |
124 | |
125 /** | |
126 Connects to given ip and port using given CProxy server. | |
127 @param p CProxy to use. | |
128 @param ip Machine to connect to. | |
129 @param port Port to which to connect. | |
130 | |
131 */ | |
132 public SocksSocket(CProxy p,InetAddress ip, int port) throws SocksException{ | |
133 if(p == null) throw new SocksException(CProxy.SOCKS_NO_PROXY); | |
134 this.proxy = p.copy(); | |
135 this.remoteIP = ip; | |
136 this.remotePort = port; | |
137 this.remoteHost = ip.getHostName(); | |
138 if(proxy.isDirect(remoteIP)) | |
100 doDirect(); | 139 doDirect(); |
101 } | 140 else |
102 | 141 processReply(proxy.connect(ip,port)); |
103 /** | 142 } |
104 Connects to given ip and port using given Proxy server. | 143 |
105 @param p Proxy to use. | 144 |
106 @param ip Machine to connect to. | 145 /** |
107 @param port Port to which to connect. | 146 * These 2 constructors are used by the SocksServerSocket. |
108 | 147 * This socket simply overrides remoteHost, remotePort |
109 */ | 148 */ |
110 public SocksSocket(InetAddress ip, int port) throws SocksException { | 149 protected SocksSocket(String host,int port,CProxy proxy){ |
111 this.remoteIP = ip; | 150 this.remotePort = port; |
112 this.remotePort = port; | 151 this.proxy = proxy; |
113 this.remoteHost = ip.getHostName(); | 152 this.localIP = proxy.proxySocket.getLocalAddress(); |
114 doDirect(); | 153 this.localPort = proxy.proxySocket.getLocalPort(); |
115 } | 154 this.remoteHost = host; |
116 | 155 } |
117 | 156 protected SocksSocket(InetAddress ip,int port,CProxy proxy){ |
118 /** | 157 remoteIP = ip; |
119 * These 2 constructors are used by the SocksServerSocket. | 158 remotePort = port; |
120 * This socket simply overrides remoteHost, remotePort | 159 this.proxy = proxy; |
121 */ | 160 this.localIP = proxy.proxySocket.getLocalAddress(); |
122 protected SocksSocket(String host, int port, Proxy proxy) { | 161 this.localPort = proxy.proxySocket.getLocalPort(); |
123 this.remotePort = port; | 162 remoteHost = remoteIP.getHostName(); |
124 this.proxy = proxy; | 163 } |
125 this.localIP = proxy.proxySocket.getLocalAddress(); | 164 |
126 this.localPort = proxy.proxySocket.getLocalPort(); | 165 /** |
127 this.remoteHost = host; | 166 * Same as Socket |
128 } | 167 */ |
129 protected SocksSocket(InetAddress ip, int port, Proxy proxy) { | 168 public void close() throws IOException{ |
130 remoteIP = ip; | 169 if(proxy!= null)proxy.endSession(); |
131 remotePort = port; | 170 proxy = null; |
132 this.proxy = proxy; | 171 } |
133 this.localIP = proxy.proxySocket.getLocalAddress(); | 172 /** |
134 this.localPort = proxy.proxySocket.getLocalPort(); | 173 * Same as Socket |
135 remoteHost = remoteIP.getHostName(); | 174 */ |
136 } | 175 public InputStream getInputStream(){ |
137 | 176 return proxy.in; |
138 /** | 177 } |
139 * Same as Socket | 178 /** |
140 */ | 179 * Same as Socket |
141 public void close() throws IOException { | 180 */ |
142 if (proxy != null)proxy.endSession(); | 181 public OutputStream getOutputStream(){ |
143 | 182 return proxy.out; |
144 proxy = null; | 183 } |
145 } | 184 /** |
146 /** | 185 * Same as Socket |
147 * Same as Socket | 186 */ |
148 */ | 187 public int getPort(){ |
149 public InputStream getInputStream() { | 188 return remotePort; |
150 return proxy.in; | 189 } |
151 } | 190 /** |
152 /** | 191 * Returns remote host name, it is usefull in cases when addresses |
153 * Same as Socket | 192 * are resolved by proxy, and we can't create InetAddress object. |
154 */ | 193 @return The name of the host this socket is connected to. |
155 public OutputStream getOutputStream() { | 194 */ |
156 return proxy.out; | 195 public String getHost(){ |
157 } | 196 return remoteHost; |
158 /** | 197 } |
159 * Same as Socket | 198 /** |
160 */ | 199 * Get remote host as InetAddress object, might return null if |
161 public int getPort() { | 200 * addresses are resolved by proxy, and it is not possible to resolve |
162 return remotePort; | 201 * it locally |
163 } | 202 @return Ip address of the host this socket is connected to, or null |
164 /** | 203 if address was returned by the proxy as DOMAINNAME and can't be |
165 * Returns remote host name, it is usefull in cases when addresses | 204 resolved locally. |
166 * are resolved by proxy, and we can't create InetAddress object. | 205 */ |
167 @return The name of the host this socket is connected to. | 206 public InetAddress getInetAddress(){ |
168 */ | 207 if(remoteIP == null){ |
169 public String getHost() { | 208 try{ |
170 return remoteHost; | 209 remoteIP = InetAddress.getByName(remoteHost); |
171 } | 210 }catch(UnknownHostException e){ |
172 /** | 211 return null; |
173 * Get remote host as InetAddress object, might return null if | 212 } |
174 * addresses are resolved by proxy, and it is not possible to resolve | 213 } |
175 * it locally | 214 return remoteIP; |
176 @return Ip address of the host this socket is connected to, or null | 215 } |
177 if address was returned by the proxy as DOMAINNAME and can't be | 216 |
178 resolved locally. | 217 /** |
179 */ | 218 * Get the port assigned by the proxy for the socket, not |
180 public InetAddress getInetAddress() { | 219 * the port on locall machine as in Socket. |
181 if (remoteIP == null) { | 220 @return Port of the socket used on the proxy server. |
182 try { | 221 */ |
183 remoteIP = InetAddress.getByName(remoteHost); | 222 public int getLocalPort(){ |
184 } | 223 return localPort; |
185 catch (UnknownHostException e) { | 224 } |
186 return null; | 225 |
187 } | 226 /** |
188 } | 227 * Get address assigned by proxy to make a remote connection, |
189 | 228 * it might be different from the host specified for the proxy. |
190 return remoteIP; | 229 * Can return null if socks server returned this address as hostname |
191 } | 230 * and it can't be resolved locally, use getLocalHost() then. |
192 | 231 @return Address proxy is using to make a connection. |
193 /** | 232 */ |
194 * Get the port assigned by the proxy for the socket, not | 233 public InetAddress getLocalAddress(){ |
195 * the port on locall machine as in Socket. | 234 if(localIP == null){ |
196 @return Port of the socket used on the proxy server. | 235 try{ |
197 */ | 236 localIP = InetAddress.getByName(localHost); |
198 public int getLocalPort() { | 237 }catch(UnknownHostException e){ |
199 return localPort; | 238 return null; |
200 } | 239 } |
201 | 240 } |
202 /** | 241 return localIP; |
203 * Get address assigned by proxy to make a remote connection, | 242 } |
204 * it might be different from the host specified for the proxy. | 243 /** |
205 * Can return null if socks server returned this address as hostname | 244 Get name of the host, proxy has assigned to make a remote connection |
206 * and it can't be resolved locally, use getLocalHost() then. | 245 for this socket. This method is usefull when proxy have returned |
207 @return Address proxy is using to make a connection. | 246 address as hostname, and we can't resolve it on this machine. |
208 */ | 247 @return The name of the host proxy is using to make a connection. |
209 public InetAddress getLocalAddress() { | 248 */ |
210 if (localIP == null) { | 249 public String getLocalHost(){ |
211 try { | 250 return localHost; |
212 localIP = InetAddress.getByName(localHost); | 251 } |
213 } | 252 |
214 catch (UnknownHostException e) { | 253 /** |
215 return null; | 254 Same as socket. |
216 } | 255 */ |
217 } | 256 public void setSoLinger(boolean on,int val) throws SocketException{ |
218 | 257 proxy.proxySocket.setSoLinger(on,val); |
219 return localIP; | 258 } |
220 } | 259 /** |
221 /** | 260 Same as socket. |
222 Get name of the host, proxy has assigned to make a remote connection | 261 */ |
223 for this socket. This method is usefull when proxy have returned | 262 public int getSoLinger(int timeout) throws SocketException{ |
224 address as hostname, and we can't resolve it on this machine. | 263 return proxy.proxySocket.getSoLinger(); |
225 @return The name of the host proxy is using to make a connection. | 264 } |
226 */ | 265 /** |
227 public String getLocalHost() { | 266 Same as socket. |
228 return localHost; | 267 */ |
229 } | 268 public void setSoTimeout(int timeout) throws SocketException{ |
230 | 269 proxy.proxySocket.setSoTimeout(timeout); |
231 /** | 270 } |
232 Same as socket. | 271 /** |
233 */ | 272 Same as socket. |
234 public void setSoLinger(boolean on, int val) throws SocketException { | 273 */ |
235 proxy.proxySocket.setSoLinger(on, val); | 274 public int getSoTimeout(int timeout) throws SocketException{ |
236 } | 275 return proxy.proxySocket.getSoTimeout(); |
237 /** | 276 } |
238 Same as socket. | 277 /** |
239 */ | 278 Same as socket. |
240 public int getSoLinger(int timeout) throws SocketException { | 279 */ |
241 return proxy.proxySocket.getSoLinger(); | 280 public void setTcpNoDelay(boolean on) throws SocketException{ |
242 } | 281 proxy.proxySocket.setTcpNoDelay(on); |
243 /** | 282 } |
244 Same as socket. | 283 /** |
245 */ | 284 Same as socket. |
246 public void setSoTimeout(int timeout) throws SocketException { | 285 */ |
247 proxy.proxySocket.setSoTimeout(timeout); | 286 public boolean getTcpNoDelay() throws SocketException{ |
248 } | 287 return proxy.proxySocket.getTcpNoDelay(); |
249 /** | 288 } |
250 Same as socket. | 289 |
251 */ | 290 /** |
252 public int getSoTimeout(int timeout) throws SocketException { | 291 Get string representation of the socket. |
253 return proxy.proxySocket.getSoTimeout(); | 292 */ |
254 } | 293 public String toString(){ |
255 /** | 294 if(directSock!=null) return "Direct connection:"+directSock; |
256 Same as socket. | 295 return ("Proxy:"+proxy+";"+"addr:"+remoteHost+",port:"+remotePort |
257 */ | 296 +",localport:"+localPort); |
258 public void setTcpNoDelay(boolean on) throws SocketException { | 297 |
259 proxy.proxySocket.setTcpNoDelay(on); | 298 } |
260 } | |
261 /** | |
262 Same as socket. | |
263 */ | |
264 public boolean getTcpNoDelay() throws SocketException { | |
265 return proxy.proxySocket.getTcpNoDelay(); | |
266 } | |
267 | |
268 /** | |
269 Get string representation of the socket. | |
270 */ | |
271 public String toString() { | |
272 if (directSock != null) return "Direct connection:" + directSock; | |
273 | |
274 return ("Proxy:" + proxy + ";" + "addr:" + remoteHost + ",port:" + remotePort | |
275 + ",localport:" + localPort); | |
276 } | |
277 | 299 |
278 //Private Methods | 300 //Private Methods |
279 ////////////////// | 301 ////////////////// |
280 | 302 |
281 private void doDirect()throws SocksException { | 303 private void processReply(ProxyMessage reply)throws SocksException{ |
282 try { | 304 localPort = reply.port; |
283 //System.out.println("IP:"+remoteIP+":"+remotePort); | 305 /* |
284 directSock = new Socket(remoteIP, remotePort); | 306 * If the server have assigned same host as it was contacted on |
285 proxy.out = directSock.getOutputStream(); | 307 * it might return an address of all zeros |
286 proxy.in = directSock.getInputStream(); | 308 */ |
287 proxy.proxySocket = directSock; | 309 if(reply.host.equals("0.0.0.0")){ |
288 localIP = directSock.getLocalAddress(); | 310 localIP = proxy.proxyIP; |
289 localPort = directSock.getLocalPort(); | 311 localHost = localIP.getHostName(); |
290 } | 312 }else{ |
291 catch (IOException io_ex) { | 313 localHost = reply.host; |
292 throw new SocksException(Proxy.SOCKS_DIRECT_FAILED, | 314 localIP = reply.ip; |
293 "Direct connect failed:" + io_ex); | 315 } |
294 } | 316 } |
295 } | 317 private void doDirect()throws SocksException{ |
318 try{ | |
319 //System.out.println("IP:"+remoteIP+":"+remotePort); | |
320 directSock = new Socket(remoteIP,remotePort); | |
321 proxy.out = directSock.getOutputStream(); | |
322 proxy.in = directSock.getInputStream(); | |
323 proxy.proxySocket = directSock; | |
324 localIP = directSock.getLocalAddress(); | |
325 localPort = directSock.getLocalPort(); | |
326 }catch(IOException io_ex){ | |
327 throw new SocksException(CProxy.SOCKS_DIRECT_FAILED, | |
328 "Direct connect failed:"+io_ex); | |
329 } | |
330 } | |
296 | 331 |
297 } | 332 } |