Browse Source

update RouterInfo after 30 minutes if no changes

pull/93/head
orignal 10 years ago
parent
commit
da9c281d9a
  1. 70
      RouterContext.cpp
  2. 5
      RouterContext.h

70
RouterContext.cpp

@ -3,6 +3,7 @@
#include <cryptopp/dsa.h> #include <cryptopp/dsa.h>
#include "CryptoConst.h" #include "CryptoConst.h"
#include "RouterContext.h" #include "RouterContext.h"
#include "Timestamp.h"
#include "util.h" #include "util.h"
#include "version.h" #include "version.h"
@ -10,27 +11,30 @@ namespace i2p
{ {
RouterContext context; RouterContext context;
RouterContext::RouterContext () RouterContext::RouterContext ():
m_LastUpdateTime (0)
{ {
if (!Load ()) if (!Load ())
CreateNewRouter (); CreateNewRouter ();
Save (); UpdateRouterInfo ();
} }
void RouterContext::CreateNewRouter () void RouterContext::CreateNewRouter ()
{ {
m_Keys = i2p::data::CreateRandomKeys (); m_Keys = i2p::data::CreateRandomKeys ();
UpdateRouterInfo (); SaveKeys ();
NewRouterInfo ();
} }
void RouterContext::UpdateRouterInfo () void RouterContext::NewRouterInfo ()
{ {
i2p::data::RouterInfo routerInfo; i2p::data::RouterInfo routerInfo;
routerInfo.SetRouterIdentity (GetIdentity ().GetStandardIdentity ()); routerInfo.SetRouterIdentity (GetIdentity ().GetStandardIdentity ());
routerInfo.AddSSUAddress (i2p::util::config::GetCharArg("-host", "127.0.0.1"), int port = i2p::util::config::GetArg("-port", 0);
i2p::util::config::GetArg("-port", 17007), routerInfo.GetIdentHash ()); if (!port)
routerInfo.AddNTCPAddress (i2p::util::config::GetCharArg("-host", "127.0.0.1"), port = m_Rnd.GenerateWord32 (9111, 30777); // I2P network ports range
i2p::util::config::GetArg("-port", 17007)); routerInfo.AddSSUAddress (i2p::util::config::GetCharArg("-host", "127.0.0.1"), port, routerInfo.GetIdentHash ());
routerInfo.AddNTCPAddress (i2p::util::config::GetCharArg("-host", "127.0.0.1"), port);
routerInfo.SetProperty ("caps", "LR"); routerInfo.SetProperty ("caps", "LR");
routerInfo.SetProperty ("coreVersion", I2P_VERSION); routerInfo.SetProperty ("coreVersion", I2P_VERSION);
routerInfo.SetProperty ("netId", "2"); routerInfo.SetProperty ("netId", "2");
@ -40,6 +44,13 @@ namespace i2p
m_RouterInfo.Update (routerInfo.GetBuffer (), routerInfo.GetBufferLen ()); m_RouterInfo.Update (routerInfo.GetBuffer (), routerInfo.GetBufferLen ());
} }
void RouterContext::UpdateRouterInfo ()
{
m_RouterInfo.CreateBuffer (m_Keys);
m_RouterInfo.SaveToFile (i2p::util::filesystem::GetFullPath (ROUTER_INFO));
m_LastUpdateTime = i2p::util::GetSecondsSinceEpoch ();
}
void RouterContext::OverrideNTCPAddress (const char * host, int port) void RouterContext::OverrideNTCPAddress (const char * host, int port)
{ {
m_RouterInfo.CreateBuffer (m_Keys); m_RouterInfo.CreateBuffer (m_Keys);
@ -49,16 +60,24 @@ namespace i2p
address->host = boost::asio::ip::address::from_string (host); address->host = boost::asio::ip::address::from_string (host);
address->port = port; address->port = port;
} }
UpdateRouterInfo ();
m_RouterInfo.CreateBuffer (m_Keys);
Save (true);
} }
void RouterContext::UpdateAddress (const char * host) void RouterContext::UpdateAddress (const char * host)
{ {
bool updated = false;
auto newAddress = boost::asio::ip::address::from_string (host);
for (auto& address : m_RouterInfo.GetAddresses ()) for (auto& address : m_RouterInfo.GetAddresses ())
address.host = boost::asio::ip::address::from_string (host); {
m_RouterInfo.CreateBuffer (m_Keys); if (address.host != newAddress)
{
address.host = newAddress;
updated = true;
}
}
auto ts = i2p::util::GetSecondsSinceEpoch ();
if (updated || ts > m_LastUpdateTime + ROUTER_INFO_UPDATE_INTERVAL)
UpdateRouterInfo ();
} }
bool RouterContext::Load () bool RouterContext::Load ()
@ -72,25 +91,20 @@ namespace i2p
i2p::data::RouterInfo routerInfo(i2p::util::filesystem::GetFullPath (ROUTER_INFO)); // TODO i2p::data::RouterInfo routerInfo(i2p::util::filesystem::GetFullPath (ROUTER_INFO)); // TODO
m_RouterInfo.Update (routerInfo.GetBuffer (), routerInfo.GetBufferLen ()); m_RouterInfo.Update (routerInfo.GetBuffer (), routerInfo.GetBufferLen ());
m_RouterInfo.SetProperty ("router.version", I2P_VERSION);
return true; return true;
} }
void RouterContext::Save (bool infoOnly) void RouterContext::SaveKeys ()
{ {
if (!infoOnly) std::ofstream fk (i2p::util::filesystem::GetFullPath (ROUTER_KEYS).c_str (), std::ofstream::binary | std::ofstream::out);
{ i2p::data::Keys keys;
std::ofstream fk (i2p::util::filesystem::GetFullPath (ROUTER_KEYS).c_str (), std::ofstream::binary | std::ofstream::out); memcpy (keys.privateKey, m_Keys.GetPrivateKey (), sizeof (keys.privateKey));
i2p::data::Keys keys; memcpy (keys.signingPrivateKey, m_Keys.GetSigningPrivateKey (), sizeof (keys.signingPrivateKey));
memcpy (keys.privateKey, m_Keys.GetPrivateKey (), sizeof (keys.privateKey)); auto& ident = GetIdentity ().GetStandardIdentity ();
memcpy (keys.signingPrivateKey, m_Keys.GetSigningPrivateKey (), sizeof (keys.signingPrivateKey)); memcpy (keys.publicKey, ident.publicKey, sizeof (keys.publicKey));
auto& ident = GetIdentity ().GetStandardIdentity (); memcpy (keys.signingKey, ident.signingKey, sizeof (keys.signingKey));
memcpy (keys.publicKey, ident.publicKey, sizeof (keys.publicKey)); fk.write ((char *)&keys, sizeof (keys));
memcpy (keys.signingKey, ident.signingKey, sizeof (keys.signingKey));
fk.write ((char *)&keys, sizeof (keys));
}
m_RouterInfo.SaveToFile (i2p::util::filesystem::GetFullPath (ROUTER_INFO));
} }
} }

