Skip to content

Commit

Permalink
Add option for concurrent transport
Browse files Browse the repository at this point in the history
Useful only for high-end situations where a single connection
is throttled at 10MB/s somehow.

Makes traffic obfuscation less secure.
  • Loading branch information
klzgrad committed May 21, 2020
1 parent a60d168 commit 82ee107
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 6 deletions.
7 changes: 5 additions & 2 deletions src/net/tools/naive/naive_connection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ NaiveConnection::NaiveConnection(
const SSLConfig& proxy_ssl_config,
RedirectResolver* resolver,
HttpNetworkSession* session,
const NetworkIsolationKey& network_isolation_key,
const NetLogWithSource& net_log,
std::unique_ptr<StreamSocket> accepted_socket,
const NetworkTrafficAnnotationTag& traffic_annotation)
Expand All @@ -66,6 +67,7 @@ NaiveConnection::NaiveConnection(
proxy_ssl_config_(proxy_ssl_config),
resolver_(resolver),
session_(session),
network_isolation_key_(network_isolation_key),
net_log_(net_log),
next_state_(STATE_NONE),
client_socket_(std::move(accepted_socket)),
Expand Down Expand Up @@ -239,8 +241,9 @@ int NaiveConnection::DoConnectServer() {
// 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_);
server_ssl_config_, proxy_ssl_config_, PRIVACY_MODE_DISABLED,
network_isolation_key_, net_log_, server_socket_handle_.get(),
io_callback_);
}

