Skip to content

Commit

Permalink
net/proxy add datagram support
Browse files Browse the repository at this point in the history
  • Loading branch information
iceboy233 committed Jan 8, 2024
1 parent 435c038 commit d48db2e
Show file tree
Hide file tree
Showing 20 changed files with 258 additions and 14 deletions.
2 changes: 2 additions & 0 deletions net/proxy/handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <memory>

#include "net/proxy/datagram.h"
#include "net/proxy/stream.h"

namespace net {
Expand All @@ -13,6 +14,7 @@ class Handler {
virtual ~Handler() = default;

virtual void handle_stream(std::unique_ptr<Stream> stream) = 0;
virtual void handle_datagram(std::unique_ptr<Datagram> datagram) = 0;
};

} // namespace proxy
Expand Down
53 changes: 53 additions & 0 deletions net/proxy/misc/echo-handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@ class StreamConnection {
size_t size_;
};

class DatagramConnection {
public:
explicit DatagramConnection(std::unique_ptr<Datagram> datagram);

void start() { read(); }

private:
void read();
void write();
void finish() { delete this; }

std::unique_ptr<Datagram> datagram_;
absl::FixedArray<uint8_t, 0> buffer_;
udp::endpoint endpoint_;
size_t size_;
};

StreamConnection::StreamConnection(std::unique_ptr<Stream> stream)
: stream_(std::move(stream)),
buffer_(8192) {}
Expand Down Expand Up @@ -58,13 +75,49 @@ void StreamConnection::write() {
});
}

DatagramConnection::DatagramConnection(std::unique_ptr<Datagram> datagram)
: datagram_(std::move(datagram)),
buffer_(8192) {}

void DatagramConnection::read() {
datagram_->async_receive_from(
buffer(buffer_.data(), buffer_.size()),
endpoint_,
[this](std::error_code ec, size_t size) {
if (ec) {
finish();
return;
}
size_ = size;
write();
});
}

void DatagramConnection::write() {
datagram_->async_send_to(
const_buffer(buffer_.data(), size_),
endpoint_,
[this](std::error_code ec, size_t) {
if (ec) {
finish();
return;
}
read();
});
}

} // namespace

void EchoHandler::handle_stream(std::unique_ptr<Stream> stream) {
auto *connection = new StreamConnection(std::move(stream));
connection->start();
}

void EchoHandler::handle_datagram(std::unique_ptr<Datagram> datagram) {
auto *connection = new DatagramConnection(std::move(datagram));
connection->start();
}

} // namespace misc
} // namespace proxy
} // namespace net
1 change: 1 addition & 0 deletions net/proxy/misc/echo-handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace misc {
class EchoHandler : public Handler {
public:
void handle_stream(std::unique_ptr<Stream> stream) override;
void handle_datagram(std::unique_ptr<Datagram> datagram) override;
};

} // namespace misc
Expand Down
37 changes: 37 additions & 0 deletions net/proxy/misc/null-handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ class StreamConnection {
absl::FixedArray<uint8_t, 0> buffer_;
};

class DatagramConnection {
public:
explicit DatagramConnection(std::unique_ptr<Datagram> datagram);

void start() { read(); }

private:
void read();
void finish() { delete this; }

std::unique_ptr<Datagram> datagram_;
absl::FixedArray<uint8_t, 0> buffer_;
udp::endpoint endpoint_;
};

StreamConnection::StreamConnection(std::unique_ptr<Stream> stream)
: stream_(std::move(stream)),
buffer_(8192) {}
Expand All @@ -42,13 +57,35 @@ void StreamConnection::read() {
});
}

DatagramConnection::DatagramConnection(std::unique_ptr<Datagram> datagram)
: datagram_(std::move(datagram)),
buffer_(8192) {}

