From 977967f7934bc85b36b6f4ec6fc5c88c2d238b78 Mon Sep 17 00:00:00 2001 From: orignal Date: Mon, 28 Jul 2025 15:51:47 -0400 Subject: [PATCH] try to resolve host again in server tunnel if failed before --- libi2pd_client/I2PTunnel.cpp | 59 ++++++++++++++++++++++++------------ libi2pd_client/I2PTunnel.h | 5 ++- 2 files changed, 43 insertions(+), 21 deletions(-) diff --git a/libi2pd_client/I2PTunnel.cpp b/libi2pd_client/I2PTunnel.cpp index c1cf247a..6fc7d1bc 100644 --- a/libi2pd_client/I2PTunnel.cpp +++ b/libi2pd_client/I2PTunnel.cpp @@ -738,17 +738,10 @@ namespace client boost::system::error_code ec; auto addr = boost::asio::ip::make_address (m_Address, ec); if (!ec) - { m_Endpoint.address (addr); - Accept (); - } else - { - auto resolver = std::make_shared(GetService ()); - resolver->async_resolve (m_Address, "", - std::bind (&I2PServerTunnel::HandleResolve, this, - std::placeholders::_1, std::placeholders::_2, resolver)); - } + Resolve (nullptr); + Accept (); } void I2PServerTunnel::Stop () @@ -758,13 +751,26 @@ namespace client auto localDestination = GetLocalDestination (); if (localDestination) localDestination->StopAcceptingStreams (); - + if (m_Resolver) + m_Resolver->cancel (); + ClearHandlers (); } - void I2PServerTunnel::HandleResolve (const boost::system::error_code& ecode, boost::asio::ip::tcp::resolver::results_type endpoints, - std::shared_ptr resolver) + bool I2PServerTunnel::Resolve (std::shared_ptr stream) { + if (m_Resolver) return false; // already resolving + m_Resolver = std::make_shared(GetService ()); + m_Resolver->async_resolve (m_Address, "", + std::bind (&I2PServerTunnel::HandleResolve, this, + std::placeholders::_1, std::placeholders::_2, stream)); + return true; + } + + void I2PServerTunnel::HandleResolve (const boost::system::error_code& ecode, boost::asio::ip::tcp::resolver::results_type endpoints, + std::shared_ptr stream) + { + m_Resolver = nullptr; if (!ecode) { bool found = false; @@ -808,7 +814,8 @@ namespace client auto addr = ep.address (); LogPrint (eLogInfo, "I2PTunnel: Server tunnel ", (*endpoints.begin ()).host_name (), " has been resolved to ", addr); m_Endpoint.address (addr); - Accept (); + if (stream) + Connect (stream); } else LogPrint (eLogError, "I2PTunnel: Unable to resolve server tunnel address ", m_Address, ": ", ecode.message ()); @@ -869,16 +876,28 @@ namespace client return; } } - // new connection - auto conn = CreateI2PConnection (stream); - AddHandler (conn); - if (m_LocalAddress) - conn->Connect (*m_LocalAddress); - else - conn->Connect (m_IsUniqueLocal); + if (!m_Endpoint.address ().is_unspecified ()) + Connect (stream); + else if (!Resolve (stream)) + { + LogPrint (eLogWarning, "I2PTunnel: Address ", m_Address, " cann't be resolved. Incoming connection dropped"); + stream->Close (); + return; + } } } + void I2PServerTunnel::Connect (std::shared_ptr stream) + { + // new connection + auto conn = CreateI2PConnection (stream); + AddHandler (conn); + if (m_LocalAddress) + conn->Connect (*m_LocalAddress); + else + conn->Connect (m_IsUniqueLocal); + } + std::shared_ptr I2PServerTunnel::CreateI2PConnection (std::shared_ptr stream) { return std::make_shared (this, stream, GetEndpoint (), m_SSLCtx); diff --git a/libi2pd_client/I2PTunnel.h b/libi2pd_client/I2PTunnel.h index 3fb98c5c..00b95726 100644 --- a/libi2pd_client/I2PTunnel.h +++ b/libi2pd_client/I2PTunnel.h @@ -212,11 +212,13 @@ namespace client private: + bool Resolve (std::shared_ptr stream); void HandleResolve (const boost::system::error_code& ecode, boost::asio::ip::tcp::resolver::results_type endpoints, - std::shared_ptr resolver); + std::shared_ptr stream); void Accept (); void HandleAccept (std::shared_ptr stream); + void Connect (std::shared_ptr stream); virtual std::shared_ptr CreateI2PConnection (std::shared_ptr stream); private: @@ -230,6 +232,7 @@ namespace client bool m_IsAccessList; std::unique_ptr m_LocalAddress; std::shared_ptr m_SSLCtx; + std::shared_ptr m_Resolver; }; class I2PServerTunnelHTTP: public I2PServerTunnel