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
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/proxy_resolution/proxy_config.h"
#include <memory>
#include <utility>
#include "base/check.h"
#include "base/check_op.h"
#include "base/notreached.h"
#include "base/strings/string_tokenizer.h"
#include "base/strings/string_util.h"
#include "base/values.h"
#include "build/buildflag.h"
#include "net/base/proxy_server.h"
#include "net/base/proxy_string_util.h"
#include "net/net_buildflags.h"
#include "net/proxy_resolution/proxy_info.h"
namespace net {
namespace {
// If |proxies| is non-empty, sets it in |dict| under the key |name|.
void AddProxyListToValue(const char* name,
const ProxyList& proxies,
base::Value::Dict* dict) {
if (!proxies.IsEmpty()) {
dict->Set(name, proxies.ToValue());
}
}
// Split the |uri_list| on commas and add each entry to |proxy_list| in turn.
void AddProxyURIListToProxyList(std::string_view uri_list,
ProxyList* proxy_list,
ProxyServer::Scheme default_scheme,
bool allow_bracketed_proxy_chains,
bool is_quic_allowed) {
base::StringViewTokenizer proxy_uri_list(uri_list, ",");
while (proxy_uri_list.GetNext()) {
proxy_list->AddProxyChain(
allow_bracketed_proxy_chains
? MultiProxyUrisToProxyChain(proxy_uri_list.token(), default_scheme,
is_quic_allowed)
: ProxyUriToProxyChain(proxy_uri_list.token(), default_scheme,
is_quic_allowed));
}
}
} // namespace
ProxyConfig::ProxyRules::ProxyRules() = default;
ProxyConfig::ProxyRules::ProxyRules(const ProxyRules& other) = default;
ProxyConfig::ProxyRules::~ProxyRules() = default;
void ProxyConfig::ProxyRules::Apply(const GURL& url, ProxyInfo* result) const {
if (empty()) {
result->UseDirect();
return;
}
if (bypass_rules.Matches(url, reverse_bypass)) {
result->UseDirectWithBypassedProxy();
return;
}
switch (type) {
case ProxyRules::Type::PROXY_LIST: {
result->UseProxyList(single_proxies);
return;
}
case ProxyRules::Type::PROXY_LIST_PER_SCHEME: {
const ProxyList* entry = MapUrlSchemeToProxyList(url.scheme());
if (entry) {
result->UseProxyList(*entry);
} else {
// We failed to find a matching proxy server for the current URL
// scheme. Default to direct.
result->UseDirect();
}
return;
}
default: {
NOTREACHED();
}
}
}
void ProxyConfig::ProxyRules::ParseFromString(std::string_view proxy_rules,
bool allow_bracketed_proxy_chains,
bool is_quic_allowed) {
// Reset.
type = Type::EMPTY;
single_proxies = ProxyList();
proxies_for_http = ProxyList();
proxies_for_https = ProxyList();
proxies_for_ftp = ProxyList();
fallback_proxies = ProxyList();
#if !BUILDFLAG(ENABLE_BRACKETED_PROXY_URIS)
// `allow_multi_proxy_chains` can only be true in non-release builds;
CHECK(!allow_bracketed_proxy_chains);
#endif // !BUILDFLAG(ENABLE_BRACKETED_PROXY_URIS)
#if !BUILDFLAG(ENABLE_QUIC_PROXY_SUPPORT)
CHECK(!is_quic_allowed);
#endif // BUILDFLAG(ENABLE_QUIC_PROXY_SUPPORT)
base::StringViewTokenizer proxy_server_list(proxy_rules, ";");
while (proxy_server_list.GetNext()) {
// Have to use the constructor that takes iterators here (or copy the
// current token() on the stack), since StringViewTokenizer's constructor
// that takes a string_view doesn't make its own copy of the string_view,
// but instead holds onto iterators to it.
base::StringViewTokenizer proxy_server_for_scheme(
proxy_server_list.token_begin(), proxy_server_list.token_end(), "=");
while (proxy_server_for_scheme.GetNext()) {
std::string_view url_scheme = proxy_server_for_scheme.token();
// If we fail to get the proxy server here, it means that
// this is a regular proxy server configuration, i.e. proxies
// are not configured per protocol.
if (!proxy_server_for_scheme.GetNext()) {
if (type == Type::PROXY_LIST_PER_SCHEME) {
continue; // Unexpected.
}
AddProxyURIListToProxyList(
url_scheme, &single_proxies, ProxyServer::SCHEME_HTTP,
allow_bracketed_proxy_chains, is_quic_allowed);
type = Type::PROXY_LIST;
return;
}
// Trim whitespace off the url scheme.
url_scheme = base::TrimWhitespaceASCII(url_scheme, base::TRIM_ALL);
// Add it to the per-scheme mappings (if supported scheme).
type = Type::PROXY_LIST_PER_SCHEME;
ProxyList* entry = MapUrlSchemeToProxyListNoFallback(url_scheme);
ProxyServer::Scheme default_scheme = ProxyServer::SCHEME_HTTP;
// socks=XXX is inconsistent with the other formats, since "socks"
// is not a URL scheme. Rather this means "for everything else, send
// it to the SOCKS proxy server XXX".
if (url_scheme == "socks") {
DCHECK(!entry);
entry = &fallback_proxies;
// Note that here 'socks' is understood to be SOCKS4, even though
// 'socks' maps to SOCKS5 in ProxyServer::GetSchemeFromURIInternal.
default_scheme = ProxyServer::SCHEME_SOCKS4;
}
if (entry) {
AddProxyURIListToProxyList(proxy_server_for_scheme.token(), entry,
default_scheme, allow_bracketed_proxy_chains,
is_quic_allowed);
}
}
}
}
const ProxyList* ProxyConfig::ProxyRules::MapUrlSchemeToProxyList(
const std::string& url_scheme) const {
const ProxyList* proxy_server_list =
const_cast<ProxyRules*>(this)->MapUrlSchemeToProxyListNoFallback(
url_scheme);
if (proxy_server_list && !proxy_server_list->IsEmpty()) {
return proxy_server_list;
}
if (url_scheme == "ws" || url_scheme == "wss") {
return GetProxyListForWebSocketScheme();
}
if (!fallback_proxies.IsEmpty()) {
return &fallback_proxies;
}
return nullptr; // No mapping for this scheme. Use direct.
}
bool ProxyConfig::ProxyRules::Equals(const ProxyRules& other) const {
return type == other.type && single_proxies.Equals(other.single_proxies) &&
proxies_for_http.Equals(other.proxies_for_http) &&
proxies_for_https.Equals(other.proxies_for_https) &&
proxies_for_ftp.Equals(other.proxies_for_ftp) &&
fallback_proxies.Equals(other.fallback_proxies) &&
bypass_rules == other.bypass_rules &&
reverse_bypass == other.reverse_bypass;
}
ProxyList* ProxyConfig::ProxyRules::MapUrlSchemeToProxyListNoFallback(
std::string_view scheme) {
DCHECK_EQ(Type::PROXY_LIST_PER_SCHEME, type);
if (scheme == "http") {
return &proxies_for_http;
}
if (scheme == "https") {
return &proxies_for_https;
}
if (scheme == "ftp") {
return &proxies_for_ftp;
}
return nullptr; // No mapping for this scheme.
}
const ProxyList* ProxyConfig::ProxyRules::GetProxyListForWebSocketScheme()
const {
// Follow the recommendation from RFC 6455 section 4.1.3:
//
// NOTE: Implementations that do not expose explicit UI for
// selecting a proxy for WebSocket connections separate from other
// proxies are encouraged to use a SOCKS5 [RFC1928] proxy for
// WebSocket connections, if available, or failing that, to prefer
// the proxy configured for HTTPS connections over the proxy
// configured for HTTP connections.
//
// This interpretation is a bit different from the RFC, in
// that it favors both SOCKSv4 and SOCKSv5.
//
// When the net::ProxyRules came from system proxy settings,
// "fallback_proxies" will be empty, or a a single SOCKS
// proxy, making this ordering match the RFC.
//
// However for other configurations it is possible for
// "fallback_proxies" to be a list of any ProxyServer,
// including non-SOCKS. In this case "fallback_proxies" is
// still prioritized over proxies_for_http and
// proxies_for_https.
if (!fallback_proxies.IsEmpty()) {
return &fallback_proxies;
}
if (!proxies_for_https.IsEmpty()) {
return &proxies_for_https;
}
if (!proxies_for_http.IsEmpty()) {
return &proxies_for_http;
}
return nullptr;
}
ProxyConfig::ProxyConfig() = default;
ProxyConfig::ProxyConfig(const ProxyConfig& config) = default;
ProxyConfig::ProxyConfig(ProxyConfig&& config) = default;
ProxyConfig& ProxyConfig::operator=(const ProxyConfig& config) = default;
ProxyConfig& ProxyConfig::operator=(ProxyConfig&& config) = default;
ProxyConfig::~ProxyConfig() = default;
bool ProxyConfig::Equals(const ProxyConfig& other) const {
return auto_detect_ == other.auto_detect_ && pac_url_ == other.pac_url_ &&
pac_mandatory_ == other.pac_mandatory_ &&
from_system_ == other.from_system_ &&
proxy_rules_.Equals(other.proxy_rules());
}
bool ProxyConfig::HasAutomaticSettings() const {
return auto_detect_ || has_pac_url();
}
void ProxyConfig::ClearAutomaticSettings() {
auto_detect_ = false;
pac_url_ = GURL();
}
base::Value ProxyConfig::ToValue() const {
base::Value::Dict dict;
// Output the automatic settings.
if (auto_detect_) {
dict.Set("auto_detect", auto_detect_);
}
if (has_pac_url()) {
dict.Set("pac_url", pac_url_.possibly_invalid_spec());
if (pac_mandatory_) {
dict.Set("pac_mandatory", pac_mandatory_);
}
}
if (from_system_) {
dict.Set("from_system", from_system_);
}
// Output the manual settings.
if (proxy_rules_.type != ProxyRules::Type::EMPTY) {
switch (proxy_rules_.type) {
case ProxyRules::Type::PROXY_LIST:
AddProxyListToValue("single_proxy", proxy_rules_.single_proxies, &dict);
break;
case ProxyRules::Type::PROXY_LIST_PER_SCHEME: {
base::Value::Dict dict2;
AddProxyListToValue("http", proxy_rules_.proxies_for_http, &dict2);
AddProxyListToValue("https", proxy_rules_.proxies_for_https, &dict2);
AddProxyListToValue("ftp", proxy_rules_.proxies_for_ftp, &dict2);
AddProxyListToValue("fallback", proxy_rules_.fallback_proxies, &dict2);
dict.Set("proxy_per_scheme", std::move(dict2));
break;
}
default:
NOTREACHED();
}
// Output the bypass rules.
const ProxyBypassRules& bypass = proxy_rules_.bypass_rules;
if (!bypass.rules().empty()) {
if (proxy_rules_.reverse_bypass) {
dict.Set("reverse_bypass", true);
}
base::Value::List list;
for (const auto& bypass_rule : bypass.rules()) {
list.Append(bypass_rule->ToString());
}
dict.Set("bypass_list", std::move(list));
}
}
return base::Value(std::move(dict));
}
} // namespace net
|