diff --git a/daemon/HTTPServer.cpp b/daemon/HTTPServer.cpp
index aba30fd7..0847f85d 100644
--- a/daemon/HTTPServer.cpp
+++ b/daemon/HTTPServer.cpp
@@ -706,23 +706,23 @@ namespace http {
std::stringstream tmp_s, tmp_s6; uint16_t cnt = 0, cnt6 = 0;
for (const auto& it: sessions )
{
- if (it.second && it.second->IsEstablished () && !it.second->GetSocket ().remote_endpoint ().address ().is_v6 ())
+ if (it.second && it.second->IsEstablished () && !it.second->GetRemoteEndpoint ().address ().is_v6 ())
{
tmp_s << "
\r\n";
if (it.second->IsOutgoing ()) tmp_s << " ⇒ ";
tmp_s << i2p::data::GetIdentHashAbbreviation (it.second->GetRemoteIdentity ()->GetIdentHash ()) << ": "
- << it.second->GetSocket ().remote_endpoint().address ().to_string ();
+ << it.second->GetRemoteEndpoint ().address ().to_string ();
if (!it.second->IsOutgoing ()) tmp_s << " ⇒ ";
tmp_s << " [" << it.second->GetNumSentBytes () << ":" << it.second->GetNumReceivedBytes () << "]";
tmp_s << "
\r\n" << std::endl;
cnt++;
}
- if (it.second && it.second->IsEstablished () && it.second->GetSocket ().remote_endpoint ().address ().is_v6 ())
+ if (it.second && it.second->IsEstablished () && it.second->GetRemoteEndpoint ().address ().is_v6 ())
{
tmp_s6 << "\r\n";
if (it.second->IsOutgoing ()) tmp_s6 << " ⇒ ";
tmp_s6 << i2p::data::GetIdentHashAbbreviation (it.second->GetRemoteIdentity ()->GetIdentHash ()) << ": "
- << "[" << it.second->GetSocket ().remote_endpoint().address ().to_string () << "]";
+ << "[" << it.second->GetRemoteEndpoint ().address ().to_string () << "]";
if (!it.second->IsOutgoing ()) tmp_s6 << " ⇒ ";
tmp_s6 << " [" << it.second->GetNumSentBytes () << ":" << it.second->GetNumReceivedBytes () << "]";
tmp_s6 << "
\r\n" << std::endl;
diff --git a/libi2pd/NTCP2.cpp b/libi2pd/NTCP2.cpp
index dc485d5b..dd777794 100644
--- a/libi2pd/NTCP2.cpp
+++ b/libi2pd/NTCP2.cpp
@@ -339,6 +339,7 @@ namespace transport
{
memcpy (m_Establisher->m_RemoteStaticKey, addr->ntcp2->staticKey, 32);
memcpy (m_Establisher->m_IV, addr->ntcp2->iv, 16);
+ m_RemoteEndpoint = boost::asio::ip::tcp::endpoint (addr->host, addr->port);
}
else
LogPrint (eLogWarning, "NTCP2: Missing NTCP2 parameters");
@@ -1274,10 +1275,15 @@ namespace transport
return nullptr;
}
- void NTCP2Server::Connect(const boost::asio::ip::address & address, uint16_t port, std::shared_ptr conn)
+ void NTCP2Server::Connect(std::shared_ptr conn)
{
- LogPrint (eLogDebug, "NTCP2: Connecting to ", address ,":", port);
- GetService ().post([this, address, port, conn]()
+ if (!conn || conn->GetRemoteEndpoint ().address ().is_unspecified ())
+ {
+ LogPrint (eLogError, "NTCP2: Can't connect to unspecified address");
+ return;
+ }
+ LogPrint (eLogDebug, "NTCP2: Connecting to ", conn->GetRemoteEndpoint ());
+ GetService ().post([this, conn]()
{
if (this->AddNTCP2Session (conn))
{
@@ -1295,7 +1301,7 @@ namespace transport
conn->Terminate ();
}
});
- conn->GetSocket ().async_connect (boost::asio::ip::tcp::endpoint (address, port), std::bind (&NTCP2Server::HandleConnect, this, std::placeholders::_1, conn, timer));
+ conn->GetSocket ().async_connect (conn->GetRemoteEndpoint (), std::bind (&NTCP2Server::HandleConnect, this, std::placeholders::_1, conn, timer));
}
else
conn->Terminate ();
@@ -1312,7 +1318,7 @@ namespace transport
}
else
{
- LogPrint (eLogDebug, "NTCP2: Connected to ", conn->GetSocket ().remote_endpoint ());
+ LogPrint (eLogDebug, "NTCP2: Connected to ", conn->GetRemoteEndpoint ());
conn->ClientLogin ();
}
}
@@ -1328,6 +1334,7 @@ namespace transport
LogPrint (eLogDebug, "NTCP2: Connected from ", ep);
if (conn)
{
+ conn->SetRemoteEndpoint (ep);
conn->ServerLogin ();
m_PendingIncomingSessions.push_back (conn);
conn = nullptr;
@@ -1361,6 +1368,7 @@ namespace transport
LogPrint (eLogDebug, "NTCP2: Connected from ", ep);
if (conn)
{
+ conn->SetRemoteEndpoint (ep);
conn->ServerLogin ();
m_PendingIncomingSessions.push_back (conn);
}
@@ -1415,13 +1423,13 @@ namespace transport
}
}
- void NTCP2Server::ConnectWithProxy (const std::string& host, uint16_t port, RemoteAddressType addrtype, std::shared_ptr conn)
+ void NTCP2Server::ConnectWithProxy (std::shared_ptr conn)
{
if(!m_ProxyEndpoint) return;
- GetService().post([this, host, port, addrtype, conn]() {
+ GetService().post([this, conn]()
+ {
if (this->AddNTCP2Session (conn))
{
-
auto timer = std::make_shared(GetService());
auto timeout = NTCP2_CONNECT_TIMEOUT * 5;
conn->SetTerminationTimeout(timeout * 2);
@@ -1435,7 +1443,7 @@ namespace transport
conn->Terminate ();
}
});
- conn->GetSocket ().async_connect (*m_ProxyEndpoint, std::bind (&NTCP2Server::HandleProxyConnect, this, std::placeholders::_1, conn, timer, host, port, addrtype));
+ conn->GetSocket ().async_connect (*m_ProxyEndpoint, std::bind (&NTCP2Server::HandleProxyConnect, this, std::placeholders::_1, conn, timer));
}
});
}
@@ -1447,7 +1455,7 @@ namespace transport
m_ProxyPort = port;
}
- void NTCP2Server::HandleProxyConnect(const boost::system::error_code& ecode, std::shared_ptr conn, std::shared_ptr timer, const std::string & host, uint16_t port, RemoteAddressType addrtype)
+ void NTCP2Server::HandleProxyConnect(const boost::system::error_code& ecode, std::shared_ptr conn, std::shared_ptr timer)
{
if (ecode)
{
@@ -1473,7 +1481,7 @@ namespace transport
});
auto readbuff = std::make_shared >(2);
boost::asio::async_read(conn->GetSocket(), boost::asio::buffer(readbuff->data (), 2),
- [this, readbuff, timer, conn, host, port, addrtype](const boost::system::error_code & ec, std::size_t transferred)
+ [this, readbuff, timer, conn](const boost::system::error_code & ec, std::size_t transferred)
{
if(ec)
{
@@ -1486,7 +1494,7 @@ namespace transport
{
if((*readbuff)[1] == 0x00)
{
- AfterSocksHandshake(conn, timer, host, port, addrtype);
+ AfterSocksHandshake(conn, timer);
return;
}
else if ((*readbuff)[1] == 0xff)
@@ -1506,13 +1514,14 @@ namespace transport
}
case eHTTPProxy:
{
+ auto& ep = conn->GetRemoteEndpoint ();
i2p::http::HTTPReq req;
req.method = "CONNECT";
req.version ="HTTP/1.1";
- if(addrtype == eIP6Address)
- req.uri = "[" + host + "]:" + std::to_string(port);
+ if(ep.address ().is_v6 ())
+ req.uri = "[" + ep.address ().to_string() + "]:" + std::to_string(ep.port ());
else
- req.uri = host + ":" + std::to_string(port);
+ req.uri = ep.address ().to_string() + ":" + std::to_string(ep.port ());
boost::asio::streambuf writebuff;
std::ostream out(&writebuff);
@@ -1566,7 +1575,7 @@ namespace transport
}
}
- void NTCP2Server::AfterSocksHandshake(std::shared_ptr conn, std::shared_ptr timer, const std::string & host, uint16_t port, RemoteAddressType addrtype)
+ void NTCP2Server::AfterSocksHandshake(std::shared_ptr conn, std::shared_ptr timer)
{
// build request
size_t sz = 6; // header + port
@@ -1576,27 +1585,28 @@ namespace transport
(*buff)[1] = 0x01;
(*buff)[2] = 0x00;
- if(addrtype == eIP4Address)
+ auto& ep = conn->GetRemoteEndpoint ();
+ if(ep.address ().is_v4 ())
{
(*buff)[3] = 0x01;
- auto addrbytes = boost::asio::ip::address::from_string(host).to_v4().to_bytes();
+ auto addrbytes = ep.address ().to_v4().to_bytes();
sz += 4;
memcpy(buff->data () + 4, addrbytes.data(), 4);
}
- else if (addrtype == eIP6Address)
+ else if (ep.address ().is_v6 ())
{
(*buff)[3] = 0x04;
- auto addrbytes = boost::asio::ip::address::from_string(host).to_v6().to_bytes();
+ auto addrbytes = ep.address ().to_v6().to_bytes();
sz += 16;
memcpy(buff->data () + 4, addrbytes.data(), 16);
}
- else if (addrtype == eHostname)
+ else
{
// We mustn't really fall here because all connections are made to IP addresses
- LogPrint(eLogError, "NTCP2: Tried to connect to domain name via socks proxy");
+ LogPrint(eLogError, "NTCP2: Tried to connect to unexpected address via proxy");
return;
}
- htobe16buf(buff->data () + sz - 2, port);
+ htobe16buf(buff->data () + sz - 2, ep.port ());
boost::asio::async_write(conn->GetSocket(), boost::asio::buffer(buff->data (), sz), boost::asio::transfer_all(),
[buff](const boost::system::error_code & ec, std::size_t written)
{
diff --git a/libi2pd/NTCP2.h b/libi2pd/NTCP2.h
index a7708872..c3909225 100644
--- a/libi2pd/NTCP2.h
+++ b/libi2pd/NTCP2.h
@@ -134,6 +134,8 @@ namespace transport
void Close () { m_Socket.close (); }; // for accept
boost::asio::ip::tcp::socket& GetSocket () { return m_Socket; };
+ const boost::asio::ip::tcp::endpoint& GetRemoteEndpoint () { return m_RemoteEndpoint; };
+ void SetRemoteEndpoint (const boost::asio::ip::tcp::endpoint& ep) { m_RemoteEndpoint = ep; };
bool IsEstablished () const { return m_IsEstablished; };
bool IsTerminated () const { return m_IsTerminated; };
@@ -189,6 +191,7 @@ namespace transport
NTCP2Server& m_Server;
boost::asio::ip::tcp::socket m_Socket;
+ boost::asio::ip::tcp::endpoint m_RemoteEndpoint;
bool m_IsEstablished, m_IsTerminated;
std::unique_ptr m_Establisher;
@@ -221,13 +224,6 @@ namespace transport
{
public:
- enum RemoteAddressType
- {
- eIP4Address,
- eIP6Address,
- eHostname
- };
-
enum ProxyType
{
eNoProxy,
@@ -246,11 +242,8 @@ namespace transport
void RemoveNTCP2Session (std::shared_ptr session);
std::shared_ptr FindNTCP2Session (const i2p::data::IdentHash& ident);
- void ConnectWithProxy (const std::string& addr, uint16_t port, RemoteAddressType addrtype, std::shared_ptr conn);
- void Connect(const boost::asio::ip::address & address, uint16_t port, std::shared_ptr conn);
-
- void AfterSocksHandshake(std::shared_ptr conn, std::shared_ptr timer, const std::string & host, uint16_t port, RemoteAddressType addrtype);
-
+ void ConnectWithProxy (std::shared_ptr conn);
+ void Connect(std::shared_ptr conn);
bool UsingProxy() const { return m_ProxyType != eNoProxy; };
void UseProxy(ProxyType proxy, const std::string & address, uint16_t port);
@@ -261,8 +254,9 @@ namespace transport
void HandleAcceptV6 (std::shared_ptr conn, const boost::system::error_code& error);
void HandleConnect (const boost::system::error_code& ecode, std::shared_ptr conn, std::shared_ptr timer);
- void HandleProxyConnect(const boost::system::error_code& ecode, std::shared_ptr conn, std::shared_ptr timer, const std::string & host, uint16_t port, RemoteAddressType adddrtype);
-
+ void HandleProxyConnect(const boost::system::error_code& ecode, std::shared_ptr conn, std::shared_ptr timer);
+ void AfterSocksHandshake(std::shared_ptr conn, std::shared_ptr timer);
+
// timer
void ScheduleTermination ();
void HandleTerminationTimer (const boost::system::error_code& ecode);
diff --git a/libi2pd/Transports.cpp b/libi2pd/Transports.cpp
index 2eadda85..6bed627f 100644
--- a/libi2pd/Transports.cpp
+++ b/libi2pd/Transports.cpp
@@ -418,19 +418,10 @@ namespace transport
if (address)
{
auto s = std::make_shared (*m_NTCP2Server, peer.router, address);
-
- if(m_NTCP2Server->UsingProxy())
- {
- NTCP2Server::RemoteAddressType remote = NTCP2Server::eIP4Address;
- std::string addr = address->host.to_string();
-
- if(address->host.is_v6())
- remote = NTCP2Server::eIP6Address;
-
- m_NTCP2Server->ConnectWithProxy(addr, address->port, remote, s);
- }
+ if( m_NTCP2Server->UsingProxy())
+ m_NTCP2Server->ConnectWithProxy(s);
else
- m_NTCP2Server->Connect (address->host, address->port, s);
+ m_NTCP2Server->Connect (s);
return true;
}
}
@@ -480,7 +471,7 @@ namespace transport
if (address)
{
auto s = std::make_shared (*m_NTCP2Server, peer.router, address);
- m_NTCP2Server->Connect (address->host, address->port, s);
+ m_NTCP2Server->Connect (s);
return true;
}
}