Skip to content

Commit

Permalink
Add QUIC client
Browse files Browse the repository at this point in the history
  • Loading branch information
klzgrad committed Nov 15, 2021
1 parent e3a371e commit 1fd28b6
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 180 deletions.
47 changes: 38 additions & 9 deletions src/net/tools/naive/naive_connection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,19 @@
#include "base/callback_helpers.h"
#include "base/logging.h"
#include "base/rand_util.h"
#include "base/strings/strcat.h"
#include "base/threading/thread_task_runner_handle.h"
#include "net/base/io_buffer.h"
#include "net/base/load_flags.h"
#include "net/base/net_errors.h"
#include "net/base/privacy_mode.h"
#include "net/http/http_network_session.h"
#include "net/proxy_resolution/proxy_config.h"
#include "net/proxy_resolution/proxy_info.h"
#include "net/proxy_resolution/proxy_list.h"
#include "net/proxy_resolution/proxy_resolution_service.h"
#include "net/socket/client_socket_handle.h"
#include "net/socket/client_socket_pool_manager.h"
#include "net/socket/stream_socket.h"
#include "net/spdy/spdy_session.h"
#include "net/tools/naive/http_proxy_socket.h"
#include "net/tools/naive/socks5_server_socket.h"

namespace net {

Expand All @@ -38,14 +37,24 @@ constexpr int kMaxPaddingSize = 255;

NaiveConnection::NaiveConnection(
unsigned int id,
Protocol protocol,
Direction pad_direction,
const ProxyInfo& proxy_info,
const SSLConfig& server_ssl_config,
const SSLConfig& proxy_ssl_config,
HttpNetworkSession* session,
const NetLogWithSource& net_log,
std::unique_ptr<StreamSocket> accepted_socket,
Delegate* delegate,
const NetworkTrafficAnnotationTag& traffic_annotation)
: id_(id),
protocol_(protocol),
pad_direction_(pad_direction),
proxy_info_(proxy_info),
server_ssl_config_(server_ssl_config),
proxy_ssl_config_(proxy_ssl_config),
session_(session),
net_log_(net_log),
next_state_(STATE_NONE),
delegate_(delegate),
client_socket_(std::move(accepted_socket)),
server_socket_handle_(std::make_unique<ClientSocketHandle>()),
sockets_{client_socket_.get(), nullptr},
Expand Down Expand Up @@ -167,11 +176,31 @@ int NaiveConnection::DoConnectClientComplete(int result) {
}

int NaiveConnection::DoConnectServer() {
DCHECK(delegate_);
next_state_ = STATE_CONNECT_SERVER_COMPLETE;

return delegate_->OnConnectServer(id_, client_socket_.get(),
server_socket_handle_.get(), io_callback_);
HostPortPair origin;
if (protocol_ == kSocks5) {
const auto* socket =
static_cast<const Socks5ServerSocket*>(client_socket_.get());
origin = socket->request_endpoint();
} else if (protocol_ == kHttp) {
const auto* socket =
static_cast<const HttpProxySocket*>(client_socket_.get());
origin = socket->request_endpoint();
}

if (origin.IsEmpty()) {
LOG(ERROR) << "Connection " << id_ << " to invalid origin";
return ERR_ADDRESS_INVALID;
}

LOG(INFO) << "Connection " << id_ << " to " << origin.ToString();

// Ignores socket limit set by socket pool for this type of socket.
return InitSocketHandleForRawConnect2(
origin, session_, LOAD_IGNORE_LIMITS, MAXIMUM_PRIORITY, proxy_info_,
server_ssl_config_, proxy_ssl_config_, PRIVACY_MODE_DISABLED, net_log_,
server_socket_handle_.get(), io_callback_);
}

int NaiveConnection::DoConnectServerComplete(int result) {
Expand Down
38 changes: 21 additions & 17 deletions src/net/tools/naive/naive_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,23 @@ namespace net {

class ClientSocketHandle;
class DrainableIOBuffer;
class HttpNetworkSession;
class IOBuffer;
class NetLogWithSource;
class ProxyInfo;
class StreamSocket;
struct NetworkTrafficAnnotationTag;
struct SSLConfig;

class NaiveConnection {
public:
using TimeFunc = base::TimeTicks (*)();

enum Protocol {
kSocks5,
kHttp,
};

// From this direction.
enum Direction {
kClient = 0,
Expand All @@ -36,24 +45,15 @@ class NaiveConnection {
kNone = 2,
};

class Delegate {
public:
Delegate() {}
virtual ~Delegate() {}

virtual int OnConnectServer(unsigned int connection_id,
const StreamSocket* accepted_socket,
ClientSocketHandle* server_socket,
CompletionRepeatingCallback callback) = 0;

private:
DISALLOW_COPY_AND_ASSIGN(Delegate);
};

NaiveConnection(unsigned int id,
Protocol protocol,
Direction pad_direction,
const ProxyInfo& proxy_info,
const SSLConfig& server_ssl_config,
const SSLConfig& proxy_ssl_config,
HttpNetworkSession* session,
const NetLogWithSource& net_log,
std::unique_ptr<StreamSocket> accepted_socket,
Delegate* delegate,
const NetworkTrafficAnnotationTag& traffic_annotation);
~NaiveConnection();

Expand Down Expand Up @@ -97,16 +97,20 @@ class NaiveConnection {
void OnPushComplete(Direction from, Direction to, int result);

unsigned int id_;
Protocol protocol_;
Direction pad_direction_;
const ProxyInfo& proxy_info_;
const SSLConfig& server_ssl_config_;
const SSLConfig& proxy_ssl_config_;
HttpNetworkSession* session_;
const NetLogWithSource& net_log_;

CompletionRepeatingCallback io_callback_;
CompletionOnceCallback connect_callback_;
CompletionOnceCallback run_callback_;

State next_state_;

Delegate* delegate_;

std::unique_ptr<StreamSocket> client_socket_;
std::unique_ptr<ClientSocketHandle> server_socket_handle_;

Expand Down
105 changes: 31 additions & 74 deletions src/net/tools/naive/naive_proxy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include "net/http/http_network_session.h"
#include "net/proxy_resolution/configured_proxy_resolution_service.h"
#include "net/proxy_resolution/proxy_config.h"
#include "net/proxy_resolution/proxy_info.h"
#include "net/proxy_resolution/proxy_list.h"
#include "net/socket/client_socket_pool_manager.h"
#include "net/socket/server_socket.h"
Expand All @@ -27,18 +26,32 @@
namespace net {

NaiveProxy::NaiveProxy(std::unique_ptr<ServerSocket> listen_socket,
Protocol protocol,
bool use_proxy,
NaiveConnection::Protocol protocol,
bool use_padding,
HttpNetworkSession* session,
const NetworkTrafficAnnotationTag& traffic_annotation)
: listen_socket_(std::move(listen_socket)),
protocol_(protocol),
use_proxy_(use_proxy),
use_padding_(use_padding),
session_(session),
net_log_(
NetLogWithSource::Make(session->net_log(), NetLogSourceType::NONE)),
last_id_(0),
traffic_annotation_(traffic_annotation) {
const auto& proxy_config = static_cast<ConfiguredProxyResolutionService*>(
session_->proxy_resolution_service())
->config();
DCHECK(proxy_config);
const ProxyList& proxy_list =
proxy_config.value().value().proxy_rules().single_proxies;
DCHECK(!proxy_list.IsEmpty());
proxy_info_.UseProxyList(proxy_list);
proxy_info_.set_traffic_annotation(
net::MutableNetworkTrafficAnnotationTag(traffic_annotation_));

session_->GetSSLConfig(&server_ssl_config_, &proxy_ssl_config_);
proxy_ssl_config_.disable_cert_verification_network_fetches = true;

DCHECK(listen_socket_);
// Start accepting connections in next run loop in case when delegate is not
// ready to get callbacks.
Expand Down Expand Up @@ -78,19 +91,24 @@ void NaiveProxy::HandleAcceptResult(int result) {
void NaiveProxy::DoConnect() {
std::unique_ptr<StreamSocket> socket;
NaiveConnection::Direction pad_direction;
if (protocol_ == kSocks5) {
if (protocol_ == NaiveConnection::kSocks5) {
socket = std::make_unique<Socks5ServerSocket>(std::move(accepted_socket_),
traffic_annotation_);
pad_direction = NaiveConnection::kClient;
} else if (protocol_ == kHttp) {
} else if (protocol_ == NaiveConnection::kHttp) {
socket = std::make_unique<HttpProxySocket>(std::move(accepted_socket_),
traffic_annotation_);
pad_direction = NaiveConnection::kServer;
} else {
return;
}
if (!use_padding_) {
pad_direction = NaiveConnection::kNone;
}
auto connection_ptr = std::make_unique<NaiveConnection>(
++last_id_, pad_direction, std::move(socket), this, traffic_annotation_);
++last_id_, protocol_, pad_direction, proxy_info_, server_ssl_config_,
proxy_ssl_config_, session_, net_log_, std::move(socket),
traffic_annotation_);
auto* connection = connection_ptr.get();
connection_by_id_[connection->id()] = std::move(connection_ptr);
int result = connection->Connect(
Expand All @@ -101,61 +119,8 @@ void NaiveProxy::DoConnect() {
HandleConnectResult(connection, result);
}

int NaiveProxy::OnConnectServer(unsigned int connection_id,
const StreamSocket* client_socket,
ClientSocketHandle* server_socket,
CompletionRepeatingCallback callback) {
// Ignores socket limit set by socket pool for this type of socket.
constexpr int request_load_flags = LOAD_IGNORE_LIMITS;
constexpr RequestPriority request_priority = MAXIMUM_PRIORITY;

ProxyInfo proxy_info;
SSLConfig server_ssl_config;
SSLConfig proxy_ssl_config;

if (use_proxy_) {
const auto& proxy_config = static_cast<ConfiguredProxyResolutionService*>(
session_->proxy_resolution_service())
->config();
DCHECK(proxy_config);
const ProxyList& proxy_list =
proxy_config.value().value().proxy_rules().single_proxies;
if (proxy_list.IsEmpty())
return ERR_MANDATORY_PROXY_CONFIGURATION_FAILED;
proxy_info.UseProxyList(proxy_list);
proxy_info.set_traffic_annotation(
net::MutableNetworkTrafficAnnotationTag(traffic_annotation_));

session_->GetSSLConfig(&server_ssl_config, &proxy_ssl_config);
proxy_ssl_config.disable_cert_verification_network_fetches = true;
} else {
proxy_info.UseDirect();
}

HostPortPair request_endpoint;
if (protocol_ == kSocks5) {
const auto* socket = static_cast<const Socks5ServerSocket*>(client_socket);
request_endpoint = socket->request_endpoint();
} else if (protocol_ == kHttp) {
const auto* socket = static_cast<const HttpProxySocket*>(client_socket);
request_endpoint = socket->request_endpoint();
}
if (request_endpoint.IsEmpty()) {
LOG(ERROR) << "Connection " << connection_id << " to invalid origin";
return ERR_ADDRESS_INVALID;
}

LOG(INFO) << "Connection " << connection_id << " to "
<< request_endpoint.ToString();

return InitSocketHandleForRawConnect2(
request_endpoint, session_, request_load_flags, request_priority,
proxy_info, server_ssl_config, proxy_ssl_config, PRIVACY_MODE_DISABLED,
net_log_, server_socket, callback);
}

void NaiveProxy::OnConnectComplete(int connection_id, int result) {
NaiveConnection* connection = FindConnection(connection_id);
void NaiveProxy::OnConnectComplete(unsigned int connection_id, int result) {
auto* connection = FindConnection(connection_id);
if (!connection)
return;
HandleConnectResult(connection, result);
Expand All @@ -178,8 +143,8 @@ void NaiveProxy::DoRun(NaiveConnection* connection) {
HandleRunResult(connection, result);
}

void NaiveProxy::OnRunComplete(int connection_id, int result) {
NaiveConnection* connection = FindConnection(connection_id);
void NaiveProxy::OnRunComplete(unsigned int connection_id, int result) {
auto* connection = FindConnection(connection_id);
if (!connection)
return;
HandleRunResult(connection, result);
Expand All @@ -189,7 +154,7 @@ void NaiveProxy::HandleRunResult(NaiveConnection* connection, int result) {
Close(connection->id(), result);
}

void NaiveProxy::Close(int connection_id, int reason) {
void NaiveProxy::Close(unsigned int connection_id, int reason) {
auto it = connection_by_id_.find(connection_id);
if (it == connection_by_id_.end())
return;
Expand All @@ -206,19 +171,11 @@ void NaiveProxy::Close(int connection_id, int reason) {
connection_by_id_.erase(it);
}

NaiveConnection* NaiveProxy::FindConnection(int connection_id) {
NaiveConnection* NaiveProxy::FindConnection(unsigned int connection_id) {
auto it = connection_by_id_.find(connection_id);
if (it == connection_by_id_.end())
return nullptr;
return it->second.get();
}

// This is called after any delegate callbacks are called to check if Close()
// has been called during callback processing. Using the pointer of connection,
// |connection| is safe here because Close() deletes the connection in next run
// loop.
bool NaiveProxy::HasClosedConnection(NaiveConnection* connection) {
return FindConnection(connection->id()) != connection;
}

} // namespace net
Loading

0 comments on commit 1fd28b6

Please sign in to comment.