1 parent
d146f11f02
commit
5c036d91fb
72 files changed
+5875
-1243
No files matched your search
@@ -0,0 +1,90 @@
|
||||
#pragma once
|
||||
|
||||
#include <Types.hpp>
|
||||
#include <Void/Net/Http.hpp>
|
||||
#include <boost/asio.hpp>
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
namespace Echoion::Core
|
||||
{
|
||||
|
||||
// Local HTTP CONNECT proxy that tunnels through a SOCKS5 upstream.
|
||||
//
|
||||
// FFmpeg has no SOCKS5 support at all — its http protocol only knows the
|
||||
// `http_proxy` option, which speaks HTTP CONNECT. To stream IP-locked
|
||||
// googlevideo URLs directly through a SOCKS5 proxy, the decoder is pointed
|
||||
// at 127.0.0.1:<port()> and every CONNECT is forwarded through the
|
||||
// configured SOCKS5 proxy (greeting/auth/CONNECT handshake, then a raw
|
||||
// byte pump in both directions).
|
||||
//
|
||||
// Only the two decoder slots stream concurrently, so a blocking
|
||||
// thread-per-connection design is deliberate: no io_context pump, no
|
||||
// async state machines.
|
||||
class Socks5HttpBridge
|
||||
{
|
||||
public:
|
||||
|
||||
Socks5HttpBridge() = default;
|
||||
~Socks5HttpBridge();
|
||||
|
||||
Socks5HttpBridge(const Socks5HttpBridge&) = delete;
|
||||
Socks5HttpBridge& operator=(const Socks5HttpBridge&) = delete;
|
||||
|
||||
// Bind 127.0.0.1:<ephemeral>, start the accept thread and return
|
||||
// the bound port. Restarts (against the new upstream) when already
|
||||
// running.
|
||||
Api::Result<uint16_t> start(const Void::Net::ProxyConfig& socks5);
|
||||
|
||||
// Close the acceptor and every live tunnel; joins all threads.
|
||||
void stop();
|
||||
|
||||
[[nodiscard]] bool running() const { return running_.load(); }
|
||||
[[nodiscard]] uint16_t port() const { return port_; }
|
||||
|
||||
// The SOCKS5 endpoint the bridge currently forwards to (empty when
|
||||
// stopped). Lets callers skip a restart when the config is
|
||||
// unchanged.
|
||||
[[nodiscard]] std::optional<Void::Net::ProxyConfig> upstream() const;
|
||||
|
||||
private:
|
||||
|
||||
struct Connection {
|
||||
boost::asio::ip::tcp::socket client;
|
||||
boost::asio::ip::tcp::socket upstream;
|
||||
std::atomic<bool> done{false};
|
||||
std::thread worker;
|
||||
|
||||
explicit Connection(boost::asio::io_context& io)
|
||||
: client(io), upstream(io)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
void acceptLoop();
|
||||
void serve(Connection& conn);
|
||||
// SOCKS5 greeting/auth/CONNECT for host:port on conn.upstream.
|
||||
// Throws std::runtime_error on any protocol failure.
|
||||
void socks5Connect(boost::asio::ip::tcp::socket& upstream,
|
||||
const std::string& host, uint16_t targetPort,
|
||||
const Void::Net::ProxyConfig& proxy);
|
||||
// Drop connections whose worker already finished (joins them).
|
||||
void pruneFinished();
|
||||
|
||||
boost::asio::io_context io_;
|
||||
std::optional<boost::asio::ip::tcp::acceptor> acceptor_;
|
||||
std::thread acceptThread_;
|
||||
uint16_t port_ = 0;
|
||||
std::atomic<bool> running_{false};
|
||||
|
||||
mutable std::mutex mutex_; // guards upstream_ and connections_
|
||||
std::optional<Void::Net::ProxyConfig> upstream_;
|
||||
std::vector<std::unique_ptr<Connection>> connections_;
|
||||
};
|
||||
|
||||
} // namespace Echoion::Core
|
||||
Reference in new issue
Block a user