author | michaelm |
Thu, 24 Jan 2019 15:48:05 +0000 | |
changeset 53473 | 9366628d727b |
parent 52499 | 768b1c612100 |
child 54155 | b5a73f22b2bd |
child 57112 | d7b54daf5e1a |
permissions | -rw-r--r-- |
2 | 1 |
/* |
31529
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
29986
diff
changeset
|
2 |
* Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
package java.net; |
|
26 |
import java.io.IOException; |
|
27 |
import java.io.InputStream; |
|
28 |
import java.io.OutputStream; |
|
29 |
import java.io.BufferedOutputStream; |
|
30 |
import java.security.AccessController; |
|
50817
fa1e04811ff6
8066709: Make some JDK system properties read only
rriggs
parents:
47478
diff
changeset
|
31 |
|
fa1e04811ff6
8066709: Make some JDK system properties read only
rriggs
parents:
47478
diff
changeset
|
32 |
import jdk.internal.util.StaticProperty; |
7982
65f5328a67a2
6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
7668
diff
changeset
|
33 |
import sun.net.SocksProxy; |
31529
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
29986
diff
changeset
|
34 |
import sun.net.spi.DefaultProxySelector; |
2 | 35 |
import sun.net.www.ParseUtil; |
36 |
||
37 |
/** |
|
38 |
* SOCKS (V4 & V5) TCP socket implementation (RFC 1928). |
|
39 |
* This is a subclass of PlainSocketImpl. |
|
40 |
* Note this class should <b>NOT</b> be public. |
|
41 |
*/ |
|
42 |
||
43 |
class SocksSocketImpl extends PlainSocketImpl implements SocksConsts { |
|
44 |
private String server = null; |
|
3051
9481bd560a57
6852108: Remove Preferences dependance from SocksSocketImpl
jccollet
parents:
715
diff
changeset
|
45 |
private int serverPort = DEFAULT_PORT; |
2 | 46 |
private InetSocketAddress external_address; |
47 |
private boolean useV4 = false; |
|
48 |
private Socket cmdsock = null; |
|
49 |
private InputStream cmdIn = null; |
|
50 |
private OutputStream cmdOut = null; |
|
51 |
||
52 |
SocksSocketImpl() { |
|
53 |
// Nothing needed |
|
54 |
} |
|
55 |
||
56 |
SocksSocketImpl(Proxy proxy) { |
|
57 |
SocketAddress a = proxy.address(); |
|
58 |
if (a instanceof InetSocketAddress) { |
|
59 |
InetSocketAddress ad = (InetSocketAddress) a; |
|
60 |
// Use getHostString() to avoid reverse lookups |
|
61 |
server = ad.getHostString(); |
|
3051
9481bd560a57
6852108: Remove Preferences dependance from SocksSocketImpl
jccollet
parents:
715
diff
changeset
|
62 |
serverPort = ad.getPort(); |
2 | 63 |
} |
31529
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
29986
diff
changeset
|
64 |
useV4 = useV4(proxy); |
2 | 65 |
} |
66 |
||
31529
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
29986
diff
changeset
|
67 |
private static boolean useV4(Proxy proxy) { |
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
29986
diff
changeset
|
68 |
if (proxy instanceof SocksProxy |
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
29986
diff
changeset
|
69 |
&& ((SocksProxy)proxy).protocolVersion() == 4) { |
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
29986
diff
changeset
|
70 |
return true; |
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
29986
diff
changeset
|
71 |
} |
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
29986
diff
changeset
|
72 |
return DefaultProxySelector.socksProxyVersion() == 4; |
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
29986
diff
changeset
|
73 |
} |
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
29986
diff
changeset
|
74 |
|
2 | 75 |
private synchronized void privilegedConnect(final String host, |
76 |
final int port, |
|
77 |
final int timeout) |
|
78 |
throws IOException |
|
79 |
{ |
|
80 |
try { |
|
81 |
AccessController.doPrivileged( |
|
29986
97167d851fc4
8078467: Update core libraries to use diamond with anonymous classes
darcy
parents:
29112
diff
changeset
|
82 |
new java.security.PrivilegedExceptionAction<>() { |
51 | 83 |
public Void run() throws IOException { |
2 | 84 |
superConnectServer(host, port, timeout); |
85 |
cmdIn = getInputStream(); |
|
86 |
cmdOut = getOutputStream(); |
|
87 |
return null; |
|
88 |
} |
|
89 |
}); |
|
90 |
} catch (java.security.PrivilegedActionException pae) { |
|
91 |
throw (IOException) pae.getException(); |
|
92 |
} |
|
93 |
} |
|
94 |
||
95 |
private void superConnectServer(String host, int port, |
|
96 |
int timeout) throws IOException { |
|
97 |
super.connect(new InetSocketAddress(host, port), timeout); |
|
98 |
} |
|
99 |
||
5147
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
100 |
private static int remainingMillis(long deadlineMillis) throws IOException { |
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
101 |
if (deadlineMillis == 0L) |
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
102 |
return 0; |
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
103 |
|
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
104 |
final long remaining = deadlineMillis - System.currentTimeMillis(); |
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
105 |
if (remaining > 0) |
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
106 |
return (int) remaining; |
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
107 |
|
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
108 |
throw new SocketTimeoutException(); |
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
109 |
} |
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
110 |
|
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
111 |
private int readSocksReply(InputStream in, byte[] data, long deadlineMillis) throws IOException { |
2 | 112 |
int len = data.length; |
113 |
int received = 0; |
|
22276
7fc4c8b08e49
7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
21278
diff
changeset
|
114 |
while (received < len) { |
5147
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
115 |
int count; |
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
116 |
try { |
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
117 |
count = ((SocketInputStream)in).read(data, received, len - received, remainingMillis(deadlineMillis)); |
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
118 |
} catch (SocketTimeoutException e) { |
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
119 |
throw new SocketTimeoutException("Connect timed out"); |
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
120 |
} |
2 | 121 |
if (count < 0) |
122 |
throw new SocketException("Malformed reply from SOCKS server"); |
|
123 |
received += count; |
|
124 |
} |
|
125 |
return received; |
|
126 |
} |
|
127 |
||
5147
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
128 |
private boolean authenticate(byte method, InputStream in, |
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
129 |
BufferedOutputStream out, |
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
130 |
long deadlineMillis) throws IOException { |
2 | 131 |
// No Authentication required. We're done then! |
132 |
if (method == NO_AUTH) |
|
133 |
return true; |
|
52499
768b1c612100
8213490: Networking area typos and inconsistencies cleanup
prappo
parents:
50817
diff
changeset
|
134 |
/* |
2 | 135 |
* User/Password authentication. Try, in that order : |
136 |
* - The application provided Authenticator, if any |
|
137 |
* - the user.name & no password (backward compatibility behavior). |
|
138 |
*/ |
|
139 |
if (method == USER_PASSW) { |
|
140 |
String userName; |
|
141 |
String password = null; |
|
142 |
final InetAddress addr = InetAddress.getByName(server); |
|
51 | 143 |
PasswordAuthentication pw = |
2 | 144 |
java.security.AccessController.doPrivileged( |
29986
97167d851fc4
8078467: Update core libraries to use diamond with anonymous classes
darcy
parents:
29112
diff
changeset
|
145 |
new java.security.PrivilegedAction<>() { |
51 | 146 |
public PasswordAuthentication run() { |
2 | 147 |
return Authenticator.requestPasswordAuthentication( |
3051
9481bd560a57
6852108: Remove Preferences dependance from SocksSocketImpl
jccollet
parents:
715
diff
changeset
|
148 |
server, addr, serverPort, "SOCKS5", "SOCKS authentication", null); |
2 | 149 |
} |
150 |
}); |
|
151 |
if (pw != null) { |
|
152 |
userName = pw.getUserName(); |
|
153 |
password = new String(pw.getPassword()); |
|
154 |
} else { |
|
50817
fa1e04811ff6
8066709: Make some JDK system properties read only
rriggs
parents:
47478
diff
changeset
|
155 |
userName = StaticProperty.userName(); |
2 | 156 |
} |
157 |
if (userName == null) |
|
158 |
return false; |
|
159 |
out.write(1); |
|
160 |
out.write(userName.length()); |
|
161 |
try { |
|
162 |
out.write(userName.getBytes("ISO-8859-1")); |
|
163 |
} catch (java.io.UnsupportedEncodingException uee) { |
|
164 |
assert false; |
|
165 |
} |
|
166 |
if (password != null) { |
|
167 |
out.write(password.length()); |
|
168 |
try { |
|
169 |
out.write(password.getBytes("ISO-8859-1")); |
|
170 |
} catch (java.io.UnsupportedEncodingException uee) { |
|
171 |
assert false; |
|
172 |
} |
|
173 |
} else |
|
174 |
out.write(0); |
|
175 |
out.flush(); |
|
3051
9481bd560a57
6852108: Remove Preferences dependance from SocksSocketImpl
jccollet
parents:
715
diff
changeset
|
176 |
byte[] data = new byte[2]; |
5147
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
177 |
int i = readSocksReply(in, data, deadlineMillis); |
2 | 178 |
if (i != 2 || data[1] != 0) { |
179 |
/* RFC 1929 specifies that the connection MUST be closed if |
|
180 |
authentication fails */ |
|
181 |
out.close(); |
|
182 |
in.close(); |
|
183 |
return false; |
|
184 |
} |
|
185 |
/* Authentication succeeded */ |
|
186 |
return true; |
|
187 |
} |
|
188 |
return false; |
|
189 |
} |
|
190 |
||
191 |
private void connectV4(InputStream in, OutputStream out, |
|
5147
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
192 |
InetSocketAddress endpoint, |
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
193 |
long deadlineMillis) throws IOException { |
2 | 194 |
if (!(endpoint.getAddress() instanceof Inet4Address)) { |
195 |
throw new SocketException("SOCKS V4 requires IPv4 only addresses"); |
|
196 |
} |
|
197 |
out.write(PROTO_VERS4); |
|
198 |
out.write(CONNECT); |
|
199 |
out.write((endpoint.getPort() >> 8) & 0xff); |
|
200 |
out.write((endpoint.getPort() >> 0) & 0xff); |
|
201 |
out.write(endpoint.getAddress().getAddress()); |
|
3450
2f08a8bb9b83
6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents:
715
diff
changeset
|
202 |
String userName = getUserName(); |
2 | 203 |
try { |
204 |
out.write(userName.getBytes("ISO-8859-1")); |
|
205 |
} catch (java.io.UnsupportedEncodingException uee) { |
|
206 |
assert false; |
|
207 |
} |
|
208 |
out.write(0); |
|
209 |
out.flush(); |
|
210 |
byte[] data = new byte[8]; |
|
5147
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
211 |
int n = readSocksReply(in, data, deadlineMillis); |
2 | 212 |
if (n != 8) |
213 |
throw new SocketException("Reply from SOCKS server has bad length: " + n); |
|
214 |
if (data[0] != 0 && data[0] != 4) |
|
215 |
throw new SocketException("Reply from SOCKS server has bad version"); |
|
216 |
SocketException ex = null; |
|
217 |
switch (data[1]) { |
|
218 |
case 90: |
|
219 |
// Success! |
|
220 |
external_address = endpoint; |
|
221 |
break; |
|
222 |
case 91: |
|
223 |
ex = new SocketException("SOCKS request rejected"); |
|
224 |
break; |
|
225 |
case 92: |
|
226 |
ex = new SocketException("SOCKS server couldn't reach destination"); |
|
227 |
break; |
|
228 |
case 93: |
|
229 |
ex = new SocketException("SOCKS authentication failed"); |
|
230 |
break; |
|
231 |
default: |
|
232 |
ex = new SocketException("Reply from SOCKS server contains bad status"); |
|
233 |
break; |
|
234 |
} |
|
235 |
if (ex != null) { |
|
236 |
in.close(); |
|
237 |
out.close(); |
|
238 |
throw ex; |
|
239 |
} |
|
240 |
} |
|
241 |
||
242 |
/** |
|
243 |
* Connects the Socks Socket to the specified endpoint. It will first |
|
244 |
* connect to the SOCKS proxy and negotiate the access. If the proxy |
|
245 |
* grants the connections, then the connect is successful and all |
|
246 |
* further traffic will go to the "real" endpoint. |
|
247 |
* |
|
19069 | 248 |
* @param endpoint the {@code SocketAddress} to connect to. |
2 | 249 |
* @param timeout the timeout value in milliseconds |
250 |
* @throws IOException if the connection can't be established. |
|
251 |
* @throws SecurityException if there is a security manager and it |
|
252 |
* doesn't allow the connection |
|
253 |
* @throws IllegalArgumentException if endpoint is null or a |
|
254 |
* SocketAddress subclass not supported by this socket |
|
255 |
*/ |
|
3051
9481bd560a57
6852108: Remove Preferences dependance from SocksSocketImpl
jccollet
parents:
715
diff
changeset
|
256 |
@Override |
2 | 257 |
protected void connect(SocketAddress endpoint, int timeout) throws IOException { |
5147
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
258 |
final long deadlineMillis; |
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
259 |
|
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
260 |
if (timeout == 0) { |
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
261 |
deadlineMillis = 0L; |
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
262 |
} else { |
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
263 |
long finish = System.currentTimeMillis() + timeout; |
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
264 |
deadlineMillis = finish < 0 ? Long.MAX_VALUE : finish; |
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
265 |
} |
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
266 |
|
2 | 267 |
SecurityManager security = System.getSecurityManager(); |
268 |
if (endpoint == null || !(endpoint instanceof InetSocketAddress)) |
|
269 |
throw new IllegalArgumentException("Unsupported address type"); |
|
270 |
InetSocketAddress epoint = (InetSocketAddress) endpoint; |
|
271 |
if (security != null) { |
|
272 |
if (epoint.isUnresolved()) |
|
273 |
security.checkConnect(epoint.getHostName(), |
|
274 |
epoint.getPort()); |
|
275 |
else |
|
276 |
security.checkConnect(epoint.getAddress().getHostAddress(), |
|
277 |
epoint.getPort()); |
|
278 |
} |
|
279 |
if (server == null) { |
|
280 |
// This is the general case |
|
281 |
// server is not null only when the socket was created with a |
|
282 |
// specified proxy in which case it does bypass the ProxySelector |
|
51 | 283 |
ProxySelector sel = java.security.AccessController.doPrivileged( |
29986
97167d851fc4
8078467: Update core libraries to use diamond with anonymous classes
darcy
parents:
29112
diff
changeset
|
284 |
new java.security.PrivilegedAction<>() { |
51 | 285 |
public ProxySelector run() { |
2 | 286 |
return ProxySelector.getDefault(); |
287 |
} |
|
288 |
}); |
|
289 |
if (sel == null) { |
|
290 |
/* |
|
291 |
* No default proxySelector --> direct connection |
|
292 |
*/ |
|
5147
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
293 |
super.connect(epoint, remainingMillis(deadlineMillis)); |
2 | 294 |
return; |
295 |
} |
|
3051
9481bd560a57
6852108: Remove Preferences dependance from SocksSocketImpl
jccollet
parents:
715
diff
changeset
|
296 |
URI uri; |
2 | 297 |
// Use getHostString() to avoid reverse lookups |
298 |
String host = epoint.getHostString(); |
|
52499
768b1c612100
8213490: Networking area typos and inconsistencies cleanup
prappo
parents:
50817
diff
changeset
|
299 |
// IPv6 literal? |
2 | 300 |
if (epoint.getAddress() instanceof Inet6Address && |
24685
215fa91e1b4c
8044461: Cleanup new Boolean and single character strings
rriggs
parents:
22276
diff
changeset
|
301 |
(!host.startsWith("[")) && (host.indexOf(':') >= 0)) { |
2 | 302 |
host = "[" + host + "]"; |
303 |
} |
|
304 |
try { |
|
305 |
uri = new URI("socket://" + ParseUtil.encodePath(host) + ":"+ epoint.getPort()); |
|
306 |
} catch (URISyntaxException e) { |
|
307 |
// This shouldn't happen |
|
308 |
assert false : e; |
|
3051
9481bd560a57
6852108: Remove Preferences dependance from SocksSocketImpl
jccollet
parents:
715
diff
changeset
|
309 |
uri = null; |
2 | 310 |
} |
311 |
Proxy p = null; |
|
312 |
IOException savedExc = null; |
|
313 |
java.util.Iterator<Proxy> iProxy = null; |
|
314 |
iProxy = sel.select(uri).iterator(); |
|
315 |
if (iProxy == null || !(iProxy.hasNext())) { |
|
5147
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
316 |
super.connect(epoint, remainingMillis(deadlineMillis)); |
2 | 317 |
return; |
318 |
} |
|
319 |
while (iProxy.hasNext()) { |
|
320 |
p = iProxy.next(); |
|
29112
df7e4edb6566
7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
25859
diff
changeset
|
321 |
if (p == null || p.type() != Proxy.Type.SOCKS) { |
5147
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
322 |
super.connect(epoint, remainingMillis(deadlineMillis)); |
2 | 323 |
return; |
324 |
} |
|
29112
df7e4edb6566
7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
25859
diff
changeset
|
325 |
|
2 | 326 |
if (!(p.address() instanceof InetSocketAddress)) |
29112
df7e4edb6566
7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents:
25859
diff
changeset
|
327 |
throw new SocketException("Unknown address type for proxy: " + p); |
2 | 328 |
// Use getHostString() to avoid reverse lookups |
329 |
server = ((InetSocketAddress) p.address()).getHostString(); |
|
3051
9481bd560a57
6852108: Remove Preferences dependance from SocksSocketImpl
jccollet
parents:
715
diff
changeset
|
330 |
serverPort = ((InetSocketAddress) p.address()).getPort(); |
31529
31d7d82b39ff
8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents:
29986
diff
changeset
|
331 |
useV4 = useV4(p); |
2 | 332 |
|
333 |
// Connects to the SOCKS server |
|
334 |
try { |
|
5147
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
335 |
privilegedConnect(server, serverPort, remainingMillis(deadlineMillis)); |
2 | 336 |
// Worked, let's get outta here |
337 |
break; |
|
338 |
} catch (IOException e) { |
|
339 |
// Ooops, let's notify the ProxySelector |
|
340 |
sel.connectFailed(uri,p.address(),e); |
|
341 |
server = null; |
|
3051
9481bd560a57
6852108: Remove Preferences dependance from SocksSocketImpl
jccollet
parents:
715
diff
changeset
|
342 |
serverPort = -1; |
2 | 343 |
savedExc = e; |
344 |
// Will continue the while loop and try the next proxy |
|
345 |
} |
|
346 |
} |
|
347 |
||
348 |
/* |
|
349 |
* If server is still null at this point, none of the proxy |
|
350 |
* worked |
|
351 |
*/ |
|
352 |
if (server == null) { |
|
353 |
throw new SocketException("Can't connect to SOCKS proxy:" |
|
354 |
+ savedExc.getMessage()); |
|
355 |
} |
|
356 |
} else { |
|
357 |
// Connects to the SOCKS server |
|
358 |
try { |
|
5147
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
359 |
privilegedConnect(server, serverPort, remainingMillis(deadlineMillis)); |
2 | 360 |
} catch (IOException e) { |
361 |
throw new SocketException(e.getMessage()); |
|
362 |
} |
|
363 |
} |
|
364 |
||
21278 | 365 |
// cmdIn & cmdOut were initialized during the privilegedConnect() call |
2 | 366 |
BufferedOutputStream out = new BufferedOutputStream(cmdOut, 512); |
367 |
InputStream in = cmdIn; |
|
368 |
||
369 |
if (useV4) { |
|
370 |
// SOCKS Protocol version 4 doesn't know how to deal with |
|
371 |
// DOMAIN type of addresses (unresolved addresses here) |
|
372 |
if (epoint.isUnresolved()) |
|
373 |
throw new UnknownHostException(epoint.toString()); |
|
5147
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
374 |
connectV4(in, out, epoint, deadlineMillis); |
2 | 375 |
return; |
376 |
} |
|
377 |
||
378 |
// This is SOCKS V5 |
|
379 |
out.write(PROTO_VERS); |
|
380 |
out.write(2); |
|
381 |
out.write(NO_AUTH); |
|
382 |
out.write(USER_PASSW); |
|
383 |
out.flush(); |
|
384 |
byte[] data = new byte[2]; |
|
5147
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
385 |
int i = readSocksReply(in, data, deadlineMillis); |
2 | 386 |
if (i != 2 || ((int)data[0]) != PROTO_VERS) { |
387 |
// Maybe it's not a V5 sever after all |
|
388 |
// Let's try V4 before we give up |
|
389 |
// SOCKS Protocol version 4 doesn't know how to deal with |
|
390 |
// DOMAIN type of addresses (unresolved addresses here) |
|
391 |
if (epoint.isUnresolved()) |
|
392 |
throw new UnknownHostException(epoint.toString()); |
|
5147
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
393 |
connectV4(in, out, epoint, deadlineMillis); |
2 | 394 |
return; |
395 |
} |
|
396 |
if (((int)data[1]) == NO_METHODS) |
|
397 |
throw new SocketException("SOCKS : No acceptable methods"); |
|
5147
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
398 |
if (!authenticate(data[1], in, out, deadlineMillis)) { |
2 | 399 |
throw new SocketException("SOCKS : authentication failed"); |
400 |
} |
|
401 |
out.write(PROTO_VERS); |
|
402 |
out.write(CONNECT); |
|
403 |
out.write(0); |
|
404 |
/* Test for IPV4/IPV6/Unresolved */ |
|
405 |
if (epoint.isUnresolved()) { |
|
406 |
out.write(DOMAIN_NAME); |
|
407 |
out.write(epoint.getHostName().length()); |
|
408 |
try { |
|
409 |
out.write(epoint.getHostName().getBytes("ISO-8859-1")); |
|
410 |
} catch (java.io.UnsupportedEncodingException uee) { |
|
411 |
assert false; |
|
412 |
} |
|
413 |
out.write((epoint.getPort() >> 8) & 0xff); |
|
414 |
out.write((epoint.getPort() >> 0) & 0xff); |
|
415 |
} else if (epoint.getAddress() instanceof Inet6Address) { |
|
416 |
out.write(IPV6); |
|
417 |
out.write(epoint.getAddress().getAddress()); |
|
418 |
out.write((epoint.getPort() >> 8) & 0xff); |
|
419 |
out.write((epoint.getPort() >> 0) & 0xff); |
|
420 |
} else { |
|
421 |
out.write(IPV4); |
|
422 |
out.write(epoint.getAddress().getAddress()); |
|
423 |
out.write((epoint.getPort() >> 8) & 0xff); |
|
424 |
out.write((epoint.getPort() >> 0) & 0xff); |
|
425 |
} |
|
426 |
out.flush(); |
|
427 |
data = new byte[4]; |
|
5147
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
428 |
i = readSocksReply(in, data, deadlineMillis); |
2 | 429 |
if (i != 4) |
430 |
throw new SocketException("Reply from SOCKS server has bad length"); |
|
431 |
SocketException ex = null; |
|
3051
9481bd560a57
6852108: Remove Preferences dependance from SocksSocketImpl
jccollet
parents:
715
diff
changeset
|
432 |
int len; |
2 | 433 |
byte[] addr; |
434 |
switch (data[1]) { |
|
435 |
case REQUEST_OK: |
|
436 |
// success! |
|
437 |
switch(data[3]) { |
|
438 |
case IPV4: |
|
439 |
addr = new byte[4]; |
|
5147
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
440 |
i = readSocksReply(in, addr, deadlineMillis); |
2 | 441 |
if (i != 4) |
442 |
throw new SocketException("Reply from SOCKS server badly formatted"); |
|
443 |
data = new byte[2]; |
|
5147
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
444 |
i = readSocksReply(in, data, deadlineMillis); |
2 | 445 |
if (i != 2) |
446 |
throw new SocketException("Reply from SOCKS server badly formatted"); |
|
447 |
break; |
|
448 |
case DOMAIN_NAME: |
|
22276
7fc4c8b08e49
7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
21278
diff
changeset
|
449 |
byte[] lenByte = new byte[1]; |
7fc4c8b08e49
7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
21278
diff
changeset
|
450 |
i = readSocksReply(in, lenByte, deadlineMillis); |
7fc4c8b08e49
7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
21278
diff
changeset
|
451 |
if (i != 1) |
7fc4c8b08e49
7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
21278
diff
changeset
|
452 |
throw new SocketException("Reply from SOCKS server badly formatted"); |
7fc4c8b08e49
7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
21278
diff
changeset
|
453 |
len = lenByte[0] & 0xFF; |
2 | 454 |
byte[] host = new byte[len]; |
5147
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
455 |
i = readSocksReply(in, host, deadlineMillis); |
2 | 456 |
if (i != len) |
457 |
throw new SocketException("Reply from SOCKS server badly formatted"); |
|
458 |
data = new byte[2]; |
|
5147
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
459 |
i = readSocksReply(in, data, deadlineMillis); |
2 | 460 |
if (i != 2) |
461 |
throw new SocketException("Reply from SOCKS server badly formatted"); |
|
462 |
break; |
|
463 |
case IPV6: |
|
22276
7fc4c8b08e49
7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents:
21278
diff
changeset
|
464 |
len = 16; |
2 | 465 |
addr = new byte[len]; |
5147
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
466 |
i = readSocksReply(in, addr, deadlineMillis); |
2 | 467 |
if (i != len) |
468 |
throw new SocketException("Reply from SOCKS server badly formatted"); |
|
469 |
data = new byte[2]; |
|
5147
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
3464
diff
changeset
|
470 |
i = readSocksReply(in, data, deadlineMillis); |
2 | 471 |
if (i != 2) |
472 |
throw new SocketException("Reply from SOCKS server badly formatted"); |
|
473 |
break; |
|
474 |
default: |
|
475 |
ex = new SocketException("Reply from SOCKS server contains wrong code"); |
|
476 |
break; |
|
477 |
} |
|
478 |
break; |
|
479 |
case GENERAL_FAILURE: |
|
480 |
ex = new SocketException("SOCKS server general failure"); |
|
481 |
break; |
|
482 |
case NOT_ALLOWED: |
|
483 |
ex = new SocketException("SOCKS: Connection not allowed by ruleset"); |
|
484 |
break; |
|
485 |
case NET_UNREACHABLE: |
|
486 |
ex = new SocketException("SOCKS: Network unreachable"); |
|
487 |
break; |
|
488 |
case HOST_UNREACHABLE: |
|
489 |
ex = new SocketException("SOCKS: Host unreachable"); |
|
490 |
break; |
|
491 |
case CONN_REFUSED: |
|
492 |
ex = new SocketException("SOCKS: Connection refused"); |
|
493 |
break; |
|
494 |
case TTL_EXPIRED: |
|
495 |
ex = new SocketException("SOCKS: TTL expired"); |
|
496 |
break; |
|
497 |
case CMD_NOT_SUPPORTED: |
|
498 |
ex = new SocketException("SOCKS: Command not supported"); |
|
499 |
break; |
|
500 |
case ADDR_TYPE_NOT_SUP: |
|
501 |
ex = new SocketException("SOCKS: address type not supported"); |
|
502 |
break; |
|
503 |
} |
|
504 |
if (ex != null) { |
|
505 |
in.close(); |
|
506 |
out.close(); |
|
507 |
throw ex; |
|
508 |
} |
|
509 |
external_address = epoint; |
|
510 |
} |
|
511 |
||
512 |
||
513 |
||
514 |
/** |
|
19069 | 515 |
* Returns the value of this socket's {@code address} field. |
2 | 516 |
* |
19069 | 517 |
* @return the value of this socket's {@code address} field. |
2 | 518 |
* @see java.net.SocketImpl#address |
519 |
*/ |
|
3051
9481bd560a57
6852108: Remove Preferences dependance from SocksSocketImpl
jccollet
parents:
715
diff
changeset
|
520 |
@Override |
2 | 521 |
protected InetAddress getInetAddress() { |
522 |
if (external_address != null) |
|
523 |
return external_address.getAddress(); |
|
524 |
else |
|
525 |
return super.getInetAddress(); |
|
526 |
} |
|
527 |
||
528 |
/** |
|
19069 | 529 |
* Returns the value of this socket's {@code port} field. |
2 | 530 |
* |
19069 | 531 |
* @return the value of this socket's {@code port} field. |
2 | 532 |
* @see java.net.SocketImpl#port |
533 |
*/ |
|
3051
9481bd560a57
6852108: Remove Preferences dependance from SocksSocketImpl
jccollet
parents:
715
diff
changeset
|
534 |
@Override |
2 | 535 |
protected int getPort() { |
536 |
if (external_address != null) |
|
537 |
return external_address.getPort(); |
|
538 |
else |
|
539 |
return super.getPort(); |
|
540 |
} |
|
541 |
||
3051
9481bd560a57
6852108: Remove Preferences dependance from SocksSocketImpl
jccollet
parents:
715
diff
changeset
|
542 |
@Override |
2 | 543 |
protected void close() throws IOException { |
544 |
if (cmdsock != null) |
|
545 |
cmdsock.close(); |
|
546 |
cmdsock = null; |
|
547 |
super.close(); |
|
548 |
} |
|
549 |
||
3450
2f08a8bb9b83
6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents:
715
diff
changeset
|
550 |
private String getUserName() { |
53473
9366628d727b
8216986: Remove unused code from SocksSocketImpl
michaelm
parents:
52499
diff
changeset
|
551 |
return StaticProperty.userName(); |
3450
2f08a8bb9b83
6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents:
715
diff
changeset
|
552 |
} |
2 | 553 |
} |