Skip to content

Commit

Permalink
net/proxy:interface move to trunk
Browse files Browse the repository at this point in the history
iceboy233 committed Nov 21, 2024
1 parent a631019 commit 237f78f
Showing 44 changed files with 166 additions and 483 deletions.
2 changes: 1 addition & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
@@ -47,6 +47,6 @@ boost_deps()

git_repository(
name = "org_iceboy_trunk",
commit = "24fc6171c1512293423d9c2fb093ac2bec86f728",
commit = "ee7aa17695b5b8e65171b23fba3a13a4c6632d64",
remote = "https://github.com/iceboy233/trunk.git",
)
19 changes: 2 additions & 17 deletions net/proxy/BUILD
Original file line number Diff line number Diff line change
@@ -1,32 +1,17 @@
package(default_visibility = ["//visibility:public"])

cc_library(
name = "interface",
hdrs = [
"connector.h",
"datagram.h",
"handler.h",
"stream.h",
],
deps = [
"@com_google_absl//absl/functional:any_invocable",
"@com_google_absl//absl/types:span",
"@org_iceboy_trunk//net:asio",
]
)

cc_library(
name = "proxy",
srcs = ["proxy.cc"],
hdrs = ["proxy.h"],
deps = [
":interface",
":registry",
"//net/proxy/system:listener",
"@com_google_absl//absl/container:flat_hash_map",
"@org_boost_boost//:property_tree",
"@org_iceboy_trunk//base:logging",
"@org_iceboy_trunk//net:asio",
"@org_iceboy_trunk//net/interface",
],
)

