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
|
// Copyright 2012 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_SOCKET_SOCKS_CONNECT_JOB_H_
#define NET_SOCKET_SOCKS_CONNECT_JOB_H_
#include <memory>
#include <set>
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/time/time.h"
#include "net/base/completion_once_callback.h"
#include "net/base/host_port_pair.h"
#include "net/base/net_errors.h"
#include "net/base/net_export.h"
#include "net/base/network_isolation_key.h"
#include "net/base/request_priority.h"
#include "net/dns/public/resolve_error_info.h"
#include "net/socket/connect_job.h"
#include "net/socket/connect_job_params.h"
#include "net/socket/socks_client_socket.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
namespace net {
class SocketTag;
class StreamSocket;
class TransportSocketParams;
class NET_EXPORT_PRIVATE SOCKSSocketParams
: public base::RefCounted<SOCKSSocketParams> {
public:
SOCKSSocketParams(ConnectJobParams nested_params,
bool socks_v5,
const HostPortPair& host_port_pair,
const NetworkAnonymizationKey& network_anonymization_key,
const NetworkTrafficAnnotationTag& traffic_annotation);
SOCKSSocketParams(const SOCKSSocketParams&) = delete;
SOCKSSocketParams& operator=(const SOCKSSocketParams&) = delete;
const scoped_refptr<TransportSocketParams>& transport_params() const {
return transport_params_;
}
const HostPortPair& destination() const { return destination_; }
bool is_socks_v5() const { return socks_v5_; }
const NetworkAnonymizationKey& network_anonymization_key() {
return network_anonymization_key_;
}
const NetworkTrafficAnnotationTag traffic_annotation() {
return traffic_annotation_;
}
private:
friend class base::RefCounted<SOCKSSocketParams>;
~SOCKSSocketParams();
// The transport (likely TCP) connection must point toward the proxy server.
const scoped_refptr<TransportSocketParams> transport_params_;
// This is the HTTP destination.
const HostPortPair destination_;
const bool socks_v5_;
const NetworkAnonymizationKey network_anonymization_key_;
NetworkTrafficAnnotationTag traffic_annotation_;
};
// SOCKSConnectJob handles establishing a connection to a SOCKS4 or SOCKS5 proxy
// and then sending a handshake to establish a tunnel.
class NET_EXPORT_PRIVATE SOCKSConnectJob : public ConnectJob,
public ConnectJob::Delegate {
public:
class NET_EXPORT_PRIVATE Factory {
public:
Factory() = default;
virtual ~Factory() = default;
virtual std::unique_ptr<SOCKSConnectJob> Create(
RequestPriority priority,
const SocketTag& socket_tag,
const CommonConnectJobParams* common_connect_job_params,
scoped_refptr<SOCKSSocketParams> socks_params,
ConnectJob::Delegate* delegate,
const NetLogWithSource* net_log);
};
SOCKSConnectJob(RequestPriority priority,
const SocketTag& socket_tag,
const CommonConnectJobParams* common_connect_job_params,
scoped_refptr<SOCKSSocketParams> socks_params,
ConnectJob::Delegate* delegate,
const NetLogWithSource* net_log);
SOCKSConnectJob(const SOCKSConnectJob&) = delete;
SOCKSConnectJob& operator=(const SOCKSConnectJob&) = delete;
~SOCKSConnectJob() override;
// ConnectJob methods.
LoadState GetLoadState() const override;
bool HasEstablishedConnection() const override;
ResolveErrorInfo GetResolveErrorInfo() const override;
// Returns the handshake timeout used by SOCKSConnectJobs.
static base::TimeDelta HandshakeTimeoutForTesting();
private:
enum State {
STATE_TRANSPORT_CONNECT,
STATE_TRANSPORT_CONNECT_COMPLETE,
STATE_SOCKS_CONNECT,
STATE_SOCKS_CONNECT_COMPLETE,
STATE_NONE,
};
void OnIOComplete(int result);
// ConnectJob::Delegate methods.
void OnConnectJobComplete(int result, ConnectJob* job) override;
void OnNeedsProxyAuth(const HttpResponseInfo& response,
HttpAuthController* auth_controller,
base::OnceClosure restart_with_auth_callback,
ConnectJob* job) override;
Error OnDestinationDnsAliasesResolved(const std::set<std::string>& aliases,
ConnectJob* job) override;
// Runs the state transition loop.
int DoLoop(int result);
int DoTransportConnect();
int DoTransportConnectComplete(int result);
int DoSOCKSConnect();
int DoSOCKSConnectComplete(int result);
// Begins the transport connection and the SOCKS handshake. Returns OK on
// success and ERR_IO_PENDING if it cannot immediately service the request.
// Otherwise, it returns a net error code.
int ConnectInternal() override;
void ChangePriorityInternal(RequestPriority priority) override;
scoped_refptr<SOCKSSocketParams> socks_params_;
State next_state_;
std::unique_ptr<ConnectJob> transport_connect_job_;
std::unique_ptr<StreamSocket> socket_;
raw_ptr<SOCKSClientSocket> socks_socket_ptr_;
ResolveErrorInfo resolve_error_info_;
};
} // namespace net
#endif // NET_SOCKET_SOCKS_CONNECT_JOB_H_
|