proxy_config.cc 9.4 KB

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