2013-10-23 06:45:40 +04:00
|
|
|
#include <fstream>
|
|
|
|
#include <cryptopp/dh.h>
|
|
|
|
#include <cryptopp/dsa.h>
|
|
|
|
#include "CryptoConst.h"
|
|
|
|
#include "RouterContext.h"
|
2014-01-30 04:56:48 +04:00
|
|
|
#include "util.h"
|
2013-10-23 06:45:40 +04:00
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
RouterContext context;
|
|
|
|
|
|
|
|
RouterContext::RouterContext ()
|
|
|
|
{
|
|
|
|
if (!Load ())
|
|
|
|
CreateNewRouter ();
|
|
|
|
Save ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RouterContext::CreateNewRouter ()
|
|
|
|
{
|
2013-12-21 05:22:55 +04:00
|
|
|
m_Keys = i2p::data::CreateRandomKeys ();
|
|
|
|
m_SigningPrivateKey.Initialize (i2p::crypto::dsap, i2p::crypto::dsaq, i2p::crypto::dsag,
|
|
|
|
CryptoPP::Integer (m_Keys.signingPrivateKey, 20));
|
2014-02-23 20:48:09 +04:00
|
|
|
UpdateRouterInfo ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RouterContext::UpdateRouterInfo ()
|
|
|
|
{
|
2013-12-21 05:22:55 +04:00
|
|
|
i2p::data::Identity ident;
|
2013-12-22 20:29:57 +04:00
|
|
|
ident = m_Keys;
|
2013-10-23 06:45:40 +04:00
|
|
|
|
2014-02-24 05:48:28 +04:00
|
|
|
i2p::data::RouterInfo routerInfo;
|
|
|
|
routerInfo.SetRouterIdentity (ident);
|
2014-02-25 04:25:26 +04:00
|
|
|
routerInfo.AddSSUAddress ("127.0.0.1", 17007, routerInfo.GetIdentHash ());
|
2014-02-24 05:48:28 +04:00
|
|
|
routerInfo.AddNTCPAddress ("127.0.0.1", 17007); // TODO:
|
|
|
|
routerInfo.SetProperty ("caps", "LR");
|
2014-07-08 15:45:16 +04:00
|
|
|
routerInfo.SetProperty ("coreVersion", "0.9.11");
|
2014-02-24 05:48:28 +04:00
|
|
|
routerInfo.SetProperty ("netId", "2");
|
2014-07-08 15:45:16 +04:00
|
|
|
routerInfo.SetProperty ("router.version", "0.9.11");
|
2014-02-24 05:48:28 +04:00
|
|
|
routerInfo.SetProperty ("start_uptime", "90m");
|
|
|
|
routerInfo.CreateBuffer ();
|
2013-10-23 06:45:40 +04:00
|
|
|
|
2014-02-24 05:48:28 +04:00
|
|
|
m_RouterInfo = routerInfo;
|
2014-02-23 20:48:09 +04:00
|
|
|
}
|
|
|
|
|
2013-12-10 17:10:49 +04:00
|
|
|
void RouterContext::OverrideNTCPAddress (const char * host, int port)
|
|
|
|
{
|
|
|
|
m_RouterInfo.CreateBuffer ();
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_RouterInfo.CreateBuffer ();
|
2014-02-23 20:48:09 +04:00
|
|
|
Save (true);
|
2013-12-10 17:10:49 +04:00
|
|
|
}
|
2014-02-09 06:06:40 +04:00
|
|
|
|
|
|
|
void RouterContext::UpdateAddress (const char * host)
|
|
|
|
{
|
|
|
|
for (auto& address : m_RouterInfo.GetAddresses ())
|
|
|
|
address.host = boost::asio::ip::address::from_string (host);
|
|
|
|
m_RouterInfo.CreateBuffer ();
|
|
|
|
}
|
2013-12-10 17:10:49 +04:00
|
|
|
|
2013-10-23 06:45:40 +04:00
|
|
|
void RouterContext::Sign (uint8_t * buf, int len, uint8_t * signature)
|
|
|
|
{
|
|
|
|
CryptoPP::DSA::Signer signer (m_SigningPrivateKey);
|
|
|
|
signer.SignMessage (m_Rnd, buf, len, signature);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2013-12-21 05:22:55 +04:00
|
|
|
fk.read ((char *)&m_Keys, sizeof (m_Keys));
|
2013-10-23 06:45:40 +04:00
|
|
|
m_SigningPrivateKey.Initialize (i2p::crypto::dsap, i2p::crypto::dsaq, i2p::crypto::dsag,
|
2013-12-21 05:22:55 +04:00
|
|
|
CryptoPP::Integer (m_Keys.signingPrivateKey, 20));
|
2013-10-23 06:45:40 +04:00
|
|
|
|
2014-03-14 15:32:11 +04:00
|
|
|
m_RouterInfo = i2p::data::RouterInfo (i2p::util::filesystem::GetFullPath (ROUTER_INFO).c_str ()); // TODO
|
2013-10-23 06:45:40 +04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-02-23 20:48:09 +04:00
|
|
|
void RouterContext::Save (bool infoOnly)
|
2013-10-23 06:45:40 +04:00
|
|
|
{
|
2014-02-23 20:48:09 +04:00
|
|
|
if (!infoOnly)
|
|
|
|
{
|
2014-03-14 15:32:11 +04:00
|
|
|
std::ofstream fk (i2p::util::filesystem::GetFullPath (ROUTER_KEYS).c_str (), std::ofstream::binary | std::ofstream::out);
|
2014-02-23 20:48:09 +04:00
|
|
|
fk.write ((char *)&m_Keys, sizeof (m_Keys));
|
|
|
|
}
|
|
|
|
|
2014-03-14 15:32:11 +04:00
|
|
|
std::ofstream fi (i2p::util::filesystem::GetFullPath (ROUTER_INFO).c_str (), std::ofstream::binary | std::ofstream::out);
|
2013-10-23 06:45:40 +04:00
|
|
|
fi.write ((char *)m_RouterInfo.GetBuffer (), m_RouterInfo.GetBufferLen ());
|
|
|
|
}
|
2014-01-22 01:07:16 +04:00
|
|
|
}
|