5
RouterContext.h

@ -11,6 +11,7 @@ namespace i2p
{ {
const char ROUTER_INFO[] = "router.info"; const char ROUTER_INFO[] = "router.info";
const char ROUTER_KEYS[] = "router.keys"; const char ROUTER_KEYS[] = "router.keys";
const int ROUTER_INFO_UPDATE_INTERVAL = 1800; // 30 minutes
class RouterContext: public i2p::data::LocalDestination class RouterContext: public i2p::data::LocalDestination
{ {
@ -36,15 +37,17 @@ namespace i2p
private: private:
void CreateNewRouter (); void CreateNewRouter ();
void NewRouterInfo ();
void UpdateRouterInfo (); void UpdateRouterInfo ();
bool Load (); bool Load ();
void Save (bool infoOnly = false); void SaveKeys ();
private: private:
i2p::data::RouterInfo m_RouterInfo; i2p::data::RouterInfo m_RouterInfo;
i2p::data::PrivateKeys m_Keys; i2p::data::PrivateKeys m_Keys;
CryptoPP::AutoSeededRandomPool m_Rnd; CryptoPP::AutoSeededRandomPool m_Rnd;
uint64_t m_LastUpdateTime;
}; };
extern RouterContext context; extern RouterContext context;

Loading…
Cancel
Save