select random introducer session. don't update creation time

This commit is contained in:
orignal 2024-10-03 18:44:09 -04:00
parent 34f1ba5bd9
commit 7f3a04a72f
2 changed files with 19 additions and 31 deletions

View File

@ -1222,31 +1222,25 @@ namespace transport
} }
std::vector<std::shared_ptr<SSU2Session> > SSU2Server::FindIntroducers (int maxNumIntroducers, std::vector<std::shared_ptr<SSU2Session> > SSU2Server::FindIntroducers (int maxNumIntroducers,
bool v4, const std::unordered_set<i2p::data::IdentHash>& excluded) const bool v4, const std::unordered_set<i2p::data::IdentHash>& excluded)
{ {
std::vector<std::shared_ptr<SSU2Session> > ret; std::vector<std::shared_ptr<SSU2Session> > ret;
if (maxNumIntroducers <= 0) return ret; if (maxNumIntroducers <= 0 || m_Sessions.empty ()) return ret;
auto newer = [](const std::shared_ptr<SSU2Session>& s1, const std::shared_ptr<SSU2Session>& s2) -> bool
{ std::vector<std::shared_ptr<SSU2Session> > eligible;
auto t1 = s1->GetCreationTime (), t2 = s2->GetCreationTime (); eligible.reserve (m_Sessions.size ()/2);
return (t1 != t2) ? (t1 > t2) : (s1->GetConnID () > s2->GetConnID ()); auto ts = i2p::util::GetSecondsSinceEpoch ();
};
std::set<std::shared_ptr<SSU2Session>, decltype (newer)> introducers(newer);
for (const auto& s : m_Sessions) for (const auto& s : m_Sessions)
{ {
if (s.second->IsEstablished () && (s.second->GetRelayTag () && s.second->IsOutgoing ()) && 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 ()) && !excluded.count (s.second->GetRemoteIdentity ()->GetIdentHash ()) &&
((v4 && (s.second->GetRemoteTransports () & i2p::data::RouterInfo::eSSU2V4)) || ((v4 && (s.second->GetRemoteTransports () & i2p::data::RouterInfo::eSSU2V4)) ||
(!v4 && (s.second->GetRemoteTransports () & i2p::data::RouterInfo::eSSU2V6)))) (!v4 && (s.second->GetRemoteTransports () & i2p::data::RouterInfo::eSSU2V6))))
introducers.insert (s.second); eligible.push_back (s.second);
} }
int i = 0;
for (auto it: introducers) std::sample (eligible.begin(), eligible.end(), std::back_inserter(ret), maxNumIntroducers, m_Rng);
{
ret.push_back (it);
i++;
if (i >= maxNumIntroducers) break;
}
return ret; return ret;
} }
@ -1293,33 +1287,28 @@ namespace transport
if (newList.size () < SSU2_MAX_NUM_INTRODUCERS) if (newList.size () < SSU2_MAX_NUM_INTRODUCERS)
{ {
auto sessions = FindIntroducers (SSU2_MAX_NUM_INTRODUCERS - newList.size (), v4, excluded); 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"); LogPrint (eLogDebug, "SSU2: No new introducers found. Trying to reuse existing");
impliedList.clear (); for (const auto& it : impliedList)
for (const auto& it : introducers)
{ {
auto session = FindSession (it.first); 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 (), if (std::find_if (newList.begin (), newList.end (),
[&ident = it.first](const auto& s){ return ident == s.first; }) == newList.end ()) [&ident = it.first](const auto& s){ return ident == s.first; }) == newList.end ())
sessions.push_back (session); sessions.push_back (session);
} }
} }
impliedList.clear ();
} }
for (const auto& it : sessions) for (const auto& it : sessions)
{ {
uint32_t tag = it->GetRelayTag (); 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 exp = it->GetCreationTime () + SSU2_TO_INTRODUCER_SESSION_EXPIRATION; uint32_t exp = it->GetCreationTime () + SSU2_TO_INTRODUCER_SESSION_EXPIRATION;
if (!tag || ts + SSU2_TO_INTRODUCER_SESSION_DURATION/2 > exp) if (!tag && ts >= exp)
continue; // don't pick too old session for introducer continue; // don't publish expired introducer
i2p::data::RouterInfo::Introducer introducer; i2p::data::RouterInfo::Introducer introducer;
introducer.iTag = tag; introducer.iTag = tag;
introducer.iH = it->GetRemoteIdentity ()->GetIdentHash (); introducer.iH = it->GetRemoteIdentity ()->GetIdentHash ();

View File

@ -35,9 +35,8 @@ namespace transport
const uint64_t SSU2_SOCKET_MAX_BUFFER_SIZE = 4 * 1024 * 1024; const uint64_t SSU2_SOCKET_MAX_BUFFER_SIZE = 4 * 1024 * 1024;
const size_t SSU2_MAX_NUM_INTRODUCERS = 3; 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 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 = 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 = 15; // in seconds
const int SSU2_KEEP_ALIVE_INTERVAL_VARIANCE = 4; // in seconds const int SSU2_KEEP_ALIVE_INTERVAL_VARIANCE = 4; // in seconds
const int SSU2_PROXY_CONNECT_RETRY_TIMEOUT = 30; // in seconds const int SSU2_PROXY_CONNECT_RETRY_TIMEOUT = 30; // in seconds
@ -160,7 +159,7 @@ namespace transport
void ConnectThroughIntroducer (std::shared_ptr<SSU2Session> session); void ConnectThroughIntroducer (std::shared_ptr<SSU2Session> session);
std::vector<std::shared_ptr<SSU2Session> > FindIntroducers (int maxNumIntroducers, std::vector<std::shared_ptr<SSU2Session> > FindIntroducers (int maxNumIntroducers,
bool v4, const std::unordered_set<i2p::data::IdentHash>& excluded) const; bool v4, const std::unordered_set<i2p::data::IdentHash>& excluded);
void UpdateIntroducers (bool v4); void UpdateIntroducers (bool v4);
void ScheduleIntroducersUpdateTimer (); void ScheduleIntroducersUpdateTimer ();
void HandleIntroducersUpdateTimer (const boost::system::error_code& ecode, bool v4); void HandleIntroducersUpdateTimer (const boost::system::error_code& ecode, bool v4);