void DatagramConnection::read() {
datagram_->async_receive_from(
buffer(buffer_.data(), buffer_.size()),
endpoint_,
[this](std::error_code ec, size_t) {
if (ec) {
finish();
return;
}
read();
});
}

} // namespace

void NullHandler::handle_stream(std::unique_ptr<Stream> stream) {
auto *connection = new StreamConnection(std::move(stream));
connection->start();
}

void NullHandler::handle_datagram(std::unique_ptr<Datagram> datagram) {
auto *connection = new DatagramConnection(std::move(datagram));
connection->start();
}

} // namespace misc
} // namespace proxy
} // namespace net
1 change: 1 addition & 0 deletions net/proxy/misc/null-handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace misc {
class NullHandler : public Handler {
public:
void handle_stream(std::unique_ptr<Stream> stream) override;
void handle_datagram(std::unique_ptr<Datagram> datagram) override;
};

} // namespace misc
Expand Down
58 changes: 58 additions & 0 deletions net/proxy/misc/random-handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ class StreamConnection : public boost::intrusive_ref_counter<
absl::FixedArray<uint8_t, 0> write_buffer_;
};

class DatagramConnection {
public:
explicit DatagramConnection(std::unique_ptr<Datagram> datagram);

void start() { read(); }

private:
void read();
void write();
void finish() { delete this; }

std::unique_ptr<Datagram> datagram_;
absl::FixedArray<uint8_t, 0> read_buffer_;
absl::FixedArray<uint8_t, 0> write_buffer_;
udp::endpoint endpoint_;
size_t size_;
};

StreamConnection::StreamConnection(std::unique_ptr<Stream> stream)
: stream_(std::move(stream)),
read_buffer_(8192),
Expand Down Expand Up @@ -72,6 +90,41 @@ void StreamConnection::write() {
});
}

DatagramConnection::DatagramConnection(std::unique_ptr<Datagram> datagram)
: datagram_(std::move(datagram)),
read_buffer_(8192),
write_buffer_(8192) {
RAND_bytes(write_buffer_.data(), write_buffer_.size());
}

void DatagramConnection::read() {
datagram_->async_receive_from(
buffer(read_buffer_.data(), read_buffer_.size()),
endpoint_,
[this](std::error_code ec, size_t size) {
if (ec) {
finish();
return;
}
size_ = size;
write();
});
}

void DatagramConnection::write() {
datagram_->async_send_to(
const_buffer(write_buffer_.data(), size_),
endpoint_,
[this](std::error_code ec, size_t size) {
if (ec) {
finish();
return;
}
RAND_bytes(write_buffer_.data(), size);
read();
});
}

} // namespace

void RandomHandler::handle_stream(std::unique_ptr<Stream> stream) {
Expand All @@ -80,6 +133,11 @@ void RandomHandler::handle_stream(std::unique_ptr<Stream> stream) {
connection->start();
}

void RandomHandler::handle_datagram(std::unique_ptr<Datagram> datagram) {
auto *connection = new DatagramConnection(std::move(datagram));
connection->start();
}

} // namespace misc
} // namespace proxy
} // namespace net
1 change: 1 addition & 0 deletions net/proxy/misc/random-handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace misc {
class RandomHandler : public Handler {
public:
void handle_stream(std::unique_ptr<Stream> stream) override;
void handle_datagram(std::unique_ptr<Datagram> datagram) override;
};

} // namespace misc
Expand Down
55 changes: 55 additions & 0 deletions net/proxy/misc/zero-handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@ class StreamConnection : public boost::intrusive_ref_counter<
absl::FixedArray<uint8_t, 0> write_buffer_;
};

class DatagramConnection {
public:
explicit DatagramConnection(std::unique_ptr<Datagram> datagram);

void start() { read(); }

private:
void read();
void write();
void finish() { delete this; }

std::unique_ptr<Datagram> datagram_;
absl::FixedArray<uint8_t, 0> read_buffer_;
absl::FixedArray<uint8_t, 0> write_buffer_;
udp::endpoint endpoint_;
size_t size_;
};

