|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 4 | *
|
5 | 5 | * This code is free software; you can redistribute it and/or modify it
|
|
33 | 33 | import java.util.Collections;
|
34 | 34 | import java.util.List;
|
35 | 35 | import java.io.IOException;
|
36 |
| -import java.security.AccessController; |
37 |
| -import java.security.PrivilegedAction; |
38 | 36 | import java.util.Locale;
|
39 | 37 | import java.util.StringJoiner;
|
40 | 38 | import java.util.regex.Pattern;
|
@@ -93,26 +91,15 @@ public class DefaultProxySelector extends ProxySelector {
|
93 | 91 |
|
94 | 92 | static {
|
95 | 93 | final String key = "java.net.useSystemProxies";
|
96 |
| - @SuppressWarnings("removal") |
97 |
| - Boolean b = AccessController.doPrivileged( |
98 |
| - new PrivilegedAction<Boolean>() { |
99 |
| - public Boolean run() { |
100 |
| - return NetProperties.getBoolean(key); |
101 |
| - }}); |
| 94 | + Boolean b = NetProperties.getBoolean(key); |
102 | 95 | if (b != null && b.booleanValue()) {
|
103 | 96 | jdk.internal.loader.BootLoader.loadLibrary("net");
|
104 | 97 | hasSystemProxies = init();
|
105 | 98 | }
|
106 | 99 | }
|
107 | 100 |
|
108 |
| - @SuppressWarnings("removal") |
109 | 101 | public static int socksProxyVersion() {
|
110 |
| - return AccessController.doPrivileged( |
111 |
| - new PrivilegedAction<Integer>() { |
112 |
| - @Override public Integer run() { |
113 |
| - return NetProperties.getInteger(SOCKS_PROXY_VERSION, 5); |
114 |
| - } |
115 |
| - }); |
| 102 | + return NetProperties.getInteger(SOCKS_PROXY_VERSION, 5); |
116 | 103 | }
|
117 | 104 |
|
118 | 105 | /**
|
@@ -187,148 +174,133 @@ public java.util.List<Proxy> select(URI uri) {
|
187 | 174 | throw new IllegalArgumentException("protocol = "+protocol+" host = "+host);
|
188 | 175 | }
|
189 | 176 |
|
190 |
| - NonProxyInfo pinfo = null; |
| 177 | + NonProxyInfo nonProxyInfo = null; |
191 | 178 |
|
192 | 179 | if ("http".equalsIgnoreCase(protocol)) {
|
193 |
| - pinfo = NonProxyInfo.httpNonProxyInfo; |
| 180 | + nonProxyInfo = NonProxyInfo.httpNonProxyInfo; |
194 | 181 | } else if ("https".equalsIgnoreCase(protocol)) {
|
195 | 182 | // HTTPS uses the same property as HTTP, for backward
|
196 | 183 | // compatibility
|
197 |
| - pinfo = NonProxyInfo.httpNonProxyInfo; |
| 184 | + nonProxyInfo = NonProxyInfo.httpNonProxyInfo; |
198 | 185 | } else if ("ftp".equalsIgnoreCase(protocol)) {
|
199 |
| - pinfo = NonProxyInfo.ftpNonProxyInfo; |
| 186 | + nonProxyInfo = NonProxyInfo.ftpNonProxyInfo; |
200 | 187 | } else if ("socket".equalsIgnoreCase(protocol)) {
|
201 |
| - pinfo = NonProxyInfo.socksNonProxyInfo; |
| 188 | + nonProxyInfo = NonProxyInfo.socksNonProxyInfo; |
202 | 189 | }
|
203 |
| - |
204 |
| - /** |
205 |
| - * Let's check the System properties for that protocol |
206 |
| - */ |
207 |
| - final String proto = protocol; |
208 |
| - final NonProxyInfo nprop = pinfo; |
209 | 190 | final String urlhost = host.toLowerCase(Locale.ROOT);
|
210 |
| - |
211 |
| - /** |
212 |
| - * This is one big doPrivileged call, but we're trying to optimize |
213 |
| - * the code as much as possible. Since we're checking quite a few |
214 |
| - * System properties it does help having only 1 call to doPrivileged. |
215 |
| - * Be mindful what you do in here though! |
216 |
| - */ |
217 |
| - @SuppressWarnings("removal") |
218 |
| - Proxy[] proxyArray = AccessController.doPrivileged( |
219 |
| - new PrivilegedAction<Proxy[]>() { |
220 |
| - public Proxy[] run() { |
221 |
| - int i, j; |
222 |
| - String phost = null; |
223 |
| - int pport = 0; |
224 |
| - String nphosts = null; |
225 |
| - InetSocketAddress saddr = null; |
226 |
| - |
227 |
| - // Then let's walk the list of protocols in our array |
228 |
| - for (i=0; i<props.length; i++) { |
229 |
| - if (props[i][0].equalsIgnoreCase(proto)) { |
230 |
| - for (j = 1; j < props[i].length; j++) { |
231 |
| - /* System.getProp() will give us an empty |
232 |
| - * String, "" for a defined but "empty" |
233 |
| - * property. |
234 |
| - */ |
235 |
| - phost = NetProperties.get(props[i][j]+"Host"); |
236 |
| - if (phost != null && phost.length() != 0) |
237 |
| - break; |
238 |
| - } |
239 |
| - if (phost == null || phost.isEmpty()) { |
240 |
| - /** |
241 |
| - * No system property defined for that |
242 |
| - * protocol. Let's check System Proxy |
243 |
| - * settings (Gnome, MacOsX & Windows) if |
244 |
| - * we were instructed to. |
245 |
| - */ |
246 |
| - if (hasSystemProxies) { |
247 |
| - String sproto; |
248 |
| - if (proto.equalsIgnoreCase("socket")) |
249 |
| - sproto = "socks"; |
250 |
| - else |
251 |
| - sproto = proto; |
252 |
| - return getSystemProxies(sproto, urlhost); |
253 |
| - } |
254 |
| - return null; |
255 |
| - } |
256 |
| - // If a Proxy Host is defined for that protocol |
257 |
| - // Let's get the NonProxyHosts property |
258 |
| - if (nprop != null) { |
259 |
| - nphosts = NetProperties.get(nprop.property); |
260 |
| - synchronized (nprop) { |
261 |
| - if (nphosts == null) { |
262 |
| - if (nprop.defaultVal != null) { |
263 |
| - nphosts = nprop.defaultVal; |
264 |
| - } else { |
265 |
| - nprop.hostsSource = null; |
266 |
| - nprop.pattern = null; |
267 |
| - } |
268 |
| - } else if (!nphosts.isEmpty()) { |
269 |
| - // add the required default patterns |
270 |
| - // but only if property no set. If it |
271 |
| - // is empty, leave empty. |
272 |
| - nphosts += "|" + NonProxyInfo |
273 |
| - .defStringVal; |
274 |
| - } |
275 |
| - if (nphosts != null) { |
276 |
| - if (!nphosts.equals(nprop.hostsSource)) { |
277 |
| - nprop.pattern = toPattern(nphosts); |
278 |
| - nprop.hostsSource = nphosts; |
279 |
| - } |
280 |
| - } |
281 |
| - if (shouldNotUseProxyFor(nprop.pattern, urlhost)) { |
282 |
| - return null; |
283 |
| - } |
284 |
| - } |
285 |
| - } |
286 |
| - // We got a host, let's check for port |
287 |
| - |
288 |
| - pport = NetProperties.getInteger(props[i][j]+"Port", 0).intValue(); |
289 |
| - if (pport == 0 && j < (props[i].length - 1)) { |
290 |
| - // Can't find a port with same prefix as Host |
291 |
| - // AND it's not a SOCKS proxy |
292 |
| - // Let's try the other prefixes for that proto |
293 |
| - for (int k = 1; k < (props[i].length - 1); k++) { |
294 |
| - if ((k != j) && (pport == 0)) |
295 |
| - pport = NetProperties.getInteger(props[i][k]+"Port", 0).intValue(); |
296 |
| - } |
297 |
| - } |
298 |
| - |
299 |
| - // Still couldn't find a port, let's use default |
300 |
| - if (pport == 0) { |
301 |
| - if (j == (props[i].length - 1)) // SOCKS |
302 |
| - pport = defaultPort("socket"); |
303 |
| - else |
304 |
| - pport = defaultPort(proto); |
305 |
| - } |
306 |
| - // We did find a proxy definition. |
307 |
| - // Let's create the address, but don't resolve it |
308 |
| - // as this will be done at connection time |
309 |
| - saddr = InetSocketAddress.createUnresolved(phost, pport); |
310 |
| - // Socks is *always* the last on the list. |
311 |
| - if (j == (props[i].length - 1)) { |
312 |
| - return new Proxy[] {SocksProxy.create(saddr, socksProxyVersion())}; |
313 |
| - } |
314 |
| - return new Proxy[] {new Proxy(Proxy.Type.HTTP, saddr)}; |
315 |
| - } |
316 |
| - } |
317 |
| - return null; |
318 |
| - }}); |
319 |
| - |
320 |
| - |
| 191 | + // determine the proxies |
| 192 | + final Proxy[] proxyArray = determineProxies(urlhost, protocol, nonProxyInfo); |
321 | 193 | if (proxyArray != null) {
|
322 | 194 | // Remove duplicate entries, while preserving order.
|
323 | 195 | return Stream.of(proxyArray).distinct().collect(
|
324 | 196 | collectingAndThen(toList(), Collections::unmodifiableList));
|
325 | 197 | }
|
326 |
| - |
327 | 198 | // If no specific proxy was found, return a standard list containing
|
328 | 199 | // only one NO_PROXY entry.
|
329 | 200 | return NO_PROXY_LIST;
|
330 | 201 | }
|
331 | 202 |
|
| 203 | + private Proxy[] determineProxies(final String urlhost, final String protocol, |
| 204 | + final NonProxyInfo nonProxyInfo) { |
| 205 | + int i, j; |
| 206 | + String phost = null; |
| 207 | + int pport = 0; |
| 208 | + String nphosts = null; |
| 209 | + InetSocketAddress saddr = null; |
| 210 | + |
| 211 | + // Then let's walk the list of protocols in our array |
| 212 | + for (i = 0; i < props.length; i++) { |
| 213 | + if (props[i][0].equalsIgnoreCase(protocol)) { |
| 214 | + for (j = 1; j < props[i].length; j++) { |
| 215 | + /* System.getProp() will give us an empty |
| 216 | + * String, "" for a defined but "empty" |
| 217 | + * property. |
| 218 | + */ |
| 219 | + phost = NetProperties.get(props[i][j] + "Host"); |
| 220 | + if (phost != null && phost.length() != 0) |
| 221 | + break; |
| 222 | + } |
| 223 | + if (phost == null || phost.isEmpty()) { |
| 224 | + /** |
| 225 | + * No system property defined for that |
| 226 | + * protocol. Let's check System Proxy |
| 227 | + * settings (Gnome, MacOsX & Windows) if |
| 228 | + * we were instructed to. |
| 229 | + */ |
| 230 | + if (hasSystemProxies) { |
| 231 | + String sproto; |
| 232 | + if (protocol.equalsIgnoreCase("socket")) |
| 233 | + sproto = "socks"; |
| 234 | + else |
| 235 | + sproto = protocol; |
| 236 | + return getSystemProxies(sproto, urlhost); |
| 237 | + } |
| 238 | + return null; |
| 239 | + } |
| 240 | + // If a Proxy Host is defined for that protocol |
| 241 | + // Let's get the NonProxyHosts property |
| 242 | + if (nonProxyInfo != null) { |
| 243 | + nphosts = NetProperties.get(nonProxyInfo.property); |
| 244 | + synchronized (nonProxyInfo) { |
| 245 | + if (nphosts == null) { |
| 246 | + if (nonProxyInfo.defaultVal != null) { |
| 247 | + nphosts = nonProxyInfo.defaultVal; |
| 248 | + } else { |
| 249 | + nonProxyInfo.hostsSource = null; |
| 250 | + nonProxyInfo.pattern = null; |
| 251 | + } |
| 252 | + } else if (!nphosts.isEmpty()) { |
| 253 | + // add the required default patterns |
| 254 | + // but only if property no set. If it |
| 255 | + // is empty, leave empty. |
| 256 | + nphosts += "|" + NonProxyInfo |
| 257 | + .defStringVal; |
| 258 | + } |
| 259 | + if (nphosts != null) { |
| 260 | + if (!nphosts.equals(nonProxyInfo.hostsSource)) { |
| 261 | + nonProxyInfo.pattern = toPattern(nphosts); |
| 262 | + nonProxyInfo.hostsSource = nphosts; |
| 263 | + } |
| 264 | + } |
| 265 | + if (shouldNotUseProxyFor(nonProxyInfo.pattern, urlhost)) { |
| 266 | + return null; |
| 267 | + } |
| 268 | + } |
| 269 | + } |
| 270 | + // We got a host, let's check for port |
| 271 | + |
| 272 | + pport = NetProperties.getInteger(props[i][j] + "Port", 0).intValue(); |
| 273 | + if (pport == 0 && j < (props[i].length - 1)) { |
| 274 | + // Can't find a port with same prefix as Host |
| 275 | + // AND it's not a SOCKS proxy |
| 276 | + // Let's try the other prefixes for that proto |
| 277 | + for (int k = 1; k < (props[i].length - 1); k++) { |
| 278 | + if ((k != j) && (pport == 0)) |
| 279 | + pport = NetProperties.getInteger(props[i][k] + "Port", 0).intValue(); |
| 280 | + } |
| 281 | + } |
| 282 | + |
| 283 | + // Still couldn't find a port, let's use default |
| 284 | + if (pport == 0) { |
| 285 | + if (j == (props[i].length - 1)) // SOCKS |
| 286 | + pport = defaultPort("socket"); |
| 287 | + else |
| 288 | + pport = defaultPort(protocol); |
| 289 | + } |
| 290 | + // We did find a proxy definition. |
| 291 | + // Let's create the address, but don't resolve it |
| 292 | + // as this will be done at connection time |
| 293 | + saddr = InetSocketAddress.createUnresolved(phost, pport); |
| 294 | + // Socks is *always* the last on the list. |
| 295 | + if (j == (props[i].length - 1)) { |
| 296 | + return new Proxy[]{SocksProxy.create(saddr, socksProxyVersion())}; |
| 297 | + } |
| 298 | + return new Proxy[]{new Proxy(Proxy.Type.HTTP, saddr)}; |
| 299 | + } |
| 300 | + } |
| 301 | + return null; |
| 302 | + } |
| 303 | + |
332 | 304 | public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
|
333 | 305 | if (uri == null || sa == null || ioe == null) {
|
334 | 306 | throw new IllegalArgumentException("Arguments can't be null.");
|
|
0 commit comments