i2pd/RouterContext.cpp

102 lines
3 KiB
C++
Raw Normal View History

2013-10-23 04:45:40 +02:00
#include <fstream>
#include <cryptopp/dh.h>
#include <cryptopp/dsa.h>
#include "CryptoConst.h"
#include "RouterContext.h"
2014-01-30 01:56:48 +01:00
#include "util.h"
#include "version.h"
2013-10-23 04:45:40 +02:00
namespace i2p
{
RouterContext context;
RouterContext::RouterContext ()
{
if (!Load ())
CreateNewRouter ();
Save ();
}
2013-10-23 04:45:40 +02:00
void RouterContext::CreateNewRouter ()
{
2013-12-21 02:22:55 +01:00
m_Keys = i2p::data::CreateRandomKeys ();
2014-02-23 17:48:09 +01:00
UpdateRouterInfo ();
}
void RouterContext::UpdateRouterInfo ()
{
2014-02-24 02:48:28 +01:00
i2p::data::RouterInfo routerInfo;
2014-08-25 22:25:12 +02:00
routerInfo.SetRouterIdentity (GetIdentity ().GetStandardIdentity ());
routerInfo.AddSSUAddress (i2p::util::config::GetCharArg("-host", "127.0.0.1"),
i2p::util::config::GetArg("-port", 17007), routerInfo.GetIdentHash ());
routerInfo.AddNTCPAddress (i2p::util::config::GetCharArg("-host", "127.0.0.1"),
i2p::util::config::GetArg("-port", 17007));
2014-02-24 02:48:28 +01:00
routerInfo.SetProperty ("caps", "LR");
routerInfo.SetProperty ("coreVersion", I2P_VERSION);
2014-02-24 02:48:28 +01:00
routerInfo.SetProperty ("netId", "2");
routerInfo.SetProperty ("router.version", I2P_VERSION);
2014-02-24 02:48:28 +01:00
routerInfo.SetProperty ("start_uptime", "90m");
routerInfo.CreateBuffer ();
2014-07-22 14:03:02 +02:00
m_RouterInfo.Update (routerInfo.GetBuffer (), routerInfo.GetBufferLen ());
}
2013-12-10 14:10:49 +01:00
void RouterContext::OverrideNTCPAddress (const char * host, int port)
{
m_RouterInfo.CreateBuffer ();
2014-02-09 14:52:56 +01:00
auto address = const_cast<i2p::data::RouterInfo::Address *>(m_RouterInfo.GetNTCPAddress ());
2013-12-10 14:10:49 +01:00
if (address)
{
2014-01-21 22:07:16 +01:00
address->host = boost::asio::ip::address::from_string (host);
2013-12-10 14:10:49 +01:00
address->port = port;
}
2013-12-10 14:10:49 +01:00
m_RouterInfo.CreateBuffer ();
2014-02-23 17:48:09 +01:00
Save (true);
}
2014-02-09 03:06:40 +01:00
void RouterContext::UpdateAddress (const char * host)
{
for (auto& address : m_RouterInfo.GetAddresses ())
address.host = boost::asio::ip::address::from_string (host);
2014-02-09 03:06:40 +01:00
m_RouterInfo.CreateBuffer ();
}
2014-07-29 19:44:54 +02:00
void RouterContext::Sign (const uint8_t * buf, int len, uint8_t * signature) const
2013-10-23 04:45:40 +02:00
{
2014-08-25 22:25:12 +02:00
m_Keys.Sign(buf, len, signature);
2013-10-23 04:45:40 +02:00
}
bool RouterContext::Load ()
{
2014-03-14 12:32:11 +01:00
std::ifstream fk (i2p::util::filesystem::GetFullPath (ROUTER_KEYS).c_str (), std::ifstream::binary | std::ofstream::in);
2013-10-23 04:45:40 +02:00
if (!fk.is_open ()) return false;
2014-08-25 22:25:12 +02:00
i2p::data::Keys keys;
fk.read ((char *)&keys, sizeof (keys));
m_Keys = keys;
2013-10-23 04:45:40 +02:00
2014-07-24 03:45:45 +02:00
i2p::data::RouterInfo routerInfo(i2p::util::filesystem::GetFullPath (ROUTER_INFO)); // TODO
m_RouterInfo.Update (routerInfo.GetBuffer (), routerInfo.GetBufferLen ());
2013-10-23 04:45:40 +02:00
return true;
}
2014-02-23 17:48:09 +01:00
void RouterContext::Save (bool infoOnly)
2013-10-23 04:45:40 +02:00
{
2014-02-23 17:48:09 +01:00
if (!infoOnly)
{
2014-03-14 12:32:11 +01:00
std::ofstream fk (i2p::util::filesystem::GetFullPath (ROUTER_KEYS).c_str (), std::ofstream::binary | std::ofstream::out);
2014-08-25 22:25:12 +02:00
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));
2014-02-23 17:48:09 +01:00
}
2014-07-10 21:33:42 +02:00
m_RouterInfo.SaveToFile (i2p::util::filesystem::GetFullPath (ROUTER_INFO));
}
}