I2P: End-to-End encrypted and anonymous Internet https://i2pd.website/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
3.1 KiB

#include <fstream>
#include <cryptopp/dh.h>
#include <cryptopp/dsa.h>
#include "CryptoConst.h"
#include "RouterContext.h"
#include "util.h"
#include "version.h"
namespace i2p
{
RouterContext context;
RouterContext::RouterContext ()
{
if (!Load ())
CreateNewRouter ();
Save ();
}
void RouterContext::CreateNewRouter ()
{
11 years ago
m_Keys = i2p::data::CreateRandomKeys ();
m_SigningPrivateKey.Initialize (i2p::crypto::dsap, i2p::crypto::dsaq, i2p::crypto::dsag,
11 years ago
CryptoPP::Integer (m_Keys.signingPrivateKey, 20));
UpdateRouterInfo ();
}
void RouterContext::UpdateRouterInfo ()
{
11 years ago
i2p::data::Identity ident;
ident = m_Keys;
11 years ago
i2p::data::RouterInfo routerInfo;
routerInfo.SetRouterIdentity (ident);
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));
11 years ago
routerInfo.SetProperty ("caps", "LR");
routerInfo.SetProperty ("coreVersion", I2P_VERSION);
11 years ago
routerInfo.SetProperty ("netId", "2");
routerInfo.SetProperty ("router.version", I2P_VERSION);
11 years ago
routerInfo.SetProperty ("start_uptime", "90m");
routerInfo.CreateBuffer ();
m_RouterInfo.Update (routerInfo.GetBuffer (), routerInfo.GetBufferLen ());
m_Identity = m_RouterInfo.GetRouterIdentity ();
}
11 years ago
void RouterContext::OverrideNTCPAddress (const char * host, int port)
{
m_RouterInfo.CreateBuffer ();
auto address = const_cast<i2p::data::RouterInfo::Address *>(m_RouterInfo.GetNTCPAddress ());
11 years ago
if (address)
{
address->host = boost::asio::ip::address::from_string (host);
11 years ago
address->port = port;
}
11 years ago
m_RouterInfo.CreateBuffer ();
Save (true);
}
void RouterContext::UpdateAddress (const char * host)
{
for (auto& address : m_RouterInfo.GetAddresses ())
address.host = boost::asio::ip::address::from_string (host);
m_RouterInfo.CreateBuffer ();
}
void RouterContext::Sign (const uint8_t * buf, int len, uint8_t * signature) const
{
CryptoPP::DSA::Signer signer (m_SigningPrivateKey);
signer.SignMessage (i2p::context.GetRandomNumberGenerator (), buf, len, signature);
}
bool RouterContext::Load ()
{
11 years ago
std::ifstream fk (i2p::util::filesystem::GetFullPath (ROUTER_KEYS).c_str (), std::ifstream::binary | std::ofstream::in);
if (!fk.is_open ()) return false;
11 years ago
fk.read ((char *)&m_Keys, sizeof (m_Keys));
m_SigningPrivateKey.Initialize (i2p::crypto::dsap, i2p::crypto::dsaq, i2p::crypto::dsag,
11 years ago
CryptoPP::Integer (m_Keys.signingPrivateKey, 20));
10 years ago
i2p::data::RouterInfo routerInfo(i2p::util::filesystem::GetFullPath (ROUTER_INFO)); // TODO
m_RouterInfo.Update (routerInfo.GetBuffer (), routerInfo.GetBufferLen ());
m_Identity = m_RouterInfo.GetRouterIdentity ();
return true;
}
void RouterContext::Save (bool infoOnly)
{
if (!infoOnly)
{
11 years ago
std::ofstream fk (i2p::util::filesystem::GetFullPath (ROUTER_KEYS).c_str (), std::ofstream::binary | std::ofstream::out);
fk.write ((char *)&m_Keys, sizeof (m_Keys));
}
m_RouterInfo.SaveToFile (i2p::util::filesystem::GetFullPath (ROUTER_INFO));
}
}