Version without SSL

This commit is contained in:
Vladislav Khorev 2018-08-24 00:15:23 +03:00
parent 6193a49001
commit 330d838ffc

139
main.cpp
View File

@ -9,10 +9,12 @@
#include <boost/thread.hpp> #include <boost/thread.hpp>
#if 0
#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>
#endif
#if defined(close) #if defined(close)
#undef close #undef close
@ -78,17 +80,13 @@ std::string ConnectRequestRecord::getRequestData()
typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> ssl_socket;
class ProxySession : class ProxySession :
public std::enable_shared_from_this<ProxySession> public std::enable_shared_from_this<ProxySession>
{ {
public: public:
ProxySession(std::shared_ptr<ssl_socket> socket, boost::asio::io_service& inIoService) ProxySession(boost::asio::ip::tcp::socket inSocket, boost::asio::io_service& inIoService)
: ioService(inIoService) : ioService(inIoService)
, mSocket(socket) , mSocket(std::move(inSocket))
, outsideConnectSocket(ioService) , outsideConnectSocket(ioService)
{ {
std::cout << "ProxySession Create" << std::endl; std::cout << "ProxySession Create" << std::endl;
@ -102,61 +100,18 @@ public:
void start() void start()
{ {
std::cout << "ProxySession::start" << std::endl; std::cout << "ProxySession::start" << std::endl;
asyncHandshake(); //asyncHandshake();
//readClientVersion(); readClientVersion();
} }
private: private:
ssl_socket& socket() boost::asio::ip::tcp::socket& socket()
{ {
return *mSocket; return mSocket;
} }
ssl_socket::lowest_layer_type& lowerSocket()
{
return mSocket->lowest_layer();
}
void asyncHandshake()
{
std::cout << "ProxySession::asyncHandshake" << std::endl;
auto self(shared_from_this());
socket().async_handshake(boost::asio::ssl::stream_base::server,
[this, self](boost::system::error_code ec) {
std::cout << "ProxySession::asyncHandshake inner" << std::endl;
if (!ec)
{
readClientVersion();
}
else
{
std::string msg = ec.message();
std::cout << msg << std::endl;
lowerSocket().close();
}
});
}
/*
void handleHandshake(const boost::system::error_code& error)
{
if (!error)
{
readClientVersion();
}
else
{
lowerSocket().close();
}
}*/
std::array<unsigned char, 3> clientVersion; std::array<unsigned char, 3> clientVersion;
@ -184,14 +139,14 @@ private:
{ {
std::string msg = ec.message(); std::string msg = ec.message();
std::cout << msg << std::endl; std::cout << msg << std::endl;
lowerSocket().close(); socket().close();
} }
} }
else else
{ {
std::string msg = ec.message(); std::string msg = ec.message();
std::cout << msg << std::endl; std::cout << msg << std::endl;
lowerSocket().close(); socket().close();
} }
}); });
} }
@ -219,7 +174,7 @@ private:
{ {
std::string msg = ec.message(); std::string msg = ec.message();
std::cout << msg << std::endl; std::cout << msg << std::endl;
lowerSocket().close(); socket().close();
} }
}); });
} }
@ -247,7 +202,7 @@ private:
{ {
std::string msg = ec.message(); std::string msg = ec.message();
std::cout << msg << std::endl; std::cout << msg << std::endl;
lowerSocket().close(); socket().close();
} }
}); });
@ -288,14 +243,14 @@ private:
{ {
std::string msg = ec.message(); std::string msg = ec.message();
std::cout << msg << std::endl; std::cout << msg << std::endl;
lowerSocket().close(); socket().close();
} }
} }
else else
{ {
std::string msg = ec.message(); std::string msg = ec.message();
std::cout << msg << std::endl; std::cout << msg << std::endl;
lowerSocket().close(); socket().close();
} }
}); });
@ -322,7 +277,7 @@ private:
{ {
std::string msg = ec.message(); std::string msg = ec.message();
std::cout << msg << std::endl; std::cout << msg << std::endl;
lowerSocket().close(); socket().close();
} }
}); });
} }
@ -362,14 +317,14 @@ private:
{ {
std::string msg = ec.message(); std::string msg = ec.message();
std::cout << msg << std::endl; std::cout << msg << std::endl;
lowerSocket().close(); socket().close();
} }
} }
else else
{ {
std::string msg = ec.message(); std::string msg = ec.message();
std::cout << msg << std::endl; std::cout << msg << std::endl;
lowerSocket().close(); socket().close();
} }
}); });
@ -411,7 +366,7 @@ private:
{ {
std::string msg = ec.message(); std::string msg = ec.message();
std::cout << msg << std::endl; std::cout << msg << std::endl;
lowerSocket().close(); socket().close();
} }
}); });
@ -455,7 +410,7 @@ private:
{ {
std::string msg = ec.message(); std::string msg = ec.message();
std::cout << msg << std::endl; std::cout << msg << std::endl;
lowerSocket().close(); socket().close();
} }
}); });
@ -477,7 +432,7 @@ private:
{ {
std::string msg = ec.message(); std::string msg = ec.message();
std::cout << msg << std::endl; std::cout << msg << std::endl;
lowerSocket().close(); socket().close();
outsideConnectSocket.close(); outsideConnectSocket.close();
} }
}); });
@ -498,7 +453,7 @@ private:
{ {
std::string msg = ec.message(); std::string msg = ec.message();
std::cout << msg << std::endl; std::cout << msg << std::endl;
lowerSocket().close(); socket().close();
outsideConnectSocket.close(); outsideConnectSocket.close();
} }
}); });
@ -536,7 +491,7 @@ private:
{ {
std::string msg = ec.message(); std::string msg = ec.message();
std::cout << msg << std::endl; std::cout << msg << std::endl;
lowerSocket().close(); socket().close();
outsideConnectSocket.close(); outsideConnectSocket.close();
} }
}); });
@ -577,7 +532,7 @@ private:
{ {
std::string msg = ec.message(); std::string msg = ec.message();
std::cout << msg << std::endl; std::cout << msg << std::endl;
lowerSocket().close(); socket().close();
outsideConnectSocket.close(); outsideConnectSocket.close();
} }
}); });
@ -621,7 +576,7 @@ private:
std::string msg = ec.message(); std::string msg = ec.message();
std::cout << msg << std::endl; std::cout << msg << std::endl;
outsideConnectSocket.close(); outsideConnectSocket.close();
lowerSocket().close(); socket().close();
} }
}); });
} }
@ -639,7 +594,7 @@ private:
std::string msg = ec.message(); std::string msg = ec.message();
std::cout << msg << std::endl; std::cout << msg << std::endl;
outsideConnectSocket.close(); outsideConnectSocket.close();
lowerSocket().close(); socket().close();
} }
}); });
} }
@ -674,7 +629,7 @@ private:
std::string msg = ec.message(); std::string msg = ec.message();
std::cout << msg << std::endl; std::cout << msg << std::endl;
outsideConnectSocket.close(); outsideConnectSocket.close();
lowerSocket().close(); socket().close();
} }
}); });
} }
@ -692,14 +647,14 @@ private:
std::string msg = ec.message(); std::string msg = ec.message();
std::cout << msg << std::endl; std::cout << msg << std::endl;
outsideConnectSocket.close(); outsideConnectSocket.close();
lowerSocket().close(); socket().close();
} }
}); });
} }
boost::asio::io_service& ioService; boost::asio::io_service& ioService;
std::shared_ptr<ssl_socket> mSocket; boost::asio::ip::tcp::socket mSocket;
boost::asio::ip::tcp::socket outsideConnectSocket; boost::asio::ip::tcp::socket outsideConnectSocket;
@ -714,12 +669,10 @@ class ProxyServer
{ {
public: public:
ProxyServer(boost::asio::io_service& inIoService, ProxyServer(boost::asio::io_service& inIoService,
const boost::asio::ip::tcp::endpoint& endpoint, const boost::asio::ip::tcp::endpoint& endpoint)
boost::asio::ssl::context& inSslContext)
: ioService(inIoService) : ioService(inIoService)
, acceptor(inIoService, endpoint) , acceptor(inIoService, endpoint)
, socket(std::make_shared<ssl_socket>(inIoService, inSslContext)) , socket(inIoService)
, sslContext(inSslContext)
{ {
doAccept(); doAccept();
} }
@ -727,17 +680,17 @@ public:
private: private:
void doAccept() void doAccept()
{ {
acceptor.async_accept(socket->lowest_layer(), acceptor.async_accept(socket,
[this](boost::system::error_code ec) [this](boost::system::error_code ec)
{ {
if (!ec) if (!ec)
{ {
std::make_shared<ProxySession>(socket, ioService)->start(); std::make_shared<ProxySession>(std::move(socket), ioService)->start();
} }
counter++; counter++;
socket = std::make_shared<ssl_socket>(ioService, sslContext); socket = boost::asio::ip::tcp::socket(ioService);
doAccept(); doAccept();
}); });
@ -746,11 +699,7 @@ private:
boost::asio::io_service& ioService; boost::asio::io_service& ioService;
boost::asio::ip::tcp::acceptor acceptor; boost::asio::ip::tcp::acceptor acceptor;
std::shared_ptr<ssl_socket> socket; boost::asio::ip::tcp::socket socket;
boost::asio::ssl::context& sslContext;
//std::map<size_t, ProxySession> proxySessionMap;
size_t counter = 0; size_t counter = 0;
}; };
@ -766,25 +715,9 @@ int main()
boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), 8043); boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), 8043);
boost::asio::ssl::context sslContext(boost::asio::ssl::context::sslv23);
sslContext.set_options(
boost::asio::ssl::context::default_workarounds
| boost::asio::ssl::context::no_sslv2
| boost::asio::ssl::context::single_dh_use);
std::function<std::string(std::size_t, boost::asio::ssl::context_base::password_purpose)> f = [](std::size_t, boost::asio::ssl::context_base::password_purpose) -> std::string { return ""; }; ProxyServer proxyServer(ioService, endpoint);
sslContext.set_password_callback(f);
sslContext.use_certificate_chain_file("server.crt");
sslContext.use_private_key_file("server.key", boost::asio::ssl::context::pem);
sslContext.use_tmp_dh_file("dh2048.pem");
ProxyServer proxyServer(ioService, endpoint, sslContext);
boost::thread_group threadpool; boost::thread_group threadpool;