i2pd/RouterContext.cpp

133 lines
4.0 KiB
C++
Raw Normal View History

2013-10-23 06:45:40 +04:00
#include <fstream>
#include <cryptopp/dh.h>
#include <cryptopp/dsa.h>
#include "CryptoConst.h"
#include "RouterContext.h"
#include "Timestamp.h"
2014-01-30 04:56:48 +04:00
#include "util.h"
#include "version.h"
2013-10-23 06:45:40 +04:00
namespace i2p
{
RouterContext context;
RouterContext::RouterContext ():
m_LastUpdateTime (0)
2014-09-04 17:31:42 +04:00
{
}
void RouterContext::Init ()
2013-10-23 06:45:40 +04:00
{
if (!Load ())
CreateNewRouter ();
UpdateRouterInfo ();
}
2013-10-23 06:45:40 +04:00
void RouterContext::CreateNewRouter ()
{
2013-12-21 05:22:55 +04:00
m_Keys = i2p::data::CreateRandomKeys ();
SaveKeys ();
NewRouterInfo ();
2014-02-23 20:48:09 +04:00
}
void RouterContext::NewRouterInfo ()
2014-02-23 20:48:09 +04:00
{
2014-02-24 05:48:28 +04:00
i2p::data::RouterInfo routerInfo;
2014-08-26 00:25:12 +04:00
routerInfo.SetRouterIdentity (GetIdentity ().GetStandardIdentity ());
int port = i2p::util::config::GetArg("-port", 0);
if (!port)
port = m_Rnd.GenerateWord32 (9111, 30777); // I2P network ports range
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);
2014-09-05 15:26:36 +04:00
routerInfo.SetCaps (i2p::data::RouterInfo::eReachable |
i2p::data::RouterInfo::eSSUTesting | i2p::data::RouterInfo::eSSUIntroducer); // LR, BC
routerInfo.SetProperty ("coreVersion", I2P_VERSION);
2014-02-24 05:48:28 +04:00
routerInfo.SetProperty ("netId", "2");
routerInfo.SetProperty ("router.version", I2P_VERSION);
2014-09-03 21:00:15 +04:00
routerInfo.SetProperty ("stat_uptime", "90m");
2014-08-26 06:47:12 +04:00
routerInfo.CreateBuffer (m_Keys);
2014-07-22 16:03:02 +04:00
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 ();
}
2013-12-10 17:10:49 +04:00
void RouterContext::OverrideNTCPAddress (const char * host, int port)
{
2014-08-26 06:47:12 +04:00
m_RouterInfo.CreateBuffer (m_Keys);
2014-02-09 17:52:56 +04:00
auto address = const_cast<i2p::data::RouterInfo::Address *>(m_RouterInfo.GetNTCPAddress ());
2013-12-10 17:10:49 +04:00
if (address)
{
2014-01-22 01:07:16 +04:00
address->host = boost::asio::ip::address::from_string (host);
2013-12-10 17:10:49 +04:00
address->port = port;
}
UpdateRouterInfo ();
}
2014-02-09 06:06:40 +04:00
void RouterContext::UpdateAddress (const char * host)
{
bool updated = false;
auto newAddress = boost::asio::ip::address::from_string (host);
2014-02-09 06:06:40 +04:00
for (auto& address : m_RouterInfo.GetAddresses ())
{
if (address.host != newAddress)
{
address.host = newAddress;
updated = true;
}
}
auto ts = i2p::util::GetSecondsSinceEpoch ();
if (updated || ts > m_LastUpdateTime + ROUTER_INFO_UPDATE_INTERVAL)
UpdateRouterInfo ();
2013-10-23 06:45:40 +04:00
}
2014-09-02 01:34:20 +04:00
void RouterContext::AddIntroducer (const i2p::data::RouterInfo& routerInfo, uint32_t tag)
{
auto address = routerInfo.GetSSUAddress ();
if (address)
{
if (m_RouterInfo.AddIntroducer (address, tag))
UpdateRouterInfo ();
}
}
2014-09-07 04:43:20 +04:00
void RouterContext::RemoveIntroducer (const boost::asio::ip::udp::endpoint& e)
2014-09-02 01:34:20 +04:00
{
2014-09-07 04:43:20 +04:00
if (m_RouterInfo.RemoveIntroducer (e))
2014-09-02 01:34:20 +04:00
UpdateRouterInfo ();
}
2013-10-23 06:45:40 +04:00
bool RouterContext::Load ()
{
2014-03-14 15:32:11 +04:00
std::ifstream fk (i2p::util::filesystem::GetFullPath (ROUTER_KEYS).c_str (), std::ifstream::binary | std::ofstream::in);
2013-10-23 06:45:40 +04:00
if (!fk.is_open ()) return false;
2014-08-26 00:25:12 +04:00
i2p::data::Keys keys;
fk.read ((char *)&keys, sizeof (keys));
m_Keys = keys;
2013-10-23 06:45:40 +04:00
2014-07-24 05:45:45 +04:00
i2p::data::RouterInfo routerInfo(i2p::util::filesystem::GetFullPath (ROUTER_INFO)); // TODO
m_RouterInfo.Update (routerInfo.GetBuffer (), routerInfo.GetBufferLen ());
2014-09-02 20:28:57 +04:00
m_RouterInfo.SetProperty ("coreVersion", I2P_VERSION);
m_RouterInfo.SetProperty ("router.version", I2P_VERSION);
2013-10-23 06:45:40 +04:00
return true;
}
void RouterContext::SaveKeys ()
{
std::ofstream fk (i2p::util::filesystem::GetFullPath (ROUTER_KEYS).c_str (), std::ofstream::binary | std::ofstream::out);
i2p::data::Keys keys;
memcpy (keys.privateKey, m_Keys.GetPrivateKey (), sizeof (keys.privateKey));
memcpy (keys.signingPrivateKey, m_Keys.GetSigningPrivateKey (), sizeof (keys.signingPrivateKey));
auto& ident = GetIdentity ().GetStandardIdentity ();
memcpy (keys.publicKey, ident.publicKey, sizeof (keys.publicKey));
memcpy (keys.signingKey, ident.signingKey, sizeof (keys.signingKey));
fk.write ((char *)&keys, sizeof (keys));
}
}