From 3c36177f722cb9fefc7b28453ebfeb5e7b0a6b97 Mon Sep 17 00:00:00 2001 From: Vladislav Khorev Date: Mon, 7 Aug 2017 05:27:16 +0300 Subject: [PATCH] Fixing bugs, add more output --- main.cpp | 90 ++++++++++++++++++++++++++++++++++++++--------- proxyTest.vcxproj | 4 +++ 2 files changed, 77 insertions(+), 17 deletions(-) diff --git a/main.cpp b/main.cpp index 038e78d..e686777 100755 --- a/main.cpp +++ b/main.cpp @@ -8,6 +8,8 @@ #include +#include + #define SSL_R_SHORT_READ 219 #include "ssl/ssl_locl.h" #include @@ -118,7 +120,11 @@ private: void doConnect() { - boost::asio::ip::tcp::resolver::iterator endpointIterator(resolver.resolve({ "127.0.0.1", "8043" })); + //boost::asio::ip::tcp::resolver::iterator endpointIterator(resolver.resolve({ "127.0.0.1", "8043" })); + //boost::asio::ip::tcp::resolver::iterator endpointIterator(resolver.resolve({ "https-proxy.fishrungames.com", "8043" })); + + boost::asio::ip::tcp::resolver::iterator endpointIterator(resolver.resolve({ "52.89.37.158", "8043" })); + boost::asio::async_connect(lowerSocket(), endpointIterator, [this](boost::system::error_code ec, boost::asio::ip::tcp::resolver::iterator) { @@ -552,30 +558,40 @@ public: protected: - unsigned char forwardChar; - unsigned char backwardChar; + //unsigned char forwardChar; + //unsigned char backwardChar; + + std::array forwardBuffer; + std::array backwardBuffer; + + void onProxyReady() { + transferDataForward(); transferDataBackward(); + proxyClient.onReady = nullptr; } 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*/) + socket.async_read_some(boost::asio::buffer(forwardBuffer), + [this, self](boost::system::error_code ec, std::size_t length) { if (!ec) { - std::cout << static_cast(forwardChar) << " "; + + std::shared_ptr> data = std::make_shared>(); + data->resize(length); + + std::copy(&forwardBuffer[0], &forwardBuffer[0] + length, &(*data)[0]); boost::asio::async_write(proxyClient.getSocket(), - boost::asio::buffer(&forwardChar, 1), - [this, self](boost::system::error_code ec, std::size_t length) + boost::asio::buffer(*data), + [this, self, data](boost::system::error_code ec, std::size_t length) { if (!ec) @@ -590,6 +606,15 @@ protected: } }); } + else if (ec == boost::asio::error::eof) + { + std::cout << "transferDataForward read end of file" << std::endl; + if (length > 0) + { + std::cout << "but length is positive" << std::endl; + } + return; + } else { std::cout << "transferDataForward read error" << std::endl; @@ -604,15 +629,19 @@ protected: { 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*/) + proxyClient.getSocket().async_read_some(boost::asio::buffer(backwardBuffer), + [this, self](boost::system::error_code ec, std::size_t length) { if (!ec) { + std::shared_ptr> data = std::make_shared>(); + data->resize(length); + + std::copy(&backwardBuffer[0], &backwardBuffer[0] + length, &(*data)[0]); + boost::asio::async_write(socket, - boost::asio::buffer(&backwardChar, 1), - [this, self](boost::system::error_code ec, std::size_t length) + boost::asio::buffer(*data), + [this, self, data](boost::system::error_code ec, std::size_t length) { if (!ec) @@ -628,6 +657,15 @@ protected: } }); } + else if (ec == boost::asio::error::eof) + { + std::cout << "transferDataBackward read end of file" << std::endl; + if (length > 0) + { + std::cout << "but length is positive" << std::endl; + } + return; + } else { std::cout << "transferDataBackward read error" << std::endl; @@ -659,7 +697,6 @@ public: , 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) { @@ -715,10 +752,29 @@ int main() //ProxyClient c(ioService, ctx, endpointIterator); - std::thread t([&ioService]() { ioService.run(); }); + //std::thread t([&ioService]() { ioService.run(); }); + + //t.join(); - t.join(); + boost::thread_group threadpool; + /* + threadpool.create_thread( + boost::bind(&boost::asio::io_service::run, &ioService) + ); + threadpool.create_thread( + boost::bind(&boost::asio::io_service::run, &ioService) + ); + threadpool.create_thread( + boost::bind(&boost::asio::io_service::run, &ioService) + );*/ + threadpool.create_thread( + boost::bind(&boost::asio::io_service::run, &ioService) + ); + + threadpool.join_all(); + + return 0; } \ No newline at end of file diff --git a/proxyTest.vcxproj b/proxyTest.vcxproj index d14c551..30b0187 100755 --- a/proxyTest.vcxproj +++ b/proxyTest.vcxproj @@ -75,6 +75,7 @@ Disabled true ../boost_1_63_0;../../openssl-master;../../openssl-master/include;../../openssl-master/output/include + _SCL_SECURE_NO_WARNINGS;_MBCS;%(PreprocessorDefinitions) true @@ -88,6 +89,7 @@ Disabled true ../boost_1_63_0 + _SCL_SECURE_NO_WARNINGS;_MBCS;%(PreprocessorDefinitions) true @@ -101,6 +103,7 @@ true true ../boost_1_63_0;../../openssl-master;../../openssl-master/include;../../openssl-master/output/include + _SCL_SECURE_NO_WARNINGS;_MBCS;%(PreprocessorDefinitions) true @@ -118,6 +121,7 @@ true true ../boost_1_63_0 + _SCL_SECURE_NO_WARNINGS;_MBCS;%(PreprocessorDefinitions) true