xref: /aosp_15_r20/external/cronet/net/socket/socks_connect_job.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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_SOCKET_SOCKS_CONNECT_JOB_H_
6 #define NET_SOCKET_SOCKS_CONNECT_JOB_H_
7 
8 #include <memory>
9 
10 #include "base/memory/raw_ptr.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/time/time.h"
13 #include "net/base/completion_once_callback.h"
14 #include "net/base/host_port_pair.h"
15 #include "net/base/net_export.h"
16 #include "net/base/network_isolation_key.h"
17 #include "net/base/request_priority.h"
18 #include "net/dns/public/resolve_error_info.h"
19 #include "net/socket/connect_job.h"
20 #include "net/socket/connect_job_params.h"
21 #include "net/socket/socks_client_socket.h"
22 #include "net/traffic_annotation/network_traffic_annotation.h"
23 
24 namespace net {
25 
26 class SocketTag;
27 class StreamSocket;
28 class TransportSocketParams;
29 
30 class NET_EXPORT_PRIVATE SOCKSSocketParams
31     : public base::RefCounted<SOCKSSocketParams> {
32  public:
33   SOCKSSocketParams(ConnectJobParams nested_params,
34                     bool socks_v5,
35                     const HostPortPair& host_port_pair,
36                     const NetworkAnonymizationKey& network_anonymization_key,
37                     const NetworkTrafficAnnotationTag& traffic_annotation);
38 
39   SOCKSSocketParams(const SOCKSSocketParams&) = delete;
40   SOCKSSocketParams& operator=(const SOCKSSocketParams&) = delete;
41 
transport_params()42   const scoped_refptr<TransportSocketParams>& transport_params() const {
43     return transport_params_;
44   }
destination()45   const HostPortPair& destination() const { return destination_; }
is_socks_v5()46   bool is_socks_v5() const { return socks_v5_; }
network_anonymization_key()47   const NetworkAnonymizationKey& network_anonymization_key() {
48     return network_anonymization_key_;
49   }
50 
traffic_annotation()51   const NetworkTrafficAnnotationTag traffic_annotation() {
52     return traffic_annotation_;
53   }
54 
55  private:
56   friend class base::RefCounted<SOCKSSocketParams>;
57   ~SOCKSSocketParams();
58 
59   // The transport (likely TCP) connection must point toward the proxy server.
60   const scoped_refptr<TransportSocketParams> transport_params_;
61   // This is the HTTP destination.
62   const HostPortPair destination_;
63   const bool socks_v5_;
64   const NetworkAnonymizationKey network_anonymization_key_;
65 
66   NetworkTrafficAnnotationTag traffic_annotation_;
67 };
68 
69 // SOCKSConnectJob handles establishing a connection to a SOCKS4 or SOCKS5 proxy
70 // and then sending a handshake to establish a tunnel.
71 class NET_EXPORT_PRIVATE SOCKSConnectJob : public ConnectJob,
72                                            public ConnectJob::Delegate {
73  public:
74   class NET_EXPORT_PRIVATE Factory {
75    public:
76     Factory() = default;
77     virtual ~Factory() = default;
78 
79     virtual std::unique_ptr<SOCKSConnectJob> Create(
80         RequestPriority priority,
81         const SocketTag& socket_tag,
82         const CommonConnectJobParams* common_connect_job_params,
83         scoped_refptr<SOCKSSocketParams> socks_params,
84         ConnectJob::Delegate* delegate,
85         const NetLogWithSource* net_log);
86   };
87 
88   SOCKSConnectJob(RequestPriority priority,
89                   const SocketTag& socket_tag,
90                   const CommonConnectJobParams* common_connect_job_params,
91                   scoped_refptr<SOCKSSocketParams> socks_params,
92                   ConnectJob::Delegate* delegate,
93                   const NetLogWithSource* net_log);
94 
95   SOCKSConnectJob(const SOCKSConnectJob&) = delete;
96   SOCKSConnectJob& operator=(const SOCKSConnectJob&) = delete;
97 
98   ~SOCKSConnectJob() override;
99 
100   // ConnectJob methods.
101   LoadState GetLoadState() const override;
102   bool HasEstablishedConnection() const override;
103   ResolveErrorInfo GetResolveErrorInfo() const override;
104 
105   // Returns the handshake timeout used by SOCKSConnectJobs.
106   static base::TimeDelta HandshakeTimeoutForTesting();
107 
108  private:
109   enum State {
110     STATE_TRANSPORT_CONNECT,
111     STATE_TRANSPORT_CONNECT_COMPLETE,
112     STATE_SOCKS_CONNECT,
113     STATE_SOCKS_CONNECT_COMPLETE,
114     STATE_NONE,
115   };
116 
117   void OnIOComplete(int result);
118 
119   // ConnectJob::Delegate methods.
120   void OnConnectJobComplete(int result, ConnectJob* job) override;
121   void OnNeedsProxyAuth(const HttpResponseInfo& response,
122                         HttpAuthController* auth_controller,
123                         base::OnceClosure restart_with_auth_callback,
124                         ConnectJob* job) override;
125 
126   // Runs the state transition loop.
127   int DoLoop(int result);
128 
129   int DoTransportConnect();
130   int DoTransportConnectComplete(int result);
131   int DoSOCKSConnect();
132   int DoSOCKSConnectComplete(int result);
133 
134   // Begins the transport connection and the SOCKS handshake.  Returns OK on
135   // success and ERR_IO_PENDING if it cannot immediately service the request.
136   // Otherwise, it returns a net error code.
137   int ConnectInternal() override;
138 
139   void ChangePriorityInternal(RequestPriority priority) override;
140 
141   scoped_refptr<SOCKSSocketParams> socks_params_;
142 
143   State next_state_;
144   std::unique_ptr<ConnectJob> transport_connect_job_;
145   std::unique_ptr<StreamSocket> socket_;
146   raw_ptr<SOCKSClientSocket> socks_socket_ptr_;
147 
148   ResolveErrorInfo resolve_error_info_;
149 };
150 
151 }  // namespace net
152 
153 #endif  // NET_SOCKET_SOCKS_CONNECT_JOB_H_
154