Path: blob/trunk/java/src/org/openqa/selenium/Proxy.java
1865 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617package org.openqa.selenium;1819import java.util.Arrays;20import java.util.HashMap;21import java.util.List;22import java.util.Locale;23import java.util.Map;24import java.util.Objects;25import java.util.Optional;26import java.util.function.Consumer;27import java.util.stream.Collectors;28import org.jspecify.annotations.NullMarked;29import org.jspecify.annotations.Nullable;3031/**32* Configuration parameters for using proxies in WebDriver. Generally you should pass an object of33* this type to a WebDriver constructor, or in some cases to the profile object used in the34* WebDriver construction. For simplicity, setting values here commits the proxy to a certain35* configuration. That is, it is an error to set an <code>httpProxy</code> manually and then turn on36* proxy autodetect.37*/38@NullMarked39public class Proxy {4041public enum ProxyType {42// Keep these in sync with the Firefox preferences numbers:43// http://kb.mozillazine.org/Network.proxy.type4445DIRECT("direct"), // Direct connection, no proxy (default on Windows)46MANUAL("manual"), // Manual proxy settings (e.g. for httpProxy)47PAC("pac"), // Proxy auto-configuration from URL4849RESERVED_1("reserved_1"), // Never used (but reserved in Firefox)5051AUTODETECT("autodetect"), // Proxy auto-detection (presumably with WPAD)52SYSTEM("system"), // Use system settings (default on Linux)5354UNSPECIFIED("unspecified");5556private final String type;5758ProxyType(String type) {59this.type = type;60}6162@Override63public String toString() {64return String.valueOf(type);65}66}6768private static final String PROXY_TYPE = "proxyType";69@Deprecated private static final String FTP_PROXY = "ftpProxy";70private static final String HTTP_PROXY = "httpProxy";71private static final String NO_PROXY = "noProxy";72private static final String SSL_PROXY = "sslProxy";73private static final String SOCKS_PROXY = "socksProxy";74private static final String SOCKS_VERSION = "socksVersion";75private static final String SOCKS_USERNAME = "socksUsername";76private static final String SOCKS_PASSWORD = "socksPassword";77private static final String PROXY_AUTOCONFIG_URL = "proxyAutoconfigUrl";78private static final String AUTODETECT = "autodetect";7980private ProxyType proxyType = ProxyType.UNSPECIFIED;81private boolean autodetect = false;82@Deprecated private @Nullable String ftpProxy;83private @Nullable String httpProxy;84private @Nullable String noProxy;85private @Nullable String sslProxy;86private @Nullable String socksProxy;87private @Nullable Integer socksVersion;88private @Nullable String socksUsername;89private @Nullable String socksPassword;90private @Nullable String proxyAutoconfigUrl;9192public Proxy() {93// Empty default constructor94}9596public Proxy(Map<String, ?> raw) {97Map<String, Consumer<Object>> setters = new HashMap<>();98setters.put(99PROXY_TYPE,100value -> setProxyType(ProxyType.valueOf(((String) value).toUpperCase(Locale.ENGLISH))));101setters.put(FTP_PROXY, value -> setFtpProxy((String) value));102setters.put(HTTP_PROXY, value -> setHttpProxy((String) value));103setters.put(104NO_PROXY,105rawData -> {106if (rawData instanceof List) {107// w3c108setNoProxy(109((List<?>) rawData)110.stream().map(Object::toString).collect(Collectors.joining(", ")));111} else {112// legacy113setNoProxy((String) rawData);114}115});116setters.put(SSL_PROXY, value -> setSslProxy((String) value));117setters.put(SOCKS_PROXY, value -> setSocksProxy((String) value));118setters.put(SOCKS_VERSION, value -> setSocksVersion(((Number) value).intValue()));119setters.put(SOCKS_USERNAME, value -> setSocksUsername((String) value));120setters.put(SOCKS_PASSWORD, value -> setSocksPassword((String) value));121setters.put(PROXY_AUTOCONFIG_URL, value -> setProxyAutoconfigUrl((String) value));122setters.put(AUTODETECT, value -> setAutodetect((Boolean) value));123raw.forEach(124(key, value) -> {125if (key != null && value != null && setters.containsKey(key)) {126setters.get(key).accept(value);127}128});129}130131public Map<String, Object> toJson() {132Map<String, Object> m = new HashMap<>();133134if (proxyType != ProxyType.UNSPECIFIED) {135m.put(PROXY_TYPE, proxyType.toString());136}137if (ftpProxy != null) {138m.put(FTP_PROXY, ftpProxy);139}140if (httpProxy != null) {141m.put(HTTP_PROXY, httpProxy);142}143if (noProxy != null) {144m.put(NO_PROXY, Arrays.asList(noProxy.split(",\\s*")));145}146if (sslProxy != null) {147m.put(SSL_PROXY, sslProxy);148}149if (socksProxy != null) {150m.put(SOCKS_PROXY, socksProxy);151}152if (socksVersion != null) {153m.put(SOCKS_VERSION, socksVersion);154}155if (socksUsername != null) {156m.put(SOCKS_USERNAME, socksUsername);157}158if (socksPassword != null) {159m.put(SOCKS_PASSWORD, socksPassword);160}161if (proxyAutoconfigUrl != null) {162m.put(PROXY_AUTOCONFIG_URL, proxyAutoconfigUrl);163}164if (autodetect) {165m.put(AUTODETECT, true);166}167return m;168}169170/**171* Gets the {@link ProxyType}. This can signal if set to use a direct connection (without proxy),172* manually set proxy settings, auto-configured proxy settings, or whether to use the default173* system proxy settings. It defaults to {@link ProxyType#UNSPECIFIED}.174*175* @return the proxy type employed176*/177public ProxyType getProxyType() {178return this.proxyType;179}180181/**182* Explicitly sets the proxy type, useful for forcing direct connection on Linux.183*184* @param proxyType type of proxy being used185* @return reference to self186*/187public Proxy setProxyType(ProxyType proxyType) {188verifyProxyTypeCompatibility(proxyType);189this.proxyType = proxyType;190return this;191}192193/**194* Whether to autodetect proxy settings.195*196* @return true if set to autodetect proxy settings, false otherwise197*/198public boolean isAutodetect() {199return autodetect;200}201202/**203* Specifies whether to autodetect proxy settings.204*205* @param autodetect set to true to use proxy auto detection, false to leave proxy settings206* unspecified207* @return reference to self208*/209public Proxy setAutodetect(boolean autodetect) {210if (this.autodetect == autodetect) {211return this;212}213if (autodetect) {214verifyProxyTypeCompatibility(ProxyType.AUTODETECT);215this.proxyType = ProxyType.AUTODETECT;216} else {217this.proxyType = ProxyType.UNSPECIFIED;218}219this.autodetect = autodetect;220return this;221}222223/**224* Gets the FTP proxy.225*226* @return the FTP proxy hostname if present, or null if not set227* @deprecated getFtpProxy is deprecated and will be removed in a future release.228*/229@Deprecated230public @Nullable String getFtpProxy() {231return ftpProxy;232}233234/**235* Specify which proxy to use for FTP connections.236*237* @param ftpProxy the proxy host, expected format is <code>hostname.com:1234</code>238* @return reference to self239* @deprecated setFtpProxy is deprecated and will be removed in a future release.240*/241@Deprecated242public Proxy setFtpProxy(String ftpProxy) {243verifyProxyTypeCompatibility(ProxyType.MANUAL);244this.proxyType = ProxyType.MANUAL;245this.ftpProxy = ftpProxy;246return this;247}248249/**250* Gets the HTTP proxy.251*252* @return the HTTP proxy hostname if present, or null if not set253*/254public @Nullable String getHttpProxy() {255return httpProxy;256}257258/**259* Specify which proxy to use for HTTP connections.260*261* @param httpProxy the proxy host, expected format is <code>hostname:1234</code>262* @return reference to self263*/264public Proxy setHttpProxy(String httpProxy) {265verifyProxyTypeCompatibility(ProxyType.MANUAL);266this.proxyType = ProxyType.MANUAL;267this.httpProxy = httpProxy;268return this;269}270271/**272* Gets proxy bypass (noproxy) addresses.273*274* @return The proxy bypass (noproxy) addresses275*/276public @Nullable String getNoProxy() {277return noProxy;278}279280/**281* Sets proxy bypass (noproxy) addresses282*283* @param noProxy The proxy bypass (noproxy) addresses separated by commas284* @return reference to self285*/286public Proxy setNoProxy(String noProxy) {287verifyProxyTypeCompatibility(ProxyType.MANUAL);288this.proxyType = ProxyType.MANUAL;289this.noProxy = noProxy;290return this;291}292293/**294* Gets the SSL tunnel proxy.295*296* @return the SSL tunnel proxy hostname if present, null otherwise297*/298public @Nullable String getSslProxy() {299return sslProxy;300}301302/**303* Specify which proxy to use for SSL connections.304*305* @param sslProxy the proxy host, expected format is <code>hostname.com:1234</code>306* @return reference to self307*/308public Proxy setSslProxy(String sslProxy) {309verifyProxyTypeCompatibility(ProxyType.MANUAL);310this.proxyType = ProxyType.MANUAL;311this.sslProxy = sslProxy;312return this;313}314315/**316* Gets the SOCKS proxy.317*318* @return the SOCKS proxy if present, null otherwise319*/320public @Nullable String getSocksProxy() {321return socksProxy;322}323324/**325* Specifies which proxy to use for SOCKS.326*327* @param socksProxy the proxy host, expected format is <code>hostname.com:1234</code>328* @return reference to self329*/330public Proxy setSocksProxy(String socksProxy) {331verifyProxyTypeCompatibility(ProxyType.MANUAL);332this.proxyType = ProxyType.MANUAL;333this.socksProxy = socksProxy;334return this;335}336337/**338* Gets the SOCKS version (4 or 5).339*340* @return the SOCKS version if present, null otherwise341*/342public @Nullable Integer getSocksVersion() {343return socksVersion;344}345346/**347* Specifies which version of SOCKS to use (4 or 5).348*349* @param socksVersion SOCKS version, 4 or 5350* @return reference to self351*/352public Proxy setSocksVersion(Integer socksVersion) {353verifyProxyTypeCompatibility(ProxyType.MANUAL);354this.proxyType = ProxyType.MANUAL;355this.socksVersion = socksVersion;356return this;357}358359/**360* Gets the SOCKS proxy's username. Supported by SOCKS v5 and above.361*362* @return the SOCKS proxy's username363*/364public @Nullable String getSocksUsername() {365return socksUsername;366}367368/**369* Specifies a username for the SOCKS proxy. Supported by SOCKS v5 and above.370*371* @param username username for the SOCKS proxy372* @return reference to self373*/374public Proxy setSocksUsername(String username) {375verifyProxyTypeCompatibility(ProxyType.MANUAL);376this.proxyType = ProxyType.MANUAL;377this.socksUsername = username;378return this;379}380381/**382* Gets the SOCKS proxy's password. Supported by SOCKS v5 and above.383*384* @return the SOCKS proxy's password385*/386public @Nullable String getSocksPassword() {387return socksPassword;388}389390/**391* Specifies a password for the SOCKS proxy. Supported by SOCKS v5 and above.392*393* @param password password for the SOCKS proxy394* @return reference to self395*/396public Proxy setSocksPassword(String password) {397verifyProxyTypeCompatibility(ProxyType.MANUAL);398this.proxyType = ProxyType.MANUAL;399this.socksPassword = password;400return this;401}402403/**404* Gets the proxy auto-configuration URL.405*406* @return the proxy auto-configuration URL407*/408public @Nullable String getProxyAutoconfigUrl() {409return proxyAutoconfigUrl;410}411412/**413* Specifies the URL to be used for proxy auto-configuration. Expected format is <code>414* http://hostname.com:1234/pacfile</code>. This is required if {@link #getProxyType()} is set to415* {@link ProxyType#PAC}, ignored otherwise.416*417* @param proxyAutoconfigUrl the URL for proxy auto-configuration418* @return reference to self419*/420public Proxy setProxyAutoconfigUrl(String proxyAutoconfigUrl) {421verifyProxyTypeCompatibility(ProxyType.PAC);422this.proxyType = ProxyType.PAC;423this.proxyAutoconfigUrl = proxyAutoconfigUrl;424return this;425}426427private void verifyProxyTypeCompatibility(ProxyType compatibleProxy) {428if (proxyType != ProxyType.UNSPECIFIED && proxyType != compatibleProxy) {429throw new IllegalStateException(430String.format(431"Specified proxy type (%s) not compatible with current setting (%s)",432compatibleProxy, proxyType));433}434}435436@SuppressWarnings({"unchecked"})437public static @Nullable Proxy extractFrom(Capabilities capabilities) {438Object rawProxy = capabilities.getCapability("proxy");439Proxy proxy = null;440if (rawProxy != null) {441if (rawProxy instanceof Proxy) {442proxy = (Proxy) rawProxy;443} else if (rawProxy instanceof Map) {444proxy = new Proxy((Map<String, ?>) rawProxy);445}446}447return proxy;448}449450@Override451public String toString() {452StringBuilder builder = new StringBuilder("Proxy(");453454switch (getProxyType()) {455case AUTODETECT:456case DIRECT:457case MANUAL:458case SYSTEM:459builder.append(getProxyType().toString().toLowerCase(Locale.ENGLISH));460break;461462case PAC:463builder.append("pac: ").append(getProxyAutoconfigUrl());464break;465466case RESERVED_1:467case UNSPECIFIED:468break;469}470471Optional.ofNullable(getFtpProxy()).ifPresent(p -> builder.append(", ftp=").append(p));472Optional.ofNullable(getHttpProxy()).ifPresent(p -> builder.append(", http=").append(p));473Optional.ofNullable(getSocksProxy()).ifPresent(p -> builder.append(", socks=").append(p));474Optional.ofNullable(getSslProxy()).ifPresent(p -> builder.append(", ssl=").append(p));475476builder.append(")");477return builder.toString();478}479480@Override481public boolean equals(@Nullable Object o) {482if (this == o) {483return true;484}485if (o == null || getClass() != o.getClass()) {486return false;487}488Proxy proxy = (Proxy) o;489return isAutodetect() == proxy.isAutodetect()490&& getProxyType() == proxy.getProxyType()491&& Objects.equals(getFtpProxy(), proxy.getFtpProxy())492&& Objects.equals(getHttpProxy(), proxy.getHttpProxy())493&& Objects.equals(getNoProxy(), proxy.getNoProxy())494&& Objects.equals(getSslProxy(), proxy.getSslProxy())495&& Objects.equals(getSocksProxy(), proxy.getSocksProxy())496&& Objects.equals(getSocksVersion(), proxy.getSocksVersion())497&& Objects.equals(getSocksUsername(), proxy.getSocksUsername())498&& Objects.equals(getSocksPassword(), proxy.getSocksPassword())499&& Objects.equals(getProxyAutoconfigUrl(), proxy.getProxyAutoconfigUrl());500}501502@Override503public int hashCode() {504return Objects.hash(505getProxyType(),506isAutodetect(),507getFtpProxy(),508getHttpProxy(),509getNoProxy(),510getSslProxy(),511getSocksProxy(),512getSocksVersion(),513getSocksUsername(),514getSocksPassword(),515getProxyAutoconfigUrl());516}517}518519520