diff --git a/libcrypto-1_1.dll b/libcrypto-1_1.dll new file mode 100755 index 0000000..02ed3a7 Binary files /dev/null and b/libcrypto-1_1.dll differ diff --git a/libssl-1_1.dll b/libssl-1_1.dll new file mode 100755 index 0000000..80085ac Binary files /dev/null and b/libssl-1_1.dll differ diff --git a/main.cpp b/main.cpp index 0741f00..2b4fad7 100755 --- a/main.cpp +++ b/main.cpp @@ -5,6 +5,17 @@ #include #include + +#include + +#define SSL_R_SHORT_READ 219 +#include "ssl/ssl_locl.h" +#include + +#if defined(close) +#undef close +#endif + enum AddressType { AT_IPV4 = 0, @@ -66,46 +77,86 @@ std::string ConnectRequestRecord::getRequestData() class proxyClient { public: - proxyClient(boost::asio::io_service& ioServiceIn, boost::asio::ip::tcp::resolver::iterator endpointIn) + proxyClient(boost::asio::io_service& ioServiceIn, boost::asio::ssl::context& context, boost::asio::ip::tcp::resolver::iterator endpointIn) : ioService(ioServiceIn) - , socket(ioServiceIn) + , socket(ioServiceIn, context) { - do_connect(endpointIn); + 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)); + + + doConnect(endpointIn); } - /* - void write(const chat_message& msg) - { - io_service_.post( - [this, msg]() - { - bool write_in_progress = !write_msgs_.empty(); - write_msgs_.push_back(msg); - if (!write_in_progress) - { - do_write(); - } - }); - } - */ + void close() { - ioService.post([this]() { socket.close(); }); + ioService.post([this]() { lowerSocket().close(); }); } private: - void do_connect(boost::asio::ip::tcp::resolver::iterator endpointIterator) + + boost::asio::ssl::stream::lowest_layer_type& lowerSocket() { - boost::asio::async_connect(socket, endpointIterator, + return socket.lowest_layer(); + } + + void doConnect(boost::asio::ip::tcp::resolver::iterator endpointIterator) + { + boost::asio::async_connect(lowerSocket(), endpointIterator, [this](boost::system::error_code ec, boost::asio::ip::tcp::resolver::iterator) { if (!ec) { - sendVersion(); + //sendVersion(); + + doHandshake(); } }); } + void doHandshake() + { + socket.async_handshake(boost::asio::ssl::stream_base::client, + [this](const boost::system::error_code& error) { + + + if (!error) + { + sendVersion(); + } + else + { + std::cout << "Handshake failed: " << error.message() << "\n"; + lowerSocket().close(); + } + + }); + } + + + + bool verify_certificate(bool preverified, + boost::asio::ssl::verify_context& ctx) + { + // The verify callback can be used to check whether the certificate that is + // being presented is valid for the peer. For example, RFC 2818 describes + // the steps involved in doing this for HTTPS. Consult the OpenSSL + // documentation for more details. Note that the callback is called once + // for each certificate in the certificate chain, starting from the root + // certificate authority. + + // In this example we will simply print the certificate's subject name. + char subject_name[256]; + X509* cert = X509_STORE_CTX_get_current_cert(ctx.native_handle()); + X509_NAME_oneline(X509_get_subject_name(cert), subject_name, 256); + std::cout << "Verifying " << subject_name << "\n"; + + return preverified; + } + + void sendVersion() { std::array version = {0x05, 0x01, 0x02}; @@ -121,7 +172,7 @@ private: } else { - socket.close(); + lowerSocket().close(); } }); } @@ -146,12 +197,12 @@ private: else { //Others not supported - socket.close(); + lowerSocket().close(); } } else { - socket.close(); + lowerSocket().close(); } }); } @@ -178,7 +229,7 @@ private: } else { - socket.close(); + lowerSocket().close(); } }); } @@ -203,7 +254,7 @@ private: } else { - socket.close(); + lowerSocket().close(); } }); @@ -230,7 +281,7 @@ private: } else { - socket.close(); + lowerSocket().close(); } }); } @@ -252,12 +303,12 @@ private: else { //Authorization is not succeed - socket.close(); + lowerSocket().close(); } } else { - socket.close(); + lowerSocket().close(); } }); } @@ -278,7 +329,7 @@ private: } else { - socket.close(); + lowerSocket().close(); } }); } @@ -324,7 +375,7 @@ private: } connectResponseRecord.connectResponseType = static_cast(connectResponseRecord.rawData[1]); - connectResponseRecord.addressType = static_cast(connectResponseRecord.rawData[2]); + connectResponseRecord.addressType = static_cast(connectResponseRecord.rawData[3]); size_t portOffset = 8; @@ -389,7 +440,7 @@ private: } else { - socket.close(); + lowerSocket().close(); } }); } @@ -411,7 +462,7 @@ private: } else { - socket.close(); + lowerSocket().close(); } }); } @@ -430,87 +481,36 @@ private: } else { - socket.close(); + lowerSocket().close(); } }); } - /* - void do_read_header() - { - boost::asio::async_read(socket_, - boost::asio::buffer(read_msg_.data(), chat_message::header_length), - [this](boost::system::error_code ec, std::size_t length) - { - if (!ec && read_msg_.decode_header()) - { - do_read_body(); - } - else - { - socket_.close(); - } - }); - } - - void do_read_body() - { - boost::asio::async_read(socket_, - boost::asio::buffer(read_msg_.body(), read_msg_.body_length()), - [this](boost::system::error_code ec, std::size_t length) - { - if (!ec) - { - std::cout.write(read_msg_.body(), read_msg_.body_length()); - std::cout << "\n"; - do_read_header(); - } - else - { - socket_.close(); - } - }); - } - - void do_write() - { - boost::asio::async_write(socket_, - boost::asio::buffer(write_msgs_.front().data(), - write_msgs_.front().length()), - [this](boost::system::error_code ec, std::size_t length) - { - if (!ec) - { - write_msgs_.pop_front(); - if (!write_msgs_.empty()) - { - do_write(); - } - } - else - { - socket_.close(); - } - }); - } - */ private: boost::asio::io_service& ioService; - boost::asio::ip::tcp::socket socket; + + boost::asio::ssl::stream socket; }; + int main() { std::cout << "Hello" << std::endl; - + 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({ "telegram-proxy.fishrungames.com", "8043" }); + auto endpointIterator = resolver.resolve({ "127.0.0.1", "8043" }); + + + boost::asio::ssl::context ctx(boost::asio::ssl::context::sslv23); + ctx.load_verify_file("rootca.crt"); - proxyClient c(ioService, endpointIterator); + proxyClient c(ioService, ctx, endpointIterator); std::thread t([&ioService]() { ioService.run(); }); diff --git a/proxyTest.vcxproj b/proxyTest.vcxproj index 8cf0acc..d14c551 100755 --- a/proxyTest.vcxproj +++ b/proxyTest.vcxproj @@ -74,11 +74,12 @@ Level3 Disabled true - ../boost_1_63_0 + ../boost_1_63_0;../../openssl-master;../../openssl-master/include;../../openssl-master/output/include true - ../boost_1_63_0/stage/x86/lib/ + ../boost_1_63_0/stage/x86/lib/;../../openssl-master/output/lib + libcrypto.lib;libssl.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) @@ -99,13 +100,14 @@ true true true - ../boost_1_63_0 + ../boost_1_63_0;../../openssl-master;../../openssl-master/include;../../openssl-master/output/include true true true - ../boost_1_63_0/stage/x86/lib/ + ../boost_1_63_0/stage/x86/lib/;../../openssl-master/output/lib + libcrypto.lib;libssl.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) diff --git a/rootca.crt b/rootca.crt new file mode 100755 index 0000000..647a744 --- /dev/null +++ b/rootca.crt @@ -0,0 +1,24 @@ +-----BEGIN CERTIFICATE----- +MIIECTCCAvGgAwIBAgIJAKlzlxSAr1BkMA0GCSqGSIb3DQEBCwUAMIGZMQswCQYD +VQQGEwJMVjETMBEGA1UECAwKU29tZS1TdGF0ZTENMAsGA1UEBwwEUmlnYTEbMBkG +A1UECgwSRmlzaCBSdW4gR2FtZXMgU0lBMRcwFQYDVQQDDA5mcmctcHJveHktcm9v +dDEwMC4GCSqGSIb3DQEJARYhdmxhZGlzbGF2Lmtob3JldkBmaXNocnVuZ2FtZXMu +Y29tMCAXDTE3MDgwNjE4MjgxMFoYDzIwNzIwNTA5MTgyODEwWjCBmTELMAkGA1UE +BhMCTFYxEzARBgNVBAgMClNvbWUtU3RhdGUxDTALBgNVBAcMBFJpZ2ExGzAZBgNV +BAoMEkZpc2ggUnVuIEdhbWVzIFNJQTEXMBUGA1UEAwwOZnJnLXByb3h5LXJvb3Qx +MDAuBgkqhkiG9w0BCQEWIXZsYWRpc2xhdi5raG9yZXZAZmlzaHJ1bmdhbWVzLmNv +bTCCASIwDQYJKoZIhvc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