Browse Source

NTCP2 proxy

pull/1479/head
orignal 5 years ago
parent
commit
ae20e3aa95
  1. 144
      libi2pd/NTCP2.cpp
  2. 4
      libi2pd/NTCP2.h

144
libi2pd/NTCP2.cpp

@ -1175,16 +1175,18 @@ namespace transport
m_ProxyPort = port; m_ProxyPort = port;
} }
void NTCP2Server::HandleProxyConnect(const boost::system::error_code& ecode, std::shared_ptr<NTCP2Session> conn, std::shared_ptr<boost::asio::deadline_timer> timer, const std::string & host, uint16_t port, RemoteAddressType addrtype) void NTCP2Server::HandleProxyConnect(const boost::system::error_code& ecode, std::shared_ptr<NTCP2Session> conn, std::shared_ptr<boost::asio::deadline_timer> timer, const std::string & host, uint16_t port, RemoteAddressType addrtype)
{
if (ecode)
{ {
if(ecode) LogPrint(eLogWarning, "NTCP2: failed to connect to proxy ", ecode.message());
{ timer->cancel();
LogPrint(eLogWarning, "NTCP2: failed to connect to proxy ", ecode.message()); conn->Terminate();
timer->cancel(); return;
conn->Terminate(); }
return; switch (m_ProxyType)
} {
if(m_ProxyType == eSocksProxy) case eSocksProxy:
{ {
// TODO: support username/password auth etc // TODO: support username/password auth etc
uint8_t buff[3] = {0x05, 0x01, 0x00}; uint8_t buff[3] = {0x05, 0x01, 0x00};
@ -1196,7 +1198,8 @@ namespace transport
} }
}); });
uint8_t readbuff[2]; uint8_t readbuff[2];
boost::asio::async_read(conn->GetSocket(), boost::asio::buffer(readbuff, 2), [=](const boost::system::error_code & ec, std::size_t transferred) boost::asio::async_read(conn->GetSocket(), boost::asio::buffer(readbuff, 2),
[=](const boost::system::error_code & ec, std::size_t transferred)
{ {
LogPrint(eLogError, "NTCP2: ", transferred); LogPrint(eLogError, "NTCP2: ", transferred);
if(ec) if(ec)
@ -1226,71 +1229,73 @@ namespace transport
timer->cancel(); timer->cancel();
conn->Terminate(); conn->Terminate();
}); });
break;
} }
else if(m_ProxyType == eHTTPProxy) case eHTTPProxy:
{ {
i2p::http::HTTPReq req; i2p::http::HTTPReq req;
req.method = "CONNECT"; req.method = "CONNECT";
req.version ="HTTP/1.1"; req.version ="HTTP/1.1";
if(addrtype == eIP6Address) if(addrtype == eIP6Address)
req.uri = "[" + host + "]:" + std::to_string(port); req.uri = "[" + host + "]:" + std::to_string(port);
else else
req.uri = host + ":" + std::to_string(port); req.uri = host + ":" + std::to_string(port);
boost::asio::streambuf writebuff; boost::asio::streambuf writebuff;
std::ostream out(&writebuff); std::ostream out(&writebuff);
out << req.to_string(); out << req.to_string();
boost::asio::async_write(conn->GetSocket(), writebuff.data(), boost::asio::transfer_all(), [=](const boost::system::error_code & ec, std::size_t transferred) { boost::asio::async_write(conn->GetSocket(), writebuff.data(), boost::asio::transfer_all(), [=](const boost::system::error_code & ec, std::size_t transferred) {
(void) transferred; (void) transferred;
if(ec) if(ec)
LogPrint(eLogError, "NTCP2: http proxy write error ", ec.message()); LogPrint(eLogError, "NTCP2: http proxy write error ", ec.message());
}); });
boost::asio::streambuf * readbuff = new boost::asio::streambuf; boost::asio::streambuf * readbuff = new boost::asio::streambuf;
boost::asio::async_read_until(conn->GetSocket(), *readbuff, "\r\n\r\n", [=] (const boost::system::error_code & ec, std::size_t transferred) { boost::asio::async_read_until(conn->GetSocket(), *readbuff, "\r\n\r\n",
if(ec) [=] (const boost::system::error_code & ec, std::size_t transferred)
{
LogPrint(eLogError, "NTCP2: http proxy read error ", ec.message());
timer->cancel();
conn->Terminate();
}
else
{ {
readbuff->commit(transferred); if(ec)
i2p::http::HTTPRes res; {
if(res.parse(boost::asio::buffer_cast<const char*>(readbuff->data()), readbuff->size()) > 0) LogPrint(eLogError, "NTCP2: http proxy read error ", ec.message());
timer->cancel();
conn->Terminate();
}
else
{ {
if(res.code == 200) readbuff->commit(transferred);
i2p::http::HTTPRes res;
if(res.parse(boost::asio::buffer_cast<const char*>(readbuff->data()), readbuff->size()) > 0)
{ {
timer->cancel(); if(res.code == 200)
conn->ClientLogin(); {
delete readbuff; timer->cancel();
return; conn->ClientLogin();
delete readbuff;
return;
}
else
{
LogPrint(eLogError, "NTCP2: http proxy rejected request ", res.code);
}
} }
else else
{ LogPrint(eLogError, "NTCP2: http proxy gave malformed response");
LogPrint(eLogError, "NTCP2: http proxy rejected request ", res.code); timer->cancel();
} conn->Terminate();
delete readbuff;
} }
else });
LogPrint(eLogError, "NTCP2: http proxy gave malformed response"); break;
timer->cancel(); }
conn->Terminate(); default:
delete readbuff; LogPrint(eLogError, "NTCP2: unknown proxy type, invalid state");
} }
});
}
else
LogPrint(eLogError, "NTCP2: unknown proxy type, invalid state");
} }
void NTCP2Server::ConnectWithProxy (const std::string& host, uint16_t port, RemoteAddressType addrtype, std::shared_ptr<NTCP2Session> conn) void NTCP2Server::ConnectWithProxy (const std::string& host, uint16_t port, RemoteAddressType addrtype, std::shared_ptr<NTCP2Session> conn)
{ {
if(m_ProxyEndpoint == nullptr) if(!m_ProxyEndpoint) return
{
return;
}
GetService().post([=]() { GetService().post([=]() {
if (this->AddNTCP2Session (conn)) if (this->AddNTCP2Session (conn))
{ {
@ -1359,9 +1364,8 @@ namespace transport
} }
NTCP2Server::NTCP2Server (): NTCP2Server::NTCP2Server ():
RunnableServiceWithWork ("NTCP2"), RunnableServiceWithWork ("NTCP2"), m_TerminationTimer (GetService ()),
m_ProxyType(eNoProxy), m_Resolver(GetService ()), m_ProxyEndpoint(nullptr), m_Resolver(GetService ())
m_TerminationTimer (GetService ())
{ {
} }
@ -1388,11 +1392,9 @@ namespace transport
} }
else else
{ {
m_ProxyEndpoint = new boost::asio::ip::tcp::endpoint(*itr); m_ProxyEndpoint.reset (new boost::asio::ip::tcp::endpoint(*itr));
if (m_ProxyEndpoint) if (m_ProxyEndpoint)
{ LogPrint(eLogError, "NTCP2: m_ProxyEndpoint ", *m_ProxyEndpoint);
LogPrint(eLogError, "NTCP2: m_ProxyEndpoint %s", m_ProxyEndpoint);
}
} }
} }
else else
@ -1461,11 +1463,7 @@ namespace transport
if (IsRunning ()) if (IsRunning ())
{ {
m_TerminationTimer.cancel (); m_TerminationTimer.cancel ();
if(m_ProxyEndpoint) m_ProxyEndpoint = nullptr;
{
delete m_ProxyEndpoint;
m_ProxyEndpoint = nullptr;
}
} }
StopIOService (); StopIOService ();
} }

4
libi2pd/NTCP2.h

@ -274,11 +274,11 @@ namespace transport
std::map<i2p::data::IdentHash, std::shared_ptr<NTCP2Session> > m_NTCP2Sessions; std::map<i2p::data::IdentHash, std::shared_ptr<NTCP2Session> > m_NTCP2Sessions;
std::list<std::shared_ptr<NTCP2Session> > m_PendingIncomingSessions; std::list<std::shared_ptr<NTCP2Session> > m_PendingIncomingSessions;
ProxyType m_ProxyType; ProxyType m_ProxyType =eNoProxy;
std::string m_ProxyAddress; std::string m_ProxyAddress;
uint16_t m_ProxyPort; uint16_t m_ProxyPort;
boost::asio::ip::tcp::resolver m_Resolver; boost::asio::ip::tcp::resolver m_Resolver;
boost::asio::ip::tcp::endpoint * m_ProxyEndpoint; std::unique_ptr<boost::asio::ip::tcp::endpoint> m_ProxyEndpoint;
public: public:

Loading…
Cancel
Save