diff --git a/main.cpp b/main.cpp index 1b90663..d202299 100755 --- a/main.cpp +++ b/main.cpp @@ -99,6 +99,7 @@ public: void start() { + std::cout << "ProxySession::start" << std::endl; asyncHandshake(); //readClientVersion(); } @@ -119,11 +120,14 @@ private: 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(); @@ -154,12 +158,14 @@ private: void readClientVersion() { + std::cout << "ProxySession::readClientVersion" << std::endl; auto self(shared_from_this()); boost::asio::async_read(socket(), boost::asio::buffer(clientVersion.data(), clientVersion.size()), [this, self](boost::system::error_code ec, std::size_t /*length*/) { + std::cout << "ProxySession::readClientVersion inner" << std::endl; if (!ec) { if (clientVersion[0] == 0x05 && clientVersion[1] == 0x01 && clientVersion[2] == 0x02) @@ -182,6 +188,8 @@ private: void sendServerVersion() { + std::cout << "ProxySession::sendServerVersion" << std::endl; + auto self(shared_from_this()); std::array version = { 0x05, 0x02 }; @@ -190,6 +198,7 @@ private: boost::asio::buffer(version.data(), version.size()), [this, self](boost::system::error_code ec, std::size_t length) { + std::cout << "ProxySession::sendServerVersion inner" << std::endl; if (!ec) { readLoginPassword(); @@ -283,12 +292,16 @@ private: { if (!ec) { - if ((*firstPartPtr)[0] == 0x05 && (*firstPartPtr)[1] == 0x01 && (*firstPartPtr)[2] == 0x00 && (*firstPartPtr)[3] == 0x03) + if ((*firstPartPtr)[0] == 0x05 && (*firstPartPtr)[1] == 0x01 && (*firstPartPtr)[2] == 0x00) { - //unsigned int len = (*firstPartPtr)[4]; - - readConnectRequestPart2(firstPartPtr); - + if ((*firstPartPtr)[3] == 0x03) + { + readConnectRequestPart2(firstPartPtr); + } + else if ((*firstPartPtr)[3] == 0x01) + { + readConnectRequestPart2IpAddress(firstPartPtr); + } } else @@ -345,6 +358,48 @@ private: } + void readConnectRequestPart2IpAddress(std::shared_ptr> firstPartPtr) + { + unsigned int ip1st = (*firstPartPtr)[4]; + + std::shared_ptr> secondPartPtr = std::make_shared>(); + + + auto self(shared_from_this()); + + + boost::asio::async_read(socket(), + boost::asio::buffer(secondPartPtr->data(), secondPartPtr->size()), + [this, self, firstPartPtr, secondPartPtr, ip1st](boost::system::error_code ec, std::size_t /*length*/) + { + if (!ec) + { + + ConnectRequestRecord connectRequestRecord; + + connectRequestRecord.address = boost::lexical_cast(static_cast(ip1st)) + "." + + boost::lexical_cast(static_cast((*secondPartPtr)[0])) + "." + + boost::lexical_cast(static_cast((*secondPartPtr)[1])) + "." + + boost::lexical_cast(static_cast((*secondPartPtr)[2])); + + connectRequestRecord.port = (*secondPartPtr)[3] * 256 + (*secondPartPtr)[4]; + + boost::asio::ip::tcp::resolver resolver(this->ioService); + auto endpointIterator = resolver.resolve({ connectRequestRecord.address, boost::lexical_cast(connectRequestRecord.port) }); + + + doConnectOutputIpAddress(endpointIterator, connectRequestRecord, { static_cast(ip1st) , ((*secondPartPtr)[0]) , ((*secondPartPtr)[1]) , ((*secondPartPtr)[2]) }); + + } + else + { + lowerSocket().close(); + } + }); + + } + + void doConnectOutput(boost::asio::ip::tcp::resolver::iterator endpointIterator, ConnectRequestRecord connectRequestRecord) { auto self(shared_from_this()); @@ -364,6 +419,26 @@ private: }); } + void doConnectOutputIpAddress(boost::asio::ip::tcp::resolver::iterator endpointIterator, ConnectRequestRecord connectRequestRecord, std::array ipAddress) + { + auto self(shared_from_this()); + + boost::asio::async_connect(outsideConnectSocket, endpointIterator, + [this, self, connectRequestRecord, ipAddress](boost::system::error_code ec, boost::asio::ip::tcp::resolver::iterator) + { + if (!ec) + { + sendConnectResponseIpAddress(connectRequestRecord, ipAddress); + } + else + { + lowerSocket().close(); + outsideConnectSocket.close(); + } + }); + } + + void sendConnectResponse(ConnectRequestRecord connectRequestRecord) { auto self(shared_from_this()); @@ -400,6 +475,45 @@ private: } + + void sendConnectResponseIpAddress(ConnectRequestRecord connectRequestRecord, std::array ipAddress) + { + auto self(shared_from_this()); + + std::string connectResponse; + + connectResponse += 0x05; + connectResponse += static_cast(0x00); + connectResponse += static_cast(0x00); + connectResponse += 0x01; + + connectResponse += ipAddress[0]; + connectResponse += ipAddress[1]; + connectResponse += ipAddress[2]; + connectResponse += ipAddress[3]; + connectResponse += static_cast(connectRequestRecord.port / 256); + connectResponse += static_cast(connectRequestRecord.port % 256); + + + boost::asio::async_write(socket(), + boost::asio::buffer(connectResponse.data(), connectResponse.size()), + [this, self](boost::system::error_code ec, std::size_t length) + { + + if (!ec) + { + transferDataForward(); + transferDataBackward(); + } + else + { + lowerSocket().close(); + outsideConnectSocket.close(); + } + }); + + } + unsigned char forwardChar; unsigned char backwardChar;