From db9a8c90c42720dec04c1062be2d64c727d5508d Mon Sep 17 00:00:00 2001 From: Vladislav Khorev Date: Mon, 7 Aug 2017 00:53:25 +0300 Subject: [PATCH] Added intermediate proxy, Telegram works good --- main.cpp | 235 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 219 insertions(+), 16 deletions(-) diff --git a/main.cpp b/main.cpp index 2b4fad7..038e78d 100755 --- a/main.cpp +++ b/main.cpp @@ -74,19 +74,23 @@ std::string ConnectRequestRecord::getRequestData() return data; } -class proxyClient +class ProxyClient { public: - proxyClient(boost::asio::io_service& ioServiceIn, boost::asio::ssl::context& context, boost::asio::ip::tcp::resolver::iterator endpointIn) + ProxyClient(boost::asio::io_service& ioServiceIn, boost::asio::ssl::context& context, boost::asio::ip::tcp::resolver& resolverIn) : ioService(ioServiceIn) , socket(ioServiceIn, context) + , resolver(resolverIn) { socket.set_verify_mode(boost::asio::ssl::verify_peer); socket.set_verify_callback( - std::bind(&proxyClient::verify_certificate, this, std::placeholders::_1, std::placeholders::_2)); + std::bind(&ProxyClient::verify_certificate, this, std::placeholders::_1, std::placeholders::_2)); + } - doConnect(endpointIn); + void start() + { + doConnect(); } void close() @@ -94,22 +98,32 @@ public: ioService.post([this]() { lowerSocket().close(); }); } -private: + std::function onReady; + + boost::asio::ssl::stream& getSocket() + { + return socket; + } boost::asio::ssl::stream::lowest_layer_type& lowerSocket() { return socket.lowest_layer(); } - void doConnect(boost::asio::ip::tcp::resolver::iterator endpointIterator) +private: + + + + + + void doConnect() { + boost::asio::ip::tcp::resolver::iterator endpointIterator(resolver.resolve({ "127.0.0.1", "8043" })); boost::asio::async_connect(lowerSocket(), endpointIterator, [this](boost::system::error_code ec, boost::asio::ip::tcp::resolver::iterator) { if (!ec) { - //sendVersion(); - doHandshake(); } }); @@ -124,7 +138,8 @@ private: if (!error) { - sendVersion(); + onReady(); + //sendVersion(); } else { @@ -298,7 +313,14 @@ private: { //doWriteSomething(); - connectToWebAddress("www.google.com"); + /* + if (onReady) + { + onReady(); + }*/ + + + //connectToWebAddress("www.google.com"); } else { @@ -490,6 +512,189 @@ private: boost::asio::io_service& ioService; boost::asio::ssl::stream socket; + + boost::asio::ip::tcp::resolver& resolver; +}; + + +class ProxyClientSession : public std::enable_shared_from_this { +public: + + ProxyClientSession(boost::asio::ip::tcp::socket socket, boost::asio::io_service& ioServiceIn, boost::asio::ssl::context& context, boost::asio::ip::tcp::resolver& resolverIn) + : socket(std::move(socket)) + , resolver(resolverIn) + , proxyClient(ioServiceIn, context, resolverIn) + + { + std::cout << "ProxyClientSession create" << std::endl; + } + + ~ProxyClientSession() + { + std::cout << "ProxyClientSession destroy" << std::endl; + } + + + + void start() + { + + auto self(shared_from_this()); + + proxyClient.onReady = [self]() {self->onProxyReady(); }; + + proxyClient.start(); + + } + + + +protected: + + + unsigned char forwardChar; + unsigned char backwardChar; + + void onProxyReady() + { + transferDataForward(); + transferDataBackward(); + } + + void transferDataForward() + { + auto self(shared_from_this()); + + boost::asio::async_read(socket, + boost::asio::buffer(&forwardChar, 1), + [this, self](boost::system::error_code ec, std::size_t /*length*/) + { + if (!ec) + { + std::cout << static_cast(forwardChar) << " "; + + boost::asio::async_write(proxyClient.getSocket(), + boost::asio::buffer(&forwardChar, 1), + [this, self](boost::system::error_code ec, std::size_t length) + { + + if (!ec) + { + transferDataForward(); + } + else + { + std::cout << "transferDataForward write error" << std::endl; + proxyClient.lowerSocket().close(); + socket.close(); + } + }); + } + else + { + std::cout << "transferDataForward read error" << std::endl; + + proxyClient.lowerSocket().close(); + socket.close(); + } + }); + } + + void transferDataBackward() + { + auto self(shared_from_this()); + + boost::asio::async_read(proxyClient.getSocket(), + boost::asio::buffer(&backwardChar, 1), + [this, self](boost::system::error_code ec, std::size_t /*length*/) + { + if (!ec) + { + boost::asio::async_write(socket, + boost::asio::buffer(&backwardChar, 1), + [this, self](boost::system::error_code ec, std::size_t length) + { + + if (!ec) + { + transferDataBackward(); + } + else + { + std::cout << "transferDataBackward write error" << std::endl; + + proxyClient.lowerSocket().close(); + socket.close(); + } + }); + } + else + { + std::cout << "transferDataBackward read error" << std::endl; + + + proxyClient.lowerSocket().close(); + socket.close(); + } + }); + } + + + boost::asio::ip::tcp::socket socket; + + boost::asio::ip::tcp::resolver& resolver; + + ProxyClient proxyClient; + + +}; + +class IntermediateServer +{ +public: + + IntermediateServer(boost::asio::io_service& io_service, + const boost::asio::ip::tcp::endpoint& endpoint) + : ioService(io_service) + , acceptor(io_service, endpoint) + , socket(io_service) + , resolver(io_service) + //, endpointIterator(resolver.resolve({ "127.0.0.1", "8043" })) //resolver.resolve({ "telegram-proxy.fishrungames.com", "8043" }); + , ctx(boost::asio::ssl::context::sslv23) + { + + ctx.load_verify_file("rootca.crt"); + + doAccept(); + } + +protected: + + void doAccept() + { + acceptor.async_accept(socket, + [this](boost::system::error_code ec) + { + if (!ec) + { + std::make_shared(std::move(socket), ioService, ctx, resolver)->start(); + } + + doAccept(); + }); + } + + + boost::asio::io_service& ioService; + + boost::asio::ip::tcp::acceptor acceptor; + boost::asio::ip::tcp::socket socket; + + boost::asio::ip::tcp::resolver resolver; + //boost::asio::ip::tcp::resolver::iterator endpointIterator; + boost::asio::ssl::context ctx; + + }; @@ -501,16 +706,14 @@ int main() boost::asio::io_service ioService; //boost::asio::io_service::work work(ioService); - boost::asio::ip::tcp::resolver resolver(ioService); - //auto endpointIterator = resolver.resolve({ "telegram-proxy.fishrungames.com", "8043" }); - auto endpointIterator = resolver.resolve({ "127.0.0.1", "8043" }); + //Create intermediate: + boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), 8042); - boost::asio::ssl::context ctx(boost::asio::ssl::context::sslv23); - ctx.load_verify_file("rootca.crt"); + IntermediateServer intermediateServer(ioService, endpoint); - proxyClient c(ioService, ctx, endpointIterator); + //ProxyClient c(ioService, ctx, endpointIterator); std::thread t([&ioService]() { ioService.run(); });