From 776cf02477260304ed08ee64e045c9eea7144386 Mon Sep 17 00:00:00 2001 From: orignal Date: Sun, 5 Apr 2026 09:04:00 -0400 Subject: [PATCH] replace destination's ident hash in client context --- libi2pd_client/ClientContext.cpp | 36 ++++++++++++++++++++++---------- libi2pd_client/ClientContext.h | 1 + libi2pd_client/I2PService.cpp | 3 +++ 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/libi2pd_client/ClientContext.cpp b/libi2pd_client/ClientContext.cpp index b45c4690..bd4c3fa6 100644 --- a/libi2pd_client/ClientContext.cpp +++ b/libi2pd_client/ClientContext.cpp @@ -369,26 +369,40 @@ namespace client void ClientContext::AddLocalDestination (std::shared_ptr localDestination) { - std::unique_lock l(m_DestinationsMutex); - m_Destinations[localDestination->GetIdentHash ()] = localDestination; - localDestination->Start (); + bool added = false; + { + std::unique_lock l(m_DestinationsMutex); + added = m_Destinations.emplace (localDestination->GetIdentHash (), localDestination).second; + } + if (added) + localDestination->Start (); } void ClientContext::DeleteLocalDestination (std::shared_ptr destination) { if (!destination) return; - auto it = m_Destinations.find (destination->GetIdentHash ()); - if (it != m_Destinations.end ()) + bool removed = false; { - auto d = it->second; - { - std::unique_lock l(m_DestinationsMutex); - m_Destinations.erase (it); - } - d->Stop (); + std::unique_lock l(m_DestinationsMutex); + removed = m_Destinations.erase (destination->GetIdentHash ()) > 0; } + if (removed) + destination->Stop (); } + bool ClientContext::ReplaceLocalDestinationHash (const i2p::data::IdentHash& oldIdentHash, const i2p::data::IdentHash& newIdentHash) + { + std::unique_lock l(m_DestinationsMutex); + auto it = m_Destinations.find (oldIdentHash); + if (it == m_Destinations.end ()) return false; + auto dest = it->second; + if (!dest) return false; + m_Destinations.erase (it); + m_Destinations.emplace (newIdentHash, dest); + return true; + } + + std::shared_ptr ClientContext::CreateNewLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic, const i2p::util::Mapping * params) { diff --git a/libi2pd_client/ClientContext.h b/libi2pd_client/ClientContext.h index bcc99ea3..0ca9254c 100644 --- a/libi2pd_client/ClientContext.h +++ b/libi2pd_client/ClientContext.h @@ -96,6 +96,7 @@ namespace client std::shared_ptr CreateNewMatchedTunnelDestination(const i2p::data::PrivateKeys &keys, const std::string & name, const i2p::util::Mapping * params = nullptr); void DeleteLocalDestination (std::shared_ptr destination); + bool ReplaceLocalDestinationHash (const i2p::data::IdentHash& oldIdentHash, const i2p::data::IdentHash& newIdentHash); std::shared_ptr FindLocalDestination (const i2p::data::IdentHash& destination) const; bool LoadPrivateKeys (i2p::data::PrivateKeys& keys, std::string_view filename, i2p::data::SigningKeyType sigType = i2p::data::SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519, diff --git a/libi2pd_client/I2PService.cpp b/libi2pd_client/I2PService.cpp index 4678c690..7595ee9e 100644 --- a/libi2pd_client/I2PService.cpp +++ b/libi2pd_client/I2PService.cpp @@ -83,8 +83,11 @@ namespace client { auto ident = m_LocalDestination->GetPrivateKeys ().GetPublic (); if (ident) + { m_LocalDestination->SetPrivateKeys (i2p::data::PrivateKeys::CreateRandomKeys ( ident->GetSigningKeyType (), ident->GetCryptoKeyType (), true)); + i2p::client::context.ReplaceLocalDestinationHash (ident->GetIdentHash (), m_LocalDestination->GetIdentHash ()); + } } ScheduleIdleCheckTimer (); }