@@ -35,11 +20,11 @@ cc_library(
srcs = ["registry.cc"],
hdrs = ["registry.h"],
deps = [
":interface",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/functional:any_invocable",
"@org_boost_boost//:property_tree",
"@org_iceboy_trunk//base:logging",
"@org_iceboy_trunk//net:asio",
"@org_iceboy_trunk//net/interface",
],
)
4 changes: 2 additions & 2 deletions net/proxy/ares/BUILD
Original file line number Diff line number Diff line change
@@ -14,7 +14,6 @@ cc_library(
deps = [
":error-category",
":socket",
"//net/proxy:interface",
"//third_party/cares",
"//util:int-allocator",
"@com_google_absl//absl/container:flat_hash_map",
@@ -23,6 +22,7 @@ cc_library(
"@org_iceboy_trunk//net:asio",
"@org_iceboy_trunk//net:endpoint",
"@org_iceboy_trunk//net:timer-list",
"@org_iceboy_trunk//net/interface",
],
)

@@ -31,10 +31,10 @@ cc_library(
srcs = ["socket.cc"],
hdrs = ["socket.h"],
deps = [
"//net/proxy:interface",
"//net/proxy/util:write",
"//third_party/cares",
"@org_boost_boost//:smart_ptr",
"@org_iceboy_trunk//net:asio",
"@org_iceboy_trunk//net/interface",
],
)
6 changes: 3 additions & 3 deletions net/proxy/ares/resolver.h
Original file line number Diff line number Diff line change
@@ -12,9 +12,9 @@
#include "absl/types/span.h"
#include "net/asio.h"
#include "net/endpoint.h"
#include "net/proxy/ares/socket.h"
#include "net/proxy/connector.h"
#include "net/timer-list.h"
#include "net/interface/connector.h"
#include "net/proxy/ares/socket.h"
#include "util/int-allocator.h"

namespace net {
@@ -64,7 +64,7 @@ class Resolver {
static const ares_socket_functions funcs_;

any_io_executor executor_;
proxy::Connector &connector_;
Connector &connector_;
ares_channel channel_ = nullptr;
steady_timer wait_timer_;
TimerList cache_timer_list_;
21 changes: 5 additions & 16 deletions net/proxy/ares/socket.cc
Original file line number Diff line number Diff line change
@@ -100,13 +100,7 @@ int TcpSocket::connect(const sockaddr *addr, ares_socklen_t addr_len) {
}
ares_process_fd(socket->channel_, socket->fd_, socket->fd_);
};
if (address.is_v4()) {
connector_.connect_tcp_v4(
address.to_v4(), port, {}, std::move(callback));
} else {
connector_.connect_tcp_v6(
address.to_v6(), port, {}, std::move(callback));
}
connector_.connect({address, port}, {}, std::move(callback));
SET_ERRNO(EINPROGRESS);
return -1;
}
@@ -198,16 +192,11 @@ int UdpSocket::connect(const sockaddr *addr, ares_socklen_t addr_len) {
SET_ERRNO(EINVAL);
return -1;
}
if (address.is_v4()) {
if (connector_.bind_udp_v4(datagram_)) {
return -1;
}
} else {
if (connector_.bind_udp_v6(datagram_)) {
return -1;
}
udp::endpoint endpoint(address, port);
if (connector_.bind({endpoint.protocol(), 0}, datagram_)) {
return -1;
}
send_endpoint_ = udp::endpoint(address, port);
send_endpoint_ = endpoint;
post(executor_, [socket = boost::intrusive_ptr<UdpSocket>(this)]() {
ares_process_fd(socket->channel_, socket->fd_, socket->fd_);
});
6 changes: 3 additions & 3 deletions net/proxy/ares/socket.h
Original file line number Diff line number Diff line change
@@ -8,9 +8,9 @@
#include <boost/smart_ptr/intrusive_ref_counter.hpp>

#include "net/asio.h"
#include "net/proxy/connector.h"
#include "net/proxy/datagram.h"
#include "net/proxy/stream.h"
#include "net/interface/connector.h"
#include "net/interface/datagram.h"
#include "net/interface/stream.h"

namespace net {
namespace proxy {
52 changes: 0 additions & 52 deletions net/proxy/connector.h

This file was deleted.

34 changes: 0 additions & 34 deletions net/proxy/datagram.h

This file was deleted.

23 changes: 0 additions & 23 deletions net/proxy/handler.h

This file was deleted.

4 changes: 2 additions & 2 deletions net/proxy/http/BUILD
Original file line number Diff line number Diff line change
@@ -3,14 +3,14 @@ cc_library(
srcs = ["h2-connection.cc"],
hdrs = ["h2-connection.h"],
deps = [
"//net/proxy:interface",
"//net/proxy/util:write",
"//third_party/nghttp2",
"@com_google_absl//absl/container:fixed_array",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/functional:any_invocable",
"@com_google_absl//absl/types:span",
"@org_iceboy_trunk//base:logging",
"@org_iceboy_trunk//net/interface",
"@org_iceboy_trunk//util:strings",
],
)
@@ -20,9 +20,9 @@ cc_library(
srcs = ["tls-stream.cc"],
hdrs = ["tls-stream.h"],
deps = [
"//net/proxy:interface",
"//net/proxy/util:stream-wrapper",
"@boringssl//:ssl",
"@com_google_absl//absl/container:fixed_array",
"@org_iceboy_trunk//net/interface",
],
)
2 changes: 1 addition & 1 deletion net/proxy/http/h2-connection.h
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@
#include "absl/container/flat_hash_map.h"
#include "absl/functional/any_invocable.h"
#include "absl/types/span.h"
#include "net/proxy/stream.h"
#include "net/interface/stream.h"

namespace net {
namespace proxy {
2 changes: 1 addition & 1 deletion net/proxy/http/tls-stream.h
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
#define _NET_PROXY_HTTP_TLS_STREAM_H

#include "boost/asio/ssl.hpp"
#include "net/proxy/stream.h"
#include "net/interface/stream.h"
#include "net/proxy/util/stream-wrapper.h"

namespace net {
8 changes: 4 additions & 4 deletions net/proxy/misc/BUILD
Original file line number Diff line number Diff line change
@@ -18,9 +18,9 @@ cc_library(
srcs = ["echo-handler.cc"],
hdrs = ["echo-handler.h"],
deps = [
"//net/proxy:interface",
"//net/proxy/util:write",
"@com_google_absl//absl/container:fixed_array",
"@org_iceboy_trunk//net/interface",
],
)

@@ -29,8 +29,8 @@ cc_library(
srcs = ["null-handler.cc"],
hdrs = ["null-handler.h"],
deps = [
"//net/proxy:interface",
"@com_google_absl//absl/container:fixed_array",
"@org_iceboy_trunk//net/interface",
],
)

@@ -39,9 +39,9 @@ cc_library(
srcs = ["random-handler.cc"],
hdrs = ["random-handler.h"],
deps = [
"//net/proxy:interface",
"@boringssl//:crypto",
"@com_google_absl//absl/container:fixed_array",
"@org_iceboy_trunk//net/interface",
],
)

@@ -50,7 +50,7 @@ cc_library(
srcs = ["zero-handler.cc"],
hdrs = ["zero-handler.h"],
deps = [
"//net/proxy:interface",
"@com_google_absl//absl/container:fixed_array",
"@org_iceboy_trunk//net/interface",
],
)
2 changes: 1 addition & 1 deletion net/proxy/misc/echo-handler.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef _NET_PROXY_MISC_ECHO_HANDLER_H
#define _NET_PROXY_MISC_ECHO_HANDLER_H

#include "net/proxy/handler.h"
#include "net/interface/handler.h"

namespace net {
namespace proxy {
2 changes: 1 addition & 1 deletion net/proxy/misc/null-handler.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef _NET_PROXY_MISC_NULL_HANDLER_H
#define _NET_PROXY_MISC_NULL_HANDLER_H

#include "net/proxy/handler.h"
#include "net/interface/handler.h"

namespace net {
namespace proxy {
2 changes: 1 addition & 1 deletion net/proxy/misc/random-handler.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef _NET_PROXY_MISC_RANDOM_HANDLER_H
#define _NET_PROXY_MISC_RANDOM_HANDLER_H

#include "net/proxy/handler.h"
#include "net/interface/handler.h"

namespace net {
namespace proxy {
2 changes: 1 addition & 1 deletion net/proxy/misc/zero-handler.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef _NET_PROXY_MISC_ZERO_HANDLER_H
#define _NET_PROXY_MISC_ZERO_HANDLER_H

#include "net/proxy/handler.h"
#include "net/interface/handler.h"

namespace net {
namespace proxy {
4 changes: 2 additions & 2 deletions net/proxy/proxy.h
Original file line number Diff line number Diff line change
@@ -9,8 +9,8 @@

#include "absl/container/flat_hash_map.h"
#include "net/asio.h"
#include "net/proxy/connector.h"
#include "net/proxy/handler.h"
#include "net/interface/connector.h"
#include "net/interface/handler.h"
#include "net/proxy/system/listener.h"

namespace net {
4 changes: 2 additions & 2 deletions net/proxy/registry.h
Original file line number Diff line number Diff line change
@@ -8,8 +8,8 @@
#include "absl/container/flat_hash_map.h"
#include "absl/functional/any_invocable.h"
#include "net/asio.h"
#include "net/proxy/connector.h"
#include "net/proxy/handler.h"
#include "net/interface/connector.h"
#include "net/interface/handler.h"

namespace net {
namespace proxy {
2 changes: 1 addition & 1 deletion net/proxy/route/BUILD
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ cc_library(
hdrs = ["connector.h"],
deps = [
":host-matcher",
"//net/proxy:interface",
"@org_iceboy_trunk//net/interface",
],
)

35 changes: 7 additions & 28 deletions net/proxy/route/connector.cc
Original file line number Diff line number Diff line change
@@ -22,25 +22,8 @@ Connector::Connector(absl::Span<Rule const> rules) {
host_matcher_.build();
}

void Connector::connect_tcp_v4(
const address_v4 &address,
uint16_t port,
const_buffer initial_data,
absl::AnyInvocable<void(
std::error_code, std::unique_ptr<Stream>) &&> callback) {
// TODO
auto *connector = default_connector_;
if (!connector) {
std::move(callback)(
make_error_code(std::errc::network_unreachable), nullptr);
return;
}
connector->connect_tcp_v4(address, port, initial_data, std::move(callback));
}

void Connector::connect_tcp_v6(
const address_v6 &address,
uint16_t port,
void Connector::connect(
const tcp::endpoint &endpoint,
const_buffer initial_data,
absl::AnyInvocable<void(
std::error_code, std::unique_ptr<Stream>) &&> callback) {
@@ -51,10 +34,10 @@ void Connector::connect_tcp_v6(
make_error_code(std::errc::network_unreachable), nullptr);
return;
}
connector->connect_tcp_v6(address, port, initial_data, std::move(callback));
connector->connect(endpoint, initial_data, std::move(callback));
}

void Connector::connect_tcp_host(
void Connector::connect(
std::string_view host,
uint16_t port,
const_buffer initial_data,
@@ -67,15 +50,11 @@ void Connector::connect_tcp_host(
make_error_code(std::errc::network_unreachable), nullptr);
return;
}
connector->connect_tcp_host(host, port, initial_data, std::move(callback));
}

std::error_code Connector::bind_udp_v4(std::unique_ptr<Datagram> &datagram) {
// TODO
return make_error_code(std::errc::operation_not_supported);
connector->connect(host, port, initial_data, std::move(callback));
}

std::error_code Connector::bind_udp_v6(std::unique_ptr<Datagram> &datagram) {
std::error_code Connector::bind(
const udp::endpoint &endpoint, std::unique_ptr<Datagram> &datagram) {
// TODO
return make_error_code(std::errc::operation_not_supported);
}
30 changes: 10 additions & 20 deletions net/proxy/route/connector.h
Original file line number Diff line number Diff line change
@@ -3,55 +3,45 @@

#include <vector>

#include "net/proxy/connector.h"
#include "net/interface/connector.h"
#include "net/proxy/route/host-matcher.h"

namespace net {
namespace proxy {
namespace route {

class Connector : public proxy::Connector {
class Connector : public net::Connector {
public:
struct Rule {
std::vector<std::string> hosts;
std::vector<std::string> host_suffixes;
bool is_default = false;
proxy::Connector *connector = nullptr;
net::Connector *connector = nullptr;
};

explicit Connector(absl::Span<Rule const> rules);

void connect_tcp_v4(
const address_v4 &address,
uint16_t port,
const_buffer initial_data,
absl::AnyInvocable<void(
std::error_code, std::unique_ptr<Stream>) &&> callback) override;

void connect_tcp_v6(
const address_v6 &address,
uint16_t port,
void connect(
const tcp::endpoint &endpoint,
const_buffer initial_data,
absl::AnyInvocable<void(
std::error_code, std::unique_ptr<Stream>) &&> callback) override;

void connect_tcp_host(
void connect(
std::string_view host,
uint16_t port,
const_buffer initial_data,
absl::AnyInvocable<void(
std::error_code, std::unique_ptr<Stream>) &&> callback) override;

std::error_code bind_udp_v4(
std::unique_ptr<Datagram> &datagram) override;

std::error_code bind_udp_v6(
std::error_code bind(
const udp::endpoint &endpoint,
std::unique_ptr<Datagram> &datagram) override;

private:
HostMatcher host_matcher_;
std::vector<proxy::Connector *> connectors_;
proxy::Connector *default_connector_ = nullptr;
std::vector<net::Connector *> connectors_;
net::Connector *default_connector_ = nullptr;
};

} // namespace route
4 changes: 2 additions & 2 deletions net/proxy/shadowsocks/BUILD
Original file line number Diff line number Diff line change
@@ -22,11 +22,11 @@ cc_library(
":encryptor",
":pre-shared-key",
":salt-filter",
"//net/proxy:interface",
"//net/proxy/util:write",
"@com_google_absl//absl/random",
"@org_iceboy_trunk//base:logging",
"@org_iceboy_trunk//net:endpoint",
"@org_iceboy_trunk//net/interface",
],
)

@@ -65,10 +65,10 @@ cc_library(
":pre-shared-key",
":salt-filter",
":session-subkey",
"//net/proxy:interface",
"//net/proxy/util:write",
"@com_google_absl//absl/container:fixed_array",
"@org_iceboy_trunk//base:logging",
"@org_iceboy_trunk//net/interface",
],
)

4 changes: 2 additions & 2 deletions net/proxy/shadowsocks/config.cc
Original file line number Diff line number Diff line change
@@ -64,7 +64,7 @@ REGISTER_HANDLER(shadowsocks, [](
return nullptr;
}
options.password = config.password;
proxy::Connector *connector = proxy.get_connector(config.connector);
net::Connector *connector = proxy.get_connector(config.connector);
if (!connector) {
LOG(error) << "invalid connector: " << config.connector;
return nullptr;
@@ -97,7 +97,7 @@ REGISTER_CONNECTOR(shadowsocks, [](
options.password = config.password;
options.min_padding_length = config.min_padding_length;
options.max_padding_length = config.max_padding_length;
proxy::Connector *base_connector = proxy.get_connector(config.connector);
net::Connector *base_connector = proxy.get_connector(config.connector);
if (!base_connector) {
LOG(error) << "invalid connector: " << config.connector;
return nullptr;
143 changes: 35 additions & 108 deletions net/proxy/shadowsocks/connector.cc
Original file line number Diff line number Diff line change
@@ -5,28 +5,21 @@
#include <utility>

#include "base/logging.h"
#include "net/interface/stream.h"
#include "net/proxy/shadowsocks/decryptor.h"
#include "net/proxy/shadowsocks/encryptor.h"
#include "net/proxy/stream.h"
#include "net/proxy/util/write.h"

namespace net {
namespace proxy {
namespace shadowsocks {

class Connector::TcpStream : public proxy::Stream {
class Connector::TcpStream : public net::Stream {
public:
explicit TcpStream(Connector &connector);

void start(
const net::address_v4 &address,
uint16_t port,
const_buffer initial_data,
absl::AnyInvocable<void(std::error_code) &&> callback);

void start(
const net::address_v6 &address,
uint16_t port,
const tcp::endpoint &endpoint,
const_buffer initial_data,
absl::AnyInvocable<void(std::error_code) &&> callback);

@@ -84,16 +77,14 @@ bool Connector::init(const InitOptions &options) {
return true;
}

void Connector::connect_tcp_v4(
const address_v4 &address,
uint16_t port,
void Connector::connect(
const tcp::endpoint &endpoint,
const_buffer initial_data,
absl::AnyInvocable<void(
std::error_code, std::unique_ptr<Stream>) &&> callback) {
auto stream = std::make_unique<TcpStream>(*this);
stream->start(
address,
port,
endpoint,
initial_data,
[stream = std::move(stream), callback = std::move(callback)](
std::error_code ec) mutable {
@@ -105,28 +96,7 @@ void Connector::connect_tcp_v4(
});
}

void Connector::connect_tcp_v6(
const address_v6 &address,
uint16_t port,
const_buffer initial_data,
absl::AnyInvocable<void(
std::error_code, std::unique_ptr<Stream>) &&> callback) {
auto stream = std::make_unique<TcpStream>(*this);
stream->start(
address,
port,
initial_data,
[stream = std::move(stream), callback = std::move(callback)](
std::error_code ec) mutable {
if (ec) {
std::move(callback)(ec, nullptr);
return;
}
std::move(callback)({}, std::move(stream));
});
}

void Connector::connect_tcp_host(
void Connector::connect(
std::string_view host,
uint16_t port,
const_buffer initial_data,
@@ -147,12 +117,9 @@ void Connector::connect_tcp_host(
});
}

std::error_code Connector::bind_udp_v4(std::unique_ptr<Datagram> &datagram) {
// TODO
return make_error_code(std::errc::operation_not_supported);
}

std::error_code Connector::bind_udp_v6(std::unique_ptr<Datagram> &datagram) {
std::error_code Connector::bind(
const udp::endpoint &endpoint,
std::unique_ptr<Datagram> &datagram) {
// TODO
return make_error_code(std::errc::operation_not_supported);
}
@@ -161,15 +128,16 @@ Connector::TcpStream::TcpStream(Connector &connector)
: connector_(connector) {}

void Connector::TcpStream::start(
const net::address_v4 &address,
uint16_t port,
const tcp::endpoint &endpoint,
const_buffer initial_data,
absl::AnyInvocable<void(std::error_code) &&> callback) {
encryptor_.init(connector_.pre_shared_key_);
connector_.salt_filter_.insert({
encryptor_.salt(), connector_.pre_shared_key_.method().salt_size()});
// TODO: split chunks if too large
encryptor_.start_chunk();
address address = endpoint.address();
size_t address_size = address.is_v4() ? 4 : 16;
size_t padding_size = absl::Uniform<size_t>(
connector_.bit_gen_,
connector_.min_padding_length_,
@@ -179,52 +147,21 @@ void Connector::TcpStream::start(
encryptor_.push_big_u64(
std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now().time_since_epoch()).count());
encryptor_.push_big_u16(padding_size + initial_data.size() + 9);
encryptor_.push_big_u16(
address_size + padding_size + initial_data.size() + 5);
} else {
encryptor_.push_big_u16(initial_data.size() + 7);
encryptor_.push_big_u16(address_size + initial_data.size() + 3);
}
encryptor_.finish_chunk();
encryptor_.start_chunk();
encryptor_.push_u8(1); // ipv4
encryptor_.push_buffer(address.to_bytes());
encryptor_.push_big_u16(port);
if (connector_.pre_shared_key_.method().is_spec_2022()) {
encryptor_.push_big_u16(padding_size);
encryptor_.push_random(padding_size);
}
encryptor_.push_buffer({initial_data.data(), initial_data.size()});
encryptor_.finish_chunk();
connect(std::move(callback));
}

void Connector::TcpStream::start(
const net::address_v6 &address,
uint16_t port,
const_buffer initial_data,
absl::AnyInvocable<void(std::error_code) &&> callback) {
encryptor_.init(connector_.pre_shared_key_);
connector_.salt_filter_.insert({
encryptor_.salt(), connector_.pre_shared_key_.method().salt_size()});
// TODO: split chunks if too large
encryptor_.start_chunk();
size_t padding_size = absl::Uniform<size_t>(
connector_.bit_gen_,
connector_.min_padding_length_,
connector_.max_padding_length_);
if (connector_.pre_shared_key_.method().is_spec_2022()) {
encryptor_.push_u8(0); // request
encryptor_.push_big_u64(
std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now().time_since_epoch()).count());
encryptor_.push_big_u16(padding_size + initial_data.size() + 21);
if (address.is_v4()) {
encryptor_.push_u8(1); // ipv4
encryptor_.push_buffer(address.to_v4().to_bytes());
} else {
encryptor_.push_big_u16(initial_data.size() + 19);
encryptor_.push_u8(4); // ipv6
encryptor_.push_buffer(address.to_v6().to_bytes());
}
encryptor_.finish_chunk();
encryptor_.start_chunk();
encryptor_.push_u8(4); // ipv6
encryptor_.push_buffer(address.to_bytes());
encryptor_.push_big_u16(port);
encryptor_.push_big_u16(endpoint.port());
if (connector_.pre_shared_key_.method().is_spec_2022()) {
encryptor_.push_big_u16(padding_size);
encryptor_.push_random(padding_size);
@@ -275,33 +212,23 @@ void Connector::TcpStream::start(

void Connector::TcpStream::connect(
absl::AnyInvocable<void(std::error_code) &&> callback) {
ConstBufferSpan write_buffer = encryptor_.buffer();
auto wrapped_callback = [this, callback = std::move(callback)](
std::error_code ec, std::unique_ptr<Stream> stream) mutable {
if (ec) {
std::move(callback)(ec);
return;
}
base_stream_ = std::move(stream);
std::move(callback)({});
};
const Endpoint &endpoint = *connector_.endpoints_iter_++;
if (connector_.endpoints_iter_ == connector_.endpoints_.end()) {
connector_.endpoints_iter_ = connector_.endpoints_.begin();
}
if (endpoint.address().is_v4()) {
connector_.base_connector_.connect_tcp_v4(
endpoint.address().to_v4(),
endpoint.port(),
buffer(write_buffer.data(), write_buffer.size()),
std::move(wrapped_callback));
} else {
connector_.base_connector_.connect_tcp_v6(
endpoint.address().to_v6(),
endpoint.port(),
buffer(write_buffer.data(), write_buffer.size()),
std::move(wrapped_callback));
}
ConstBufferSpan write_buffer = encryptor_.buffer();
connector_.base_connector_.connect(
endpoint,
{write_buffer.data(), write_buffer.size()},
[this, callback = std::move(callback)](
std::error_code ec, std::unique_ptr<Stream> stream) mutable {
if (ec) {
std::move(callback)(ec);
return;
}
base_stream_ = std::move(stream);
std::move(callback)({});
});
}

void Connector::TcpStream::read(
27 changes: 10 additions & 17 deletions net/proxy/shadowsocks/connector.h
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@

#include "absl/random/random.h"
#include "net/endpoint.h"
#include "net/proxy/connector.h"
#include "net/interface/connector.h"
#include "net/proxy/shadowsocks/method.h"
#include "net/proxy/shadowsocks/pre-shared-key.h"
#include "net/proxy/shadowsocks/salt-filter.h"
@@ -17,9 +17,9 @@ namespace net {
namespace proxy {
namespace shadowsocks {

class Connector : public proxy::Connector {
class Connector : public net::Connector {
public:
explicit Connector(proxy::Connector &base_connector)
explicit Connector(net::Connector &base_connector)
: base_connector_(base_connector) {}

Connector(const Connector &) = delete;
@@ -35,34 +35,27 @@ class Connector : public proxy::Connector {

bool init(const InitOptions &options);

void connect_tcp_v4(
const address_v4 &address,
uint16_t port,
const_buffer initial_data,
absl::AnyInvocable<void(
std::error_code, std::unique_ptr<Stream>) &&> callback) override;

void connect_tcp_v6(
const address_v6 &address,
uint16_t port,
void connect(
const tcp::endpoint &endpoint,
const_buffer initial_data,
absl::AnyInvocable<void(
std::error_code, std::unique_ptr<Stream>) &&> callback) override;

void connect_tcp_host(
void connect(
std::string_view host,
uint16_t port,
const_buffer initial_data,
absl::AnyInvocable<void(
std::error_code, std::unique_ptr<Stream>) &&> callback) override;

std::error_code bind_udp_v4(std::unique_ptr<Datagram> &datagram) override;
std::error_code bind_udp_v6(std::unique_ptr<Datagram> &datagram) override;
std::error_code bind(
const udp::endpoint &endpoint,
std::unique_ptr<Datagram> &datagram) override;

private:
class TcpStream;

proxy::Connector &base_connector_;
net::Connector &base_connector_;
std::vector<Endpoint> endpoints_;
std::vector<Endpoint>::iterator endpoints_iter_;
PreSharedKey pre_shared_key_;
18 changes: 6 additions & 12 deletions net/proxy/shadowsocks/handler.cc
Original file line number Diff line number Diff line change
@@ -207,10 +207,8 @@ void Handler::TcpConnection::forward_parse_ipv4(size_t header_length) {
const_buffer initial_data(
decryptor_.pop_buffer(initial_data_length),
initial_data_length);
handler_.connector_.connect_tcp_v4(
address_v4(address_bytes),
port,
initial_data,
handler_.connector_.connect(
{address_v4(address_bytes), port}, initial_data,
[connection = boost::intrusive_ptr<TcpConnection>(this)](
std::error_code ec, std::unique_ptr<Stream> stream) {
if (ec) {
@@ -259,10 +257,8 @@ void Handler::TcpConnection::forward_parse_ipv6(size_t header_length) {
const_buffer initial_data(
decryptor_.pop_buffer(initial_data_length),
initial_data_length);
handler_.connector_.connect_tcp_v6(
address_v6(address_bytes),
port,
initial_data,
handler_.connector_.connect(
{address_v6(address_bytes), port}, initial_data,
[connection = boost::intrusive_ptr<TcpConnection>(this)](
std::error_code ec, std::unique_ptr<Stream> stream) {
if (ec) {
@@ -311,10 +307,8 @@ void Handler::TcpConnection::forward_parse_host(size_t header_length) {
const_buffer initial_data(
decryptor_.pop_buffer(initial_data_length),
initial_data_length);
handler_.connector_.connect_tcp_host(
host,
port,
initial_data,
handler_.connector_.connect(
host, port, initial_data,
[connection = boost::intrusive_ptr<TcpConnection>(this)](
std::error_code ec, std::unique_ptr<Stream> stream) {
if (ec) {
10 changes: 5 additions & 5 deletions net/proxy/shadowsocks/handler.h
Original file line number Diff line number Diff line change
@@ -2,18 +2,18 @@
#define _NET_PROXY_SHADOWSOCKS_HANDLER_H

#include "net/asio.h"
#include "net/proxy/connector.h"
#include "net/proxy/handler.h"
#include "net/interface/connector.h"
#include "net/interface/handler.h"
#include "net/proxy/shadowsocks/pre-shared-key.h"
#include "net/proxy/shadowsocks/salt-filter.h"

namespace net {
namespace proxy {
namespace shadowsocks {

class Handler : public proxy::Handler {
class Handler : public net::Handler {
public:
explicit Handler(proxy::Connector &connector)
explicit Handler(net::Connector &connector)
: connector_(connector) {}

struct InitOptions {
@@ -29,7 +29,7 @@ class Handler : public proxy::Handler {
private:
class TcpConnection;

proxy::Connector &connector_;
net::Connector &connector_;
PreSharedKey pre_shared_key_;
SaltFilter salt_filter_;
};
2 changes: 1 addition & 1 deletion net/proxy/socks/BUILD
Original file line number Diff line number Diff line change
@@ -17,13 +17,13 @@ cc_library(
srcs = ["handler.cc"],
hdrs = ["handler.h"],
deps = [
"//net/proxy:interface",
"//net/proxy/util:write",
"@com_google_absl//absl/algorithm",
"@com_google_absl//absl/container:fixed_array",
"@org_boost_boost//:endian",
"@org_iceboy_trunk//base:logging",
"@org_iceboy_trunk//base:types",
"@org_iceboy_trunk//net:asio",
"@org_iceboy_trunk//net/interface",
],
)
22 changes: 9 additions & 13 deletions net/proxy/socks/handler.cc
Original file line number Diff line number Diff line change
@@ -53,7 +53,7 @@ class Handler::TcpConnection : public boost::intrusive_ref_counter<
size_t backward_size_;
};

Handler::Handler(const any_io_executor &executor, proxy::Connector &connector)
Handler::Handler(const any_io_executor &executor, net::Connector &connector)
: connector_(connector) {}

void Handler::handle_stream(std::unique_ptr<Stream> stream) {
@@ -170,10 +170,9 @@ void Handler::TcpConnection::connect_ipv4(ConstBufferSpan buffer) {
buffer.remove_prefix(6);
forward_size_ = 0;
state_ = State::connect;
handler_.connector_.connect_tcp_v4(
address_v4(address_bytes),
port,
net::buffer(buffer.data(), buffer.size()),
handler_.connector_.connect(
{address_v4(address_bytes), port},
{buffer.data(), buffer.size()},
[connection = boost::intrusive_ptr<TcpConnection>(this)](
std::error_code ec, std::unique_ptr<Stream> stream) {
if (ec) {
@@ -196,10 +195,9 @@ void Handler::TcpConnection::connect_ipv6(ConstBufferSpan buffer) {
buffer.remove_prefix(18);
forward_size_ = 0;
state_ = State::connect;
handler_.connector_.connect_tcp_v6(
address_v6(address_bytes),
port,
net::buffer(buffer.data(), buffer.size()),
handler_.connector_.connect(
{address_v6(address_bytes), port},
{buffer.data(), buffer.size()},
[connection = boost::intrusive_ptr<TcpConnection>(this)](
std::error_code ec, std::unique_ptr<Stream> stream) {
if (ec) {
@@ -226,10 +224,8 @@ void Handler::TcpConnection::connect_host(ConstBufferSpan buffer) {
buffer.remove_prefix(1 + host_length + 2);
forward_size_ = 0;
state_ = State::connect;
handler_.connector_.connect_tcp_host(
host,
port,
net::buffer(buffer.data(), buffer.size()),
handler_.connector_.connect(
host, port, {buffer.data(), buffer.size()},
[connection = boost::intrusive_ptr<TcpConnection>(this)](
std::error_code ec, std::unique_ptr<Stream> stream) {
if (ec) {
10 changes: 5 additions & 5 deletions net/proxy/socks/handler.h
Original file line number Diff line number Diff line change
@@ -2,24 +2,24 @@
#define _NET_PROXY_SOCKS_HANDLER_H

#include "net/asio.h"
#include "net/proxy/connector.h"
#include "net/proxy/handler.h"
#include "net/interface/connector.h"
#include "net/interface/handler.h"

namespace net {
namespace proxy {
namespace socks {

class Handler : public proxy::Handler {
class Handler : public net::Handler {
public:
Handler(const any_io_executor &executor, proxy::Connector &connector);
Handler(const any_io_executor &executor, net::Connector &connector);

void handle_stream(std::unique_ptr<Stream> stream) override;
void handle_datagram(std::unique_ptr<Datagram> datagram) override;

private:
class TcpConnection;

proxy::Connector &connector_;
net::Connector &connector_;
};

} // namespace socks
32 changes: 0 additions & 32 deletions net/proxy/stream.h

This file was deleted.

11 changes: 6 additions & 5 deletions net/proxy/system/BUILD
Original file line number Diff line number Diff line change
@@ -19,12 +19,13 @@ cc_library(
deps = [
":tcp-socket-stream",
":udp-socket-datagram",
"//net/proxy:interface",
"//net/proxy/ares:resolver",
"//net/proxy/util:write",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/types:span",
"@org_iceboy_trunk//net:asio",
"@org_iceboy_trunk//net:timer-list",
"@org_iceboy_trunk//net/interface",
],
)

@@ -35,11 +36,11 @@ cc_library(
deps = [
":tcp-socket-stream",
":udp-socket-datagram",
"//net/proxy:interface",
"@org_iceboy_trunk//base:logging",
"@org_iceboy_trunk//net:asio",
"@org_iceboy_trunk//net:endpoint",
"@org_iceboy_trunk//net:timer-list",
"@org_iceboy_trunk//net/interface",
],
)

@@ -48,9 +49,9 @@ cc_library(
srcs = ["stdio-stream.cc"],
hdrs = ["stdio-stream.h"],
deps = [
"//net/proxy:interface",
"@com_google_absl//absl/container:fixed_array",
"@org_iceboy_trunk//net:asio",
"@org_iceboy_trunk//net/interface",
] + select({
"@platforms//os:windows": [
"@org_iceboy_trunk//io:file-utils",
@@ -65,10 +66,10 @@ cc_library(
srcs = ["tcp-socket-stream.cc"],
hdrs = ["tcp-socket-stream.h"],
deps = [
"//net/proxy:interface",
"@com_google_absl//absl/container:fixed_array",
"@org_iceboy_trunk//net:asio",
"@org_iceboy_trunk//net:timer-list",
"@org_iceboy_trunk//net/interface",
],
)

@@ -77,8 +78,8 @@ cc_library(
srcs = ["udp-socket-datagram.cc"],
hdrs = ["udp-socket-datagram.h"],
deps = [
"//net/proxy:interface",
"@com_google_absl//absl/container:fixed_array",
"@org_iceboy_trunk//net:asio",
"@org_iceboy_trunk//net/interface",
],
)
45 changes: 11 additions & 34 deletions net/proxy/system/connector.cc
Original file line number Diff line number Diff line change
@@ -16,31 +16,15 @@ Connector::Connector(const any_io_executor &executor, const Options &options)
timer_list_(executor_, options.timeout),
tcp_no_delay_(options.tcp_no_delay) {}

void Connector::connect_tcp_v4(
const address_v4 &address,
uint16_t port,
const_buffer initial_data,
absl::AnyInvocable<void(
std::error_code, std::unique_ptr<Stream>) &&> callback) {
connect_tcp(
std::array<tcp::endpoint, 1>({tcp::endpoint(address, port)}),
initial_data,
std::move(callback));
}

void Connector::connect_tcp_v6(
const address_v6 &address,
uint16_t port,
void Connector::connect(
const tcp::endpoint &endpoint,
const_buffer initial_data,
absl::AnyInvocable<void(
std::error_code, std::unique_ptr<Stream>) &&> callback) {
connect_tcp(
std::array<tcp::endpoint, 1>({tcp::endpoint(address, port)}),
initial_data,
std::move(callback));
connect_internal({endpoint}, initial_data, std::move(callback));
}

void Connector::connect_tcp_host(
void Connector::connect(
std::string_view host,
uint16_t port,
const_buffer initial_data,
@@ -59,35 +43,28 @@ void Connector::connect_tcp_host(
for (const auto &address : addresses) {
endpoints.push_back(tcp::endpoint(address, port));
}
connect_tcp(endpoints, initial_data, std::move(callback));
connect_internal(endpoints, initial_data, std::move(callback));
});
}

std::error_code Connector::bind_udp_v4(std::unique_ptr<Datagram> &datagram) {
std::error_code Connector::bind(
const udp::endpoint &endpoint, std::unique_ptr<Datagram> &datagram) {
udp::socket socket(executor_);
boost::system::error_code ec;
socket.open(udp::v4(), ec);
socket.open(endpoint.protocol(), ec);
if (ec) {
return ec;
}
datagram = std::make_unique<UdpSocketDatagram>(std::move(socket));
return {};
}

std::error_code Connector::bind_udp_v6(std::unique_ptr<Datagram> &datagram) {
udp::socket socket(executor_);
boost::system::error_code ec;
socket.open(udp::v6(), ec);
socket.bind(endpoint, ec);
if (ec) {
return ec;
}
datagram = std::make_unique<UdpSocketDatagram>(std::move(socket));
return {};
}

template <typename EndpointsT>
void Connector::connect_tcp(
const EndpointsT &endpoints,
void Connector::connect_internal(
absl::Span<tcp::endpoint const> endpoints,
const_buffer initial_data,
absl::AnyInvocable<void(
std::error_code, std::unique_ptr<Stream>) &&> callback) {
31 changes: 12 additions & 19 deletions net/proxy/system/connector.h
Original file line number Diff line number Diff line change
@@ -3,17 +3,18 @@

#include <chrono>

#include "absl/types/span.h"
#include "net/timer-list.h"
#include "net/interface/connector.h"
#include "net/proxy/ares/resolver.h"
#include "net/proxy/connector.h"
#include "net/proxy/system/tcp-socket-stream.h"
#include "net/proxy/system/udp-socket-datagram.h"
#include "net/timer-list.h"

namespace net {
namespace proxy {
namespace system {

class Connector : public proxy::Connector {
class Connector : public net::Connector {
public:
struct Options {
std::chrono::nanoseconds timeout = std::chrono::minutes(5);
@@ -26,36 +27,28 @@ class Connector : public proxy::Connector {
Connector(const Connector &) = delete;
Connector &operator=(const Connector &) = delete;

void connect_tcp_v4(
const address_v4 &address,
uint16_t port,
const_buffer initial_data,
absl::AnyInvocable<void(
std::error_code, std::unique_ptr<Stream>) &&> callback) override;

void connect_tcp_v6(
const address_v6 &address,
uint16_t port,
void connect(
const tcp::endpoint &endpoint,
const_buffer initial_data,
absl::AnyInvocable<void(
std::error_code, std::unique_ptr<Stream>) &&> callback) override;

void connect_tcp_host(
void connect(
std::string_view host,
uint16_t port,
const_buffer initial_data,
absl::AnyInvocable<void(
std::error_code, std::unique_ptr<Stream>) &&> callback) override;

std::error_code bind_udp_v4(std::unique_ptr<Datagram> &datagram) override;
std::error_code bind_udp_v6(std::unique_ptr<Datagram> &datagram) override;
std::error_code bind(
const udp::endpoint &endpoint,
std::unique_ptr<Datagram> &datagram) override;

ares::Resolver &resolver() { return resolver_; }

private:
template <typename EndpointsT>
void connect_tcp(
const EndpointsT &endpoints,
void connect_internal(
absl::Span<tcp::endpoint const> endpoints,
const_buffer initial_data,
absl::AnyInvocable<void(
std::error_code, std::unique_ptr<Stream>) &&> callback);
2 changes: 1 addition & 1 deletion net/proxy/system/listener.h
Original file line number Diff line number Diff line change
@@ -5,8 +5,8 @@

#include "net/asio.h"
#include "net/endpoint.h"
#include "net/proxy/handler.h"
#include "net/timer-list.h"
#include "net/interface/handler.h"

namespace net {
namespace proxy {
2 changes: 1 addition & 1 deletion net/proxy/system/stdio-stream.h
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
#include "io/native-file.h"
#endif
#include "net/asio.h"
#include "net/proxy/stream.h"
#include "net/interface/stream.h"

namespace net {
namespace proxy {
2 changes: 1 addition & 1 deletion net/proxy/system/tcp-socket-stream.h
Original file line number Diff line number Diff line change
@@ -2,8 +2,8 @@
#define _NET_PROXY_SYSTEM_TCP_SOCKET_STREAM_H

#include "net/asio.h"
#include "net/proxy/stream.h"
#include "net/timer-list.h"
#include "net/interface/stream.h"

namespace net {
namespace proxy {
2 changes: 1 addition & 1 deletion net/proxy/system/udp-socket-datagram.h
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
#define _NET_PROXY_SYSTEM_UDP_SOCKET_DATAGRAM_H

#include "net/asio.h"
#include "net/proxy/datagram.h"
#include "net/interface/datagram.h"

namespace net {
namespace proxy {
6 changes: 3 additions & 3 deletions net/proxy/util/BUILD
Original file line number Diff line number Diff line change
@@ -17,18 +17,18 @@ cc_library(
hdrs = ["copy.h"],
deps = [
":write",
"//net/proxy:interface",
"@com_google_absl//absl/container:fixed_array",
"@com_google_absl//absl/functional:any_invocable",
"@org_iceboy_trunk//net/interface",
],
)

cc_library(
name = "stream-wrapper",
hdrs = ["stream-wrapper.h"],
deps = [
"//net/proxy:interface",
"@org_iceboy_trunk//net:asio",
"@org_iceboy_trunk//net/interface",
],
)

@@ -37,8 +37,8 @@ cc_library(
srcs = ["write.cc"],
hdrs = ["write.h"],
deps = [
"//net/proxy:interface",
"@com_google_absl//absl/functional:any_invocable",
"@org_iceboy_trunk//base:types",
"@org_iceboy_trunk//net/interface",
],
)
2 changes: 1 addition & 1 deletion net/proxy/util/copy.h
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
#include <system_error>

#include "absl/functional/any_invocable.h"
#include "net/proxy/stream.h"
#include "net/interface/stream.h"

namespace net {
namespace proxy {
2 changes: 1 addition & 1 deletion net/proxy/util/stream-wrapper.h
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@

#include "absl/types/span.h"
#include "net/asio.h"
#include "net/proxy/stream.h"
#include "net/interface/stream.h"

namespace net {
namespace proxy {
2 changes: 1 addition & 1 deletion net/proxy/util/write.h
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@

#include "absl/functional/any_invocable.h"
#include "base/types.h"
#include "net/proxy/stream.h"
#include "net/interface/stream.h"

namespace net {
namespace proxy {
4 changes: 2 additions & 2 deletions net/tools/miracle-proxy.cc
Original file line number Diff line number Diff line change
@@ -48,11 +48,11 @@ bool tcp_connect(Proxy &proxy) {
LOG(fatal) << "invalid connector";
return false;
}
connector->connect_tcp_host(
connector->connect(
host, port, {},
[&proxy](std::error_code ec, std::unique_ptr<Stream> remote_stream) {
if (ec) {
LOG(error) << "connect_tcp_host failed: " << ec;
LOG(error) << "connect failed: " << ec;
return;
}
auto stdio_stream = std::make_unique<system::StdioStream>(

0 comments on commit 237f78f

Please sign in to comment.