File: proxy_chain.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (240 lines) | stat: -rw-r--r-- 9,532 bytes parent folder | download | duplicates (3)
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
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NET_BASE_PROXY_CHAIN_H_
#define NET_BASE_PROXY_CHAIN_H_

#include <stdint.h>

#include <iosfwd>
#include <optional>
#include <string>
#include <string_view>
#include <tuple>
#include <vector>

#include "net/base/host_port_pair.h"
#include "net/base/net_export.h"
#include "net/base/proxy_server.h"

namespace base {
class Pickle;
class PickleIterator;
}  // namespace base

namespace net {

// ProxyChain represents a chain of ProxyServers. A chain with multiple proxy
// servers means that a single connection will go through all of the proxies in
// order, using a tunnel through the first proxy to connect to the second, etc.
// A "direct" connection is a chain of length zero.
class NET_EXPORT ProxyChain {
 public:
  // Constructs an invalid ProxyChain.
  ProxyChain();

  ProxyChain(const ProxyChain& other);      // Copy constructor
  ProxyChain(ProxyChain&& other) noexcept;  // Move constructor

  ProxyChain& operator=(const ProxyChain& other);  // Copy assignment operator
  ProxyChain& operator=(
      ProxyChain&& other) noexcept;  // Move assignment operator

  ProxyChain(ProxyServer::Scheme scheme, const HostPortPair& host_port_pair);

  explicit ProxyChain(std::vector<ProxyServer> proxy_server_list);
  explicit ProxyChain(ProxyServer proxy_server);

  ~ProxyChain();  // Destructor declaration

  // Creates a single-proxy ProxyChain, validating and canonicalizing input.
  // Port is optional and, if not provided, will be replaced with the default
  // port for the given scheme. Accepts IPv6 literal `host`s with surrounding
  // brackets (URL format) or without (HostPortPair format). On invalid input,
  // result will be a `SCHEME_INVALID` ProxyChain.
  //
  // Must not be called with `SCHEME_INVALID` or `SCHEME_DIRECT`. Use
  // `ProxyChain()` or `Direct()` respectively to create an invalid or direct
  // ProxyChain.
  static ProxyChain FromSchemeHostAndPort(ProxyServer::Scheme scheme,
                                          std::string_view host,
                                          std::string_view port_str) {
    return ProxyChain(
        ProxyServer::FromSchemeHostAndPort(scheme, host, port_str));
  }
  static ProxyChain FromSchemeHostAndPort(ProxyServer::Scheme scheme,
                                          std::string_view host,
                                          std::optional<uint16_t> port) {
    return ProxyChain(ProxyServer::FromSchemeHostAndPort(scheme, host, port));
  }
  // Create a "direct" proxy chain, which includes no proxy servers.
  static ProxyChain Direct() { return ProxyChain(std::vector<ProxyServer>()); }

  // Creates a `ProxyChain` for use by the IP Protection feature. This is used
  // for metrics collection and for special handling.  If not given, the
  // chain_id defaults to 0 which corresponds to an un-identified chain.
  static ProxyChain ForIpProtection(std::vector<ProxyServer> proxy_server_list,
                                    int chain_id = 0) {
    return ProxyChain(std::move(proxy_server_list), chain_id,
                      /*opaque_data=*/std::nullopt);
  }

  // Creates a `ProxyChain` with `opaque_data` attached to it. This can be later
  // on retrieved via `opaque_data()`.
  static ProxyChain WithOpaqueData(std::vector<ProxyServer> proxy_server_list,
                                   int opaque_data) {
    return ProxyChain(std::move(proxy_server_list),
                      /*ip_protection_chain_id=*/kNotIpProtectionChainId,
                      opaque_data);
  }

  // Initialize from a pickle that contains data generated by a call to the
  // `Persist` method.
  //
  // Returns true upon success, otherwise returns false.
  bool InitFromPickle(base::PickleIterator* pickle_iter);

  // Call this method to persist `ProxyChain`. Illegal to call this on an
  // invalid object.
  void Persist(base::Pickle* pickle) const;

  // Get ProxyServer at index in chain. This is not valid for direct or invalid
  // proxy chains.
  const ProxyServer& GetProxyServer(size_t chain_index) const;

  // Get the ProxyServers in this chain. This must not be called on invalid
  // proxy chains. An empty vector is returned for direct proxy chains.
  const std::vector<ProxyServer>& proxy_servers() const;

  // Return the last proxy server in the chain, together with all of the
  // preceding proxies. The chain must have at least one proxy server. If it
  // only has one proxy server, then the resulting chain will be direct.
  std::pair<ProxyChain, const ProxyServer&> SplitLast() const;

