Browse Source

show actual IP addresses for proxy connections

pull/1638/head
orignal 4 years ago
parent
commit
b1fcd4d27b
  1. 8
      daemon/HTTPServer.cpp
  2. 56
      libi2pd/NTCP2.cpp
  3. 20
      libi2pd/NTCP2.h
  4. 17
      libi2pd/Transports.cpp

8
daemon/HTTPServer.cpp

@ -706,23 +706,23 @@ namespace http {
std::stringstream tmp_s, tmp_s6; uint16_t cnt = 0, cnt6 = 0; std::stringstream tmp_s, tmp_s6; uint16_t cnt = 0, cnt6 = 0;
for (const auto& it: sessions ) 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 << "<div class=\"listitem\">\r\n"; tmp_s << "<div class=\"listitem\">\r\n";
if (it.second->IsOutgoing ()) tmp_s << " &#8658; "; if (it.second->IsOutgoing ()) tmp_s << " &#8658; ";
tmp_s << i2p::data::GetIdentHashAbbreviation (it.second->GetRemoteIdentity ()->GetIdentHash ()) << ": " 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 << " &#8658; "; if (!it.second->IsOutgoing ()) tmp_s << " &#8658; ";
tmp_s << " [" << it.second->GetNumSentBytes () << ":" << it.second->GetNumReceivedBytes () << "]"; tmp_s << " [" << it.second->GetNumSentBytes () << ":" << it.second->GetNumReceivedBytes () << "]";
tmp_s << "</div>\r\n" << std::endl; tmp_s << "</div>\r\n" << std::endl;
cnt++; 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 << "<div class=\"listitem\">\r\n"; tmp_s6 << "<div class=\"listitem\">\r\n";
if (it.second->IsOutgoing ()) tmp_s6 << " &#8658; "; if (it.second->IsOutgoing ()) tmp_s6 << " &#8658; ";
tmp_s6 << i2p::data::GetIdentHashAbbreviation (it.second->GetRemoteIdentity ()->GetIdentHash ()) << ": " 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 << " &#8658; "; if (!it.second->IsOutgoing ()) tmp_s6 << " &#8658; ";
tmp_s6 << " [" << it.second->GetNumSentBytes () << ":" << it.second->GetNumReceivedBytes () << "]"; tmp_s6 << " [" << it.second->GetNumSentBytes () << ":" << it.second->GetNumReceivedBytes () << "]";
tmp_s6 << "</div>\r\n" << std::endl; tmp_s6 << "</div>\r\n" << std::endl;

56
libi2pd/NTCP2.cpp

@ -339,6 +339,7 @@ namespace transport
{ {
memcpy (m_Establisher->m_RemoteStaticKey, addr->ntcp2->staticKey, 32); memcpy (m_Establisher->m_RemoteStaticKey, addr->ntcp2->staticKey, 32);
memcpy (m_Establisher->m_IV, addr->ntcp2->iv, 16); memcpy (m_Establisher->m_IV, addr->ntcp2->iv, 16);
m_RemoteEndpoint = boost::asio::ip::tcp::endpoint (addr->host, addr->port);
} }
else else
LogPrint (eLogWarning, "NTCP2: Missing NTCP2 parameters"); LogPrint (eLogWarning, "NTCP2: Missing NTCP2 parameters");
@ -1274,10 +1275,15 @@ namespace transport
return nullptr; return nullptr;
} }
void NTCP2Server::Connect(const boost::asio::ip::address & address, uint16_t port, std::shared_ptr<NTCP2Session> conn) void NTCP2Server::Connect(std::shared_ptr<NTCP2Session> conn)
{ {
LogPrint (eLogDebug, "NTCP2: Connecting to ", address ,":", port); if (!conn || conn->GetRemoteEndpoint ().address ().is_unspecified ())
GetService ().post([this, address, port, conn]() {
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)) if (this->AddNTCP2Session (conn))
{ {
@ -1295,7 +1301,7 @@ namespace transport
conn->Terminate (); 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 else
conn->Terminate (); conn->Terminate ();
@ -1312,7 +1318,7 @@ namespace transport
} }
else else
{ {
LogPrint (eLogDebug, "NTCP2: Connected to ", conn->GetSocket ().remote_endpoint ()); LogPrint (eLogDebug, "NTCP2: Connected to ", conn->GetRemoteEndpoint ());
conn->ClientLogin (); conn->ClientLogin ();
} }
} }
@ -1328,6 +1334,7 @@ namespace transport
LogPrint (eLogDebug, "NTCP2: Connected from ", ep); LogPrint (eLogDebug, "NTCP2: Connected from ", ep);
if (conn) if (conn)
{ {
conn->SetRemoteEndpoint (ep);
conn->ServerLogin (); conn->ServerLogin ();
m_PendingIncomingSessions.push_back (conn); m_PendingIncomingSessions.push_back (conn);
conn = nullptr; conn = nullptr;
@ -1361,6 +1368,7 @@ namespace transport
LogPrint (eLogDebug, "NTCP2: Connected from ", ep); LogPrint (eLogDebug, "NTCP2: Connected from ", ep);
if (conn) if (conn)
{ {
conn->SetRemoteEndpoint (ep);
conn->ServerLogin (); conn->ServerLogin ();
m_PendingIncomingSessions.push_back (conn); 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<NTCP2Session> conn) void NTCP2Server::ConnectWithProxy (std::shared_ptr<NTCP2Session> conn)
{ {
if(!m_ProxyEndpoint) return; if(!m_ProxyEndpoint) return;
GetService().post([this, host, port, addrtype, conn]() { GetService().post([this, conn]()
{
if (this->AddNTCP2Session (conn)) if (this->AddNTCP2Session (conn))
{ {
auto timer = std::make_shared<boost::asio::deadline_timer>(GetService()); auto timer = std::make_shared<boost::asio::deadline_timer>(GetService());
auto timeout = NTCP2_CONNECT_TIMEOUT * 5; auto timeout = NTCP2_CONNECT_TIMEOUT * 5;
conn->SetTerminationTimeout(timeout * 2); conn->SetTerminationTimeout(timeout * 2);
@ -1435,7 +1443,7 @@ namespace transport
conn->Terminate (); 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; 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)
{ {
if (ecode) if (ecode)
{ {
@ -1473,7 +1481,7 @@ namespace transport
}); });
auto readbuff = std::make_shared<std::vector<uint8_t> >(2); auto readbuff = std::make_shared<std::vector<uint8_t> >(2);
boost::asio::async_read(conn->GetSocket(), boost::asio::buffer(readbuff->data (), 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) if(ec)
{ {
@ -1486,7 +1494,7 @@ namespace transport
{ {
if((*readbuff)[1] == 0x00) if((*readbuff)[1] == 0x00)
{ {
AfterSocksHandshake(conn, timer, host, port, addrtype); AfterSocksHandshake(conn, timer);
return; return;
} }
else if ((*readbuff)[1] == 0xff) else if ((*readbuff)[1] == 0xff)
@ -1506,13 +1514,14 @@ namespace transport
} }
case eHTTPProxy: case eHTTPProxy:
{ {
auto& ep = conn->GetRemoteEndpoint ();
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(ep.address ().is_v6 ())
req.uri = "[" + host + "]:" + std::to_string(port); req.uri = "[" + ep.address ().to_string() + "]:" + std::to_string(ep.port ());
else else
req.uri = host + ":" + std::to_string(port); req.uri = ep.address ().to_string() + ":" + std::to_string(ep.port ());
boost::asio::streambuf writebuff; boost::asio::streambuf writebuff;
std::ostream out(&writebuff); std::ostream out(&writebuff);
@ -1566,7 +1575,7 @@ namespace transport
} }
} }
void NTCP2Server::AfterSocksHandshake(std::shared_ptr<NTCP2Session> conn, std::shared_ptr<boost::asio::deadline_timer> timer, const std::string & host, uint16_t port, RemoteAddressType addrtype) void NTCP2Server::AfterSocksHandshake(std::shared_ptr<NTCP2Session> conn, std::shared_ptr<boost::asio::deadline_timer> timer)
{ {
// build request // build request
size_t sz = 6; // header + port size_t sz = 6; // header + port
@ -1576,27 +1585,28 @@ namespace transport
(*buff)[1] = 0x01; (*buff)[1] = 0x01;
(*buff)[2] = 0x00; (*buff)[2] = 0x00;
if(addrtype == eIP4Address) auto& ep = conn->GetRemoteEndpoint ();
if(ep.address ().is_v4 ())
{ {
(*buff)[3] = 0x01; (*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; sz += 4;
memcpy(buff->data () + 4, addrbytes.data(), 4); memcpy(buff->data () + 4, addrbytes.data(), 4);
} }
else if (addrtype == eIP6Address) else if (ep.address ().is_v6 ())
{ {
(*buff)[3] = 0x04; (*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; sz += 16;
memcpy(buff->data () + 4, addrbytes.data(), 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 // 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; 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(), 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) [buff](const boost::system::error_code & ec, std::size_t written)
{ {

20
libi2pd/NTCP2.h

@ -134,6 +134,8 @@ namespace transport
void Close () { m_Socket.close (); }; // for accept void Close () { m_Socket.close (); }; // for accept
boost::asio::ip::tcp::socket& GetSocket () { return m_Socket; }; 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 IsEstablished () const { return m_IsEstablished; };
bool IsTerminated () const { return m_IsTerminated; }; bool IsTerminated () const { return m_IsTerminated; };
@ -189,6 +191,7 @@ namespace transport
NTCP2Server& m_Server; NTCP2Server& m_Server;
boost::asio::ip::tcp::socket m_Socket; boost::asio::ip::tcp::socket m_Socket;
boost::asio::ip::tcp::endpoint m_RemoteEndpoint;
bool m_IsEstablished, m_IsTerminated; bool m_IsEstablished, m_IsTerminated;
std::unique_ptr<NTCP2Establisher> m_Establisher; std::unique_ptr<NTCP2Establisher> m_Establisher;
@ -221,13 +224,6 @@ namespace transport
{ {
public: public:
enum RemoteAddressType
{
eIP4Address,
eIP6Address,
eHostname
};
enum ProxyType enum ProxyType
{ {
eNoProxy, eNoProxy,
@ -246,11 +242,8 @@ namespace transport
void RemoveNTCP2Session (std::shared_ptr<NTCP2Session> session); void RemoveNTCP2Session (std::shared_ptr<NTCP2Session> session);
std::shared_ptr<NTCP2Session> FindNTCP2Session (const i2p::data::IdentHash& ident); std::shared_ptr<NTCP2Session> FindNTCP2Session (const i2p::data::IdentHash& ident);
void ConnectWithProxy (const std::string& addr, uint16_t port, RemoteAddressType addrtype, std::shared_ptr<NTCP2Session> conn); void ConnectWithProxy (std::shared_ptr<NTCP2Session> conn);
void Connect(const boost::asio::ip::address & address, uint16_t port, std::shared_ptr<NTCP2Session> conn); void Connect(std::shared_ptr<NTCP2Session> conn);
void AfterSocksHandshake(std::shared_ptr<NTCP2Session> conn, std::shared_ptr<boost::asio::deadline_timer> timer, const std::string & host, uint16_t port, RemoteAddressType addrtype);
bool UsingProxy() const { return m_ProxyType != eNoProxy; }; bool UsingProxy() const { return m_ProxyType != eNoProxy; };
void UseProxy(ProxyType proxy, const std::string & address, uint16_t port); void UseProxy(ProxyType proxy, const std::string & address, uint16_t port);
@ -261,7 +254,8 @@ namespace transport
void HandleAcceptV6 (std::shared_ptr<NTCP2Session> conn, const boost::system::error_code& error); void HandleAcceptV6 (std::shared_ptr<NTCP2Session> conn, const boost::system::error_code& error);
void HandleConnect (const boost::system::error_code& ecode, std::shared_ptr<NTCP2Session> conn, std::shared_ptr<boost::asio::deadline_timer> timer); void HandleConnect (const boost::system::error_code& ecode, std::shared_ptr<NTCP2Session> conn, std::shared_ptr<boost::asio::deadline_timer> timer);
void 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 adddrtype); void HandleProxyConnect(const boost::system::error_code& ecode, std::shared_ptr<NTCP2Session> conn, std::shared_ptr<boost::asio::deadline_timer> timer);
void AfterSocksHandshake(std::shared_ptr<NTCP2Session> conn, std::shared_ptr<boost::asio::deadline_timer> timer);
// timer // timer
void ScheduleTermination (); void ScheduleTermination ();

17
libi2pd/Transports.cpp

@ -418,19 +418,10 @@ namespace transport
if (address) if (address)
{ {
auto s = std::make_shared<NTCP2Session> (*m_NTCP2Server, peer.router, address); auto s = std::make_shared<NTCP2Session> (*m_NTCP2Server, peer.router, address);
if( m_NTCP2Server->UsingProxy())
if(m_NTCP2Server->UsingProxy()) m_NTCP2Server->ConnectWithProxy(s);
{
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);
}
else else
m_NTCP2Server->Connect (address->host, address->port, s); m_NTCP2Server->Connect (s);
return true; return true;
} }
} }
@ -480,7 +471,7 @@ namespace transport
if (address) if (address)
{ {
auto s = std::make_shared<NTCP2Session> (*m_NTCP2Server, peer.router, address); auto s = std::make_shared<NTCP2Session> (*m_NTCP2Server, peer.router, address);
m_NTCP2Server->Connect (address->host, address->port, s); m_NTCP2Server->Connect (s);
return true; return true;
} }
} }

Loading…
Cancel
Save