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