  // Return a prefix of this proxy chain, of the given length. This length must
  // be less than or equal to the chain's length.
  ProxyChain Prefix(size_t length) const;

  // Get the first ProxyServer in this chain, which must have at least one
  // server.
  const ProxyServer& First() const;

  // Get the last ProxyServer in this chain, which must have at least one
  // server.
  const ProxyServer& Last() const;

  // Get the ProxyServers in this chain, or `nullopt` if the chain is not valid.
  const std::optional<std::vector<ProxyServer>>& proxy_servers_if_valid()
      const {
    return proxy_server_list_;
  }

  // Returns number of proxy servers in chain.
  size_t length() const {
    return proxy_server_list_.has_value() ? proxy_server_list_.value().size()
                                          : 0;
  }

  // Returns true if this chain contains more than one proxy.
  bool is_multi_proxy() const {
    return proxy_server_list_.has_value()
               ? proxy_server_list_.value().size() > 1
               : false;
  }

  // Returns true if this chain contains exactly one proxy.
  bool is_single_proxy() const {
    return proxy_server_list_.has_value()
               ? proxy_server_list_.value().size() == 1
               : false;
  }

  // Returns true if this is a direct (equivalently, zero-proxy) chain.
  bool is_direct() const {
    return proxy_server_list_.has_value() ? proxy_server_list_.value().empty()
                                          : false;
  }

  template <class Predicate>
  bool AnyProxy(Predicate p) const {
    return proxy_server_list_.has_value() &&
           std::any_of(proxy_server_list_->begin(), proxy_server_list_->end(),
                       p);
  }

  // Determines if HTTP GETs to the last proxy in the chain are allowed,
  // instead of establishing a tunnel with CONNECT. This is no longer supported
  // for QUIC proxy chains and is not currently supported for multi-proxy
  // chains.
  bool is_get_to_proxy_allowed() const {
    return is_single_proxy() && (First().is_http() || First().is_https());
  }

  // Returns true if a proxy server list is available.
  bool IsValid() const { return proxy_server_list_.has_value(); }

  // A negative value for `ip_protection_chain_id()` indicating this is not an
  // IP protection chain. All IP-Protection chain IDs are non-negative.
  static constexpr int kNotIpProtectionChainId = -1;

  // A value for `ip_protection_chain_id()` for IP protection chains for which
  // no other chain ID was specified.
  static constexpr int kDefaultIpProtectionChainId = 0;

  // The largest allowed ip_protection_chain_id.
  // NOTE: Make sure to update the
  // `Net.IpProtection.CanFalloverToNextProxy.Error.{Chain}` histogram when
  // modifying this value.
  static constexpr int kMaxIpProtectionChainId = 3;

  bool is_for_ip_protection() const {
    return ip_protection_chain_id_ != kNotIpProtectionChainId;
  }
  int ip_protection_chain_id() const { return ip_protection_chain_id_; }

  std::optional<int> opaque_data() const { return opaque_data_; }

  friend bool operator==(const ProxyChain&, const ProxyChain&) = default;
  friend auto operator<=>(const ProxyChain&, const ProxyChain&) = default;

  std::string ToDebugString() const;

 private:
  explicit ProxyChain(std::vector<ProxyServer> proxy_server_list,
                      int ip_protection_chain_id,
                      std::optional<int> opaque_data);

  std::optional<std::vector<ProxyServer>> proxy_server_list_;

  // If used for IP protection, this is the chain_id received from the server.
  // A negative value indicates this chain is not used for IP protection.
  int ip_protection_chain_id_ = kNotIpProtectionChainId;

  // Owners of `ProxyChain` can use to store private information. For example,
  // Cronet uses this to map each net::ProxyChain to a specific
  // org.chromium.net.Proxy.Callback, when necessary. This does not get
  // persisted during calls to the `Persist` method.
  // Note: the value of this field does not affect the validity of this
  // ProxyChain.
  std::optional<int> opaque_data_;

  // Returns true if this chain is valid. A chain is considered valid if
  //  (1) it is a single valid proxy server, or
  //  (2) it is a chain of servers, composed of zero or more SCHEME_QUIC servers
  //  followed by zero or more SCHEME_HTTPS servers.
  // If any SCHEME_QUIC servers are included, then the chain must be for IP
  // protection.
  bool IsValidInternal() const;
};

NET_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
                                            const ProxyChain& proxy_chain);

// A HostPortProxyPair holds a host/port destination and a ProxyChain describing
// how that destination is reached.
typedef std::pair<HostPortPair, ProxyChain> HostPortProxyPair;

}  // namespace net

#endif  // NET_BASE_PROXY_CHAIN_H_