#pragma once #include #include #include #include #include #include #include #include #include #include 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: 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:, start the accept thread and return // the bound port. Restarts (against the new upstream) when already // running. Api::Result 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 upstream() const; private: struct Connection { boost::asio::ip::tcp::socket client; boost::asio::ip::tcp::socket upstream; std::atomic 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 acceptor_; std::thread acceptThread_; uint16_t port_ = 0; std::atomic running_{false}; mutable std::mutex mutex_; // guards upstream_ and connections_ std::optional upstream_; std::vector> connections_; }; } // namespace Echoion::Core