-
Notifications
You must be signed in to change notification settings - Fork 668
/
DefaultProxySelector.java
463 lines (404 loc) · 19.3 KB
/
DefaultProxySelector.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*
* Copyright (c) 2003, 2018 Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.net.spi;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.SocketAddress;
import java.net.URI;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Collections;
import java.util.List;
import java.util.StringJoiner;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import sun.net.NetProperties;
import sun.net.SocksProxy;
import static java.util.regex.Pattern.quote;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toList;
/**
* Supports proxy settings using system properties This proxy selector
* provides backward compatibility with the old http protocol handler
* as far as how proxy is set
*
* Most of the implementation copied from the old http protocol handler
*
* Supports http/https/ftp.proxyHost, http/https/ftp.proxyPort,
* proxyHost, proxyPort, and http/https/ftp.nonProxyHost, and socks.
* NOTE: need to do gopher as well
*/
// 代理选择器的默认实现
public class DefaultProxySelector extends ProxySelector {
/**
* This is where we define all the valid System Properties we have to
* support for each given protocol.
* The format of this 2 dimensional array is :
* - 1 row per protocol (http, ftp, ...)
* - 1st element of each row is the protocol name
* - subsequent elements are prefixes for Host & Port properties
* listed in order of priority.
* Example:
* {"ftp", "ftp.proxy", "ftpProxy", "proxy", "socksProxy"},
* means for FTP we try in that oder:
* + ftp.proxyHost & ftp.proxyPort
* + ftpProxyHost & ftpProxyPort
* + proxyHost & proxyPort
* + socksProxyHost & socksProxyPort
*
* Note that the socksProxy should *always* be the last on the list
*/
// 代理协议及其该协议下可用的前缀信息
static final String[][] props = {
/* protocol, Property prefix 1, Property prefix 2, ... */
{"http", "http.proxy", "proxy", "socksProxy"}, {"https", "https.proxy", "proxy", "socksProxy"}, {"ftp", "ftp.proxy", "ftpProxy", "proxy", "socksProxy"}, {"gopher", "gopherProxy", "socksProxy"}, {"socket", "socksProxy"}};
private static final String SOCKS_PROXY_VERSION = "socksProxyVersion";
private static final List<Proxy> NO_PROXY_LIST = List.of(Proxy.NO_PROXY);
// 是否存在系统代理设置
private static boolean hasSystemProxies = false;
static {
final String key = "java.net.useSystemProxies";
// 查找预设的"java.net.useSystemProxies"属性的值,用来判断是否需要加载系统代理设置
Boolean b = AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
public Boolean run() {
// 从运行参数中获取布尔类型的网络属性;如果未找到,则从net.properties配置中查找
return NetProperties.getBoolean(key);
}
});
if(b != null && b) {
AccessController.doPrivileged(new PrivilegedAction<Void>() {
public Void run() {
System.loadLibrary("net");
return null;
}
});
hasSystemProxies = init();
}
}
// 返回运行参数中指定的SOCKS代理版本;如果未指定,则使用SOCKS V5版本
public static int socksProxyVersion() {
return AccessController.doPrivileged(new PrivilegedAction<Integer>() {
@Override
public Integer run() {
return NetProperties.getInteger(SOCKS_PROXY_VERSION, 5);
}
});
}
/**
* select() method.
*
* Where all the hard work is done.
* Build a list of proxies depending on URI.
* Since we're only providing compatibility with the system properties from previous releases (see list above),
* that list will typically contain one single proxy, default being NO_PROXY.
* If we can get a system proxy it might contain more entries.
*/
// 根据指定的目标URI解析出可用的网络代理
public List<Proxy> select(URI uri) {
if(uri == null) {
throw new IllegalArgumentException("URI can't be null.");
}
// uri中的协议
String protocol = uri.getScheme();
// uri中的域名/IP
String host = uri.getHost();
if(host == null) {
/*
* This is a hack to ensure backward compatibility in two cases:
* 1. hostnames contain non-ascii characters, internationalized domain names. in which case, URI will return null, see BugID 4957669;
* 2. Some hostnames can contain '_' chars even though it's not supposed to be legal, in which case URI will return null for getHost, but not for getAuthority() See BugID 4913253
*/
// 尝试从登陆信息中解析host
String auth = uri.getAuthority();
if(auth != null) {
int i;
i = auth.indexOf('@');
if(i >= 0) {
auth = auth.substring(i + 1);
}
i = auth.lastIndexOf(':');
if(i >= 0) {
auth = auth.substring(0, i);
}
host = auth;
}
}
if(protocol == null || host == null) {
throw new IllegalArgumentException("protocol = " + protocol + " host = " + host);
}
NonProxyInfo pinfo = null;
// 判断协议类型
if("http".equalsIgnoreCase(protocol)) {
pinfo = NonProxyInfo.httpNonProxyInfo;
} else if("https".equalsIgnoreCase(protocol)) {
// HTTPS uses the same property as HTTP, for backward compatibility
pinfo = NonProxyInfo.httpNonProxyInfo;
} else if("ftp".equalsIgnoreCase(protocol)) {
pinfo = NonProxyInfo.ftpNonProxyInfo;
} else if("socket".equalsIgnoreCase(protocol)) {
pinfo = NonProxyInfo.socksNonProxyInfo;
}
/*
* Let's check the System properties for that protocol
*/
final String proto = protocol;
final NonProxyInfo nprop = pinfo;
final String urlhost = host.toLowerCase();
/*
* This is one big doPrivileged call, but we're trying to optimize the code as much as possible.
* Since we're checking quite a few System properties it does help having only 1 call to doPrivileged.
* Be mindful what you do in here though!
*/
Proxy[] proxyArray = AccessController.doPrivileged(new PrivilegedAction<Proxy[]>() {
public Proxy[] run() {
int i, j;
String phost = null;
int pport = 0;
String nphosts = null;
InetSocketAddress saddr = null;
/* Then let's walk the list of protocols in our array */
// 遍历系统支持的协议列表,找到与uri匹配的协议,协议名称应当是{"http", "https", "ftp", "gopher", "socket"}中的一种
for(i = 0; i<props.length; i++) {
// 跳过不匹配的协议
if(!props[i][0].equalsIgnoreCase(proto)) {
continue;
}
// 遍历该协议支持的前缀
for(j = 1; j<props[i].length; j++) {
/*
* System.getProp() will give us an empty String,
* "" for a defined but "empty" property.
*/
/*
* 查找与当前协议匹配的代理主机地址
*
* 先从运行参数中获取指定的网络属性;如果未找到,则从net.properties配置中查找
*
* 拿"socket"协议举例,这里会查找"socketProxyHost"属性的值
*/
phost = NetProperties.get(props[i][j] + "Host");
if(phost != null && phost.length() != 0) {
break;
}
}
// 如果未找到与当前协议匹配的主机地址
if(phost == null || phost.length() == 0) {
/*
* No system property defined for that protocol.
* Let's check System Proxy settings (Gnome, MacOsX & Windows) if we were instructed to.
*/
// 如果不存在系统代理设置,直接返回
if(!hasSystemProxies) {
return null;
}
String sproto;
if(proto.equalsIgnoreCase("socket")) {
sproto = "socks"; // 尝试将"socket"名称回退为"socks"名称
} else {
sproto = proto;
}
// 查询系统代理设置
return getSystemProxies(sproto, urlhost);
}
/* If a Proxy Host is defined for that protocol. Let's get the NonProxyHosts property */
if(nprop != null) {
/*
* 从运行参数中获取指定的网络属性;如果未找到,则从net.properties配置中查找
* 这里需要查找的是在无代理下的一些默认设置。
*/
nphosts = NetProperties.get(nprop.property);
synchronized(nprop) {
if(nphosts == null) {
if(nprop.defaultVal != null) {
nphosts = nprop.defaultVal;
} else {
nprop.hostsSource = null;
nprop.pattern = null;
}
} else if(nphosts.length() != 0) {
// add the required default patterns
// but only if property no set. If it
// is empty, leave empty.
nphosts += "|" + NonProxyInfo.defStringVal;
}
if(nphosts != null) {
if(!nphosts.equals(nprop.hostsSource)) {
nprop.pattern = toPattern(nphosts);
nprop.hostsSource = nphosts;
}
}
// 判断正则表达式nprop.pattern与目标字符串urlhost是否完全匹配
if(shouldNotUseProxyFor(nprop.pattern, urlhost)) {
return null;
}
}
}
/* We got a host, let's check for port */
// 类似地,下面开始查找预设的代理端口号
pport = NetProperties.getInteger(props[i][j] + "Port", 0);
// 回退,查找其他前缀对应的端口号
if(pport == 0 && j<(props[i].length - 1)) {
/*
* Can't find a port with same prefix as Host.
* AND it's not a SOCKS proxy.
* Let's try the other prefixes for that proto.
*/
for(int k = 1; k<(props[i].length - 1); k++) {
if((k != j) && (pport == 0)) {
pport = NetProperties.getInteger(props[i][k] + "Port", 0);
}
}
}
// Still couldn't find a port, let's use default
if(pport == 0) {
if(j == (props[i].length - 1)) {
pport = defaultPort("socket");
} else {
pport = defaultPort(proto);
}
}
// We did find a proxy definition.
// Let's create the address, but don't resolve it
// as this will be done at connection time
saddr = InetSocketAddress.createUnresolved(phost, pport);
/* Socks is *always* the last on the list */
if(j == (props[i].length - 1)) {
// 获取运行参数中指定的SOCKS代理版本;如果未指定,则使用SOCKS V5版本
int proxyVersion = socksProxyVersion();
// 工厂方法,创建Socket代理对象
SocksProxy socksProxy = SocksProxy.create(saddr, proxyVersion);
// 返回找到的socket代理信息
return new Proxy[]{socksProxy};
}
return new Proxy[]{new Proxy(Proxy.Type.HTTP, saddr)};
}
return null;
}
});
// If no specific proxy was found, return a standard list containing only one NO_PROXY entry.
if(proxyArray == null) {
return NO_PROXY_LIST;
}
// Remove duplicate entries, while preserving order.
return Stream.of(proxyArray).distinct().collect(collectingAndThen(toList(), Collections::unmodifiableList));
}
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
if(uri == null || sa == null || ioe == null) {
throw new IllegalArgumentException("Arguments can't be null.");
}
// ignored
}
/**
* @return {@code true} if given this pattern for non-proxy hosts and this
* urlhost the proxy should NOT be used to access this urlhost
*/
// 判断正则表达式pattern与目标字符串urlhost是否完全匹配
static boolean shouldNotUseProxyFor(Pattern pattern, String urlhost) {
if(pattern == null || urlhost.isEmpty()) {
return false;
}
return pattern.matcher(urlhost).matches();
}
/**
* @param mask non-null mask
*
* @return {@link java.util.regex.Pattern} corresponding to this mask or {@code null} in case mask should not match anything
*/
static Pattern toPattern(String mask) {
boolean disjunctionEmpty = true;
StringJoiner joiner = new StringJoiner("|");
for(String disjunct : mask.split("\\|")) {
if(disjunct.isEmpty()) {
continue;
}
disjunctionEmpty = false;
String regex = disjunctToRegex(disjunct.toLowerCase());
joiner.add(regex);
}
return disjunctionEmpty ? null : Pattern.compile(joiner.toString());
}
/**
* @param disjunct non-null mask disjunct
*
* @return java regex string corresponding to this mask
*/
static String disjunctToRegex(String disjunct) {
String regex;
if(disjunct.startsWith("*") && disjunct.endsWith("*")) {
regex = ".*" + quote(disjunct.substring(1, disjunct.length() - 1)) + ".*";
} else if(disjunct.startsWith("*")) {
regex = ".*" + quote(disjunct.substring(1));
} else if(disjunct.endsWith("*")) {
regex = quote(disjunct.substring(0, disjunct.length() - 1)) + ".*";
} else {
regex = quote(disjunct);
}
return regex;
}
// 返回指定协议对应的默认端口号;如果协议无法识别,返回-1
private int defaultPort(String protocol) {
if("http".equalsIgnoreCase(protocol)) {
return 80;
} else if("https".equalsIgnoreCase(protocol)) {
return 443;
} else if("ftp".equalsIgnoreCase(protocol)) {
return 80;
} else if("socket".equalsIgnoreCase(protocol)) {
return 1080;
} else if("gopher".equalsIgnoreCase(protocol)) {
return 80;
} else {
return -1;
}
}
private synchronized native Proxy[] getSystemProxies(String protocol, String host);
private static native boolean init();
/**
* How to deal with "non proxy hosts":
* since we do have to generate a pattern we don't want to do that if it's not necessary.
* Therefore we do cache the result, on a per-protocol basis,
* and change it only when the "source", i.e. the system property, did change.
*/
// 无代理设置
static class NonProxyInfo {
// Default value for nonProxyHosts, this provides backward compatibility by excluding localhost and its litteral notations.
static final String defStringVal = "localhost|127.*|[::1]|0.0.0.0|[::0]";
static NonProxyInfo ftpNonProxyInfo = new NonProxyInfo("ftp.nonProxyHosts", null, null, defStringVal);
static NonProxyInfo httpNonProxyInfo = new NonProxyInfo("http.nonProxyHosts", null, null, defStringVal);
static NonProxyInfo socksNonProxyInfo = new NonProxyInfo("socksNonProxyHosts", null, null, defStringVal);
final String property;
final String defaultVal;
String hostsSource;
Pattern pattern;
NonProxyInfo(String p, String s, Pattern pattern, String d) {
this.property = p;
this.hostsSource = s;
this.pattern = pattern;
this.defaultVal = d;
}
}
}