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