Fixing bugs, add more output

This commit is contained in:
Vladislav Khorev 2017-08-07 05:27:16 +03:00
parent db9a8c90c4
commit 3c36177f72
2 changed files with 77 additions and 17 deletions

View File

@ -8,6 +8,8 @@
#include <boost/bind.hpp> #include <boost/bind.hpp>
#include <boost/thread/thread.hpp>
#define SSL_R_SHORT_READ 219 #define SSL_R_SHORT_READ 219
#include "ssl/ssl_locl.h" #include "ssl/ssl_locl.h"
#include <boost/asio/ssl.hpp> #include <boost/asio/ssl.hpp>
@ -118,7 +120,11 @@ private:
void doConnect() 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, boost::asio::async_connect(lowerSocket(), endpointIterator,
[this](boost::system::error_code ec, boost::asio::ip::tcp::resolver::iterator) [this](boost::system::error_code ec, boost::asio::ip::tcp::resolver::iterator)
{ {
@ -552,30 +558,40 @@ public:
protected: protected:
unsigned char forwardChar; //unsigned char forwardChar;
unsigned char backwardChar; //unsigned char backwardChar;
std::array<unsigned char, 8192> forwardBuffer;
std::array<unsigned char, 8192> backwardBuffer;
void onProxyReady() void onProxyReady()
{ {
transferDataForward(); transferDataForward();
transferDataBackward(); transferDataBackward();
proxyClient.onReady = nullptr;
} }
void transferDataForward() void transferDataForward()
{ {
auto self(shared_from_this()); auto self(shared_from_this());
boost::asio::async_read(socket, socket.async_read_some(boost::asio::buffer(forwardBuffer),
boost::asio::buffer(&forwardChar, 1), [this, self](boost::system::error_code ec, std::size_t length)
[this, self](boost::system::error_code ec, std::size_t /*length*/)
{ {
if (!ec) if (!ec)
{ {
std::cout << static_cast<unsigned int>(forwardChar) << " ";
std::shared_ptr<std::vector<unsigned char>> data = std::make_shared<std::vector<unsigned char>>();
data->resize(length);
std::copy(&forwardBuffer[0], &forwardBuffer[0] + length, &(*data)[0]);
boost::asio::async_write(proxyClient.getSocket(), boost::asio::async_write(proxyClient.getSocket(),
boost::asio::buffer(&forwardChar, 1), boost::asio::buffer(*data),
[this, self](boost::system::error_code ec, std::size_t length) [this, self, data](boost::system::error_code ec, std::size_t length)
{ {
if (!ec) 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 else
{ {
std::cout << "transferDataForward read error" << std::endl; std::cout << "transferDataForward read error" << std::endl;
@ -604,15 +629,19 @@ protected:
{ {
auto self(shared_from_this()); auto self(shared_from_this());
boost::asio::async_read(proxyClient.getSocket(), proxyClient.getSocket().async_read_some(boost::asio::buffer(backwardBuffer),
boost::asio::buffer(&backwardChar, 1), [this, self](boost::system::error_code ec, std::size_t length)
[this, self](boost::system::error_code ec, std::size_t /*length*/)
{ {
if (!ec) if (!ec)
{ {
std::shared_ptr<std::vector<unsigned char>> data = std::make_shared<std::vector<unsigned char>>();
data->resize(length);
std::copy(&backwardBuffer[0], &backwardBuffer[0] + length, &(*data)[0]);
boost::asio::async_write(socket, boost::asio::async_write(socket,
boost::asio::buffer(&backwardChar, 1), boost::asio::buffer(*data),
[this, self](boost::system::error_code ec, std::size_t length) [this, self, data](boost::system::error_code ec, std::size_t length)
{ {
if (!ec) 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 else
{ {
std::cout << "transferDataBackward read error" << std::endl; std::cout << "transferDataBackward read error" << std::endl;
@ -659,7 +697,6 @@ public:
, acceptor(io_service, endpoint) , acceptor(io_service, endpoint)
, socket(io_service) , socket(io_service)
, resolver(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(boost::asio::ssl::context::sslv23)
{ {
@ -715,10 +752,29 @@ int main()
//ProxyClient c(ioService, ctx, endpointIterator); //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; return 0;
} }

View File

@ -75,6 +75,7 @@
<Optimization>Disabled</Optimization> <Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck> <SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>../boost_1_63_0;../../openssl-master;../../openssl-master/include;../../openssl-master/output/include</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>../boost_1_63_0;../../openssl-master;../../openssl-master/include;../../openssl-master/output/include</AdditionalIncludeDirectories>
<PreprocessorDefinitions>_SCL_SECURE_NO_WARNINGS;_MBCS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile> </ClCompile>
<Link> <Link>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
@ -88,6 +89,7 @@
<Optimization>Disabled</Optimization> <Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck> <SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>../boost_1_63_0</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>../boost_1_63_0</AdditionalIncludeDirectories>
<PreprocessorDefinitions>_SCL_SECURE_NO_WARNINGS;_MBCS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile> </ClCompile>
<Link> <Link>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
@ -101,6 +103,7 @@
<IntrinsicFunctions>true</IntrinsicFunctions> <IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck> <SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>../boost_1_63_0;../../openssl-master;../../openssl-master/include;../../openssl-master/output/include</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>../boost_1_63_0;../../openssl-master;../../openssl-master/include;../../openssl-master/output/include</AdditionalIncludeDirectories>
<PreprocessorDefinitions>_SCL_SECURE_NO_WARNINGS;_MBCS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile> </ClCompile>
<Link> <Link>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
@ -118,6 +121,7 @@
<IntrinsicFunctions>true</IntrinsicFunctions> <IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck> <SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>../boost_1_63_0</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>../boost_1_63_0</AdditionalIncludeDirectories>
<PreprocessorDefinitions>_SCL_SECURE_NO_WARNINGS;_MBCS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile> </ClCompile>
<Link> <Link>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>