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