1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/proxy_resolution/proxy_config.h"
6
7 #include <memory>
8 #include <utility>
9
10 #include "base/check_op.h"
11 #include "base/notreached.h"
12 #include "base/strings/string_tokenizer.h"
13 #include "base/strings/string_util.h"
14 #include "base/values.h"
15 #include "net/base/proxy_server.h"
16 #include "net/base/proxy_string_util.h"
17 #include "net/proxy_resolution/proxy_info.h"
18
19 namespace net {
20
21 namespace {
22
23 // If |proxies| is non-empty, sets it in |dict| under the key |name|.
AddProxyListToValue(const char * name,const ProxyList & proxies,base::Value::Dict * dict)24 void AddProxyListToValue(const char* name,
25 const ProxyList& proxies,
26 base::Value::Dict* dict) {
27 if (!proxies.IsEmpty())
28 dict->Set(name, proxies.ToValue());
29 }
30
31 // Split the |uri_list| on commas and add each entry to |proxy_list| in turn.
AddProxyURIListToProxyList(std::string uri_list,ProxyList * proxy_list,ProxyServer::Scheme default_scheme)32 void AddProxyURIListToProxyList(std::string uri_list,
33 ProxyList* proxy_list,
34 ProxyServer::Scheme default_scheme) {
35 base::StringTokenizer proxy_uri_list(uri_list, ",");
36 while (proxy_uri_list.GetNext()) {
37 proxy_list->AddProxyChain(
38 ProxyUriToProxyChain(proxy_uri_list.token(), default_scheme));
39 }
40 }
41
42 } // namespace
43
44 ProxyConfig::ProxyRules::ProxyRules() = default;
45
46 ProxyConfig::ProxyRules::ProxyRules(const ProxyRules& other) = default;
47
48 ProxyConfig::ProxyRules::~ProxyRules() = default;
49
Apply(const GURL & url,ProxyInfo * result) const50 void ProxyConfig::ProxyRules::Apply(const GURL& url, ProxyInfo* result) const {
51 if (empty()) {
52 result->UseDirect();
53 return;
54 }
55
56 if (bypass_rules.Matches(url, reverse_bypass)) {
57 result->UseDirectWithBypassedProxy();
58 return;
59 }
60
61 switch (type) {
62 case ProxyRules::Type::PROXY_LIST: {
63 result->UseProxyList(single_proxies);
64 return;
65 }
66 case ProxyRules::Type::PROXY_LIST_PER_SCHEME: {
67 const ProxyList* entry = MapUrlSchemeToProxyList(url.scheme());
68 if (entry) {
69 result->UseProxyList(*entry);
70 } else {
71 // We failed to find a matching proxy server for the current URL
72 // scheme. Default to direct.
73 result->UseDirect();
74 }
75 return;
76 }
77 default: {
78 result->UseDirect();
79 NOTREACHED();
80 return;
81 }
82 }
83 }
84
ParseFromString(const std::string & proxy_rules)85 void ProxyConfig::ProxyRules::ParseFromString(const std::string& proxy_rules) {
86 // Reset.
87 type = Type::EMPTY;
88 single_proxies = ProxyList();
89 proxies_for_http = ProxyList();
90 proxies_for_https = ProxyList();
91 proxies_for_ftp = ProxyList();
92 fallback_proxies = ProxyList();
93
94 base::StringTokenizer proxy_server_list(proxy_rules, ";");
95 while (proxy_server_list.GetNext()) {
96 base::StringTokenizer proxy_server_for_scheme(
97 proxy_server_list.token_begin(), proxy_server_list.token_end(), "=");
98
99 while (proxy_server_for_scheme.GetNext()) {
100 std::string url_scheme = proxy_server_for_scheme.token();
101
102 // If we fail to get the proxy server here, it means that
103 // this is a regular proxy server configuration, i.e. proxies
104 // are not configured per protocol.
105 if (!proxy_server_for_scheme.GetNext()) {
106 if (type == Type::PROXY_LIST_PER_SCHEME)
107 continue; // Unexpected.
108 AddProxyURIListToProxyList(url_scheme,
109 &single_proxies,
110 ProxyServer::SCHEME_HTTP);
111 type = Type::PROXY_LIST;
112 return;
113 }
114
115 // Trim whitespace off the url scheme.
116 base::TrimWhitespaceASCII(url_scheme, base::TRIM_ALL, &url_scheme);
117
118 // Add it to the per-scheme mappings (if supported scheme).
119 type = Type::PROXY_LIST_PER_SCHEME;
120 ProxyList* entry = MapUrlSchemeToProxyListNoFallback(url_scheme);
121 ProxyServer::Scheme default_scheme = ProxyServer::SCHEME_HTTP;
122
123 // socks=XXX is inconsistent with the other formats, since "socks"
124 // is not a URL scheme. Rather this means "for everything else, send
125 // it to the SOCKS proxy server XXX".
126 if (url_scheme == "socks") {
127 DCHECK(!entry);
128 entry = &fallback_proxies;
129 // Note that here 'socks' is understood to be SOCKS4, even though
130 // 'socks' maps to SOCKS5 in ProxyServer::GetSchemeFromURIInternal.
131 default_scheme = ProxyServer::SCHEME_SOCKS4;
132 }
133
134 if (entry) {
135 AddProxyURIListToProxyList(proxy_server_for_scheme.token(),
136 entry,
137 default_scheme);
138 }
139 }
140 }
141 }
142
MapUrlSchemeToProxyList(const std::string & url_scheme) const143 const ProxyList* ProxyConfig::ProxyRules::MapUrlSchemeToProxyList(
144 const std::string& url_scheme) const {
145 const ProxyList* proxy_server_list = const_cast<ProxyRules*>(this)->
146 MapUrlSchemeToProxyListNoFallback(url_scheme);
147 if (proxy_server_list && !proxy_server_list->IsEmpty())
148 return proxy_server_list;
149 if (url_scheme == "ws" || url_scheme == "wss")
150 return GetProxyListForWebSocketScheme();
151 if (!fallback_proxies.IsEmpty())
152 return &fallback_proxies;
153 return nullptr; // No mapping for this scheme. Use direct.
154 }
155
Equals(const ProxyRules & other) const156 bool ProxyConfig::ProxyRules::Equals(const ProxyRules& other) const {
157 return type == other.type && single_proxies.Equals(other.single_proxies) &&
158 proxies_for_http.Equals(other.proxies_for_http) &&
159 proxies_for_https.Equals(other.proxies_for_https) &&
160 proxies_for_ftp.Equals(other.proxies_for_ftp) &&
161 fallback_proxies.Equals(other.fallback_proxies) &&
162 bypass_rules == other.bypass_rules &&
163 reverse_bypass == other.reverse_bypass;
164 }
165
MapUrlSchemeToProxyListNoFallback(const std::string & scheme)166 ProxyList* ProxyConfig::ProxyRules::MapUrlSchemeToProxyListNoFallback(
167 const std::string& scheme) {
168 DCHECK_EQ(Type::PROXY_LIST_PER_SCHEME, type);
169 if (scheme == "http")
170 return &proxies_for_http;
171 if (scheme == "https")
172 return &proxies_for_https;
173 if (scheme == "ftp")
174 return &proxies_for_ftp;
175 return nullptr; // No mapping for this scheme.
176 }
177
GetProxyListForWebSocketScheme() const178 const ProxyList* ProxyConfig::ProxyRules::GetProxyListForWebSocketScheme()
179 const {
180 // Follow the recommendation from RFC 6455 section 4.1.3:
181 //
182 // NOTE: Implementations that do not expose explicit UI for
183 // selecting a proxy for WebSocket connections separate from other
184 // proxies are encouraged to use a SOCKS5 [RFC1928] proxy for
185 // WebSocket connections, if available, or failing that, to prefer
186 // the proxy configured for HTTPS connections over the proxy
187 // configured for HTTP connections.
188 //
189 // This interpretation is a bit different from the RFC, in
190 // that it favors both SOCKSv4 and SOCKSv5.
191 //
192 // When the net::ProxyRules came from system proxy settings,
193 // "fallback_proxies" will be empty, or a a single SOCKS
194 // proxy, making this ordering match the RFC.
195 //
196 // However for other configurations it is possible for
197 // "fallback_proxies" to be a list of any ProxyServer,
198 // including non-SOCKS. In this case "fallback_proxies" is
199 // still prioritized over proxies_for_http and
200 // proxies_for_https.
201 if (!fallback_proxies.IsEmpty())
202 return &fallback_proxies;
203 if (!proxies_for_https.IsEmpty())
204 return &proxies_for_https;
205 if (!proxies_for_http.IsEmpty())
206 return &proxies_for_http;
207 return nullptr;
208 }
209
210 ProxyConfig::ProxyConfig() = default;
211
212 ProxyConfig::ProxyConfig(const ProxyConfig& config) = default;
213
214 ProxyConfig::~ProxyConfig() = default;
215
216 ProxyConfig& ProxyConfig::operator=(const ProxyConfig& config) = default;
217
Equals(const ProxyConfig & other) const218 bool ProxyConfig::Equals(const ProxyConfig& other) const {
219 return auto_detect_ == other.auto_detect_ && pac_url_ == other.pac_url_ &&
220 pac_mandatory_ == other.pac_mandatory_ &&
221 from_system_ == other.from_system_ &&
222 proxy_rules_.Equals(other.proxy_rules());
223 }
224
HasAutomaticSettings() const225 bool ProxyConfig::HasAutomaticSettings() const {
226 return auto_detect_ || has_pac_url();
227 }
228
ClearAutomaticSettings()229 void ProxyConfig::ClearAutomaticSettings() {
230 auto_detect_ = false;
231 pac_url_ = GURL();
232 }
233
ToValue() const234 base::Value ProxyConfig::ToValue() const {
235 base::Value::Dict dict;
236
237 // Output the automatic settings.
238 if (auto_detect_)
239 dict.Set("auto_detect", auto_detect_);
240 if (has_pac_url()) {
241 dict.Set("pac_url", pac_url_.possibly_invalid_spec());
242 if (pac_mandatory_)
243 dict.Set("pac_mandatory", pac_mandatory_);
244 }
245 if (from_system_) {
246 dict.Set("from_system", from_system_);
247 }
248
249 // Output the manual settings.
250 if (proxy_rules_.type != ProxyRules::Type::EMPTY) {
251 switch (proxy_rules_.type) {
252 case ProxyRules::Type::PROXY_LIST:
253 AddProxyListToValue("single_proxy", proxy_rules_.single_proxies, &dict);
254 break;
255 case ProxyRules::Type::PROXY_LIST_PER_SCHEME: {
256 base::Value::Dict dict2;
257 AddProxyListToValue("http", proxy_rules_.proxies_for_http, &dict2);
258 AddProxyListToValue("https", proxy_rules_.proxies_for_https, &dict2);
259 AddProxyListToValue("ftp", proxy_rules_.proxies_for_ftp, &dict2);
260 AddProxyListToValue("fallback", proxy_rules_.fallback_proxies, &dict2);
261 dict.Set("proxy_per_scheme", std::move(dict2));
262 break;
263 }
264 default:
265 NOTREACHED();
266 }
267
268 // Output the bypass rules.
269 const ProxyBypassRules& bypass = proxy_rules_.bypass_rules;
270 if (!bypass.rules().empty()) {
271 if (proxy_rules_.reverse_bypass)
272 dict.Set("reverse_bypass", true);
273
274 base::Value::List list;
275
276 for (const auto& bypass_rule : bypass.rules())
277 list.Append(bypass_rule->ToString());
278
279 dict.Set("bypass_list", std::move(list));
280 }
281 }
282
283 return base::Value(std::move(dict));
284 }
285
286 } // namespace net
287