Browse Source

NTCP2 proxy

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

40
libi2pd/NTCP2.cpp

@ -1177,14 +1177,16 @@ namespace transport @@ -1177,14 +1177,16 @@ namespace transport
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();
conn->Terminate();
return;
}
if(m_ProxyType == eSocksProxy)
switch (m_ProxyType)
{
case eSocksProxy:
{
// TODO: support username/password auth etc
uint8_t buff[3] = {0x05, 0x01, 0x00};
@ -1196,7 +1198,8 @@ namespace transport @@ -1196,7 +1198,8 @@ namespace transport
}
});
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);
if(ec)
@ -1226,8 +1229,9 @@ namespace transport @@ -1226,8 +1229,9 @@ namespace transport
timer->cancel();
conn->Terminate();
});
break;
}
else if(m_ProxyType == eHTTPProxy)
case eHTTPProxy:
{
i2p::http::HTTPReq req;
req.method = "CONNECT";
@ -1248,7 +1252,9 @@ namespace transport @@ -1248,7 +1252,9 @@ namespace transport
});
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",
[=] (const boost::system::error_code & ec, std::size_t transferred)
{
if(ec)
{
LogPrint(eLogError, "NTCP2: http proxy read error ", ec.message());
@ -1280,17 +1286,16 @@ namespace transport @@ -1280,17 +1286,16 @@ namespace transport
delete readbuff;
}
});
break;
}
else
default:
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)
{
if(m_ProxyEndpoint == nullptr)
{
return;
}
if(!m_ProxyEndpoint) return
GetService().post([=]() {
if (this->AddNTCP2Session (conn))
{
@ -1359,9 +1364,8 @@ namespace transport @@ -1359,9 +1364,8 @@ namespace transport
}
NTCP2Server::NTCP2Server ():
RunnableServiceWithWork ("NTCP2"),
m_ProxyType(eNoProxy), m_Resolver(GetService ()), m_ProxyEndpoint(nullptr),
m_TerminationTimer (GetService ())
RunnableServiceWithWork ("NTCP2"), m_TerminationTimer (GetService ()),
m_Resolver(GetService ())
{
}
@ -1388,11 +1392,9 @@ namespace transport @@ -1388,11 +1392,9 @@ namespace transport
}
else
{
m_ProxyEndpoint = new boost::asio::ip::tcp::endpoint(*itr);
m_ProxyEndpoint.reset (new boost::asio::ip::tcp::endpoint(*itr));
if (m_ProxyEndpoint)
{
LogPrint(eLogError, "NTCP2: m_ProxyEndpoint %s", m_ProxyEndpoint);
}
LogPrint(eLogError, "NTCP2: m_ProxyEndpoint ", *m_ProxyEndpoint);
}
}
else
@ -1461,12 +1463,8 @@ namespace transport @@ -1461,12 +1463,8 @@ namespace transport
if (IsRunning ())
{
m_TerminationTimer.cancel ();
if(m_ProxyEndpoint)
{
delete m_ProxyEndpoint;
m_ProxyEndpoint = nullptr;
}
}
StopIOService ();
}

4
libi2pd/NTCP2.h

@ -274,11 +274,11 @@ namespace transport @@ -274,11 +274,11 @@ namespace transport
std::map<i2p::data::IdentHash, std::shared_ptr<NTCP2Session> > m_NTCP2Sessions;
std::list<std::shared_ptr<NTCP2Session> > m_PendingIncomingSessions;
ProxyType m_ProxyType;
ProxyType m_ProxyType =eNoProxy;
std::string m_ProxyAddress;
uint16_t m_ProxyPort;
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:

Loading…
Cancel
Save