Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/net/spi/DefaultProxySelector.java
38918 views
1
/*
2
* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
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
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
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
*
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.
24
*/
25
26
package sun.net.spi;
27
28
import java.net.InetSocketAddress;
29
import java.net.Proxy;
30
import java.net.ProxySelector;
31
import java.net.SocketAddress;
32
import java.net.URI;
33
import java.util.ArrayList;
34
import java.util.List;
35
import java.io.IOException;
36
import java.security.AccessController;
37
import java.security.PrivilegedAction;
38
import java.util.StringJoiner;
39
import java.util.regex.Pattern;
40
import sun.net.NetProperties;
41
import sun.net.SocksProxy;
42
import static java.util.regex.Pattern.quote;
43
44
/**
45
* Supports proxy settings using system properties This proxy selector
46
* provides backward compatibility with the old http protocol handler
47
* as far as how proxy is set
48
*
49
* Most of the implementation copied from the old http protocol handler
50
*
51
* Supports http/https/ftp.proxyHost, http/https/ftp.proxyPort,
52
* proxyHost, proxyPort, and http/https/ftp.nonProxyHost, and socks.
53
* NOTE: need to do gopher as well
54
*/
55
public class DefaultProxySelector extends ProxySelector {
56
57
/**
58
* This is where we define all the valid System Properties we have to
59
* support for each given protocol.
60
* The format of this 2 dimensional array is :
61
* - 1 row per protocol (http, ftp, ...)
62
* - 1st element of each row is the protocol name
63
* - subsequent elements are prefixes for Host & Port properties
64
* listed in order of priority.
65
* Example:
66
* {"ftp", "ftp.proxy", "ftpProxy", "proxy", "socksProxy"},
67
* means for FTP we try in that oder:
68
* + ftp.proxyHost & ftp.proxyPort
69
* + ftpProxyHost & ftpProxyPort
70
* + proxyHost & proxyPort
71
* + socksProxyHost & socksProxyPort
72
*
73
* Note that the socksProxy should *always* be the last on the list
74
*/
75
final static String[][] props = {
76
/*
77
* protocol, Property prefix 1, Property prefix 2, ...
78
*/
79
{"http", "http.proxy", "proxy", "socksProxy"},
80
{"https", "https.proxy", "proxy", "socksProxy"},
81
{"ftp", "ftp.proxy", "ftpProxy", "proxy", "socksProxy"},
82
{"gopher", "gopherProxy", "socksProxy"},
83
{"socket", "socksProxy"}
84
};
85
86
private static final String SOCKS_PROXY_VERSION = "socksProxyVersion";
87
88
private static boolean hasSystemProxies = false;
89
90
static {
91
final String key = "java.net.useSystemProxies";
92
Boolean b = AccessController.doPrivileged(
93
new PrivilegedAction<Boolean>() {
94
public Boolean run() {
95
return NetProperties.getBoolean(key);
96
}});
97
if (b != null && b.booleanValue()) {
98
java.security.AccessController.doPrivileged(
99
new java.security.PrivilegedAction<Void>() {
100
public Void run() {
101
System.loadLibrary("net");
102
return null;
103
}
104
});
105
hasSystemProxies = init();
106
}
107
}
108
109
/**
110
* How to deal with "non proxy hosts":
111
* since we do have to generate a pattern we don't want to do that if
112
* it's not necessary. Therefore we do cache the result, on a per-protocol
113
* basis, and change it only when the "source", i.e. the system property,
114
* did change.
115
*/
116
117
static class NonProxyInfo {
118
// Default value for nonProxyHosts, this provides backward compatibility
119
// by excluding localhost and its litteral notations.
120
static final String defStringVal = "localhost|127.*|[::1]|0.0.0.0|[::0]";
121
122
String hostsSource;
123
Pattern pattern;
124
final String property;
125
final String defaultVal;
126
static NonProxyInfo ftpNonProxyInfo = new NonProxyInfo("ftp.nonProxyHosts", null, null, defStringVal);
127
static NonProxyInfo httpNonProxyInfo = new NonProxyInfo("http.nonProxyHosts", null, null, defStringVal);
128
static NonProxyInfo socksNonProxyInfo = new NonProxyInfo("socksNonProxyHosts", null, null, defStringVal);
129
130
NonProxyInfo(String p, String s, Pattern pattern, String d) {
131
property = p;
132
hostsSource = s;
133
this.pattern = pattern;
134
defaultVal = d;
135
}
136
}
137
138
139
/**
140
* select() method. Where all the hard work is done.
141
* Build a list of proxies depending on URI.
142
* Since we're only providing compatibility with the system properties
143
* from previous releases (see list above), that list will always
144
* contain 1 single proxy, default being NO_PROXY.
145
*/
146
public java.util.List<Proxy> select(URI uri) {
147
if (uri == null) {
148
throw new IllegalArgumentException("URI can't be null.");
149
}
150
String protocol = uri.getScheme();
151
String host = uri.getHost();
152
153
if (host == null) {
154
// This is a hack to ensure backward compatibility in two
155
// cases: 1. hostnames contain non-ascii characters,
156
// internationalized domain names. in which case, URI will
157
// return null, see BugID 4957669; 2. Some hostnames can
158
// contain '_' chars even though it's not supposed to be
159
// legal, in which case URI will return null for getHost,
160
// but not for getAuthority() See BugID 4913253
161
String auth = uri.getAuthority();
162
if (auth != null) {
163
int i;
164
i = auth.indexOf('@');
165
if (i >= 0) {
166
auth = auth.substring(i+1);
167
}
168
i = auth.lastIndexOf(':');
169
if (i >= 0) {
170
auth = auth.substring(0,i);
171
}
172
host = auth;
173
}
174
}
175
176
if (protocol == null || host == null) {
177
throw new IllegalArgumentException("protocol = "+protocol+" host = "+host);
178
}
179
List<Proxy> proxyl = new ArrayList<Proxy>(1);
180
181
NonProxyInfo pinfo = null;
182
183
if ("http".equalsIgnoreCase(protocol)) {
184
pinfo = NonProxyInfo.httpNonProxyInfo;
185
} else if ("https".equalsIgnoreCase(protocol)) {
186
// HTTPS uses the same property as HTTP, for backward
187
// compatibility
188
pinfo = NonProxyInfo.httpNonProxyInfo;
189
} else if ("ftp".equalsIgnoreCase(protocol)) {
190
pinfo = NonProxyInfo.ftpNonProxyInfo;
191
} else if ("socket".equalsIgnoreCase(protocol)) {
192
pinfo = NonProxyInfo.socksNonProxyInfo;
193
}
194
195
/**
196
* Let's check the System properties for that protocol
197
*/
198
final String proto = protocol;
199
final NonProxyInfo nprop = pinfo;
200
final String urlhost = host.toLowerCase();
201
202
/**
203
* This is one big doPrivileged call, but we're trying to optimize
204
* the code as much as possible. Since we're checking quite a few
205
* System properties it does help having only 1 call to doPrivileged.
206
* Be mindful what you do in here though!
207
*/
208
Proxy p = AccessController.doPrivileged(
209
new PrivilegedAction<Proxy>() {
210
public Proxy run() {
211
int i, j;
212
String phost = null;
213
int pport = 0;
214
String nphosts = null;
215
InetSocketAddress saddr = null;
216
217
// Then let's walk the list of protocols in our array
218
for (i=0; i<props.length; i++) {
219
if (props[i][0].equalsIgnoreCase(proto)) {
220
for (j = 1; j < props[i].length; j++) {
221
/* System.getProp() will give us an empty
222
* String, "" for a defined but "empty"
223
* property.
224
*/
225
phost = NetProperties.get(props[i][j]+"Host");
226
if (phost != null && phost.length() != 0)
227
break;
228
}
229
if (phost == null || phost.length() == 0) {
230
/**
231
* No system property defined for that
232
* protocol. Let's check System Proxy
233
* settings (Gnome & Windows) if we were
234
* instructed to.
235
*/
236
if (hasSystemProxies) {
237
String sproto;
238
if (proto.equalsIgnoreCase("socket"))
239
sproto = "socks";
240
else
241
sproto = proto;
242
Proxy sproxy = getSystemProxy(sproto, urlhost);
243
if (sproxy != null) {
244
return sproxy;
245
}
246
}
247
return Proxy.NO_PROXY;
248
}
249
// If a Proxy Host is defined for that protocol
250
// Let's get the NonProxyHosts property
251
if (nprop != null) {
252
nphosts = NetProperties.get(nprop.property);
253
synchronized (nprop) {
254
if (nphosts == null) {
255
if (nprop.defaultVal != null) {
256
nphosts = nprop.defaultVal;
257
} else {
258
nprop.hostsSource = null;
259
nprop.pattern = null;
260
}
261
} else if (nphosts.length() != 0) {
262
// add the required default patterns
263
// but only if property no set. If it
264
// is empty, leave empty.
265
nphosts += "|" + NonProxyInfo
266
.defStringVal;
267
}
268
if (nphosts != null) {
269
if (!nphosts.equals(nprop.hostsSource)) {
270
nprop.pattern = toPattern(nphosts);
271
nprop.hostsSource = nphosts;
272
}
273
}
274
if (shouldNotUseProxyFor(nprop.pattern, urlhost)) {
275
return Proxy.NO_PROXY;
276
}
277
}
278
}
279
// We got a host, let's check for port
280
281
pport = NetProperties.getInteger(props[i][j]+"Port", 0).intValue();
282
if (pport == 0 && j < (props[i].length - 1)) {
283
// Can't find a port with same prefix as Host
284
// AND it's not a SOCKS proxy
285
// Let's try the other prefixes for that proto
286
for (int k = 1; k < (props[i].length - 1); k++) {
287
if ((k != j) && (pport == 0))
288
pport = NetProperties.getInteger(props[i][k]+"Port", 0).intValue();
289
}
290
}
291
292
// Still couldn't find a port, let's use default
293
if (pport == 0) {
294
if (j == (props[i].length - 1)) // SOCKS
295
pport = defaultPort("socket");
296
else
297
pport = defaultPort(proto);
298
}
299
// We did find a proxy definition.
300
// Let's create the address, but don't resolve it
301
// as this will be done at connection time
302
saddr = InetSocketAddress.createUnresolved(phost, pport);
303
// Socks is *always* the last on the list.
304
if (j == (props[i].length - 1)) {
305
int version = NetProperties.getInteger(SOCKS_PROXY_VERSION, 5).intValue();
306
return SocksProxy.create(saddr, version);
307
} else {
308
return new Proxy(Proxy.Type.HTTP, saddr);
309
}
310
}
311
}
312
return Proxy.NO_PROXY;
313
}});
314
315
proxyl.add(p);
316
317
/*
318
* If no specific property was set for that URI, we should be
319
* returning an iterator to an empty List.
320
*/
321
return proxyl;
322
}
323
324
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
325
if (uri == null || sa == null || ioe == null) {
326
throw new IllegalArgumentException("Arguments can't be null.");
327
}
328
// ignored
329
}
330
331
332
private int defaultPort(String protocol) {
333
if ("http".equalsIgnoreCase(protocol)) {
334
return 80;
335
} else if ("https".equalsIgnoreCase(protocol)) {
336
return 443;
337
} else if ("ftp".equalsIgnoreCase(protocol)) {
338
return 80;
339
} else if ("socket".equalsIgnoreCase(protocol)) {
340
return 1080;
341
} else if ("gopher".equalsIgnoreCase(protocol)) {
342
return 80;
343
} else {
344
return -1;
345
}
346
}
347
348
private native static boolean init();
349
private synchronized native Proxy getSystemProxy(String protocol, String host);
350
351
/**
352
* @return {@code true} if given this pattern for non-proxy hosts and this
353
* urlhost the proxy should NOT be used to access this urlhost
354
*/
355
static boolean shouldNotUseProxyFor(Pattern pattern, String urlhost) {
356
if (pattern == null || urlhost.isEmpty())
357
return false;
358
boolean matches = pattern.matcher(urlhost).matches();
359
return matches;
360
}
361
362
/**
363
* @param mask non-null mask
364
* @return {@link java.util.regex.Pattern} corresponding to this mask
365
* or {@code null} in case mask should not match anything
366
*/
367
static Pattern toPattern(String mask) {
368
boolean disjunctionEmpty = true;
369
StringJoiner joiner = new StringJoiner("|");
370
for (String disjunct : mask.split("\\|")) {
371
if (disjunct.isEmpty())
372
continue;
373
disjunctionEmpty = false;
374
String regex = disjunctToRegex(disjunct.toLowerCase());
375
joiner.add(regex);
376
}
377
return disjunctionEmpty ? null : Pattern.compile(joiner.toString());
378
}
379
380
/**
381
* @param disjunct non-null mask disjunct
382
* @return java regex string corresponding to this mask
383
*/
384
static String disjunctToRegex(String disjunct) {
385
String regex;
386
if (disjunct.startsWith("*")) {
387
regex = ".*" + quote(disjunct.substring(1));
388
} else if (disjunct.endsWith("*")) {
389
regex = quote(disjunct.substring(0, disjunct.length() - 1)) + ".*";
390
} else {
391
regex = quote(disjunct);
392
}
393
return regex;
394
}
395
}
396
397