StreamConnection::StreamConnection(std::unique_ptr<Stream> stream)
: stream_(std::move(stream)),
read_buffer_(8192),
Expand Down Expand Up @@ -68,6 +86,38 @@ void StreamConnection::write() {
});
}

DatagramConnection::DatagramConnection(std::unique_ptr<Datagram> datagram)
: datagram_(std::move(datagram)),
read_buffer_(8192),
write_buffer_(8192) {}

void DatagramConnection::read() {
datagram_->async_receive_from(
buffer(read_buffer_.data(), read_buffer_.size()),
endpoint_,
[this](std::error_code ec, size_t size) {
if (ec) {
finish();
return;
}
size_ = size;
write();
});
}

void DatagramConnection::write() {
datagram_->async_send_to(
const_buffer(write_buffer_.data(), size_),
endpoint_,
[this](std::error_code ec, size_t) {
if (ec) {
finish();
return;
}
read();
});
}

} // namespace

void ZeroHandler::handle_stream(std::unique_ptr<Stream> stream) {
Expand All @@ -76,6 +126,11 @@ void ZeroHandler::handle_stream(std::unique_ptr<Stream> stream) {
connection->start();
}

void ZeroHandler::handle_datagram(std::unique_ptr<Datagram> datagram) {
auto *connection = new DatagramConnection(std::move(datagram));
connection->start();
}

} // namespace misc
} // namespace proxy
} // namespace net
1 change: 1 addition & 0 deletions net/proxy/misc/zero-handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace misc {
class ZeroHandler : public Handler {
public:
void handle_stream(std::unique_ptr<Stream> stream) override;
void handle_datagram(std::unique_ptr<Datagram> datagram) override;
};

} // namespace misc
Expand Down
5 changes: 5 additions & 0 deletions net/proxy/shadowsocks/handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ void Handler::handle_stream(std::unique_ptr<Stream> stream) {
connection->start();
}

void Handler::handle_datagram(std::unique_ptr<Datagram> datagram) {
// TODO: support datagram
LOG(warning) << "datagram is not supported yet";
}

Handler::TcpConnection::TcpConnection(
Handler &handler,
std::unique_ptr<Stream> stream)
Expand Down
1 change: 1 addition & 0 deletions net/proxy/shadowsocks/handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Handler : public proxy::Handler {
bool init(const InitOptions &config);

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

private:
class TcpConnection;
Expand Down
1 change: 1 addition & 0 deletions net/proxy/socks/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ cc_library(
"@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",
],
Expand Down
6 changes: 6 additions & 0 deletions net/proxy/socks/handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "absl/algorithm/algorithm.h"
#include "absl/container/fixed_array.h"
#include "base/logging.h"
#include "base/types.h"

namespace net {
Expand Down Expand Up @@ -60,6 +61,11 @@ void Handler::handle_stream(std::unique_ptr<Stream> stream) {
connection->start();
}

void Handler::handle_datagram(std::unique_ptr<Datagram> datagram) {
// TODO: Report support status to avoid this being called.
LOG(warning) << "datagram is not supported";
}

Handler::TcpConnection::TcpConnection(
Handler &handler,
std::unique_ptr<Stream> stream)
Expand Down
1 change: 1 addition & 0 deletions net/proxy/socks/handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Handler : public proxy::Handler {
Handler(const any_io_executor &executor, proxy::Connector &connector);

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

private:
class TcpConnection;
Expand Down
1 change: 1 addition & 0 deletions net/proxy/system/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ cc_library(
hdrs = ["listener.h"],
deps = [
":tcp-socket-stream",
":udp-socket-datagram",
"//net/proxy:interface",
"@org_iceboy_trunk//base:logging",
"@org_iceboy_trunk//net:asio",
Expand Down
Loading

0 comments on commit d48db2e

Please sign in to comment.