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