Skip to content

Commit

Permalink
static keys table
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed May 1, 2023
1 parent 2af4a2b commit 7c53515
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions libi2pd/NTCP2.cpp
Expand Up @@ -714,6 +714,7 @@ namespace transport
Terminate ();
return;
}
i2p::data::UpdateStaticKey (addr->s, ri.GetIdentHash ()); // good static key
i2p::data::netdb.PostI2NPMsg (CreateI2NPMessage (eI2NPDummyMsg, buf.data () + 3, size)); // TODO: should insert ri and not parse it twice
// TODO: process options

Expand Down
28 changes: 28 additions & 0 deletions libi2pd/Profiling.cpp
Expand Up @@ -301,5 +301,33 @@ namespace data
}
}
}

// static keys

struct StaticKeyProfile
{
i2p::data::IdentHash ident;
boost::posix_time::ptime lastUpdateTime;
};
//static i2p::fs::HashedStorage g_StaticKeysProfilesStorage("statickeysProfiles", "s", "statickey-", "txt");
static std::unordered_map<i2p::data::Tag<32>, std::shared_ptr<StaticKeyProfile> > g_StaticKeysProfiles;
static std::mutex g_StaticKeysProfilesMutex;

bool CheckStaticKey (const i2p::data::Tag<32>& staticKey, const i2p::data::IdentHash& ident)
{
std::unique_lock<std::mutex> l(g_StaticKeysProfilesMutex);
auto it = g_StaticKeysProfiles.find (staticKey);
if (it != g_StaticKeysProfiles.end ())
return it->second->ident == ident;
return true;
}

void UpdateStaticKey (const i2p::data::Tag<32>& staticKey, const i2p::data::IdentHash& ident)
{
std::unique_lock<std::mutex> l(g_StaticKeysProfilesMutex);
auto res = g_StaticKeysProfiles.emplace (staticKey, std::make_shared<StaticKeyProfile>(StaticKeyProfile{ident, GetTime ()}));
if (!res.second)
res.first->second->lastUpdateTime = GetTime ();
}
}
}
4 changes: 4 additions & 0 deletions libi2pd/Profiling.h
Expand Up @@ -85,6 +85,10 @@ namespace data
void DeleteObsoleteProfiles ();
void SaveProfiles ();
void PersistProfiles ();

// static keys
bool CheckStaticKey (const i2p::data::Tag<32>& staticKey, const i2p::data::IdentHash& ident);
void UpdateStaticKey (const i2p::data::Tag<32>& staticKey, const i2p::data::IdentHash& ident);
}
}

Expand Down
1 change: 1 addition & 0 deletions libi2pd/SSU2Session.cpp
Expand Up @@ -1073,6 +1073,7 @@ namespace transport
return false;
}
SetRemoteIdentity (ri->GetRouterIdentity ());
i2p::data::UpdateStaticKey (m_Address->s, ri->GetIdentHash ()); // good static key
AdjustMaxPayloadSize ();
m_Server.AddSessionByRouterHash (shared_from_this ()); // we know remote router now
m_RemoteTransports = ri->GetCompatibleTransports (false);
Expand Down
15 changes: 15 additions & 0 deletions libi2pd/Transports.cpp
Expand Up @@ -507,6 +507,11 @@ namespace transport
peer.router->GetPublishedNTCP2V6Address () : peer.router->GetPublishedNTCP2V4Address ();
if (address && m_CheckReserved && i2p::util::net::IsInReservedRange(address->host))
address = nullptr;
if (address && !i2p::data::CheckStaticKey (address->s, ident))
{
LogPrint (eLogWarning, "Transports: NTCP2 address static key router mismatch ", ident.ToBase64 ());
address = nullptr;
}
if (address)
{
auto s = std::make_shared<NTCP2Session> (*m_NTCP2Server, peer.router, address);
Expand All @@ -526,6 +531,11 @@ namespace transport
peer.router->GetSSU2V6Address () : peer.router->GetSSU2V4Address ();
if (address && m_CheckReserved && i2p::util::net::IsInReservedRange(address->host))
address = nullptr;
if (address && !i2p::data::CheckStaticKey (address->s, ident))
{
LogPrint (eLogWarning, "Transports: SSU2 address static key router mismatch ", ident.ToBase64 ());
address = nullptr;
}
if (address && address->IsReachableSSU ())
{
if (m_SSU2Server->CreateSession (peer.router, address))
Expand All @@ -537,6 +547,11 @@ namespace transport
{
if (!m_NTCP2Server) continue;
auto address = peer.router->GetYggdrasilAddress ();
if (address && !i2p::data::CheckStaticKey (address->s, ident))
{
LogPrint (eLogWarning, "Transports: Yggdrasil address static key router mismatch ", ident.ToBase64 ());
address = nullptr;
}
if (address)
{
auto s = std::make_shared<NTCP2Session> (*m_NTCP2Server, peer.router, address);
Expand Down

0 comments on commit 7c53515

Please sign in to comment.