• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <algorithm>
6 
7 #include "net/proxy_resolution/proxy_list.h"
8 
9 #include "base/check.h"
10 #include "base/functional/callback.h"
11 #include "base/notreached.h"
12 #include "base/strings/string_tokenizer.h"
13 #include "base/time/time.h"
14 #include "base/values.h"
15 #include "net/base/proxy_chain.h"
16 #include "net/base/proxy_server.h"
17 #include "net/base/proxy_string_util.h"
18 #include "net/log/net_log.h"
19 #include "net/log/net_log_event_type.h"
20 #include "net/log/net_log_with_source.h"
21 
22 using base::TimeTicks;
23 
24 namespace net {
25 
26 ProxyList::ProxyList() = default;
27 
28 ProxyList::ProxyList(const ProxyList& other) = default;
29 
30 ProxyList::ProxyList(ProxyList&& other) = default;
31 
32 ProxyList& ProxyList::operator=(const ProxyList& other) = default;
33 
34 ProxyList& ProxyList::operator=(ProxyList&& other) = default;
35 
36 ProxyList::~ProxyList() = default;
37 
Set(const std::string & proxy_uri_list)38 void ProxyList::Set(const std::string& proxy_uri_list) {
39   Clear();
40   base::StringTokenizer str_tok(proxy_uri_list, ";");
41   while (str_tok.GetNext()) {
42     ProxyChain chain =
43         ProxyUriToProxyChain(str_tok.token_piece(), ProxyServer::SCHEME_HTTP);
44     AddProxyChain(chain);
45   }
46 }
47 
SetSingleProxyChain(const ProxyChain & proxy_chain)48 void ProxyList::SetSingleProxyChain(const ProxyChain& proxy_chain) {
49   Clear();
50   AddProxyChain(proxy_chain);
51 }
52 
SetSingleProxyServer(const ProxyServer & proxy_server)53 void ProxyList::SetSingleProxyServer(const ProxyServer& proxy_server) {
54   Clear();
55   AddProxyServer(proxy_server);
56 }
57 
AddProxyChain(const ProxyChain & proxy_chain)58 void ProxyList::AddProxyChain(const ProxyChain& proxy_chain) {
59   // Silently discard malformed inputs.
60   if (proxy_chain.IsValid()) {
61     proxy_chains_.push_back(proxy_chain);
62   }
63 }
64 
AddProxyServer(const ProxyServer & proxy_server)65 void ProxyList::AddProxyServer(const ProxyServer& proxy_server) {
66   AddProxyChain(ProxyChain(proxy_server));
67 }
68 
DeprioritizeBadProxyChains(const ProxyRetryInfoMap & proxy_retry_info)69 void ProxyList::DeprioritizeBadProxyChains(
70     const ProxyRetryInfoMap& proxy_retry_info) {
71   // Partition the proxy list in two:
72   //   (1) the known bad proxy chains
73   //   (2) everything else
74   std::vector<ProxyChain> good_chains;
75   std::vector<ProxyChain> bad_chains_to_try;
76 
77   std::vector<ProxyChain>::const_iterator iter = proxy_chains_.begin();
78   for (; iter != proxy_chains_.end(); ++iter) {
79     auto bad_info = proxy_retry_info.find(*iter);
80     if (bad_info != proxy_retry_info.end()) {
81       // This proxy is bad. Check if it's time to retry.
82       if (bad_info->second.bad_until >= TimeTicks::Now()) {
83         // still invalid.
84         if (bad_info->second.try_while_bad) {
85           bad_chains_to_try.push_back(*iter);
86         }
87         continue;
88       }
89     }
90     good_chains.push_back(*iter);
91   }
92 
93   // "proxy_chains_ = good_chains + bad_proxies"
94   proxy_chains_.swap(good_chains);
95   proxy_chains_.insert(proxy_chains_.end(), bad_chains_to_try.begin(),
96                        bad_chains_to_try.end());
97 }
98 
RemoveProxiesWithoutScheme(int scheme_bit_field)99 void ProxyList::RemoveProxiesWithoutScheme(int scheme_bit_field) {
100   std::erase_if(proxy_chains_, [&](const ProxyChain& chain) {
101     auto& proxy_servers = chain.proxy_servers();
102     // Remove the chain if any of the component servers does not match
103     // at least one scheme in `scheme_bit_field`.
104     return std::any_of(proxy_servers.begin(), proxy_servers.end(),
105                        [&](const ProxyServer& server) {
106                          return !(scheme_bit_field & server.scheme());
107                        });
108   });
109 }
110 
Clear()111 void ProxyList::Clear() {
112   proxy_chains_.clear();
113 }
114 
IsEmpty() const115 bool ProxyList::IsEmpty() const {
116   return proxy_chains_.empty();
117 }
118 
size() const119 size_t ProxyList::size() const {
120   return proxy_chains_.size();
121 }
122 
123 // Returns true if |*this| lists the same proxy chains as |other|.
Equals(const ProxyList & other) const124 bool ProxyList::Equals(const ProxyList& other) const {
125   if (size() != other.size())
126     return false;
127   return proxy_chains_ == other.proxy_chains_;
128 }
129 
First() const130 const ProxyChain& ProxyList::First() const {
131   CHECK(!proxy_chains_.empty());
132   return proxy_chains_[0];
133 }
134 
GetAll() const135 std::vector<ProxyServer> ProxyList::GetAll() const {
136   std::vector<ProxyServer> proxy_servers;
137   for (const auto& proxy_chain : AllChains()) {
138     proxy_servers.push_back(proxy_chain.proxy_server());
139   }
140   return proxy_servers;
141 }
142 
AllChains() const143 const std::vector<ProxyChain>& ProxyList::AllChains() const {
144   return proxy_chains_;
145 }
146 
SetFromPacString(const std::string & pac_string)147 void ProxyList::SetFromPacString(const std::string& pac_string) {
148   Clear();
149   base::StringTokenizer entry_tok(pac_string, ";");
150   while (entry_tok.GetNext()) {
151     ProxyServer proxy_server =
152         PacResultElementToProxyServer(entry_tok.token_piece());
153     if (proxy_server.is_valid()) {
154       proxy_chains_.emplace_back(proxy_server);
155     }
156   }
157 
158   // If we failed to parse anything from the PAC results list, fallback to
159   // DIRECT (this basically means an error in the PAC script).
160   if (proxy_chains_.empty()) {
161     proxy_chains_.push_back(ProxyChain::Direct());
162   }
163 }
164 
ToPacString() const165 std::string ProxyList::ToPacString() const {
166   std::string proxy_list;
167   auto iter = proxy_chains_.begin();
168   for (; iter != proxy_chains_.end(); ++iter) {
169     if (!proxy_list.empty()) {
170       proxy_list += ";";
171     }
172     CHECK(!iter->is_multi_proxy());
173     proxy_list += ProxyServerToPacResultElement(iter->proxy_server());
174   }
175   return proxy_list.empty() ? std::string() : proxy_list;
176 }
177 
ToDebugString() const178 std::string ProxyList::ToDebugString() const {
179   std::string proxy_list;
180   auto iter = proxy_chains_.begin();
181   for (; iter != proxy_chains_.end(); ++iter) {
182     if (!proxy_list.empty())
183       proxy_list += ";";
184     if (iter->is_multi_proxy()) {
185       proxy_list += iter->ToDebugString();
186     } else {
187       proxy_list += ProxyServerToPacResultElement(iter->proxy_server());
188     }
189   }
190   return proxy_list.empty() ? std::string() : proxy_list;
191 }
192 
ToValue() const193 base::Value ProxyList::ToValue() const {
194   base::Value::List list;
195   for (const auto& proxy_chain : proxy_chains_) {
196     if (proxy_chain.is_direct()) {
197       list.Append(ProxyServerToProxyUri(ProxyServer::Direct()));
198     } else {
199       list.Append(proxy_chain.ToDebugString());
200     }
201   }
202   return base::Value(std::move(list));
203 }
204 
Fallback(ProxyRetryInfoMap * proxy_retry_info,int net_error,const NetLogWithSource & net_log)205 bool ProxyList::Fallback(ProxyRetryInfoMap* proxy_retry_info,
206                          int net_error,
207                          const NetLogWithSource& net_log) {
208   if (proxy_chains_.empty()) {
209     NOTREACHED();
210     return false;
211   }
212   // By default, proxy chains are not retried for 5 minutes.
213   UpdateRetryInfoOnFallback(proxy_retry_info, base::Minutes(5), true,
214                             std::vector<ProxyChain>(), net_error, net_log);
215 
216   // Remove this proxy from our list.
217   proxy_chains_.erase(proxy_chains_.begin());
218   return !proxy_chains_.empty();
219 }
220 
AddProxyChainToRetryList(ProxyRetryInfoMap * proxy_retry_info,base::TimeDelta retry_delay,bool try_while_bad,const ProxyChain & proxy_chain_to_retry,int net_error,const NetLogWithSource & net_log) const221 void ProxyList::AddProxyChainToRetryList(
222     ProxyRetryInfoMap* proxy_retry_info,
223     base::TimeDelta retry_delay,
224     bool try_while_bad,
225     const ProxyChain& proxy_chain_to_retry,
226     int net_error,
227     const NetLogWithSource& net_log) const {
228   // Mark this proxy chain as bad.
229   TimeTicks bad_until = TimeTicks::Now() + retry_delay;
230   auto iter = proxy_retry_info->find(proxy_chain_to_retry);
231   if (iter == proxy_retry_info->end() || bad_until > iter->second.bad_until) {
232     ProxyRetryInfo retry_info;
233     retry_info.current_delay = retry_delay;
234     retry_info.bad_until = bad_until;
235     retry_info.try_while_bad = try_while_bad;
236     retry_info.net_error = net_error;
237     (*proxy_retry_info)[proxy_chain_to_retry] = retry_info;
238   }
239   net_log.AddEventWithStringParams(NetLogEventType::PROXY_LIST_FALLBACK,
240                                    "bad_proxy_chain",
241                                    proxy_chain_to_retry.ToDebugString());
242 }
243 
UpdateRetryInfoOnFallback(ProxyRetryInfoMap * proxy_retry_info,base::TimeDelta retry_delay,bool reconsider,const std::vector<ProxyChain> & additional_proxies_to_bypass,int net_error,const NetLogWithSource & net_log) const244 void ProxyList::UpdateRetryInfoOnFallback(
245     ProxyRetryInfoMap* proxy_retry_info,
246     base::TimeDelta retry_delay,
247     bool reconsider,
248     const std::vector<ProxyChain>& additional_proxies_to_bypass,
249     int net_error,
250     const NetLogWithSource& net_log) const {
251   DCHECK(!retry_delay.is_zero());
252 
253   if (proxy_chains_.empty()) {
254     NOTREACHED();
255     return;
256   }
257 
258   auto& first_chain = proxy_chains_[0];
259   if (!first_chain.is_direct()) {
260     AddProxyChainToRetryList(proxy_retry_info, retry_delay, reconsider,
261                              first_chain, net_error, net_log);
262     // If any additional proxies to bypass are specified, add to the retry map
263     // as well.
264     for (const ProxyChain& additional_proxy_chain :
265          additional_proxies_to_bypass) {
266       AddProxyChainToRetryList(
267           proxy_retry_info, retry_delay, reconsider,
268           ProxyChain(additional_proxy_chain.proxy_servers()), net_error,
269           net_log);
270     }
271   }
272 }
273 
274 }  // namespace net
275