Added intermediate proxy, Telegram works good

This commit is contained in:
Vladislav Khorev 2017-08-07 00:53:58 +03:00
parent 0a0a436d73
commit 1d6f2bfd84

122
main.cpp
View File

@ -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<char, 2> 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)
{
if ((*firstPartPtr)[3] == 0x03)
{
//unsigned int len = (*firstPartPtr)[4];
readConnectRequestPart2(firstPartPtr);
}
else if ((*firstPartPtr)[3] == 0x01)
{
readConnectRequestPart2IpAddress(firstPartPtr);
}
}
else
@ -345,6 +358,48 @@ private:
}
void readConnectRequestPart2IpAddress(std::shared_ptr<std::array<unsigned char, 5>> firstPartPtr)
{
unsigned int ip1st = (*firstPartPtr)[4];
std::shared_ptr<std::array<unsigned char, 5>> secondPartPtr = std::make_shared<std::array<unsigned char, 5>>();
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<std::string>(static_cast<unsigned int>(ip1st)) + "."
+ boost::lexical_cast<std::string>(static_cast<unsigned int>((*secondPartPtr)[0])) + "."
+ boost::lexical_cast<std::string>(static_cast<unsigned int>((*secondPartPtr)[1])) + "."
+ boost::lexical_cast<std::string>(static_cast<unsigned int>((*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<std::string>(connectRequestRecord.port) });
doConnectOutputIpAddress(endpointIterator, connectRequestRecord, { static_cast<unsigned char>(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<unsigned char, 4> 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<unsigned char, 4> ipAddress)
{
auto self(shared_from_this());
std::string connectResponse;
connectResponse += 0x05;
connectResponse += static_cast<char>(0x00);
connectResponse += static_cast<char>(0x00);
connectResponse += 0x01;
connectResponse += ipAddress[0];
connectResponse += ipAddress[1];
connectResponse += ipAddress[2];
connectResponse += ipAddress[3];
connectResponse += static_cast<unsigned char>(connectRequestRecord.port / 256);
connectResponse += static_cast<unsigned char>(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;