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