i2pd/libi2pd/RouterContext.cpp

895 lines
27 KiB
C++
Raw Normal View History

/*
* Copyright (c) 2013-2020, The PurpleI2P Project
*
* This file is part of Purple i2pd project and licensed under BSD3
*
* See full license text in LICENSE file at top of project tree
*/
2013-10-23 02:45:40 +00:00
#include <fstream>
2018-06-11 19:33:48 +00:00
#include <openssl/rand.h>
2016-01-20 00:00:00 +00:00
#include "Config.h"
2015-11-03 14:15:49 +00:00
#include "Crypto.h"
2018-06-15 16:52:43 +00:00
#include "Ed25519.h"
#include "Timestamp.h"
2014-10-12 01:27:55 +00:00
#include "I2NPProtocol.h"
#include "NetDb.hpp"
#include "FS.h"
2014-01-30 00:56:48 +00:00
#include "util.h"
#include "version.h"
2015-11-03 14:15:49 +00:00
#include "Log.h"
2016-02-21 01:20:19 +00:00
#include "Family.h"
#include "ECIESX25519AEADRatchetSession.h"
2015-11-03 14:15:49 +00:00
#include "RouterContext.h"
2013-10-23 02:45:40 +00:00
namespace i2p
{
RouterContext context;
RouterContext::RouterContext ():
2017-10-12 10:52:36 +00:00
m_LastUpdateTime (0), m_AcceptsTunnels (true), m_IsFloodfill (false),
m_ShareRatio (100), m_Status (eRouterStatusUnknown),
m_Error (eRouterErrorNone), m_NetID (I2PD_NET_ID)
2014-09-04 13:31:42 +00:00
{
}
void RouterContext::Init ()
2013-10-23 02:45:40 +00:00
{
2015-11-03 14:15:49 +00:00
srand (i2p::util::GetMillisecondsSinceEpoch () % 1000);
2020-01-07 20:20:55 +00:00
m_StartupTime = std::chrono::steady_clock::now();
2013-10-23 02:45:40 +00:00
if (!Load ())
CreateNewRouter ();
2018-01-06 03:48:51 +00:00
m_Decryptor = m_Keys.CreateDecryptor (nullptr);
m_TunnelDecryptor = m_Keys.CreateDecryptor (nullptr);
UpdateRouterInfo ();
if (IsECIES ())
{
auto initState = new i2p::crypto::NoiseSymmetricState ();
i2p::crypto::InitNoiseNState (*initState, GetIdentity ()->GetEncryptionPublicKey ());
m_InitialNoiseState.reset (initState);
m_ECIESSession = std::make_shared<i2p::garlic::RouterIncomingRatchetSession>(*initState);
}
}
2013-10-23 02:45:40 +00:00
void RouterContext::CreateNewRouter ()
{
2020-12-15 21:06:32 +00:00
m_Keys = i2p::data::PrivateKeys::CreateRandomKeys (i2p::data::SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519,
i2p::data::CRYPTO_KEY_TYPE_ECIES_X25519_AEAD);
2019-05-07 21:45:52 +00:00
SaveKeys ();
NewRouterInfo ();
2014-02-23 16:48:09 +00:00
}
void RouterContext::NewRouterInfo ()
2014-02-23 16:48:09 +00:00
{
2014-02-24 01:48:28 +00:00
i2p::data::RouterInfo routerInfo;
2014-11-20 17:21:27 +00:00
routerInfo.SetRouterIdentity (GetIdentity ());
2016-01-20 00:00:00 +00:00
uint16_t port; i2p::config::GetOption("port", port);
if (!port)
2019-01-23 14:53:30 +00:00
{
2015-11-03 14:15:49 +00:00
port = rand () % (30777 - 9111) + 9111; // I2P network ports range
2019-01-23 14:53:30 +00:00
if (port == 9150) port = 9151; // Tor browser
2019-05-07 21:45:52 +00:00
}
bool ipv4; i2p::config::GetOption("ipv4", ipv4);
bool ipv6; i2p::config::GetOption("ipv6", ipv6);
bool ssu; i2p::config::GetOption("ssu", ssu);
bool ntcp2; i2p::config::GetOption("ntcp2.enabled", ntcp2);
bool ygg; i2p::config::GetOption("meshnets.yggdrasil", ygg);
2019-05-07 21:45:52 +00:00
bool nat; i2p::config::GetOption("nat", nat);
if ((ntcp2 || ygg) && !m_NTCP2Keys)
NewNTCP2Keys ();
bool ntcp2Published = false;
if (ntcp2)
{
i2p::config::GetOption("ntcp2.published", ntcp2Published);
if (ntcp2Published)
{
std::string ntcp2proxy; i2p::config::GetOption("ntcp2.proxy", ntcp2proxy);
if (!ntcp2proxy.empty ()) ntcp2Published = false;
}
}
uint8_t caps = 0, addressCaps = 0;
2016-07-22 14:16:57 +00:00
if (ipv4)
{
std::string host = "127.0.0.1";
if (!i2p::config::IsDefault("host"))
i2p::config::GetOption("host", host);
2021-03-09 20:28:07 +00:00
else if (!nat)
{
// we have no NAT so set external address from local address
std::string address4; i2p::config::GetOption("address4", address4);
if (!address4.empty ()) host = address4;
}
if (ntcp2)
{
if (ntcp2Published)
routerInfo.AddNTCP2Address (m_NTCP2Keys->staticPublicKey, m_NTCP2Keys->iv, boost::asio::ip::address_v4::from_string (host), port);
else // add non-published NTCP2 address
{
addressCaps = i2p::data::RouterInfo::AddressCaps::eV4;
routerInfo.AddNTCP2Address (m_NTCP2Keys->staticPublicKey, m_NTCP2Keys->iv);
}
}
if (ssu)
{
2020-11-21 23:44:40 +00:00
routerInfo.AddSSUAddress (host.c_str(), port, nullptr);
caps |= i2p::data::RouterInfo::eReachable; // R
}
2016-07-22 14:16:57 +00:00
}
if (ipv6)
{
2019-05-07 21:45:52 +00:00
std::string host = "::1";
2016-07-22 14:34:56 +00:00
if (!i2p::config::IsDefault("host") && !ipv4) // override if v6 only
i2p::config::GetOption("host", host);
2021-03-09 20:28:07 +00:00
else
{
std::string address6; i2p::config::GetOption("address6", address6);
if (!address6.empty ()) host = address6;
}
if (ntcp2)
{
if (ntcp2Published)
{
std::string ntcp2Host;
if (!i2p::config::IsDefault ("ntcp2.addressv6"))
i2p::config::GetOption ("ntcp2.addressv6", ntcp2Host);
else
ntcp2Host = host;
routerInfo.AddNTCP2Address (m_NTCP2Keys->staticPublicKey, m_NTCP2Keys->iv, boost::asio::ip::address_v6::from_string (ntcp2Host), port);
}
else
{
if (!ipv4) // no other ntcp2 addresses yet
routerInfo.AddNTCP2Address (m_NTCP2Keys->staticPublicKey, m_NTCP2Keys->iv);
addressCaps |= i2p::data::RouterInfo::AddressCaps::eV6;
}
}
if (ssu)
{
2020-11-21 23:44:40 +00:00
routerInfo.AddSSUAddress (host.c_str(), port, nullptr);
caps |= i2p::data::RouterInfo::eReachable; // R
}
2016-11-24 15:11:46 +00:00
}
if (ygg)
{
auto yggaddr = i2p::util::net::GetYggdrasilAddress ();
if (!yggaddr.is_unspecified ())
routerInfo.AddNTCP2Address (m_NTCP2Keys->staticPublicKey, m_NTCP2Keys->iv, yggaddr, port);
}
if (addressCaps)
routerInfo.SetUnreachableAddressesTransportCaps (addressCaps);
routerInfo.SetCaps (caps); // caps + L
2017-10-12 10:52:36 +00:00
routerInfo.SetProperty ("netId", std::to_string (m_NetID));
routerInfo.SetProperty ("router.version", I2P_VERSION);
2014-08-26 02:47:12 +00:00
routerInfo.CreateBuffer (m_Keys);
2015-11-03 14:15:49 +00:00
m_RouterInfo.SetRouterIdentity (GetIdentity ());
2014-07-22 12:03:02 +00:00
m_RouterInfo.Update (routerInfo.GetBuffer (), routerInfo.GetBufferLen ());
}
void RouterContext::UpdateRouterInfo ()
{
m_RouterInfo.CreateBuffer (m_Keys);
m_RouterInfo.SaveToFile (i2p::fs::DataDirPath (ROUTER_INFO));
m_LastUpdateTime = i2p::util::GetSecondsSinceEpoch ();
2017-10-12 10:52:36 +00:00
}
2014-09-11 13:32:34 +00:00
2018-06-11 19:33:48 +00:00
void RouterContext::NewNTCP2Keys ()
{
2018-11-01 14:43:31 +00:00
m_StaticKeys.reset (new i2p::crypto::X25519Keys ());
2019-05-07 21:45:52 +00:00
m_StaticKeys->GenerateKeys ();
2018-06-11 19:33:48 +00:00
m_NTCP2Keys.reset (new NTCP2PrivateKeys ());
2018-11-01 14:43:31 +00:00
m_StaticKeys->GetPrivateKey (m_NTCP2Keys->staticPrivateKey);
memcpy (m_NTCP2Keys->staticPublicKey, m_StaticKeys->GetPublicKey (), 32);
2018-06-11 19:33:48 +00:00
RAND_bytes (m_NTCP2Keys->iv, 16);
// save
std::ofstream fk (i2p::fs::DataDirPath (NTCP2_KEYS), std::ofstream::binary | std::ofstream::out);
2019-05-07 21:45:52 +00:00
fk.write ((char *)m_NTCP2Keys.get (), sizeof (NTCP2PrivateKeys));
2018-06-11 19:33:48 +00:00
}
2017-10-12 10:52:36 +00:00
void RouterContext::SetStatus (RouterStatus status)
{
2015-11-03 14:15:49 +00:00
if (status != m_Status)
2017-10-12 10:52:36 +00:00
{
2015-11-03 14:15:49 +00:00
m_Status = status;
2016-09-20 01:37:04 +00:00
m_Error = eRouterErrorNone;
2015-11-03 14:15:49 +00:00
switch (m_Status)
2017-10-12 10:52:36 +00:00
{
2015-11-03 14:15:49 +00:00
case eRouterStatusOK:
SetReachable ();
break;
case eRouterStatusFirewalled:
SetUnreachable ();
2017-10-12 10:52:36 +00:00
break;
2015-11-03 14:15:49 +00:00
default:
;
}
2017-10-12 10:52:36 +00:00
}
2015-11-03 14:15:49 +00:00
}
2017-10-12 10:52:36 +00:00
2014-09-11 13:32:34 +00:00
void RouterContext::UpdatePort (int port)
2013-12-10 13:10:49 +00:00
{
2014-09-11 13:32:34 +00:00
bool updated = false;
2016-08-07 21:52:18 +00:00
for (auto& address : m_RouterInfo.GetAddresses ())
2013-12-10 13:10:49 +00:00
{
2018-07-23 17:51:29 +00:00
if (!address->IsNTCP2 () && address->port != port)
2017-10-12 10:52:36 +00:00
{
2016-03-21 17:02:51 +00:00
address->port = port;
2014-09-11 13:32:34 +00:00
updated = true;
2017-10-12 10:52:36 +00:00
}
}
2014-09-11 13:32:34 +00:00
if (updated)
UpdateRouterInfo ();
}
2014-02-09 02:06:40 +00:00
void RouterContext::PublishNTCP2Address (int port, bool publish, bool v4, bool v6, bool ygg)
2018-07-23 17:51:29 +00:00
{
if (!m_NTCP2Keys) return;
2018-07-23 17:51:29 +00:00
bool updated = false;
for (auto& address : m_RouterInfo.GetAddresses ())
{
if (address->IsNTCP2 () && (address->port != port || address->ntcp2->isPublished != publish)
&& ((v4 && address->IsV4 ()) || (v6 && address->IsV6 ()) ||
(ygg && i2p::util::net::IsYggdrasilAddress (address->host))))
2018-07-23 17:51:29 +00:00
{
if (!port && !address->port)
{
// select random port only if address's port is not set
port = rand () % (30777 - 9111) + 9111; // I2P network ports range
if (port == 9150) port = 9151; // Tor browser
}
if (port) address->port = port;
2021-03-01 16:09:25 +00:00
address->cost = publish ? i2p::data::COST_NTCP2_PUBLISHED : i2p::data::COST_NTCP2_NON_PUBLISHED;
2018-08-03 18:49:28 +00:00
address->ntcp2->isPublished = publish;
address->ntcp2->iv = m_NTCP2Keys->iv;
2018-07-23 17:51:29 +00:00
updated = true;
}
}
if (updated)
2019-05-07 21:45:52 +00:00
UpdateRouterInfo ();
2018-07-23 17:51:29 +00:00
}
2018-08-03 18:49:28 +00:00
void RouterContext::UpdateNTCP2Address (bool enable)
{
auto& addresses = m_RouterInfo.GetAddresses ();
bool found = false, updated = false;
for (auto it = addresses.begin (); it != addresses.end (); ++it)
{
if ((*it)->IsNTCP2 ())
{
found = true;
if (!enable)
2019-05-07 21:45:52 +00:00
{
2018-08-03 18:49:28 +00:00
addresses.erase (it);
updated= true;
}
break;
}
}
if (enable && !found)
2019-05-07 21:45:52 +00:00
{
m_RouterInfo.AddNTCP2Address (m_NTCP2Keys->staticPublicKey, m_NTCP2Keys->iv);
2018-08-03 18:49:28 +00:00
updated = true;
}
if (updated)
UpdateRouterInfo ();
}
2019-05-07 21:45:52 +00:00
2014-10-29 17:49:21 +00:00
void RouterContext::UpdateAddress (const boost::asio::ip::address& host)
2014-02-09 02:06:40 +00:00
{
bool updated = false;
2016-08-07 21:52:18 +00:00
for (auto& address : m_RouterInfo.GetAddresses ())
{
if (address->host != host && address->IsCompatible (host) &&
!i2p::util::net::IsYggdrasilAddress (address->host))
2017-10-12 10:52:36 +00:00
{
2016-03-21 17:02:51 +00:00
address->host = host;
if (host.is_v6 () && address->transportStyle == i2p::data::RouterInfo::eTransportSSU)
{
// update MTU
auto mtu = i2p::util::net::GetMTU (host);
if (mtu)
{
LogPrint (eLogDebug, "Router: Our v6 MTU=", mtu);
if (mtu > 1472) { // TODO: magic constant
mtu = 1472;
LogPrint(eLogWarning, "Router: MTU dropped to upper limit of 1472 bytes");
}
if (address->ssu) address->ssu->mtu = mtu;
}
}
updated = true;
2017-10-12 10:52:36 +00:00
}
}
auto ts = i2p::util::GetSecondsSinceEpoch ();
if (updated || ts > m_LastUpdateTime + ROUTER_INFO_UPDATE_INTERVAL)
UpdateRouterInfo ();
2013-10-23 02:45:40 +00:00
}
2015-11-03 14:15:49 +00:00
bool RouterContext::AddIntroducer (const i2p::data::RouterInfo::Introducer& introducer)
2014-09-01 21:34:20 +00:00
{
2015-11-03 14:15:49 +00:00
bool ret = m_RouterInfo.AddIntroducer (introducer);
if (ret)
2017-10-12 10:52:36 +00:00
UpdateRouterInfo ();
return ret;
2017-10-12 10:52:36 +00:00
}
2014-09-01 21:34:20 +00:00
2014-09-07 00:43:20 +00:00
void RouterContext::RemoveIntroducer (const boost::asio::ip::udp::endpoint& e)
2014-09-01 21:34:20 +00:00
{
2014-09-07 00:43:20 +00:00
if (m_RouterInfo.RemoveIntroducer (e))
2014-09-01 21:34:20 +00:00
UpdateRouterInfo ();
2017-10-12 10:52:36 +00:00
}
2015-01-28 20:12:15 +00:00
void RouterContext::SetFloodfill (bool floodfill)
{
m_IsFloodfill = floodfill;
if (floodfill)
m_RouterInfo.SetCaps (m_RouterInfo.GetCaps () | i2p::data::RouterInfo::eFloodfill);
else
{
2015-01-28 20:12:15 +00:00
m_RouterInfo.SetCaps (m_RouterInfo.GetCaps () & ~i2p::data::RouterInfo::eFloodfill);
// we don't publish number of routers and leaseset for non-floodfill
2016-02-21 01:20:19 +00:00
m_RouterInfo.DeleteProperty (i2p::data::ROUTER_INFO_PROPERTY_LEASESETS);
m_RouterInfo.DeleteProperty (i2p::data::ROUTER_INFO_PROPERTY_ROUTERS);
}
2015-01-28 20:12:15 +00:00
UpdateRouterInfo ();
}
std::string RouterContext::GetFamily () const
2016-04-26 22:48:23 +00:00
{
return m_RouterInfo.GetProperty (i2p::data::ROUTER_INFO_PROPERTY_FAMILY);
}
2016-02-21 01:20:19 +00:00
void RouterContext::SetFamily (const std::string& family)
{
std::string signature;
if (family.length () > 0)
signature = i2p::data::CreateFamilySignature (family, GetIdentHash ());
if (signature.length () > 0)
{
m_RouterInfo.SetProperty (i2p::data::ROUTER_INFO_PROPERTY_FAMILY, family);
m_RouterInfo.SetProperty (i2p::data::ROUTER_INFO_PROPERTY_FAMILY_SIG, signature);
2017-10-12 10:52:36 +00:00
}
2016-02-21 01:20:19 +00:00
else
{
m_RouterInfo.DeleteProperty (i2p::data::ROUTER_INFO_PROPERTY_FAMILY);
m_RouterInfo.DeleteProperty (i2p::data::ROUTER_INFO_PROPERTY_FAMILY_SIG);
2017-10-12 10:52:36 +00:00
}
}
2015-03-19 15:14:21 +00:00
2018-01-06 03:48:51 +00:00
void RouterContext::SetBandwidth (char L)
2017-10-05 14:37:28 +00:00
{
uint32_t limit = 0;
2017-01-25 21:14:01 +00:00
enum { low, high, extra, unlim } type = high;
/* detect parameters */
2017-10-12 10:52:36 +00:00
switch (L)
2016-03-31 01:31:17 +00:00
{
case i2p::data::CAPS_FLAG_LOW_BANDWIDTH1 : limit = 12; type = low; break;
case i2p::data::CAPS_FLAG_LOW_BANDWIDTH2 : limit = 48; type = low; break;
case i2p::data::CAPS_FLAG_HIGH_BANDWIDTH1 : limit = 64; type = high; break;
case i2p::data::CAPS_FLAG_HIGH_BANDWIDTH2 : limit = 128; type = high; break;
case i2p::data::CAPS_FLAG_HIGH_BANDWIDTH3 : limit = 256; type = high; break;
case i2p::data::CAPS_FLAG_EXTRA_BANDWIDTH1 : limit = 2048; type = extra; break;
2017-10-05 14:37:28 +00:00
case i2p::data::CAPS_FLAG_EXTRA_BANDWIDTH2 : limit = 1000000; type = unlim; break; // 1Gbyte/s
2016-03-31 01:31:17 +00:00
default:
limit = 48; type = low;
}
/* update caps & flags in RI */
auto caps = m_RouterInfo.GetCaps ();
caps &= ~i2p::data::RouterInfo::eHighBandwidth;
caps &= ~i2p::data::RouterInfo::eExtraBandwidth;
2017-10-12 10:52:36 +00:00
switch (type)
2016-03-31 01:31:17 +00:00
{
case low : /* not set */; break;
2017-01-25 21:14:01 +00:00
case extra : caps |= i2p::data::RouterInfo::eExtraBandwidth; break; // 'P'
case unlim : caps |= i2p::data::RouterInfo::eExtraBandwidth;
#if (__cplusplus >= 201703L) // C++ 17 or higher
[[fallthrough]];
#endif
2020-02-29 14:21:50 +00:00
// no break here, extra + high means 'X'
case high : caps |= i2p::data::RouterInfo::eHighBandwidth; break;
}
m_RouterInfo.SetCaps (caps);
UpdateRouterInfo ();
m_BandwidthLimit = limit;
2015-03-19 15:14:21 +00:00
}
2017-10-12 10:52:36 +00:00
void RouterContext::SetBandwidth (int limit)
2016-03-31 01:31:17 +00:00
{
if (limit > 2000) { SetBandwidth('X'); }
else if (limit > 256) { SetBandwidth('P'); }
else if (limit > 128) { SetBandwidth('O'); }
else if (limit > 64) { SetBandwidth('N'); }
else if (limit > 48) { SetBandwidth('M'); }
else if (limit > 12) { SetBandwidth('L'); }
else { SetBandwidth('K'); }
2021-03-02 03:13:17 +00:00
m_BandwidthLimit = limit; // set precise limit
2016-01-03 03:17:04 +00:00
}
void RouterContext::SetShareRatio (int percents)
{
if (percents < 0) percents = 0;
if (percents > 100) percents = 100;
m_ShareRatio = percents;
}
bool RouterContext::IsUnreachable () const
{
return m_RouterInfo.GetCaps () & i2p::data::RouterInfo::eUnreachable;
2017-10-12 10:52:36 +00:00
}
2020-10-04 02:29:52 +00:00
void RouterContext::RemoveNTCPAddress (bool v4only)
2019-04-17 01:04:04 +00:00
{
auto& addresses = m_RouterInfo.GetAddresses ();
2020-10-04 02:29:52 +00:00
for (auto it = addresses.begin (); it != addresses.end ();)
2019-04-17 01:04:04 +00:00
{
2020-10-04 02:29:52 +00:00
if ((*it)->transportStyle == i2p::data::RouterInfo::eTransportNTCP && !(*it)->IsNTCP2 () &&
(!v4only || (*it)->host.is_v4 ()))
2019-04-17 01:04:04 +00:00
{
2020-10-04 02:29:52 +00:00
it = addresses.erase (it);
if (v4only) break; // otherwise might be more than one address
2019-04-17 01:04:04 +00:00
}
2020-10-04 02:29:52 +00:00
else
++it;
2019-05-07 21:45:52 +00:00
}
}
2014-09-08 20:43:20 +00:00
void RouterContext::SetUnreachable ()
{
// set caps
2017-01-25 21:37:21 +00:00
uint8_t caps = m_RouterInfo.GetCaps ();
caps &= ~i2p::data::RouterInfo::eReachable;
caps |= i2p::data::RouterInfo::eUnreachable;
caps &= ~i2p::data::RouterInfo::eFloodfill; // can't be floodfill
2017-10-12 10:52:36 +00:00
m_RouterInfo.SetCaps (caps);
uint16_t port = 0;
2014-09-09 01:53:55 +00:00
// delete previous introducers
2019-04-17 01:04:04 +00:00
auto& addresses = m_RouterInfo.GetAddresses ();
2017-10-12 10:52:36 +00:00
for (auto& addr : addresses)
if (addr->ssu)
{
2021-03-01 16:09:25 +00:00
addr->cost = i2p::data::COST_SSU_THROUGH_INTRODUCERS;
addr->caps &= ~i2p::data::RouterInfo::eSSUIntroducer; // can't be introducer
addr->ssu->introducers.clear ();
port = addr->port;
}
2020-10-03 22:46:12 +00:00
// remove NTCP2 v4 address
bool ntcp2; i2p::config::GetOption("ntcp2.enabled", ntcp2);
if (ntcp2)
PublishNTCP2Address (port, false, true, false, false); // ipv4 only
2014-09-08 20:43:20 +00:00
// update
UpdateRouterInfo ();
}
void RouterContext::SetReachable ()
{
// update caps
uint8_t caps = m_RouterInfo.GetCaps ();
caps &= ~i2p::data::RouterInfo::eUnreachable;
caps |= i2p::data::RouterInfo::eReachable;
if (m_IsFloodfill)
caps |= i2p::data::RouterInfo::eFloodfill;
m_RouterInfo.SetCaps (caps);
uint16_t port = 0;
// delete previous introducers
2019-04-17 01:04:04 +00:00
auto& addresses = m_RouterInfo.GetAddresses ();
2016-08-07 21:52:18 +00:00
for (auto& addr : addresses)
if (addr->ssu)
{
2021-03-01 16:09:25 +00:00
addr->cost = i2p::data::COST_SSU_DIRECT;
addr->caps |= i2p::data::RouterInfo::eSSUIntroducer;
addr->ssu->introducers.clear ();
port = addr->port;
}
2020-10-03 22:46:12 +00:00
// insert NTCP2 back
bool ntcp2; i2p::config::GetOption("ntcp2.enabled", ntcp2);
if (ntcp2)
{
2020-10-03 22:46:12 +00:00
bool published; i2p::config::GetOption ("ntcp2.published", published);
if (published)
{
2020-10-03 22:46:12 +00:00
uint16_t ntcp2Port; i2p::config::GetOption ("ntcp2.port", ntcp2Port);
if (!ntcp2Port) ntcp2Port = port;
PublishNTCP2Address (ntcp2Port, true, true, false, false); // ipv4 only
}
}
// update
UpdateRouterInfo ();
2017-10-12 10:52:36 +00:00
}
void RouterContext::SetSupportsV6 (bool supportsV6)
{
if (supportsV6)
{
// insert v6 addresses if necessary
2020-10-03 22:46:12 +00:00
bool foundSSU = false, foundNTCP2 = false;
uint16_t port = 0;
auto& addresses = m_RouterInfo.GetAddresses ();
for (auto& addr: addresses)
{
2021-03-12 21:13:01 +00:00
if (addr->IsV6 () && !i2p::util::net::IsYggdrasilAddress (addr->host))
{
if (addr->transportStyle == i2p::data::RouterInfo::eTransportSSU)
foundSSU = true;
2020-10-03 22:46:12 +00:00
else if (addr->IsPublishedNTCP2 ())
foundNTCP2 = true;
}
port = addr->port;
}
if (!port) i2p::config::GetOption("port", port);
// SSU
if (!foundSSU)
{
bool ssu; i2p::config::GetOption("ssu", ssu);
if (ssu)
{
std::string host = "::1"; // TODO: read host
2020-11-21 23:44:40 +00:00
m_RouterInfo.AddSSUAddress (host.c_str (), port, nullptr);
}
}
// NTCP2
if (!foundNTCP2)
{
bool ntcp2; i2p::config::GetOption("ntcp2.enabled", ntcp2);
bool ntcp2Published; i2p::config::GetOption("ntcp2.published", ntcp2Published);
if (ntcp2 && ntcp2Published)
{
std::string ntcp2Host;
if (!i2p::config::IsDefault ("ntcp2.addressv6"))
i2p::config::GetOption ("ntcp2.addressv6", ntcp2Host);
else
ntcp2Host = "::1";
uint16_t ntcp2Port; i2p::config::GetOption ("ntcp2.port", ntcp2Port);
if (!ntcp2Port) ntcp2Port = port;
m_RouterInfo.AddNTCP2Address (m_NTCP2Keys->staticPublicKey, m_NTCP2Keys->iv, boost::asio::ip::address::from_string (ntcp2Host), ntcp2Port);
}
}
m_RouterInfo.EnableV6 ();
}
else
m_RouterInfo.DisableV6 ();
2014-10-29 22:46:35 +00:00
UpdateRouterInfo ();
2016-03-24 22:44:41 +00:00
}
2017-10-12 10:52:36 +00:00
2016-03-24 22:44:41 +00:00
void RouterContext::SetSupportsV4 (bool supportsV4)
{
2021-03-12 21:13:01 +00:00
// check if updates
if (supportsV4 && SupportsV4 ()) return;
if (!supportsV4 && !SupportsV4 ()) return;
// update
2016-03-24 22:44:41 +00:00
if (supportsV4)
2021-03-12 21:13:01 +00:00
{
bool foundSSU = false, foundNTCP2 = false;
std::string host = "127.0.0.1";
uint16_t port = 0;
auto& addresses = m_RouterInfo.GetAddresses ();
for (auto& addr: addresses)
{
if (addr->IsV4 ())
{
if (addr->transportStyle == i2p::data::RouterInfo::eTransportSSU)
foundSSU = true;
else if (addr->transportStyle == i2p::data::RouterInfo::eTransportNTCP)
foundNTCP2 = true;
}
if (addr->port) port = addr->port;
}
if (!port) i2p::config::GetOption("port", port);
// SSU
if (!foundSSU)
{
bool ssu; i2p::config::GetOption("ssu", ssu);
if (ssu)
m_RouterInfo.AddSSUAddress (host.c_str (), port, nullptr);
}
// NTCP2
if (!foundNTCP2)
{
bool ntcp2; i2p::config::GetOption("ntcp2.enabled", ntcp2);
if (ntcp2)
{
bool ntcp2Published; i2p::config::GetOption("ntcp2.published", ntcp2Published);
if (ntcp2Published)
{
uint16_t ntcp2Port; i2p::config::GetOption ("ntcp2.port", ntcp2Port);
if (!ntcp2Port) ntcp2Port = port;
m_RouterInfo.AddNTCP2Address (m_NTCP2Keys->staticPublicKey, m_NTCP2Keys->iv, boost::asio::ip::address::from_string (host), ntcp2Port);
}
else
m_RouterInfo.AddNTCP2Address (m_NTCP2Keys->staticPublicKey, m_NTCP2Keys->iv);
}
}
2016-03-24 22:44:41 +00:00
m_RouterInfo.EnableV4 ();
2021-03-12 21:13:01 +00:00
}
2016-03-24 22:44:41 +00:00
else
m_RouterInfo.DisableV4 ();
UpdateRouterInfo ();
}
2017-10-12 10:52:36 +00:00
2021-01-31 23:30:53 +00:00
void RouterContext::SetSupportsMesh (bool supportsmesh, const boost::asio::ip::address_v6& host)
2021-01-31 22:25:07 +00:00
{
if (supportsmesh)
2021-01-31 23:30:53 +00:00
{
2021-01-31 22:25:07 +00:00
m_RouterInfo.EnableMesh ();
2021-01-31 23:30:53 +00:00
uint16_t port = 0;
i2p::config::GetOption ("ntcp2.port", port);
if (!port) i2p::config::GetOption("port", port);
2021-02-01 00:09:38 +00:00
bool foundMesh = false;
auto& addresses = m_RouterInfo.GetAddresses ();
for (auto& addr: addresses)
{
if (!port) port = addr->port;
if (i2p::util::net::IsYggdrasilAddress (addr->host))
2021-01-31 23:30:53 +00:00
{
2021-02-01 00:09:38 +00:00
foundMesh = true;
break;
}
}
if (!foundMesh)
m_RouterInfo.AddNTCP2Address (m_NTCP2Keys->staticPublicKey, m_NTCP2Keys->iv, host, port);
2021-01-31 23:30:53 +00:00
}
2021-01-31 22:25:07 +00:00
else
m_RouterInfo.DisableMesh ();
UpdateRouterInfo ();
}
void RouterContext::UpdateNTCP2V6Address (const boost::asio::ip::address& host)
{
2021-02-01 17:47:41 +00:00
bool isYgg = i2p::util::net::IsYggdrasilAddress (host);
bool updated = false;
auto& addresses = m_RouterInfo.GetAddresses ();
for (auto& addr: addresses)
{
if (addr->IsPublishedNTCP2 ())
{
2021-02-01 17:47:41 +00:00
bool isYgg1 = i2p::util::net::IsYggdrasilAddress (addr->host);
if (addr->host.is_v6 () && ((isYgg && isYgg1) || (!isYgg && !isYgg1)))
{
if (addr->host != host)
{
addr->host = host;
updated = true;
}
2018-08-27 22:56:57 +00:00
break;
}
}
}
2018-08-27 20:01:47 +00:00
if (updated)
2019-05-07 21:45:52 +00:00
UpdateRouterInfo ();
}
2015-03-18 19:36:07 +00:00
void RouterContext::UpdateStats ()
{
if (m_IsFloodfill)
{
// update routers and leasesets
m_RouterInfo.SetProperty (i2p::data::ROUTER_INFO_PROPERTY_LEASESETS, std::to_string(i2p::data::netdb.GetNumLeaseSets ()));
m_RouterInfo.SetProperty (i2p::data::ROUTER_INFO_PROPERTY_ROUTERS, std::to_string(i2p::data::netdb.GetNumRouters ()));
2017-10-12 10:52:36 +00:00
UpdateRouterInfo ();
2015-03-18 19:36:07 +00:00
}
}
2017-10-12 10:52:36 +00:00
2018-09-21 14:13:18 +00:00
void RouterContext::UpdateTimestamp (uint64_t ts)
{
if (ts > m_LastUpdateTime + ROUTER_INFO_UPDATE_INTERVAL)
UpdateRouterInfo ();
}
2013-10-23 02:45:40 +00:00
bool RouterContext::Load ()
{
2015-11-03 18:05:37 +00:00
{
2020-11-21 01:31:50 +00:00
std::ifstream fk (i2p::fs::DataDirPath (ROUTER_KEYS), std::ifstream::in | std::ifstream::binary);
if (!fk.is_open ()) return false;
fk.seekg (0, std::ios::end);
size_t len = fk.tellg();
fk.seekg (0, std::ios::beg);
if (len == sizeof (i2p::data::Keys)) // old keys file format
{
i2p::data::Keys keys;
fk.read ((char *)&keys, sizeof (keys));
m_Keys = keys;
}
else // new keys file format
{
uint8_t * buf = new uint8_t[len];
fk.read ((char *)buf, len);
m_Keys.FromBuffer (buf, len);
delete[] buf;
}
2015-11-03 18:05:37 +00:00
}
2020-11-21 01:31:50 +00:00
std::shared_ptr<const i2p::data::IdentityEx> oldIdentity;
2021-03-07 15:07:51 +00:00
bool rekey = m_Keys.GetPublic ()->GetSigningKeyType () == i2p::data::SIGNING_KEY_TYPE_DSA_SHA1;
if (!rekey && m_Keys.GetPublic ()->GetCryptoKeyType () == i2p::data::CRYPTO_KEY_TYPE_ELGAMAL)
{
// rekey routers with bandwidth = L (or default) this time
std::string bandwidth; i2p::config::GetOption("bandwidth", bandwidth);
if (bandwidth.empty () || bandwidth[0] == 'L') rekey = true;
}
if (rekey)
2015-11-03 18:05:37 +00:00
{
2020-11-21 01:31:50 +00:00
// update keys
LogPrint (eLogInfo, "Router: router keys are obsolete. Creating new");
oldIdentity = m_Keys.GetPublic ();
2020-12-15 21:06:32 +00:00
m_Keys = i2p::data::PrivateKeys::CreateRandomKeys (i2p::data::SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519,
i2p::data::CRYPTO_KEY_TYPE_ECIES_X25519_AEAD);
2020-11-21 01:31:50 +00:00
SaveKeys ();
}
2018-08-04 01:16:35 +00:00
// read NTCP2 keys if available
std::ifstream n2k (i2p::fs::DataDirPath (NTCP2_KEYS), std::ifstream::in | std::ifstream::binary);
2019-05-07 21:45:52 +00:00
if (n2k)
2018-08-04 01:16:35 +00:00
{
n2k.seekg (0, std::ios::end);
2020-11-21 01:31:50 +00:00
size_t len = n2k.tellg();
2018-08-04 01:16:35 +00:00
n2k.seekg (0, std::ios::beg);
if (len == sizeof (NTCP2PrivateKeys))
{
m_NTCP2Keys.reset (new NTCP2PrivateKeys ());
2019-05-07 21:45:52 +00:00
n2k.read ((char *)m_NTCP2Keys.get (), sizeof (NTCP2PrivateKeys));
}
2018-08-04 01:16:35 +00:00
n2k.close ();
}
// read RouterInfo
2020-11-21 01:31:50 +00:00
m_RouterInfo.SetRouterIdentity (oldIdentity ? oldIdentity : GetIdentity ());
2017-10-12 10:52:36 +00:00
i2p::data::RouterInfo routerInfo(i2p::fs::DataDirPath (ROUTER_INFO));
if (!routerInfo.IsUnreachable ()) // router.info looks good
{
m_RouterInfo.Update (routerInfo.GetBuffer (), routerInfo.GetBufferLen ());
2020-11-21 01:31:50 +00:00
if (oldIdentity)
m_RouterInfo.SetRouterIdentity (GetIdentity ()); // from new keys
m_RouterInfo.SetProperty ("router.version", I2P_VERSION);
2021-03-05 14:29:28 +00:00
m_RouterInfo.DeleteProperty ("coreVersion"); // TODO: remove later
}
else
{
LogPrint (eLogError, ROUTER_INFO, " is malformed. Creating new");
NewRouterInfo ();
2017-10-12 10:52:36 +00:00
}
if (IsUnreachable ())
SetReachable (); // we assume reachable until we discover firewall through peer tests
2017-10-12 10:52:36 +00:00
2018-06-11 19:33:48 +00:00
// read NTCP2
2018-07-19 13:45:24 +00:00
bool ntcp2; i2p::config::GetOption("ntcp2.enabled", ntcp2);
bool ygg; i2p::config::GetOption("meshnets.yggdrasil", ygg);
if (ntcp2 || ygg)
2018-06-11 19:33:48 +00:00
{
2018-08-04 01:16:35 +00:00
if (!m_NTCP2Keys) NewNTCP2Keys ();
UpdateNTCP2Address (true); // enable NTCP2
2018-06-11 19:33:48 +00:00
}
2018-08-03 18:49:28 +00:00
else
UpdateNTCP2Address (false); // disable NTCP2
2018-06-11 19:33:48 +00:00
2013-10-23 02:45:40 +00:00
return true;
}
void RouterContext::SaveKeys ()
2017-10-12 10:52:36 +00:00
{
2015-11-03 18:05:37 +00:00
// save in the same format as .dat files
std::ofstream fk (i2p::fs::DataDirPath (ROUTER_KEYS), std::ofstream::binary | std::ofstream::out);
2015-11-03 18:05:37 +00:00
size_t len = m_Keys.GetFullLen ();
uint8_t * buf = new uint8_t[len];
m_Keys.ToBuffer (buf, len);
fk.write ((char *)buf, len);
delete[] buf;
}
2014-10-12 01:27:55 +00:00
std::shared_ptr<i2p::tunnel::TunnelPool> RouterContext::GetTunnelPool () const
{
2017-10-12 10:52:36 +00:00
return i2p::tunnel::tunnels.GetExploratoryPool ();
}
2020-01-07 20:20:55 +00:00
void RouterContext::HandleI2NPMessage (const uint8_t * buf, size_t len)
2014-10-12 01:27:55 +00:00
{
2020-01-07 20:20:55 +00:00
i2p::HandleI2NPMessage (CreateI2NPMessage (buf, GetI2NPMessageLength (buf, len)));
2015-02-23 19:41:56 +00:00
}
2020-11-05 20:27:37 +00:00
bool RouterContext::HandleCloveI2NPMessage (I2NPMessageType typeID, const uint8_t * payload, size_t len)
{
auto msg = CreateI2NPMessage (typeID, payload, len);
if (!msg) return false;
i2p::HandleI2NPMessage (msg);
return true;
}
2015-06-16 14:14:14 +00:00
void RouterContext::ProcessGarlicMessage (std::shared_ptr<I2NPMessage> msg)
2015-06-10 02:14:31 +00:00
{
std::unique_lock<std::mutex> l(m_GarlicMutex);
if (IsECIES ())
{
uint8_t * buf = msg->GetPayload ();
uint32_t len = bufbe32toh (buf);
if (len > msg->GetLength ())
{
LogPrint (eLogWarning, "Router: garlic message length ", len, " exceeds I2NP message length ", msg->GetLength ());
return;
}
buf += 4;
if (m_ECIESSession)
m_ECIESSession->HandleNextMessage (buf, len);
else
LogPrint (eLogError, "Router: Session is not set for ECIES router");
}
else
i2p::garlic::GarlicDestination::ProcessGarlicMessage (msg);
2017-10-12 10:52:36 +00:00
}
2015-06-16 14:14:14 +00:00
void RouterContext::ProcessDeliveryStatusMessage (std::shared_ptr<I2NPMessage> msg)
2015-06-10 02:14:31 +00:00
{
if (i2p::data::netdb.GetPublishReplyToken () == bufbe32toh (msg->GetPayload () + DELIVERY_STATUS_MSGID_OFFSET))
i2p::data::netdb.PostI2NPMsg (msg);
else
{
std::unique_lock<std::mutex> l(m_GarlicMutex);
i2p::garlic::GarlicDestination::ProcessDeliveryStatusMessage (msg);
}
2017-10-12 10:52:36 +00:00
}
2016-07-28 17:24:25 +00:00
void RouterContext::CleanupDestination ()
{
std::unique_lock<std::mutex> l(m_GarlicMutex);
i2p::garlic::GarlicDestination::CleanupExpiredTags ();
}
2017-10-12 10:52:36 +00:00
2015-02-23 19:41:56 +00:00
uint32_t RouterContext::GetUptime () const
{
2019-06-19 15:43:04 +00:00
return std::chrono::duration_cast<std::chrono::seconds> (std::chrono::steady_clock::now() - m_StartupTime).count ();
2017-10-12 10:52:36 +00:00
}
2020-03-31 21:35:51 +00:00
bool RouterContext::Decrypt (const uint8_t * encrypted, uint8_t * data, BN_CTX * ctx, i2p::data::CryptoKeyType preferredCrypto) const
{
return m_Decryptor ? m_Decryptor->Decrypt (encrypted, data, ctx, true) : false;
}
bool RouterContext::DecryptTunnelBuildRecord (const uint8_t * encrypted, uint8_t * data, BN_CTX * ctx)
{
if (!m_TunnelDecryptor) return false;
if (IsECIES ())
{
if (!m_InitialNoiseState) return false;
// m_InitialNoiseState is h = SHA256(h || hepk)
2020-11-04 18:31:28 +00:00
m_CurrentNoiseState.reset (new i2p::crypto::NoiseSymmetricState (*m_InitialNoiseState));
m_CurrentNoiseState->MixHash (encrypted, 32); // h = SHA256(h || sepk)
uint8_t sharedSecret[32];
if (!m_TunnelDecryptor->Decrypt (encrypted, sharedSecret, ctx, false))
2021-01-01 20:03:11 +00:00
{
LogPrint (eLogWarning, "Router: Incorrect ephemeral public key");
return false;
}
m_CurrentNoiseState->MixKey (sharedSecret);
encrypted += 32;
uint8_t nonce[12];
memset (nonce, 0, 12);
if (!i2p::crypto::AEADChaCha20Poly1305 (encrypted, ECIES_BUILD_REQUEST_RECORD_CLEAR_TEXT_SIZE,
m_CurrentNoiseState->m_H, 32, m_CurrentNoiseState->m_CK + 32, nonce, data, ECIES_BUILD_REQUEST_RECORD_CLEAR_TEXT_SIZE, false)) // decrypt
{
LogPrint (eLogWarning, "Router: Tunnel record AEAD decryption failed");
return false;
}
2020-11-04 18:31:28 +00:00
m_CurrentNoiseState->MixHash (encrypted, ECIES_BUILD_REQUEST_RECORD_CLEAR_TEXT_SIZE + 16); // h = SHA256(h || ciphertext)
return true;
}
else
return m_TunnelDecryptor->Decrypt (encrypted, data, ctx, false);
}
2018-09-09 02:08:08 +00:00
i2p::crypto::X25519Keys& RouterContext::GetStaticKeys ()
{
if (!m_StaticKeys)
2019-05-07 21:45:52 +00:00
{
2018-09-09 02:08:08 +00:00
if (!m_NTCP2Keys) NewNTCP2Keys ();
2018-09-09 12:38:12 +00:00
auto x = new i2p::crypto::X25519Keys (m_NTCP2Keys->staticPrivateKey, m_NTCP2Keys->staticPublicKey);
if (!m_StaticKeys)
m_StaticKeys.reset (x);
else
delete x;
2018-09-09 02:08:08 +00:00
}
2019-05-07 21:45:52 +00:00
return *m_StaticKeys;
}
}