int NaiveConnection::DoConnectServerComplete(int result) {
Expand Down
3 changes: 3 additions & 0 deletions src/net/tools/naive/naive_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class StreamSocket;
struct NetworkTrafficAnnotationTag;
struct SSLConfig;
class RedirectResolver;
class NetworkIsolationKey;

class NaiveConnection {
public:
Expand Down Expand Up @@ -55,6 +56,7 @@ class NaiveConnection {
const SSLConfig& proxy_ssl_config,
RedirectResolver* resolver,
HttpNetworkSession* session,
const NetworkIsolationKey& network_isolation_key,
const NetLogWithSource& net_log,
std::unique_ptr<StreamSocket> accepted_socket,
const NetworkTrafficAnnotationTag& traffic_annotation);
Expand Down Expand Up @@ -107,6 +109,7 @@ class NaiveConnection {
const SSLConfig& proxy_ssl_config_;
RedirectResolver* resolver_;
HttpNetworkSession* session_;
const NetworkIsolationKey& network_isolation_key_;
const NetLogWithSource& net_log_;

CompletionRepeatingCallback io_callback_;
Expand Down
13 changes: 11 additions & 2 deletions src/net/tools/naive/naive_proxy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "net/tools/naive/naive_proxy.h"

#include <algorithm>
#include <utility>

#include "base/bind.h"
Expand All @@ -28,12 +29,14 @@ namespace net {
NaiveProxy::NaiveProxy(std::unique_ptr<ServerSocket> listen_socket,
NaiveConnection::Protocol protocol,
bool use_padding,
int concurrency,
RedirectResolver* resolver,
HttpNetworkSession* session,
const NetworkTrafficAnnotationTag& traffic_annotation)
: listen_socket_(std::move(listen_socket)),
protocol_(protocol),
use_padding_(use_padding),
concurrency_(std::min(4, std::max(1, concurrency))),
resolver_(resolver),
session_(session),
net_log_(
Expand All @@ -54,6 +57,10 @@ NaiveProxy::NaiveProxy(std::unique_ptr<ServerSocket> listen_socket,
session_->GetSSLConfig(&server_ssl_config_, &proxy_ssl_config_);
proxy_ssl_config_.disable_cert_verification_network_fetches = true;

for (int i = 0; i < concurrency_; i++) {
network_isolation_keys_.push_back(NetworkIsolationKey::CreateTransient());
}

DCHECK(listen_socket_);
// Start accepting connections in next run loop in case when delegate is not
// ready to get callbacks.
Expand Down Expand Up @@ -110,9 +117,11 @@ void NaiveProxy::DoConnect() {
if (!use_padding_) {
pad_direction = NaiveConnection::kNone;
}
last_id_++;
const auto& nik = network_isolation_keys_[last_id_ % concurrency_];
auto connection_ptr = std::make_unique<NaiveConnection>(
++last_id_, protocol_, pad_direction, proxy_info_, server_ssl_config_,
proxy_ssl_config_, resolver_, session_, net_log_, std::move(socket),
last_id_, protocol_, pad_direction, proxy_info_, server_ssl_config_,
proxy_ssl_config_, resolver_, session_, nik, net_log_, std::move(socket),
traffic_annotation_);
auto* connection = connection_ptr.get();
connection_by_id_[connection->id()] = std::move(connection_ptr);
Expand Down
6 changes: 6 additions & 0 deletions src/net/tools/naive/naive_proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@

#include <map>
#include <memory>
#include <vector>

#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "net/base/completion_repeating_callback.h"
#include "net/base/network_isolation_key.h"
#include "net/log/net_log_with_source.h"
#include "net/proxy_resolution/proxy_info.h"
#include "net/ssl/ssl_config.h"
Expand All @@ -32,6 +34,7 @@ class NaiveProxy {
NaiveProxy(std::unique_ptr<ServerSocket> server_socket,
NaiveConnection::Protocol protocol,
bool use_padding,
int concurrency,
RedirectResolver* resolver,
HttpNetworkSession* session,
const NetworkTrafficAnnotationTag& traffic_annotation);
Expand All @@ -57,6 +60,7 @@ class NaiveProxy {
std::unique_ptr<ServerSocket> listen_socket_;
NaiveConnection::Protocol protocol_;
bool use_padding_;
int concurrency_;
ProxyInfo proxy_info_;
SSLConfig server_ssl_config_;
SSLConfig proxy_ssl_config_;
Expand All @@ -68,6 +72,8 @@ class NaiveProxy {

std::unique_ptr<StreamSocket> accepted_socket_;

std::vector<NetworkIsolationKey> network_isolation_keys_;

std::map<unsigned int, std::unique_ptr<NaiveConnection>> connection_by_id_;

const NetworkTrafficAnnotationTag& traffic_annotation_;
Expand Down
25 changes: 23 additions & 2 deletions src/net/tools/naive/naive_proxy_bin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "base/at_exit.h"
#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/files/file_path.h"
#include "base/json/json_file_value_serializer.h"
#include "base/json/json_writer.h"
Expand Down Expand Up @@ -80,6 +81,7 @@ struct CommandLine {
std::string listen;
std::string proxy;
bool padding;
std::string concurrency;
std::string extra_headers;
std::string host_resolver_rules;
std::string resolver_range;
Expand All @@ -94,6 +96,7 @@ struct Params {
std::string listen_addr;
int listen_port;
bool use_padding;
int concurrency;
net::HttpRequestHeaders extra_headers;
std::string proxy_url;
std::string proxy_user;
Expand Down Expand Up @@ -141,6 +144,7 @@ void GetCommandLine(const base::CommandLine& proc, CommandLine* cmdline) {
"--proxy=<proto>://[<user>:<pass>@]<hostname>[:<port>]\n"
" proto: https, quic\n"
"--padding Use padding\n"
"--concurrency=<N> Use N connections, less secure\n"
"--extra-headers=... Extra headers split by CRLF\n"
"--host-resolver-rules=... Resolver rules\n"
"--resolver-range=... Redirect resolver range\n"
Expand All @@ -159,6 +163,7 @@ void GetCommandLine(const base::CommandLine& proc, CommandLine* cmdline) {
cmdline->listen = proc.GetSwitchValueASCII("listen");
cmdline->proxy = proc.GetSwitchValueASCII("proxy");
cmdline->padding = proc.HasSwitch("padding");
cmdline->concurrency = proc.GetSwitchValueASCII("concurrency");
cmdline->extra_headers = proc.GetSwitchValueASCII("extra-headers");
cmdline->host_resolver_rules =
proc.GetSwitchValueASCII("host-resolver-rules");
Expand Down Expand Up @@ -193,6 +198,10 @@ void GetCommandLineFromConfig(const base::FilePath& config_path,
cmdline->proxy = *proxy;
}
cmdline->padding = value->FindBoolKey("padding").value_or(false);
const auto* concurrency = value->FindStringKey("concurrency");
if (concurrency) {
cmdline->concurrency = *concurrency;
}
const auto* extra_headers = value->FindStringKey("extra_headers");
if (extra_headers) {
cmdline->extra_headers = *extra_headers;
Expand Down Expand Up @@ -292,6 +301,16 @@ bool ParseCommandLine(const CommandLine& cmdline, Params* params) {

params->use_padding = cmdline.padding;

if (!cmdline.concurrency.empty()) {
if (!base::StringToInt(cmdline.concurrency, &params->concurrency) ||
params->concurrency < 1 || params->concurrency > 4) {
std::cerr << "Invalid concurrency" << std::endl;
return false;
}
} else {
params->concurrency = 1;
}

params->extra_headers.AddHeadersFromString(cmdline.extra_headers);

params->host_resolver_rules = cmdline.host_resolver_rules;
Expand Down Expand Up @@ -475,6 +494,8 @@ std::unique_ptr<URLRequestContext> BuildURLRequestContext(
} // namespace net

int main(int argc, char* argv[]) {
base::FeatureList::InitializeInstance(
"PartitionConnectionsByNetworkIsolationKey", std::string());
base::SingleThreadTaskExecutor io_task_executor(base::MessagePumpType::IO);
base::ThreadPoolInstance::CreateAndStartWithDefaultParams("naive");
base::AtExitManager exit_manager;
Expand Down Expand Up @@ -589,8 +610,8 @@ int main(int argc, char* argv[]) {
}

net::NaiveProxy naive_proxy(std::move(listen_socket), params.protocol,
params.use_padding, resolver.get(), session,
kTrafficAnnotation);
params.use_padding, params.concurrency,
resolver.get(), session, kTrafficAnnotation);

base::RunLoop().Run();

Expand Down

4 comments on commit 82ee107

@nutinshell
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that possible to exceed MaxConnectionsPerProxy=99? This limitation is too low for performance gateway.

@klzgrad
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MaxConnectionsPerProxy=99 is a limit in Chrome.exe. Naiveproxy's default is 2048.

@nutinshell
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I misunderstood the Parameter-Tuning... I did find several network stalls when connections reaching 100+, P.S I am using it as redir gateway in Linux.

@klzgrad
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should discuss this in an issue. Here is not the place for it.

Please sign in to comment.