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