Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/java/src/org/openqa/selenium/Proxy.java
3991 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License. You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied. See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
package org.openqa.selenium;
19
20
import java.util.HashMap;
21
import java.util.List;
22
import java.util.Locale;
23
import java.util.Map;
24
import java.util.Objects;
25
import java.util.Optional;
26
import java.util.function.Consumer;
27
import java.util.stream.Collectors;
28
import org.jspecify.annotations.Nullable;
29
30
/**
31
* Configuration parameters for using proxies in WebDriver. Generally you should pass an object of
32
* this type to a WebDriver constructor, or in some cases to the profile object used in the
33
* WebDriver construction. For simplicity, setting values here commits the proxy to a certain
34
* configuration. That is, it is an error to set an <code>httpProxy</code> manually and then turn on
35
* proxy autodetect.
36
*/
37
public class Proxy {
38
39
public enum ProxyType {
40
// Keep these in sync with the Firefox preferences numbers:
41
// http://kb.mozillazine.org/Network.proxy.type
42
43
DIRECT("direct"), // Direct connection, no proxy (default on Windows)
44
MANUAL("manual"), // Manual proxy settings (e.g. for httpProxy)
45
PAC("pac"), // Proxy auto-configuration from URL
46
47
RESERVED_1("reserved_1"), // Never used (but reserved in Firefox)
48
49
AUTODETECT("autodetect"), // Proxy auto-detection (presumably with WPAD)
50
SYSTEM("system"), // Use system settings (default on Linux)
51
52
UNSPECIFIED("unspecified");
53
54
private final String type;
55
56
ProxyType(String type) {
57
this.type = type;
58
}
59
60
@Override
61
public String toString() {
62
return String.valueOf(type);
63
}
64
}
65
66
private static final String PROXY_TYPE = "proxyType";
67
@Deprecated private static final String FTP_PROXY = "ftpProxy";
68
private static final String HTTP_PROXY = "httpProxy";
69
private static final String NO_PROXY = "noProxy";
70
private static final String SSL_PROXY = "sslProxy";
71
private static final String SOCKS_PROXY = "socksProxy";
72
private static final String SOCKS_VERSION = "socksVersion";
73
private static final String SOCKS_USERNAME = "socksUsername";
74
private static final String SOCKS_PASSWORD = "socksPassword";
75
private static final String PROXY_AUTOCONFIG_URL = "proxyAutoconfigUrl";
76
private static final String AUTODETECT = "autodetect";
77
78
private ProxyType proxyType = ProxyType.UNSPECIFIED;
79
private boolean autodetect = false;
80
@Deprecated private @Nullable String ftpProxy;
81
private @Nullable String httpProxy;
82
private @Nullable String noProxy;
83
private @Nullable String sslProxy;
84
private @Nullable String socksProxy;
85
private @Nullable Integer socksVersion;
86
private @Nullable String socksUsername;
87
private @Nullable String socksPassword;
88
private @Nullable String proxyAutoconfigUrl;
89
90
public Proxy() {
91
// Empty default constructor
92
}
93
94
public Proxy(Map<String, ?> raw) {
95
Map<String, Consumer<Object>> setters = new HashMap<>();
96
setters.put(
97
PROXY_TYPE,
98
value -> setProxyType(ProxyType.valueOf(((String) value).toUpperCase(Locale.ENGLISH))));
99
setters.put(FTP_PROXY, value -> setFtpProxy((String) value));
100
setters.put(HTTP_PROXY, value -> setHttpProxy((String) value));
101
setters.put(
102
NO_PROXY,
103
rawData -> {
104
if (rawData instanceof List) {
105
// w3c
106
setNoProxy(
107
((List<?>) rawData)
108
.stream().map(Object::toString).collect(Collectors.joining(", ")));
109
} else {
110
// legacy
111
setNoProxy((String) rawData);
112
}
113
});
114
setters.put(SSL_PROXY, value -> setSslProxy((String) value));
115
setters.put(SOCKS_PROXY, value -> setSocksProxy((String) value));
116
setters.put(SOCKS_VERSION, value -> setSocksVersion(((Number) value).intValue()));
117
setters.put(SOCKS_USERNAME, value -> setSocksUsername((String) value));
118
setters.put(SOCKS_PASSWORD, value -> setSocksPassword((String) value));
119
setters.put(PROXY_AUTOCONFIG_URL, value -> setProxyAutoconfigUrl((String) value));
120
setters.put(AUTODETECT, value -> setAutodetect((Boolean) value));
121
raw.forEach(
122
(key, value) -> {
123
if (key != null && value != null && setters.containsKey(key)) {
124
setters.get(key).accept(value);
125
}
126
});
127
}
128
129
public Map<String, Object> toJson() {
130
Map<String, Object> m = new HashMap<>();
131
132
if (proxyType != ProxyType.UNSPECIFIED) {
133
m.put(PROXY_TYPE, proxyType.toString());
134
}
135
if (ftpProxy != null) {
136
m.put(FTP_PROXY, ftpProxy);
137
}
138
if (httpProxy != null) {
139
m.put(HTTP_PROXY, httpProxy);
140
}
141
if (noProxy != null) {
142
m.put(NO_PROXY, List.of(noProxy.split(",\\s*")));
143
}
144
if (sslProxy != null) {
145
m.put(SSL_PROXY, sslProxy);
146
}
147
if (socksProxy != null) {
148
m.put(SOCKS_PROXY, socksProxy);
149
}
150
if (socksVersion != null) {
151
m.put(SOCKS_VERSION, socksVersion);
152
}
153
if (socksUsername != null) {
154
m.put(SOCKS_USERNAME, socksUsername);
155
}
156
if (socksPassword != null) {
157
m.put(SOCKS_PASSWORD, socksPassword);
158
}
159
if (proxyAutoconfigUrl != null) {
160
m.put(PROXY_AUTOCONFIG_URL, proxyAutoconfigUrl);
161
}
162
if (autodetect) {
163
m.put(AUTODETECT, true);
164
}
165
return m;
166
}
167
168
/**
169
* Gets the {@link ProxyType}. This can signal if set to use a direct connection (without proxy),
170
* manually set proxy settings, auto-configured proxy settings, or whether to use the default
171
* system proxy settings. It defaults to {@link ProxyType#UNSPECIFIED}.
172
*
173
* @return the proxy type employed
174
*/
175
public ProxyType getProxyType() {
176
return this.proxyType;
177
}
178
179
/**
180
* Explicitly sets the proxy type, useful for forcing direct connection on Linux.
181
*
182
* @param proxyType type of proxy being used
183
* @return reference to self
184
*/
185
public Proxy setProxyType(ProxyType proxyType) {
186
verifyProxyTypeCompatibility(proxyType);
187
this.proxyType = proxyType;
188
return this;
189
}
190
191
/**
192
* Whether to autodetect proxy settings.
193
*
194
* @return true if set to autodetect proxy settings, false otherwise
195
*/
196
public boolean isAutodetect() {
197
return autodetect;
198
}
199
200
/**
201
* Specifies whether to autodetect proxy settings.
202
*
203
* @param autodetect set to true to use proxy auto detection, false to leave proxy settings
204
* unspecified
205
* @return reference to self
206
*/
207
public Proxy setAutodetect(boolean autodetect) {
208
if (this.autodetect == autodetect) {
209
return this;
210
}
211
if (autodetect) {
212
verifyProxyTypeCompatibility(ProxyType.AUTODETECT);
213
this.proxyType = ProxyType.AUTODETECT;
214
} else {
215
this.proxyType = ProxyType.UNSPECIFIED;
216
}
217
this.autodetect = autodetect;
218
return this;
219
}
220
221
/**
222
* Gets the FTP proxy.
223
*
224
* @return the FTP proxy hostname if present, or null if not set
225
* @deprecated getFtpProxy is deprecated and will be removed in a future release.
226
*/
227
@Deprecated
228
public @Nullable String getFtpProxy() {
229
return ftpProxy;
230
}
231
232
/**
233
* Specify which proxy to use for FTP connections.
234
*
235
* @param ftpProxy the proxy host, expected format is <code>hostname.com:1234</code>
236
* @return reference to self
237
* @deprecated setFtpProxy is deprecated and will be removed in a future release.
238
*/
239
@Deprecated
240
public Proxy setFtpProxy(String ftpProxy) {
241
verifyProxyTypeCompatibility(ProxyType.MANUAL);
242
this.proxyType = ProxyType.MANUAL;
243
this.ftpProxy = ftpProxy;
244
return this;
245
}
246
247
/**
248
* Gets the HTTP proxy.
249
*
250
* @return the HTTP proxy hostname if present, or null if not set
251
*/
252
public @Nullable String getHttpProxy() {
253
return httpProxy;
254
}
255
256
/**
257
* Specify which proxy to use for HTTP connections.
258
*
259
* @param httpProxy the proxy host, expected format is <code>hostname:1234</code>
260
* @return reference to self
261
*/
262
public Proxy setHttpProxy(String httpProxy) {
263
verifyProxyTypeCompatibility(ProxyType.MANUAL);
264
this.proxyType = ProxyType.MANUAL;
265
this.httpProxy = httpProxy;
266
return this;
267
}
268
269
/**
270
* Gets proxy bypass (noproxy) addresses.
271
*
272
* @return The proxy bypass (noproxy) addresses
273
*/
274
public @Nullable String getNoProxy() {
275
return noProxy;
276
}
277
278
/**
279
* Sets proxy bypass (noproxy) addresses
280
*
281
* @param noProxy The proxy bypass (noproxy) addresses separated by commas
282
* @return reference to self
283
*/
284
public Proxy setNoProxy(String noProxy) {
285
verifyProxyTypeCompatibility(ProxyType.MANUAL);
286
this.proxyType = ProxyType.MANUAL;
287
this.noProxy = noProxy;
288
return this;
289
}
290
291
/**
292
* Gets the SSL tunnel proxy.
293
*
294
* @return the SSL tunnel proxy hostname if present, null otherwise
295
*/
296
public @Nullable String getSslProxy() {
297
return sslProxy;
298
}
299
300
/**
301
* Specify which proxy to use for SSL connections.
302
*
303
* @param sslProxy the proxy host, expected format is <code>hostname.com:1234</code>
304
* @return reference to self
305
*/
306
public Proxy setSslProxy(String sslProxy) {
307
verifyProxyTypeCompatibility(ProxyType.MANUAL);
308
this.proxyType = ProxyType.MANUAL;
309
this.sslProxy = sslProxy;
310
return this;
311
}
312
313
/**
314
* Gets the SOCKS proxy.
315
*
316
* @return the SOCKS proxy if present, null otherwise
317
*/
318
public @Nullable String getSocksProxy() {
319
return socksProxy;
320
}
321
322
/**
323
* Specifies which proxy to use for SOCKS.
324
*
325
* @param socksProxy the proxy host, expected format is <code>hostname.com:1234</code>
326
* @return reference to self
327
*/
328
public Proxy setSocksProxy(String socksProxy) {
329
verifyProxyTypeCompatibility(ProxyType.MANUAL);
330
this.proxyType = ProxyType.MANUAL;
331
this.socksProxy = socksProxy;
332
return this;
333
}
334
335
/**
336
* Gets the SOCKS version (4 or 5).
337
*
338
* @return the SOCKS version if present, null otherwise
339
*/
340
public @Nullable Integer getSocksVersion() {
341
return socksVersion;
342
}
343
344
/**
345
* Specifies which version of SOCKS to use (4 or 5).
346
*
347
* @param socksVersion SOCKS version, 4 or 5
348
* @return reference to self
349
*/
350
public Proxy setSocksVersion(Integer socksVersion) {
351
verifyProxyTypeCompatibility(ProxyType.MANUAL);
352
this.proxyType = ProxyType.MANUAL;
353
this.socksVersion = socksVersion;
354
return this;
355
}
356
357
/**
358
* Gets the SOCKS proxy's username. Supported by SOCKS v5 and above.
359
*
360
* @return the SOCKS proxy's username
361
*/
362
public @Nullable String getSocksUsername() {
363
return socksUsername;
364
}
365
366
/**
367
* Specifies a username for the SOCKS proxy. Supported by SOCKS v5 and above.
368
*
369
* @param username username for the SOCKS proxy
370
* @return reference to self
371
*/
372
public Proxy setSocksUsername(String username) {
373
verifyProxyTypeCompatibility(ProxyType.MANUAL);
374
this.proxyType = ProxyType.MANUAL;
375
this.socksUsername = username;
376
return this;
377
}
378
379
/**
380
* Gets the SOCKS proxy's password. Supported by SOCKS v5 and above.
381
*
382
* @return the SOCKS proxy's password
383
*/
384
public @Nullable String getSocksPassword() {
385
return socksPassword;
386
}
387
388
/**
389
* Specifies a password for the SOCKS proxy. Supported by SOCKS v5 and above.
390
*
391
* @param password password for the SOCKS proxy
392
* @return reference to self
393
*/
394
public Proxy setSocksPassword(String password) {
395
verifyProxyTypeCompatibility(ProxyType.MANUAL);
396
this.proxyType = ProxyType.MANUAL;
397
this.socksPassword = password;
398
return this;
399
}
400
401
/**
402
* Gets the proxy auto-configuration URL.
403
*
404
* @return the proxy auto-configuration URL
405
*/
406
public @Nullable String getProxyAutoconfigUrl() {
407
return proxyAutoconfigUrl;
408
}
409
410
/**
411
* Specifies the URL to be used for proxy auto-configuration. Expected format is <code>
412
* http://hostname.com:1234/pacfile</code>. This is required if {@link #getProxyType()} is set to
413
* {@link ProxyType#PAC}, ignored otherwise.
414
*
415
* @param proxyAutoconfigUrl the URL for proxy auto-configuration
416
* @return reference to self
417
*/
418
public Proxy setProxyAutoconfigUrl(String proxyAutoconfigUrl) {
419
verifyProxyTypeCompatibility(ProxyType.PAC);
420
this.proxyType = ProxyType.PAC;
421
this.proxyAutoconfigUrl = proxyAutoconfigUrl;
422
return this;
423
}
424
425
private void verifyProxyTypeCompatibility(ProxyType compatibleProxy) {
426
if (proxyType != ProxyType.UNSPECIFIED && proxyType != compatibleProxy) {
427
throw new IllegalStateException(
428
String.format(
429
"Specified proxy type (%s) not compatible with current setting (%s)",
430
compatibleProxy, proxyType));
431
}
432
}
433
434
@SuppressWarnings({"unchecked"})
435
public static @Nullable Proxy extractFrom(Capabilities capabilities) {
436
Object rawProxy = capabilities.getCapability("proxy");
437
Proxy proxy = null;
438
if (rawProxy != null) {
439
if (rawProxy instanceof Proxy) {
440
proxy = (Proxy) rawProxy;
441
} else if (rawProxy instanceof Map) {
442
proxy = new Proxy((Map<String, ?>) rawProxy);
443
}
444
}
445
return proxy;
446
}
447
448
@Override
449
public String toString() {
450
StringBuilder builder = new StringBuilder("Proxy(");
451
452
switch (getProxyType()) {
453
case AUTODETECT:
454
case DIRECT:
455
case MANUAL:
456
case SYSTEM:
457
builder.append(getProxyType().toString().toLowerCase(Locale.ENGLISH));
458
break;
459
460
case PAC:
461
builder.append("pac: ").append(getProxyAutoconfigUrl());
462
break;
463
464
case RESERVED_1:
465
case UNSPECIFIED:
466
break;
467
}
468
469
Optional.ofNullable(getFtpProxy()).ifPresent(p -> builder.append(", ftp=").append(p));
470
Optional.ofNullable(getHttpProxy()).ifPresent(p -> builder.append(", http=").append(p));
471
Optional.ofNullable(getSocksProxy()).ifPresent(p -> builder.append(", socks=").append(p));
472
Optional.ofNullable(getSslProxy()).ifPresent(p -> builder.append(", ssl=").append(p));
473
474
builder.append(")");
475
return builder.toString();
476
}
477
478
@Override
479
public boolean equals(@Nullable Object o) {
480
if (this == o) {
481
return true;
482
}
483
if (o == null || getClass() != o.getClass()) {
484
return false;
485
}
486
Proxy proxy = (Proxy) o;
487
return isAutodetect() == proxy.isAutodetect()
488
&& getProxyType() == proxy.getProxyType()
489
&& Objects.equals(getFtpProxy(), proxy.getFtpProxy())
490
&& Objects.equals(getHttpProxy(), proxy.getHttpProxy())
491
&& Objects.equals(getNoProxy(), proxy.getNoProxy())
492
&& Objects.equals(getSslProxy(), proxy.getSslProxy())
493
&& Objects.equals(getSocksProxy(), proxy.getSocksProxy())
494
&& Objects.equals(getSocksVersion(), proxy.getSocksVersion())
495
&& Objects.equals(getSocksUsername(), proxy.getSocksUsername())
496
&& Objects.equals(getSocksPassword(), proxy.getSocksPassword())
497
&& Objects.equals(getProxyAutoconfigUrl(), proxy.getProxyAutoconfigUrl());
498
}
499
500
@Override
501
public int hashCode() {
502
return Objects.hash(
503
getProxyType(),
504
isAutodetect(),
505
getFtpProxy(),
506
getHttpProxy(),
507
getNoProxy(),
508
getSslProxy(),
509
getSocksProxy(),
510
getSocksVersion(),
511
getSocksUsername(),
512
getSocksPassword(),
513
getProxyAutoconfigUrl());
514
}
515
}
516
517