Mercurial > 510Connectbot
annotate src/net/sourceforge/jsocks/ProxyServer.java @ 378:4d5cb1e185e1
remove redundancy from makefile
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Mon, 04 Aug 2014 15:02:05 -0700 |
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 import net.sourceforge.jsocks.server.ServerAuthenticator; |
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 SOCKS4 and SOCKS5 proxy, handles both protocols simultaniously. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
8 Implements all SOCKS commands, including UDP relaying. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
9 <p> |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
10 In order to use it you will need to implement ServerAuthenticator |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
11 interface. There is an implementation of this interface which does |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
12 no authentication ServerAuthenticatorNone, but it is very dangerous |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
13 to use, as it will give access to your local network to anybody |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
14 in the world. One should never use this authentication scheme unless |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
15 one have pretty good reason to do so. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
16 There is a couple of other authentication schemes in socks.server package. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
17 @see socks.server.ServerAuthenticator |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
18 */ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
19 public class ProxyServer implements Runnable{ |
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 ServerAuthenticator auth; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
22 ProxyMessage msg = null; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
23 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
24 Socket sock=null,remote_sock=null; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
25 ServerSocket ss=null; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
26 UDPRelayServer relayServer = null; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
27 InputStream in,remote_in; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
28 OutputStream out,remote_out; |
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 int mode; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
31 static final int START_MODE = 0; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
32 static final int ACCEPT_MODE = 1; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
33 static final int PIPE_MODE = 2; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
34 static final int ABORT_MODE = 3; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
35 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
36 static final int BUF_SIZE = 8192; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
37 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
38 Thread pipe_thread1,pipe_thread2; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
39 long lastReadTime; |
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 static int iddleTimeout = 180000; //3 minutes |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
42 static int acceptTimeout = 180000; //3 minutes |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
43 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
44 static PrintStream log = null; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
45 static CProxy proxy; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
46 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
47 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
48 //Public Constructors |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
49 ///////////////////// |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
50 |
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 Creates a proxy server with given Authentication scheme. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
54 @param auth Authentication scheme to be used. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
55 */ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
56 public ProxyServer(ServerAuthenticator auth){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
57 this.auth = auth; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
58 } |
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 //Other constructors |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
61 //////////////////// |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
62 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
63 ProxyServer(ServerAuthenticator auth,Socket s){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
64 this.auth = auth; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
65 this.sock = s; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
66 mode = START_MODE; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
67 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
68 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
69 //Public methods |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
70 ///////////////// |
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 /** |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
73 Set the logging stream. Specifying null disables logging. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
74 */ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
75 public static void setLog(OutputStream out){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
76 if(out == null){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
77 log = null; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
78 }else{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
79 log = new PrintStream(out,true); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
80 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
81 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
82 UDPRelayServer.log = log; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
83 } |
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 Set proxy. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
87 <p> |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
88 Allows CProxy chaining so that one CProxy server is connected to another |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
89 and so on. If proxy supports SOCKSv4, then only some SOCKSv5 requests |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
90 can be handled, UDP would not work, however CONNECT and BIND will be |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
91 translated. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
92 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
93 @param p CProxy which should be used to handle user requests. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
94 */ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
95 public static void setProxy(CProxy p){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
96 proxy =p; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
97 UDPRelayServer.proxy = proxy; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
98 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
99 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
100 /** |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
101 Get proxy. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
102 @return CProxy wich is used to handle user requests. |
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 public static CProxy getProxy(){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
105 return proxy; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
106 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
107 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
108 /** |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
109 Sets the timeout for connections, how long shoud server wait |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
110 for data to arrive before dropping the connection.<br> |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
111 Zero timeout implies infinity.<br> |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
112 Default timeout is 3 minutes. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
113 */ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
114 public static void setIddleTimeout(int timeout){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
115 iddleTimeout = timeout; |
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 /** |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
118 Sets the timeout for BIND command, how long the server should |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
119 wait for the incoming connection.<br> |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
120 Zero timeout implies infinity.<br> |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
121 Default timeout is 3 minutes. |
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 public static void setAcceptTimeout(int timeout){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
124 acceptTimeout = timeout; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
125 } |
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 /** |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
128 Sets the timeout for UDPRelay server.<br> |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
129 Zero timeout implies infinity.<br> |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
130 Default timeout is 3 minutes. |
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 public static void setUDPTimeout(int timeout){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
133 UDPRelayServer.setTimeout(timeout); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
134 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
135 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
136 /** |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
137 Sets the size of the datagrams used in the UDPRelayServer.<br> |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
138 Default size is 64K, a bit more than maximum possible size of the |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
139 datagram. |
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 static void setDatagramSize(int size){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
142 UDPRelayServer.setDatagramSize(size); |
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 /** |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
147 Start the CProxy server at given port.<br> |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
148 This methods blocks. |
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 void start(int port){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
151 start(port,5,null); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
152 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
153 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
154 /** |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
155 Create a server with the specified port, listen backlog, and local |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
156 IP address to bind to. The localIP argument can be used on a multi-homed |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
157 host for a ServerSocket that will only accept connect requests to one of |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
158 its addresses. If localIP is null, it will default accepting connections |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
159 on any/all local addresses. The port must be between 0 and 65535, |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
160 inclusive. <br> |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
161 This methods blocks. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
162 */ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
163 public void start(int port,int backlog,InetAddress localIP){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
164 try{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
165 ss = new ServerSocket(port,backlog,localIP); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
166 log("Starting SOCKS Proxy on:"+ss.getInetAddress().getHostAddress()+":" |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
167 +ss.getLocalPort()); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
168 while(true){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
169 Socket s = ss.accept(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
170 log("Accepted from:"+s.getInetAddress().getHostName()+":" |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
171 +s.getPort()); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
172 ProxyServer ps = new ProxyServer(auth,s); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
173 (new Thread(ps)).start(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
174 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
175 }catch(IOException ioe){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
176 ioe.printStackTrace(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
177 }finally{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
178 } |
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 Stop server operation.It would be wise to interrupt thread running the |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
183 server afterwards. |
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 public void stop(){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
186 try{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
187 if(ss != null) ss.close(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
188 }catch(IOException ioe){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
189 } |
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 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
192 //Runnable interface |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
193 //////////////////// |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
194 public void run(){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
195 switch(mode){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
196 case START_MODE: |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
197 try{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
198 startSession(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
199 }catch(IOException ioe){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
200 handleException(ioe); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
201 //ioe.printStackTrace(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
202 }finally{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
203 abort(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
204 if(auth!=null) auth.endSession(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
205 log("Main thread(client->remote)stopped."); |
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 break; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
208 case ACCEPT_MODE: |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
209 try{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
210 doAccept(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
211 mode = PIPE_MODE; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
212 pipe_thread1.interrupt(); //Tell other thread that connection have |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
213 //been accepted. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
214 pipe(remote_in,out); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
215 }catch(IOException ioe){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
216 //log("Accept exception:"+ioe); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
217 handleException(ioe); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
218 }finally{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
219 abort(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
220 log("Accept thread(remote->client) stopped"); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
221 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
222 break; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
223 case PIPE_MODE: |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
224 try{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
225 pipe(remote_in,out); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
226 }catch(IOException ioe){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
227 }finally{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
228 abort(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
229 log("Support thread(remote->client) stopped"); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
230 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
231 break; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
232 case ABORT_MODE: |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
233 break; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
234 default: |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
235 log("Unexpected MODE "+mode); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
236 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
237 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
238 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
239 //Private methods |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
240 ///////////////// |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
241 private void startSession() throws IOException{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
242 sock.setSoTimeout(iddleTimeout); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
243 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
244 try{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
245 auth = auth.startSession(sock); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
246 }catch(IOException ioe){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
247 log("Auth throwed exception:"+ioe); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
248 auth = null; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
249 return; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
250 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
251 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
252 if(auth == null){ //Authentication failed |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
253 log("Authentication failed"); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
254 return; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
255 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
256 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
257 in = auth.getInputStream(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
258 out = auth.getOutputStream(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
259 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
260 msg = readMsg(in); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
261 handleRequest(msg); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
262 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
263 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
264 private void handleRequest(ProxyMessage msg) |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
265 throws IOException{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
266 if(!auth.checkRequest(msg)) throw new |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
267 SocksException(CProxy.SOCKS_FAILURE); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
268 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
269 if(msg.ip == null){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
270 if(msg instanceof Socks5Message){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
271 msg.ip = InetAddress.getByName(msg.host); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
272 }else |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
273 throw new SocksException(CProxy.SOCKS_FAILURE); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
274 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
275 log(msg); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
276 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
277 switch(msg.command){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
278 case CProxy.SOCKS_CMD_CONNECT: |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
279 onConnect(msg); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
280 break; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
281 case CProxy.SOCKS_CMD_BIND: |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
282 onBind(msg); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
283 break; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
284 case CProxy.SOCKS_CMD_UDP_ASSOCIATE: |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
285 onUDP(msg); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
286 break; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
287 default: |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
288 throw new SocksException(CProxy.SOCKS_CMD_NOT_SUPPORTED); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
289 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
290 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
291 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
292 private void handleException(IOException ioe){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
293 //If we couldn't read the request, return; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
294 if(msg == null) return; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
295 //If have been aborted by other thread |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
296 if(mode == ABORT_MODE) return; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
297 //If the request was successfully completed, but exception happened later |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
298 if(mode == PIPE_MODE) return; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
299 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
300 int error_code = CProxy.SOCKS_FAILURE; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
301 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
302 if(ioe instanceof SocksException) |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
303 error_code = ((SocksException)ioe).errCode; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
304 else if(ioe instanceof NoRouteToHostException) |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
305 error_code = CProxy.SOCKS_HOST_UNREACHABLE; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
306 else if(ioe instanceof ConnectException) |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
307 error_code = CProxy.SOCKS_CONNECTION_REFUSED; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
308 else if(ioe instanceof InterruptedIOException) |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
309 error_code = CProxy.SOCKS_TTL_EXPIRE; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
310 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
311 if(error_code > CProxy.SOCKS_ADDR_NOT_SUPPORTED || error_code < 0){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
312 error_code = CProxy.SOCKS_FAILURE; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
313 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
314 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
315 sendErrorMessage(error_code); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
316 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
317 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
318 private void onConnect(ProxyMessage msg) throws IOException { |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
319 Socket s = null; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
320 ProxyMessage response = null; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
321 int iSock5Cmd = CProxy.SOCKS_FAILURE; //defaulting to failure |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
322 int iSock4Msg = Socks4Message.REPLY_NO_CONNECT; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
323 InetAddress sIp = null; int iPort = 0; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
324 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
325 try { |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
326 if (proxy == null) { |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
327 s = new Socket(msg.ip, msg.port); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
328 } else { |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
329 s = new SocksSocket(proxy, msg.ip, msg.port); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
330 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
331 log("Connected to " + s.getInetAddress() + ":" + s.getPort()); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
332 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
333 iSock5Cmd = CProxy.SOCKS_SUCCESS; iSock4Msg = Socks4Message.REPLY_OK; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
334 sIp = s.getInetAddress(); iPort = s.getPort(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
335 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
336 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
337 catch (Exception sE) { |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
338 log("Failed connecting to remote socket. Exception: " + sE.getLocalizedMessage()); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
339 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
340 //TBD Pick proper socks error for corresponding socket error, below is too generic |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
341 iSock5Cmd = CProxy.SOCKS_CONNECTION_REFUSED; iSock4Msg = Socks4Message.REPLY_NO_CONNECT; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
342 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
343 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
344 if (msg instanceof Socks5Message) { |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
345 response = new Socks5Message(iSock5Cmd, sIp, iPort); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
346 } else { |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
347 response = new Socks4Message(iSock4Msg, sIp, iPort); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
348 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
349 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
350 response.write(out); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
351 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
352 if (s != null) { |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
353 startPipe(s); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
354 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
355 else { |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
356 throw (new RuntimeException("onConnect() Failed to create Socket()")); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
357 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
358 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
359 return; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
360 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
361 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
362 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
363 private void onBind(ProxyMessage msg) throws IOException{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
364 ProxyMessage response = null; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
365 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
366 if(proxy == null) |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
367 ss = new ServerSocket(0); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
368 else |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
369 ss = new SocksServerSocket(proxy, msg.ip, msg.port); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
370 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
371 ss.setSoTimeout(acceptTimeout); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
372 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
373 log("Trying accept on "+ss.getInetAddress()+":"+ss.getLocalPort()); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
374 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
375 if(msg.version == 5) |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
376 response = new Socks5Message(CProxy.SOCKS_SUCCESS,ss.getInetAddress(), |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
377 ss.getLocalPort()); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
378 else |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
379 response = new Socks4Message(Socks4Message.REPLY_OK, |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
380 ss.getInetAddress(), |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
381 ss.getLocalPort()); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
382 response.write(out); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
383 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
384 mode = ACCEPT_MODE; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
385 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
386 pipe_thread1 = Thread.currentThread(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
387 pipe_thread2 = new Thread(this); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
388 pipe_thread2.start(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
389 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
390 //Make timeout infinit. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
391 sock.setSoTimeout(0); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
392 int eof=0; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
393 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
394 try{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
395 while((eof=in.read())>=0){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
396 if(mode != ACCEPT_MODE){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
397 if(mode != PIPE_MODE) return;//Accept failed |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
398 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
399 remote_out.write(eof); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
400 break; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
401 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
402 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
403 }catch(EOFException eofe){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
404 //System.out.println("EOF exception"); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
405 return;//Connection closed while we were trying to accept. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
406 }catch(InterruptedIOException iioe){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
407 //Accept thread interrupted us. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
408 //System.out.println("Interrupted"); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
409 if(mode != PIPE_MODE) |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
410 return;//If accept thread was not successfull return. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
411 }finally{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
412 //System.out.println("Finnaly!"); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
413 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
414 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
415 if(eof < 0)//Connection closed while we were trying to accept; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
416 return; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
417 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
418 //Do not restore timeout, instead timeout is set on the |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
419 //remote socket. It does not make any difference. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
420 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
421 pipe(in,remote_out); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
422 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
423 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
424 private void onUDP(ProxyMessage msg) throws IOException{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
425 if(msg.ip.getHostAddress().equals("0.0.0.0")) |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
426 msg.ip = sock.getInetAddress(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
427 log("Creating UDP relay server for "+msg.ip+":"+msg.port); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
428 relayServer = new UDPRelayServer(msg.ip,msg.port, |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
429 Thread.currentThread(),sock,auth); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
430 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
431 ProxyMessage response; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
432 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
433 response = new Socks5Message(CProxy.SOCKS_SUCCESS, |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
434 relayServer.relayIP,relayServer.relayPort); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
435 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
436 response.write(out); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
437 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
438 relayServer.start(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
439 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
440 //Make timeout infinit. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
441 sock.setSoTimeout(0); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
442 try{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
443 while(in.read()>=0) /*do nothing*/; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
444 }catch(EOFException eofe){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
445 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
446 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
447 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
448 //Private methods |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
449 ////////////////// |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
450 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
451 private void doAccept() throws IOException{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
452 Socket s; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
453 long startTime = System.currentTimeMillis(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
454 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
455 while(true){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
456 s = ss.accept(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
457 if(s.getInetAddress().equals(msg.ip)){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
458 //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
|
459 //Close listenning socket. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
460 ss.close(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
461 break; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
462 }else if(ss instanceof SocksServerSocket){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
463 //We can't accept more then one connection |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
464 s.close(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
465 ss.close(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
466 throw new SocksException(CProxy.SOCKS_FAILURE); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
467 }else{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
468 if(acceptTimeout!=0){ //If timeout is not infinit |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
469 int newTimeout = acceptTimeout-(int)(System.currentTimeMillis()- |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
470 startTime); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
471 if(newTimeout <= 0) throw new InterruptedIOException( |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
472 "In doAccept()"); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
473 ss.setSoTimeout(newTimeout); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
474 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
475 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
|
476 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
477 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
478 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
479 //Accepted connection |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
480 remote_sock = s; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
481 remote_in = s.getInputStream(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
482 remote_out = s.getOutputStream(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
483 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
484 //Set timeout |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
485 remote_sock.setSoTimeout(iddleTimeout); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
486 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
487 log("Accepted from "+s.getInetAddress()+":"+s.getPort()); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
488 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
489 ProxyMessage response; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
490 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
491 if(msg.version == 5) |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
492 response = new Socks5Message(CProxy.SOCKS_SUCCESS, s.getInetAddress(), |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
493 s.getPort()); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
494 else |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
495 response = new Socks4Message(Socks4Message.REPLY_OK, |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
496 s.getInetAddress(), s.getPort()); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
497 response.write(out); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
498 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
499 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
500 private ProxyMessage readMsg(InputStream in) throws IOException{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
501 PushbackInputStream push_in; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
502 if(in instanceof PushbackInputStream) |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
503 push_in = (PushbackInputStream) in; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
504 else |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
505 push_in = new PushbackInputStream(in); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
506 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
507 int version = push_in.read(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
508 push_in.unread(version); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
509 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
510 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
511 ProxyMessage msg; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
512 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
513 if(version == 5){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
514 msg = new Socks5Message(push_in,false); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
515 }else if(version == 4){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
516 msg = new Socks4Message(push_in,false); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
517 }else{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
518 throw new SocksException(CProxy.SOCKS_FAILURE); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
519 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
520 return msg; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
521 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
522 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
523 private void startPipe(Socket s){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
524 mode = PIPE_MODE; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
525 remote_sock = s; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
526 try{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
527 remote_in = s.getInputStream(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
528 remote_out = s.getOutputStream(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
529 pipe_thread1 = Thread.currentThread(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
530 pipe_thread2 = new Thread(this); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
531 pipe_thread2.start(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
532 pipe(in,remote_out); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
533 }catch(IOException ioe){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
534 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
535 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
536 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
537 private void sendErrorMessage(int error_code){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
538 ProxyMessage err_msg; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
539 if(msg instanceof Socks4Message) |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
540 err_msg = new Socks4Message(Socks4Message.REPLY_REJECTED); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
541 else |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
542 err_msg = new Socks5Message(error_code); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
543 try{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
544 err_msg.write(out); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
545 }catch(IOException ioe){} |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
546 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
547 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
548 private synchronized void abort(){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
549 if(mode == ABORT_MODE) return; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
550 mode = ABORT_MODE; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
551 try{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
552 log("Aborting operation"); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
553 if(remote_sock != null) remote_sock.close(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
554 if(sock != null) sock.close(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
555 if(relayServer!=null) relayServer.stop(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
556 if(ss!=null) ss.close(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
557 if(pipe_thread1 != null) pipe_thread1.interrupt(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
558 if(pipe_thread2 != null) pipe_thread2.interrupt(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
559 }catch(IOException ioe){} |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
560 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
561 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
562 static final void log(String s){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
563 if(log != null){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
564 log.println(s); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
565 log.flush(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
566 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
567 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
568 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
569 static final void log(ProxyMessage msg){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
570 log("Request version:"+msg.version+ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
571 "\tCommand: "+command2String(msg.command)); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
572 log("IP:"+msg.ip +"\tPort:"+msg.port+ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
573 (msg.version==4?"\tUser:"+msg.user:"")); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
574 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
575 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
576 private void pipe(InputStream in,OutputStream out) throws IOException{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
577 lastReadTime = System.currentTimeMillis(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
578 byte[] buf = new byte[BUF_SIZE]; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
579 int len = 0; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
580 while(len >= 0){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
581 try{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
582 if(len!=0){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
583 out.write(buf,0,len); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
584 out.flush(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
585 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
586 len= in.read(buf); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
587 lastReadTime = System.currentTimeMillis(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
588 }catch(InterruptedIOException iioe){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
589 if(iddleTimeout == 0) return;//Other thread interrupted us. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
590 long timeSinceRead = System.currentTimeMillis() - lastReadTime; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
591 if(timeSinceRead >= iddleTimeout - 1000) //-1s for adjustment. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
592 return; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
593 len = 0; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
594 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
595 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
596 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
597 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
598 static final String command_names[] = {"CONNECT","BIND","UDP_ASSOCIATE"}; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
599 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
600 static final String command2String(int cmd){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
601 if(cmd > 0 && cmd < 4) return command_names[cmd-1]; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
602 else return "Unknown Command "+cmd; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
603 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
604 } |