From 7f3a04a72f81e078cc8f3f30960a36fede7b05fa Mon Sep 17 00:00:00 2001 From: orignal Date: Thu, 3 Oct 2024 18:44:09 -0400 Subject: [PATCH] select random introducer session. don't update creation time --- libi2pd/SSU2.cpp | 45 +++++++++++++++++---------------------------- libi2pd/SSU2.h | 5 ++--- 2 files changed, 19 insertions(+), 31 deletions(-) diff --git a/libi2pd/SSU2.cpp b/libi2pd/SSU2.cpp index 1076a2de..87d45d04 100644 --- a/libi2pd/SSU2.cpp +++ b/libi2pd/SSU2.cpp @@ -1222,31 +1222,25 @@ namespace transport } std::vector > SSU2Server::FindIntroducers (int maxNumIntroducers, - bool v4, const std::unordered_set& excluded) const + bool v4, const std::unordered_set& excluded) { std::vector > ret; - if (maxNumIntroducers <= 0) return ret; - auto newer = [](const std::shared_ptr& s1, const std::shared_ptr& s2) -> bool - { - auto t1 = s1->GetCreationTime (), t2 = s2->GetCreationTime (); - return (t1 != t2) ? (t1 > t2) : (s1->GetConnID () > s2->GetConnID ()); - }; - std::set, decltype (newer)> introducers(newer); + if (maxNumIntroducers <= 0 || m_Sessions.empty ()) return ret; + + std::vector > eligible; + eligible.reserve (m_Sessions.size ()/2); + auto ts = i2p::util::GetSecondsSinceEpoch (); for (const auto& s : m_Sessions) { if (s.second->IsEstablished () && (s.second->GetRelayTag () && s.second->IsOutgoing ()) && + ts < s.second->GetCreationTime () + SSU2_TO_INTRODUCER_SESSION_DURATION/2 && !excluded.count (s.second->GetRemoteIdentity ()->GetIdentHash ()) && ((v4 && (s.second->GetRemoteTransports () & i2p::data::RouterInfo::eSSU2V4)) || (!v4 && (s.second->GetRemoteTransports () & i2p::data::RouterInfo::eSSU2V6)))) - introducers.insert (s.second); + eligible.push_back (s.second); } - int i = 0; - for (auto it: introducers) - { - ret.push_back (it); - i++; - if (i >= maxNumIntroducers) break; - } + + std::sample (eligible.begin(), eligible.end(), std::back_inserter(ret), maxNumIntroducers, m_Rng); return ret; } @@ -1293,33 +1287,28 @@ namespace transport if (newList.size () < SSU2_MAX_NUM_INTRODUCERS) { auto sessions = FindIntroducers (SSU2_MAX_NUM_INTRODUCERS - newList.size (), v4, excluded); - if (sessions.empty () && !introducers.empty ()) + if (sessions.empty () && !impliedList.empty ()) { - // bump creation time for previous introducers if no new sessions found LogPrint (eLogDebug, "SSU2: No new introducers found. Trying to reuse existing"); - impliedList.clear (); - for (const auto& it : introducers) + for (const auto& it : impliedList) { auto session = FindSession (it.first); - if (session && session->IsEstablished () && session->GetRelayTag () && session->IsOutgoing ()) + if (session) { - session->SetCreationTime (session->GetCreationTime () + SSU2_TO_INTRODUCER_SESSION_DURATION); if (std::find_if (newList.begin (), newList.end (), [&ident = it.first](const auto& s){ return ident == s.first; }) == newList.end ()) sessions.push_back (session); } } + impliedList.clear (); } for (const auto& it : sessions) { - uint32_t tag = it->GetRelayTag (); - auto extraTime = std::min ((int)(ts - it->GetCreationTime ()), SSU2_TO_INTRODUCER_SESSION_EXPIRATION_VARIANCE); - if( extraTime > 1) - it->SetCreationTime (it->GetCreationTime () + m_Rng () % extraTime); + uint32_t tag = it->GetRelayTag (); uint32_t exp = it->GetCreationTime () + SSU2_TO_INTRODUCER_SESSION_EXPIRATION; - if (!tag || ts + SSU2_TO_INTRODUCER_SESSION_DURATION/2 > exp) - continue; // don't pick too old session for introducer + if (!tag && ts >= exp) + continue; // don't publish expired introducer i2p::data::RouterInfo::Introducer introducer; introducer.iTag = tag; introducer.iH = it->GetRemoteIdentity ()->GetIdentHash (); diff --git a/libi2pd/SSU2.h b/libi2pd/SSU2.h index 93d7e8e2..2b97bd25 100644 --- a/libi2pd/SSU2.h +++ b/libi2pd/SSU2.h @@ -35,9 +35,8 @@ namespace transport const uint64_t SSU2_SOCKET_MAX_BUFFER_SIZE = 4 * 1024 * 1024; const size_t SSU2_MAX_NUM_INTRODUCERS = 3; const size_t SSU2_MIN_RECEIVED_PACKET_SIZE = 40; // 16 byte short header + 8 byte minimum payload + 16 byte MAC - const int SSU2_TO_INTRODUCER_SESSION_DURATION = 3680; // 1 hour + const int SSU2_TO_INTRODUCER_SESSION_DURATION = 3600; // 1 hour const int SSU2_TO_INTRODUCER_SESSION_EXPIRATION = 4800; // 80 minutes - const int SSU2_TO_INTRODUCER_SESSION_EXPIRATION_VARIANCE = 120; // 2 minutes const int SSU2_KEEP_ALIVE_INTERVAL = 15; // in seconds const int SSU2_KEEP_ALIVE_INTERVAL_VARIANCE = 4; // in seconds const int SSU2_PROXY_CONNECT_RETRY_TIMEOUT = 30; // in seconds @@ -160,7 +159,7 @@ namespace transport void ConnectThroughIntroducer (std::shared_ptr session); std::vector > FindIntroducers (int maxNumIntroducers, - bool v4, const std::unordered_set& excluded) const; + bool v4, const std::unordered_set& excluded); void UpdateIntroducers (bool v4); void ScheduleIntroducersUpdateTimer (); void HandleIntroducersUpdateTimer (const boost::system::error_code& ecode, bool v4);