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 #ifndef NET_PROXY_RESOLUTION_PROXY_INFO_H_ 6 #define NET_PROXY_RESOLUTION_PROXY_INFO_H_ 7 8 #include <string> 9 10 #include "base/gtest_prod_util.h" 11 #include "base/time/time.h" 12 #include "net/base/net_export.h" 13 #include "net/base/proxy_chain.h" 14 #include "net/base/proxy_server.h" 15 #include "net/proxy_resolution/proxy_config.h" 16 #include "net/proxy_resolution/proxy_list.h" 17 #include "net/proxy_resolution/proxy_retry_info.h" 18 #include "net/traffic_annotation/network_traffic_annotation.h" 19 20 namespace net { 21 22 class NetLogWithSource; 23 24 // This object holds proxy information returned by ResolveProxy. 25 class NET_EXPORT ProxyInfo { 26 public: 27 ProxyInfo(); 28 ProxyInfo(const ProxyInfo& other); 29 ~ProxyInfo(); 30 // Default copy-constructor and assignment operator are OK! 31 32 // Uses the same proxy server as the given |proxy_info|. 33 void Use(const ProxyInfo& proxy_info); 34 35 // Uses a direct connection. 36 void UseDirect(); 37 38 // Uses a direct connection. did_bypass_proxy() will return true to indicate 39 // that the direct connection is the result of configured proxy bypass rules. 40 void UseDirectWithBypassedProxy(); 41 42 // Uses a specific proxy server, of the form: 43 // proxy-uri = [<scheme> "://"] <hostname> [":" <port>] 44 // This may optionally be a semi-colon delimited list of <proxy-uri>. 45 // It is OK to have LWS between entries. 46 void UseNamedProxy(const std::string& proxy_uri_list); 47 48 // Sets the proxy list to a single entry, |proxy_chain|. 49 void UseProxyChain(const ProxyChain& proxy_chain); 50 51 // Parses from the given PAC result. 52 void UsePacString(const std::string& pac_string); 53 54 // Uses the proxies from the given list. 55 void UseProxyList(const ProxyList& proxy_list); 56 57 // Uses the proxies from the given list, but does not otherwise reset the 58 // proxy configuration. 59 void OverrideProxyList(const ProxyList& proxy_list); 60 61 // Indicates that this is a proxy for IP Protection. set_is_for_ip_protection(bool is_for_ip_protection)62 void set_is_for_ip_protection(bool is_for_ip_protection) { 63 is_for_ip_protection_ = is_for_ip_protection; 64 } 65 66 // Indicates that the request that uses this proxy config caused a match with 67 // the masked domain list. 68 // This is a temporary workaround to gather initial metrics for IP Protection. 69 // TODO(1507085): Remove once the experiment is concluded. set_is_mdl_match(bool is_mdl_match)70 void set_is_mdl_match(bool is_mdl_match) { is_mdl_match_ = is_mdl_match; } 71 72 // Returns true if this proxy info specifies a direct connection. is_direct()73 bool is_direct() const { 74 // We don't implicitly fallback to DIRECT unless it was added to the list. 75 if (is_empty()) { 76 return false; 77 } 78 return proxy_chain().is_direct(); 79 } 80 is_direct_only()81 bool is_direct_only() const { 82 return is_direct() && proxy_list_.size() == 1 && proxy_retry_info_.empty(); 83 } 84 85 // Returns true if any of the contained ProxyChains are multi-proxy. 86 bool ContainsMultiProxyChain() const; 87 88 // Returns true if the first valid proxy server is an https proxy. 89 // TODO(https://crbug.com/1491092): Remove this method in favor of checking 90 // the corresponding property of the relevant proxy server from the next 91 // proxy chain in the proxy list. 92 bool is_https() const; 93 94 // Returns true if the first proxy server is an HTTP compatible proxy. 95 // TODO(https://crbug.com/1491092): Remove this method in favor of checking 96 // the corresponding property of the relevant proxy server from the next 97 // proxy chain in the proxy list. 98 bool is_http_like() const; 99 100 // Returns true if the first proxy server is an HTTP compatible proxy over a 101 // secure connection. 102 // TODO(https://crbug.com/1491092): Remove this method in favor of checking 103 // the corresponding property of the relevant proxy server from the next 104 // proxy chain in the proxy list. 105 bool is_secure_http_like() const; 106 107 // Returns true if the first valid proxy server is an http proxy. 108 // TODO(https://crbug.com/1491092): Remove this method in favor of checking 109 // the corresponding property of the relevant proxy server from the next 110 // proxy chain in the proxy list. 111 bool is_http() const; 112 113 // Returns true if the first valid proxy server is a quic proxy. 114 // TODO(https://crbug.com/1491092): Remove this method in favor of checking 115 // the corresponding property of the relevant proxy server from the next 116 // proxy chain in the proxy list. 117 bool is_quic() const; 118 119 // Returns true if the first valid proxy server is a socks server. 120 // TODO(https://crbug.com/1491092): Remove this method in favor of checking 121 // the corresponding property of the relevant proxy server from the next 122 // proxy chain in the proxy list. 123 bool is_socks() const; 124 125 // Returns true if this proxy info has no proxies left to try. is_empty()126 bool is_empty() const { 127 return proxy_list_.IsEmpty(); 128 } 129 130 // Returns true if this proxy resolution is using a direct connection due to 131 // proxy bypass rules. did_bypass_proxy()132 bool did_bypass_proxy() const { 133 return did_bypass_proxy_; 134 } 135 136 // Returns true if this proxy info is for IP Protection. is_for_ip_protection()137 bool is_for_ip_protection() const { return is_for_ip_protection_; } 138 139 // Returns true if the request that uses this proxy config caused a match with 140 // the masked domain list. 141 // This is a temporary workaround to gather initial metrics for IP Protection. 142 // TODO(1507085): Remove once the experiment is concluded. is_mdl_match()143 bool is_mdl_match() const { return is_mdl_match_; } 144 145 // Returns the first valid proxy chain. is_empty() must be false to be able 146 // to call this function. proxy_chain()147 const ProxyChain& proxy_chain() const { return proxy_list_.First(); } 148 149 // Returns the full list of proxies to use. proxy_list()150 const ProxyList& proxy_list() const { return proxy_list_; } 151 152 // See description in ProxyList::ToPacString(). 153 std::string ToPacString() const; 154 155 // See description in ProxyList::ToDebugString(). 156 std::string ToDebugString() const; 157 158 // Marks the current proxy as bad. |net_error| should contain the network 159 // error encountered when this proxy was tried, if any. If this fallback 160 // is not because of a network error, then |OK| should be passed in (eg. for 161 // reasons such as local policy). Returns true if there is another proxy 162 // available to try in |proxy_list_|. 163 bool Fallback(int net_error, const NetLogWithSource& net_log); 164 165 // De-prioritizes the proxies that we have cached as not working, by moving 166 // them to the end of the proxy list. 167 void DeprioritizeBadProxyChains(const ProxyRetryInfoMap& proxy_retry_info); 168 169 // Deletes any entry which doesn't have one of the specified proxy schemes. 170 void RemoveProxiesWithoutScheme(int scheme_bit_field); 171 set_proxy_resolve_start_time(const base::TimeTicks & proxy_resolve_start_time)172 void set_proxy_resolve_start_time( 173 const base::TimeTicks& proxy_resolve_start_time) { 174 proxy_resolve_start_time_ = proxy_resolve_start_time; 175 } 176 proxy_resolve_start_time()177 base::TimeTicks proxy_resolve_start_time() const { 178 return proxy_resolve_start_time_; 179 } 180 set_proxy_resolve_end_time(const base::TimeTicks & proxy_resolve_end_time)181 void set_proxy_resolve_end_time( 182 const base::TimeTicks& proxy_resolve_end_time) { 183 proxy_resolve_end_time_ = proxy_resolve_end_time; 184 } 185 proxy_resolve_end_time()186 base::TimeTicks proxy_resolve_end_time() const { 187 return proxy_resolve_end_time_; 188 } 189 set_traffic_annotation(const MutableNetworkTrafficAnnotationTag & traffic_annotation)190 void set_traffic_annotation( 191 const MutableNetworkTrafficAnnotationTag& traffic_annotation) { 192 traffic_annotation_ = traffic_annotation; 193 } 194 traffic_annotation()195 MutableNetworkTrafficAnnotationTag traffic_annotation() const { 196 return traffic_annotation_; 197 } 198 proxy_retry_info()199 const ProxyRetryInfoMap& proxy_retry_info() const { 200 return proxy_retry_info_; 201 } 202 203 private: 204 // Reset proxy and config settings. 205 void Reset(); 206 207 // Verify that all proxies in the first chain have `SCHEME_HTTPS`. This is 208 // currently enforced by `ProxyChain::IsValid`, and assumed by various `is_..` 209 // methods in this class. 210 bool AllChainProxiesAreHttps() const; 211 212 // The ordered list of proxy servers (including DIRECT attempts) remaining to 213 // try. If proxy_list_ is empty, then there is nothing left to fall back to. 214 ProxyList proxy_list_; 215 216 // List of proxies that have been tried already. 217 ProxyRetryInfoMap proxy_retry_info_; 218 219 // The traffic annotation of the used proxy config. 220 MutableNetworkTrafficAnnotationTag traffic_annotation_; 221 222 // Whether the proxy result represent a proxy bypass. 223 bool did_bypass_proxy_ = false; 224 225 // Whether this proxy is for IP Protection. 226 bool is_for_ip_protection_ = false; 227 228 // Whether the request that uses this proxy config caused a match with the 229 // masked domain list. 230 // This is a temporary workaround to gather initial metrics for IP Protection. 231 // TODO(1507085): Remove once the experiment is concluded. 232 bool is_mdl_match_ = false; 233 234 // How long it took to resolve the proxy. Times are both null if proxy was 235 // determined synchronously without running a PAC. 236 base::TimeTicks proxy_resolve_start_time_; 237 base::TimeTicks proxy_resolve_end_time_; 238 }; 239 240 } // namespace net 241 242 #endif // NET_PROXY_RESOLUTION_PROXY_INFO_H_ 243