2020-05-22 16:18:41 +03:00
|
|
|
/*
|
2022-01-15 20:48:49 +03:00
|
|
|
* Copyright (c) 2013-2022, The PurpleI2P Project
|
2020-05-22 16:18:41 +03:00
|
|
|
*
|
|
|
|
* 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 06:45:40 +04:00
|
|
|
#include <fstream>
|
2018-06-11 22:33:48 +03:00
|
|
|
#include <openssl/rand.h>
|
2016-01-20 03:00:00 +03:00
|
|
|
#include "Config.h"
|
2015-11-03 17:15:49 +03:00
|
|
|
#include "Crypto.h"
|
2018-06-15 19:52:43 +03:00
|
|
|
#include "Ed25519.h"
|
2014-09-01 00:46:39 +04:00
|
|
|
#include "Timestamp.h"
|
2014-10-12 05:27:55 +04:00
|
|
|
#include "I2NPProtocol.h"
|
2017-04-22 03:04:16 +03:00
|
|
|
#include "NetDb.hpp"
|
2016-02-11 03:00:00 +03:00
|
|
|
#include "FS.h"
|
2014-01-30 04:56:48 +04:00
|
|
|
#include "util.h"
|
2014-08-17 09:09:15 +04:00
|
|
|
#include "version.h"
|
2015-11-03 17:15:49 +03:00
|
|
|
#include "Log.h"
|
2016-02-21 04:20:19 +03:00
|
|
|
#include "Family.h"
|
2020-12-04 06:01:58 +03:00
|
|
|
#include "ECIESX25519AEADRatchetSession.h"
|
2015-11-03 17:15:49 +03:00
|
|
|
#include "RouterContext.h"
|
2013-10-23 06:45:40 +04:00
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
RouterContext context;
|
|
|
|
|
2014-09-01 00:46:39 +04:00
|
|
|
RouterContext::RouterContext ():
|
2017-10-12 13:52:36 +03:00
|
|
|
m_LastUpdateTime (0), m_AcceptsTunnels (true), m_IsFloodfill (false),
|
2021-05-15 16:28:16 +03:00
|
|
|
m_ShareRatio (100), m_Status (eRouterStatusUnknown), m_StatusV6 (eRouterStatusUnknown),
|
2022-08-03 03:02:55 +03:00
|
|
|
m_Error (eRouterErrorNone), m_ErrorV6 (eRouterErrorNone), m_NetID (I2PD_NET_ID)
|
2014-09-04 17:31:42 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void RouterContext::Init ()
|
2013-10-23 06:45:40 +04:00
|
|
|
{
|
2015-11-03 17:15:49 +03:00
|
|
|
srand (i2p::util::GetMillisecondsSinceEpoch () % 1000);
|
2020-01-07 23:20:55 +03:00
|
|
|
m_StartupTime = std::chrono::steady_clock::now();
|
2019-11-12 23:06:04 +03:00
|
|
|
|
2013-10-23 06:45:40 +04:00
|
|
|
if (!Load ())
|
|
|
|
CreateNewRouter ();
|
2018-01-06 06:48:51 +03:00
|
|
|
m_Decryptor = m_Keys.CreateDecryptor (nullptr);
|
2021-02-27 05:02:51 +03:00
|
|
|
m_TunnelDecryptor = m_Keys.CreateDecryptor (nullptr);
|
2014-09-01 00:46:39 +04:00
|
|
|
UpdateRouterInfo ();
|
2021-09-05 18:16:41 +03:00
|
|
|
i2p::crypto::InitNoiseNState (m_InitialNoiseState, GetIdentity ()->GetEncryptionPublicKey ());
|
|
|
|
m_ECIESSession = std::make_shared<i2p::garlic::RouterIncomingRatchetSession>(m_InitialNoiseState);
|
2014-08-17 09:09:15 +04:00
|
|
|
}
|
|
|
|
|
2013-10-23 06:45:40 +04:00
|
|
|
void RouterContext::CreateNewRouter ()
|
|
|
|
{
|
2020-12-16 00:06:32 +03: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-08 00:45:52 +03:00
|
|
|
SaveKeys ();
|
2014-09-01 00:46:39 +04:00
|
|
|
NewRouterInfo ();
|
2014-02-23 20:48:09 +04:00
|
|
|
}
|
|
|
|
|
2014-09-01 00:46:39 +04:00
|
|
|
void RouterContext::NewRouterInfo ()
|
2014-02-23 20:48:09 +04:00
|
|
|
{
|
2022-01-15 20:48:49 +03:00
|
|
|
i2p::data::LocalRouterInfo routerInfo;
|
2014-11-20 20:21:27 +03:00
|
|
|
routerInfo.SetRouterIdentity (GetIdentity ());
|
2016-01-20 03:00:00 +03:00
|
|
|
uint16_t port; i2p::config::GetOption("port", port);
|
2022-08-11 05:00:11 +03:00
|
|
|
if (!port) port = SelectRandomPort ();
|
2022-05-20 19:56:05 +03:00
|
|
|
bool ipv4; i2p::config::GetOption("ipv4", ipv4);
|
|
|
|
bool ipv6; i2p::config::GetOption("ipv6", ipv6);
|
|
|
|
bool ntcp2; i2p::config::GetOption("ntcp2.enabled", ntcp2);
|
|
|
|
bool ssu2; i2p::config::GetOption("ssu2.enabled", ssu2);
|
|
|
|
bool ygg; i2p::config::GetOption("meshnets.yggdrasil", ygg);
|
|
|
|
bool nat; i2p::config::GetOption("nat", nat);
|
2021-05-15 16:28:16 +03:00
|
|
|
|
2021-02-18 05:12:17 +03:00
|
|
|
if ((ntcp2 || ygg) && !m_NTCP2Keys)
|
2021-05-15 16:28:16 +03:00
|
|
|
NewNTCP2Keys ();
|
2022-03-13 05:40:12 +03:00
|
|
|
if (ssu2 && !m_SSU2Keys)
|
|
|
|
NewSSU2Keys ();
|
2021-05-15 16:28:16 +03:00
|
|
|
bool ntcp2Published = false;
|
2021-02-18 05:12:17 +03:00
|
|
|
if (ntcp2)
|
2021-05-15 16:28:16 +03:00
|
|
|
{
|
2021-02-18 05:12:17 +03:00
|
|
|
i2p::config::GetOption("ntcp2.published", ntcp2Published);
|
2021-03-07 02:43:50 +03:00
|
|
|
if (ntcp2Published)
|
|
|
|
{
|
|
|
|
std::string ntcp2proxy; i2p::config::GetOption("ntcp2.proxy", ntcp2proxy);
|
|
|
|
if (!ntcp2proxy.empty ()) ntcp2Published = false;
|
2021-05-15 16:28:16 +03:00
|
|
|
}
|
|
|
|
}
|
2022-04-27 03:30:39 +03:00
|
|
|
bool ssu2Published = false;
|
|
|
|
if (ssu2)
|
|
|
|
i2p::config::GetOption("ssu2.published", ssu2Published);
|
2021-03-08 23:57:05 +03:00
|
|
|
uint8_t caps = 0, addressCaps = 0;
|
2016-07-22 17:16:57 +03:00
|
|
|
if (ipv4)
|
|
|
|
{
|
|
|
|
std::string host = "127.0.0.1";
|
2016-07-19 03:00:00 +03:00
|
|
|
if (!i2p::config::IsDefault("host"))
|
|
|
|
i2p::config::GetOption("host", host);
|
2021-03-09 23:28:07 +03: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;
|
2021-05-15 16:28:16 +03:00
|
|
|
}
|
|
|
|
|
2021-02-18 05:12:17 +03:00
|
|
|
if (ntcp2)
|
|
|
|
{
|
|
|
|
if (ntcp2Published)
|
2021-02-18 05:51:35 +03:00
|
|
|
routerInfo.AddNTCP2Address (m_NTCP2Keys->staticPublicKey, m_NTCP2Keys->iv, boost::asio::ip::address_v4::from_string (host), port);
|
2021-02-18 05:12:17 +03:00
|
|
|
else // add non-published NTCP2 address
|
2021-03-08 23:57:05 +03:00
|
|
|
{
|
|
|
|
addressCaps = i2p::data::RouterInfo::AddressCaps::eV4;
|
2021-05-15 16:28:16 +03:00
|
|
|
routerInfo.AddNTCP2Address (m_NTCP2Keys->staticPublicKey, m_NTCP2Keys->iv);
|
|
|
|
}
|
|
|
|
}
|
2022-03-13 05:40:12 +03:00
|
|
|
if (ssu2)
|
|
|
|
{
|
2022-04-27 03:30:39 +03:00
|
|
|
if (ssu2Published)
|
2022-08-09 21:08:13 +03:00
|
|
|
{
|
|
|
|
uint16_t ssu2Port; i2p::config::GetOption ("ssu2.port", ssu2Port);
|
2022-11-23 06:03:19 +03:00
|
|
|
if (!ssu2Port) ssu2Port = port;
|
2022-08-09 21:08:13 +03:00
|
|
|
routerInfo.AddSSU2Address (m_SSU2Keys->staticPublicKey, m_SSU2Keys->intro, boost::asio::ip::address_v4::from_string (host), ssu2Port);
|
2022-10-09 20:24:43 +03:00
|
|
|
}
|
2022-04-27 03:30:39 +03:00
|
|
|
else
|
2022-05-20 19:56:05 +03:00
|
|
|
{
|
2022-04-27 03:30:39 +03:00
|
|
|
addressCaps |= i2p::data::RouterInfo::AddressCaps::eV4;
|
|
|
|
routerInfo.AddSSU2Address (m_SSU2Keys->staticPublicKey, m_SSU2Keys->intro);
|
2022-05-20 19:56:05 +03:00
|
|
|
}
|
|
|
|
}
|
2016-07-22 17:16:57 +03:00
|
|
|
}
|
|
|
|
if (ipv6)
|
|
|
|
{
|
2019-05-08 00:45:52 +03:00
|
|
|
std::string host = "::1";
|
2016-07-22 17:34:56 +03:00
|
|
|
if (!i2p::config::IsDefault("host") && !ipv4) // override if v6 only
|
|
|
|
i2p::config::GetOption("host", host);
|
2021-05-15 16:28:16 +03:00
|
|
|
else
|
2021-03-09 23:28:07 +03:00
|
|
|
{
|
|
|
|
std::string address6; i2p::config::GetOption("address6", address6);
|
|
|
|
if (!address6.empty ()) host = address6;
|
|
|
|
}
|
2021-05-15 16:28:16 +03:00
|
|
|
|
2021-02-18 05:12:17 +03:00
|
|
|
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);
|
|
|
|
}
|
2021-05-15 16:28:16 +03:00
|
|
|
else
|
|
|
|
{
|
2021-03-08 23:57:05 +03:00
|
|
|
if (!ipv4) // no other ntcp2 addresses yet
|
|
|
|
routerInfo.AddNTCP2Address (m_NTCP2Keys->staticPublicKey, m_NTCP2Keys->iv);
|
|
|
|
addressCaps |= i2p::data::RouterInfo::AddressCaps::eV6;
|
2021-05-15 16:28:16 +03:00
|
|
|
}
|
|
|
|
}
|
2022-03-13 05:40:12 +03:00
|
|
|
if (ssu2)
|
|
|
|
{
|
2022-04-27 03:30:39 +03:00
|
|
|
if (ssu2Published)
|
2022-08-09 21:08:13 +03:00
|
|
|
{
|
|
|
|
uint16_t ssu2Port; i2p::config::GetOption ("ssu2.port", ssu2Port);
|
2022-11-23 06:03:19 +03:00
|
|
|
if (!ssu2Port) ssu2Port = port;
|
2022-08-09 21:08:13 +03:00
|
|
|
routerInfo.AddSSU2Address (m_SSU2Keys->staticPublicKey, m_SSU2Keys->intro, boost::asio::ip::address_v6::from_string (host), ssu2Port);
|
2022-10-09 20:24:43 +03:00
|
|
|
}
|
2022-04-27 03:30:39 +03:00
|
|
|
else
|
2022-05-20 19:56:05 +03:00
|
|
|
{
|
2022-04-27 03:30:39 +03:00
|
|
|
if (!ipv4) // no other ssu2 addresses yet
|
|
|
|
routerInfo.AddSSU2Address (m_SSU2Keys->staticPublicKey, m_SSU2Keys->intro);
|
|
|
|
addressCaps |= i2p::data::RouterInfo::AddressCaps::eV6;
|
2022-05-20 19:56:05 +03:00
|
|
|
}
|
|
|
|
}
|
2016-11-24 18:11:46 +03:00
|
|
|
}
|
2021-02-18 05:12:17 +03:00
|
|
|
if (ygg)
|
|
|
|
{
|
|
|
|
auto yggaddr = i2p::util::net::GetYggdrasilAddress ();
|
|
|
|
if (!yggaddr.is_unspecified ())
|
|
|
|
routerInfo.AddNTCP2Address (m_NTCP2Keys->staticPublicKey, m_NTCP2Keys->iv, yggaddr, port);
|
2021-05-15 16:28:16 +03:00
|
|
|
}
|
2021-03-08 23:57:05 +03:00
|
|
|
|
|
|
|
if (addressCaps)
|
|
|
|
routerInfo.SetUnreachableAddressesTransportCaps (addressCaps);
|
2022-01-16 02:54:02 +03:00
|
|
|
routerInfo.UpdateCaps (caps); // caps + L
|
2017-10-12 13:52:36 +03:00
|
|
|
routerInfo.SetProperty ("netId", std::to_string (m_NetID));
|
2014-08-17 09:09:15 +04:00
|
|
|
routerInfo.SetProperty ("router.version", I2P_VERSION);
|
2014-08-26 06:47:12 +04:00
|
|
|
routerInfo.CreateBuffer (m_Keys);
|
2015-11-03 17:15:49 +03:00
|
|
|
m_RouterInfo.SetRouterIdentity (GetIdentity ());
|
2014-07-22 16:03:02 +04:00
|
|
|
m_RouterInfo.Update (routerInfo.GetBuffer (), routerInfo.GetBufferLen ());
|
2014-08-17 09:09:15 +04:00
|
|
|
}
|
|
|
|
|
2022-08-11 05:00:11 +03:00
|
|
|
uint16_t RouterContext::SelectRandomPort () const
|
|
|
|
{
|
|
|
|
uint16_t port = rand () % (30777 - 9111) + 9111; // I2P network ports range
|
|
|
|
if (port == 9150) port = 9151; // Tor browser
|
|
|
|
return port;
|
2022-10-09 20:24:43 +03:00
|
|
|
}
|
|
|
|
|
2014-09-01 00:46:39 +04:00
|
|
|
void RouterContext::UpdateRouterInfo ()
|
|
|
|
{
|
|
|
|
m_RouterInfo.CreateBuffer (m_Keys);
|
2016-02-11 03:00:00 +03:00
|
|
|
m_RouterInfo.SaveToFile (i2p::fs::DataDirPath (ROUTER_INFO));
|
2014-09-01 00:46:39 +04:00
|
|
|
m_LastUpdateTime = i2p::util::GetSecondsSinceEpoch ();
|
2017-10-12 13:52:36 +03:00
|
|
|
}
|
2014-09-11 17:32:34 +04:00
|
|
|
|
2018-06-11 22:33:48 +03:00
|
|
|
void RouterContext::NewNTCP2Keys ()
|
|
|
|
{
|
2022-03-02 05:23:08 +03:00
|
|
|
m_NTCP2StaticKeys.reset (new i2p::crypto::X25519Keys ());
|
|
|
|
m_NTCP2StaticKeys->GenerateKeys ();
|
2018-06-11 22:33:48 +03:00
|
|
|
m_NTCP2Keys.reset (new NTCP2PrivateKeys ());
|
2022-03-02 05:23:08 +03:00
|
|
|
m_NTCP2StaticKeys->GetPrivateKey (m_NTCP2Keys->staticPrivateKey);
|
|
|
|
memcpy (m_NTCP2Keys->staticPublicKey, m_NTCP2StaticKeys->GetPublicKey (), 32);
|
2018-06-11 22:33:48 +03:00
|
|
|
RAND_bytes (m_NTCP2Keys->iv, 16);
|
|
|
|
// save
|
|
|
|
std::ofstream fk (i2p::fs::DataDirPath (NTCP2_KEYS), std::ofstream::binary | std::ofstream::out);
|
2019-05-08 00:45:52 +03:00
|
|
|
fk.write ((char *)m_NTCP2Keys.get (), sizeof (NTCP2PrivateKeys));
|
2018-06-11 22:33:48 +03:00
|
|
|
}
|
|
|
|
|
2022-03-02 05:23:08 +03:00
|
|
|
void RouterContext::NewSSU2Keys ()
|
|
|
|
{
|
|
|
|
m_SSU2StaticKeys.reset (new i2p::crypto::X25519Keys ());
|
|
|
|
m_SSU2StaticKeys->GenerateKeys ();
|
|
|
|
m_SSU2Keys.reset (new SSU2PrivateKeys ());
|
|
|
|
m_SSU2StaticKeys->GetPrivateKey (m_SSU2Keys->staticPrivateKey);
|
|
|
|
memcpy (m_SSU2Keys->staticPublicKey, m_SSU2StaticKeys->GetPublicKey (), 32);
|
|
|
|
RAND_bytes (m_SSU2Keys->intro, 32);
|
|
|
|
// save
|
|
|
|
std::ofstream fk (i2p::fs::DataDirPath (SSU2_KEYS), std::ofstream::binary | std::ofstream::out);
|
|
|
|
fk.write ((char *)m_SSU2Keys.get (), sizeof (SSU2PrivateKeys));
|
|
|
|
}
|
2022-05-20 19:56:05 +03:00
|
|
|
|
2017-10-12 13:52:36 +03:00
|
|
|
void RouterContext::SetStatus (RouterStatus status)
|
|
|
|
{
|
2015-11-03 17:15:49 +03:00
|
|
|
if (status != m_Status)
|
2017-10-12 13:52:36 +03:00
|
|
|
{
|
2015-11-03 17:15:49 +03:00
|
|
|
m_Status = status;
|
2016-09-20 04:37:04 +03:00
|
|
|
m_Error = eRouterErrorNone;
|
2015-11-03 17:15:49 +03:00
|
|
|
switch (m_Status)
|
2017-10-12 13:52:36 +03:00
|
|
|
{
|
2015-11-03 17:15:49 +03:00
|
|
|
case eRouterStatusOK:
|
2021-03-27 22:16:56 +03:00
|
|
|
SetReachable (true, false); // ipv4
|
2015-11-03 17:15:49 +03:00
|
|
|
break;
|
|
|
|
case eRouterStatusFirewalled:
|
2021-03-27 22:16:56 +03:00
|
|
|
SetUnreachable (true, false); // ipv4
|
2017-10-12 13:52:36 +03:00
|
|
|
break;
|
2015-11-03 17:15:49 +03:00
|
|
|
default:
|
|
|
|
;
|
|
|
|
}
|
2017-10-12 13:52:36 +03:00
|
|
|
}
|
2015-11-03 17:15:49 +03:00
|
|
|
}
|
2022-11-23 06:03:19 +03:00
|
|
|
|
2021-03-23 22:36:57 +03:00
|
|
|
void RouterContext::SetStatusV6 (RouterStatus status)
|
|
|
|
{
|
|
|
|
if (status != m_StatusV6)
|
2021-05-15 16:28:16 +03:00
|
|
|
{
|
2021-03-23 22:36:57 +03:00
|
|
|
m_StatusV6 = status;
|
2022-08-03 03:02:55 +03:00
|
|
|
m_ErrorV6 = eRouterErrorNone;
|
2021-03-27 22:16:56 +03:00
|
|
|
switch (m_StatusV6)
|
|
|
|
{
|
|
|
|
case eRouterStatusOK:
|
|
|
|
SetReachable (false, true); // ipv6
|
|
|
|
break;
|
|
|
|
case eRouterStatusFirewalled:
|
|
|
|
SetUnreachable (false, true); // ipv6
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
;
|
|
|
|
}
|
2021-05-15 16:28:16 +03:00
|
|
|
}
|
|
|
|
}
|
2022-11-23 06:03:19 +03:00
|
|
|
|
2014-09-11 17:32:34 +04:00
|
|
|
void RouterContext::UpdatePort (int port)
|
2013-12-10 17:10:49 +04:00
|
|
|
{
|
2014-09-11 17:32:34 +04:00
|
|
|
bool updated = false;
|
2016-08-08 00:52:18 +03:00
|
|
|
for (auto& address : m_RouterInfo.GetAddresses ())
|
2013-12-10 17:10:49 +04:00
|
|
|
{
|
2022-11-23 06:03:19 +03:00
|
|
|
if (address->port != port && address->transportStyle == i2p::data::RouterInfo::eTransportSSU2)
|
2017-10-12 13:52:36 +03:00
|
|
|
{
|
2016-03-21 20:02:51 +03:00
|
|
|
address->port = port;
|
2014-09-11 17:32:34 +04:00
|
|
|
updated = true;
|
2017-10-12 13:52:36 +03:00
|
|
|
}
|
|
|
|
}
|
2014-09-11 17:32:34 +04:00
|
|
|
if (updated)
|
|
|
|
UpdateRouterInfo ();
|
2014-08-17 09:09:15 +04:00
|
|
|
}
|
2014-02-09 06:06:40 +04:00
|
|
|
|
2021-03-16 20:08:10 +03:00
|
|
|
void RouterContext::PublishNTCP2Address (int port, bool publish, bool v4, bool v6, bool ygg)
|
2018-07-23 20:51:29 +03:00
|
|
|
{
|
2018-08-19 23:17:57 +03:00
|
|
|
if (!m_NTCP2Keys) return;
|
2018-07-23 20:51:29 +03:00
|
|
|
bool updated = false;
|
|
|
|
for (auto& address : m_RouterInfo.GetAddresses ())
|
|
|
|
{
|
2021-05-15 16:28:16 +03:00
|
|
|
if (address->IsNTCP2 () && (address->port != port || address->published != publish))
|
2018-07-23 20:51:29 +03:00
|
|
|
{
|
2021-03-31 02:27:40 +03:00
|
|
|
bool isAddr = v4 && address->IsV4 ();
|
|
|
|
if (!isAddr && (v6 || ygg))
|
2021-05-15 16:28:16 +03:00
|
|
|
{
|
2021-03-31 02:27:40 +03:00
|
|
|
if (i2p::util::net::IsYggdrasilAddress (address->host))
|
|
|
|
isAddr = ygg;
|
|
|
|
else
|
|
|
|
isAddr = v6 && address->IsV6 ();
|
2019-11-12 23:06:04 +03:00
|
|
|
}
|
2021-03-31 02:27:40 +03:00
|
|
|
if (isAddr)
|
2021-05-15 16:28:16 +03:00
|
|
|
{
|
2022-08-11 05:00:11 +03:00
|
|
|
if (!port && !address->port) port = SelectRandomPort ();
|
2021-03-31 02:27:40 +03:00
|
|
|
if (port) address->port = port;
|
2021-04-06 04:45:48 +03:00
|
|
|
address->published = publish;
|
2022-02-06 01:14:25 +03:00
|
|
|
memcpy (address->i, m_NTCP2Keys->iv, 16);
|
2021-03-31 02:27:40 +03:00
|
|
|
updated = true;
|
2021-05-15 16:28:16 +03:00
|
|
|
}
|
2018-07-23 20:51:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (updated)
|
2019-05-08 00:45:52 +03:00
|
|
|
UpdateRouterInfo ();
|
2018-07-23 20:51:29 +03:00
|
|
|
}
|
2022-05-20 19:56:05 +03:00
|
|
|
|
2018-08-03 21:49:28 +03:00
|
|
|
void RouterContext::UpdateNTCP2Address (bool enable)
|
|
|
|
{
|
|
|
|
auto& addresses = m_RouterInfo.GetAddresses ();
|
|
|
|
bool found = false, updated = false;
|
2022-08-06 23:25:46 +03:00
|
|
|
for (auto it = addresses.begin (); it != addresses.end ();)
|
2018-08-03 21:49:28 +03:00
|
|
|
{
|
|
|
|
if ((*it)->IsNTCP2 ())
|
|
|
|
{
|
|
|
|
found = true;
|
2022-08-06 23:25:46 +03:00
|
|
|
if (enable)
|
2019-05-08 00:45:52 +03:00
|
|
|
{
|
2022-08-06 23:25:46 +03:00
|
|
|
(*it)->s = m_NTCP2Keys->staticPublicKey;
|
|
|
|
memcpy ((*it)->i, m_NTCP2Keys->iv, 16);
|
2022-08-06 23:30:49 +03:00
|
|
|
it++;
|
2022-10-09 20:24:43 +03:00
|
|
|
}
|
2022-08-06 23:25:46 +03:00
|
|
|
else
|
|
|
|
it = addresses.erase (it);
|
2022-08-06 23:30:49 +03:00
|
|
|
updated = true;
|
2018-08-03 21:49:28 +03:00
|
|
|
}
|
2022-08-06 23:25:46 +03:00
|
|
|
else
|
|
|
|
it++;
|
2018-08-03 21:49:28 +03:00
|
|
|
}
|
|
|
|
if (enable && !found)
|
2019-05-08 00:45:52 +03:00
|
|
|
{
|
|
|
|
m_RouterInfo.AddNTCP2Address (m_NTCP2Keys->staticPublicKey, m_NTCP2Keys->iv);
|
2018-08-03 21:49:28 +03:00
|
|
|
updated = true;
|
|
|
|
}
|
|
|
|
if (updated)
|
|
|
|
UpdateRouterInfo ();
|
|
|
|
}
|
2019-05-08 00:45:52 +03:00
|
|
|
|
2022-03-29 20:56:56 +03:00
|
|
|
void RouterContext::PublishSSU2Address (int port, bool publish, bool v4, bool v6)
|
|
|
|
{
|
2022-08-12 03:16:08 +03:00
|
|
|
if (!m_SSU2Keys) return;
|
|
|
|
int newPort = 0;
|
|
|
|
if (!port)
|
|
|
|
{
|
|
|
|
for (const auto& address : m_RouterInfo.GetAddresses ())
|
|
|
|
if (address->port)
|
|
|
|
{
|
|
|
|
newPort = address->port;
|
|
|
|
break;
|
2022-10-09 20:24:43 +03:00
|
|
|
}
|
2022-08-12 03:16:08 +03:00
|
|
|
if (!newPort) newPort = SelectRandomPort ();
|
2022-10-09 20:24:43 +03:00
|
|
|
}
|
2022-03-29 20:56:56 +03:00
|
|
|
bool updated = false;
|
|
|
|
for (auto& address : m_RouterInfo.GetAddresses ())
|
|
|
|
{
|
2022-08-12 03:16:08 +03:00
|
|
|
if (address->IsSSU2 () && (!address->port || address->port != port || address->published != publish) &&
|
2022-03-29 20:56:56 +03:00
|
|
|
((v4 && address->IsV4 ()) || (v6 && address->IsV6 ())))
|
|
|
|
{
|
2022-08-12 03:16:08 +03:00
|
|
|
if (port) address->port = port;
|
|
|
|
else if (!address->port) address->port = newPort;
|
2022-03-29 20:56:56 +03:00
|
|
|
address->published = publish;
|
2022-04-26 22:20:57 +03:00
|
|
|
if (publish)
|
2022-06-10 17:33:27 +03:00
|
|
|
address->caps |= (i2p::data::RouterInfo::eSSUIntroducer | i2p::data::RouterInfo::eSSUTesting);
|
2022-04-26 22:20:57 +03:00
|
|
|
else
|
2022-06-10 17:33:27 +03:00
|
|
|
address->caps &= ~(i2p::data::RouterInfo::eSSUIntroducer | i2p::data::RouterInfo::eSSUTesting);
|
2022-03-29 20:56:56 +03:00
|
|
|
updated = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (updated)
|
|
|
|
UpdateRouterInfo ();
|
2022-05-20 19:56:05 +03:00
|
|
|
}
|
|
|
|
|
2022-03-13 05:40:12 +03:00
|
|
|
void RouterContext::UpdateSSU2Address (bool enable)
|
|
|
|
{
|
|
|
|
auto& addresses = m_RouterInfo.GetAddresses ();
|
|
|
|
bool found = false, updated = false;
|
2022-08-06 23:25:46 +03:00
|
|
|
for (auto it = addresses.begin (); it != addresses.end ();)
|
2022-03-13 05:40:12 +03:00
|
|
|
{
|
|
|
|
if ((*it)->IsSSU2 ())
|
|
|
|
{
|
|
|
|
found = true;
|
2022-08-06 23:25:46 +03:00
|
|
|
if (enable)
|
2022-03-13 05:40:12 +03:00
|
|
|
{
|
2022-08-06 23:25:46 +03:00
|
|
|
(*it)->s = m_SSU2Keys->staticPublicKey;
|
|
|
|
(*it)->i = m_SSU2Keys->intro;
|
2022-08-06 23:30:49 +03:00
|
|
|
it++;
|
2022-10-09 20:24:43 +03:00
|
|
|
}
|
|
|
|
else
|
2022-08-06 23:25:46 +03:00
|
|
|
it = addresses.erase (it);
|
|
|
|
updated = true;
|
2022-03-13 05:40:12 +03:00
|
|
|
}
|
2022-08-06 23:25:46 +03:00
|
|
|
else
|
|
|
|
it++;
|
2022-03-13 05:40:12 +03:00
|
|
|
}
|
|
|
|
if (enable && !found)
|
|
|
|
{
|
2022-03-13 18:04:37 +03:00
|
|
|
bool ipv4; i2p::config::GetOption("ipv4", ipv4);
|
|
|
|
bool ipv6; i2p::config::GetOption("ipv6", ipv6);
|
2022-08-10 22:50:30 +03:00
|
|
|
bool published; i2p::config::GetOption("ntcp2.published", published);
|
|
|
|
if (published)
|
|
|
|
{
|
|
|
|
if (ipv4) m_RouterInfo.AddSSU2Address (m_SSU2Keys->staticPublicKey, m_SSU2Keys->intro, i2p::data::RouterInfo::AddressCaps::eV4);
|
|
|
|
if (ipv6) m_RouterInfo.AddSSU2Address (m_SSU2Keys->staticPublicKey, m_SSU2Keys->intro, i2p::data::RouterInfo::AddressCaps::eV6);
|
|
|
|
}
|
|
|
|
else
|
2022-10-09 20:24:43 +03:00
|
|
|
{
|
2022-08-10 22:50:30 +03:00
|
|
|
uint8_t addressCaps = 0;
|
|
|
|
if (ipv4) addressCaps |= i2p::data::RouterInfo::AddressCaps::eV4;
|
|
|
|
if (ipv6) addressCaps |= i2p::data::RouterInfo::AddressCaps::eV6;
|
|
|
|
m_RouterInfo.AddSSU2Address (m_SSU2Keys->staticPublicKey, m_SSU2Keys->intro, addressCaps);
|
2022-10-09 20:24:43 +03:00
|
|
|
}
|
2022-03-13 05:40:12 +03:00
|
|
|
updated = true;
|
|
|
|
}
|
|
|
|
if (updated)
|
|
|
|
UpdateRouterInfo ();
|
|
|
|
}
|
2022-05-20 19:56:05 +03:00
|
|
|
|
2014-10-29 20:49:21 +03:00
|
|
|
void RouterContext::UpdateAddress (const boost::asio::ip::address& host)
|
2014-02-09 06:06:40 +04:00
|
|
|
{
|
2014-09-01 00:46:39 +04:00
|
|
|
bool updated = false;
|
2016-08-08 00:52:18 +03:00
|
|
|
for (auto& address : m_RouterInfo.GetAddresses ())
|
2014-09-01 00:46:39 +04:00
|
|
|
{
|
2021-05-15 16:28:16 +03:00
|
|
|
if (address->host != host && address->IsCompatible (host) &&
|
2022-05-20 19:56:05 +03:00
|
|
|
!i2p::util::net::IsYggdrasilAddress (address->host))
|
2017-10-12 13:52:36 +03:00
|
|
|
{
|
2022-07-24 22:24:01 +03:00
|
|
|
// update host
|
2016-03-21 20:02:51 +03:00
|
|
|
address->host = host;
|
2022-07-24 22:24:01 +03:00
|
|
|
updated = true;
|
|
|
|
}
|
2022-10-09 20:24:43 +03:00
|
|
|
if (host.is_v6 () && address->IsV6 () && address->ssu &&
|
2022-10-27 22:02:54 +03:00
|
|
|
(!address->ssu->mtu || updated) && m_StatusV6 != eRouterStatusProxy)
|
2022-07-24 22:24:01 +03:00
|
|
|
{
|
|
|
|
// update MTU
|
|
|
|
auto mtu = i2p::util::net::GetMTU (host);
|
|
|
|
if (mtu)
|
2019-05-29 18:36:58 +03:00
|
|
|
{
|
2022-07-24 22:24:01 +03:00
|
|
|
LogPrint (eLogDebug, "Router: Our v6 MTU=", mtu);
|
|
|
|
int maxMTU = i2p::util::net::GetMaxMTU (host.to_v6 ());
|
2022-10-09 20:24:43 +03:00
|
|
|
if (mtu > maxMTU)
|
|
|
|
{
|
2022-07-24 22:24:01 +03:00
|
|
|
mtu = maxMTU;
|
|
|
|
LogPrint(eLogWarning, "Router: MTU dropped to upper limit of ", maxMTU, " bytes");
|
2019-05-29 18:36:58 +03:00
|
|
|
}
|
2022-07-24 22:24:01 +03:00
|
|
|
address->ssu->mtu = mtu;
|
|
|
|
updated = true;
|
2019-05-29 18:36:58 +03:00
|
|
|
}
|
2017-10-12 13:52:36 +03:00
|
|
|
}
|
|
|
|
}
|
2014-09-01 00:46:39 +04:00
|
|
|
auto ts = i2p::util::GetSecondsSinceEpoch ();
|
|
|
|
if (updated || ts > m_LastUpdateTime + ROUTER_INFO_UPDATE_INTERVAL)
|
|
|
|
UpdateRouterInfo ();
|
2013-10-23 06:45:40 +04:00
|
|
|
}
|
|
|
|
|
2022-07-20 23:01:08 +03:00
|
|
|
bool RouterContext::AddSSU2Introducer (const i2p::data::RouterInfo::Introducer& introducer, bool v4)
|
|
|
|
{
|
|
|
|
bool ret = m_RouterInfo.AddSSU2Introducer (introducer, v4);
|
|
|
|
if (ret)
|
|
|
|
UpdateRouterInfo ();
|
|
|
|
return ret;
|
|
|
|
}
|
2022-07-27 13:24:07 +03:00
|
|
|
|
2022-07-20 23:01:08 +03:00
|
|
|
void RouterContext::RemoveSSU2Introducer (const i2p::data::IdentHash& h, bool v4)
|
|
|
|
{
|
|
|
|
if (m_RouterInfo.RemoveSSU2Introducer (h, v4))
|
|
|
|
UpdateRouterInfo ();
|
2022-07-27 13:24:07 +03:00
|
|
|
}
|
|
|
|
|
2022-08-06 04:23:23 +03:00
|
|
|
void RouterContext::ClearSSU2Introducers (bool v4)
|
|
|
|
{
|
|
|
|
bool updated = false;
|
|
|
|
auto& addresses = m_RouterInfo.GetAddresses ();
|
|
|
|
for (auto& addr : addresses)
|
|
|
|
if (addr->IsSSU2 () && ((v4 && addr->IsV4 ()) || (!v4 && addr->IsV6 ())) &&
|
|
|
|
addr->ssu && !addr->ssu->introducers.empty ())
|
|
|
|
{
|
|
|
|
addr->ssu->introducers.clear ();
|
|
|
|
updated = true;
|
|
|
|
}
|
|
|
|
if (updated)
|
|
|
|
UpdateRouterInfo ();
|
2022-10-09 20:24:43 +03:00
|
|
|
}
|
|
|
|
|
2015-01-28 23:12:15 +03:00
|
|
|
void RouterContext::SetFloodfill (bool floodfill)
|
|
|
|
{
|
|
|
|
m_IsFloodfill = floodfill;
|
|
|
|
if (floodfill)
|
2022-01-16 02:54:02 +03:00
|
|
|
m_RouterInfo.UpdateCaps (m_RouterInfo.GetCaps () | i2p::data::RouterInfo::eFloodfill);
|
2015-01-28 23:12:15 +03:00
|
|
|
else
|
2015-03-18 22:06:15 +03:00
|
|
|
{
|
2022-01-16 02:54:02 +03:00
|
|
|
m_RouterInfo.UpdateCaps (m_RouterInfo.GetCaps () & ~i2p::data::RouterInfo::eFloodfill);
|
2015-03-18 22:06:15 +03:00
|
|
|
// we don't publish number of routers and leaseset for non-floodfill
|
2016-02-21 04:20:19 +03:00
|
|
|
m_RouterInfo.DeleteProperty (i2p::data::ROUTER_INFO_PROPERTY_LEASESETS);
|
|
|
|
m_RouterInfo.DeleteProperty (i2p::data::ROUTER_INFO_PROPERTY_ROUTERS);
|
2015-03-18 22:06:15 +03:00
|
|
|
}
|
2015-01-28 23:12:15 +03:00
|
|
|
UpdateRouterInfo ();
|
|
|
|
}
|
|
|
|
|
2016-04-29 01:16:11 +03:00
|
|
|
std::string RouterContext::GetFamily () const
|
2016-04-27 01:48:23 +03:00
|
|
|
{
|
2016-04-29 01:16:11 +03:00
|
|
|
return m_RouterInfo.GetProperty (i2p::data::ROUTER_INFO_PROPERTY_FAMILY);
|
2016-04-27 01:31:33 +03:00
|
|
|
}
|
|
|
|
|
2016-02-21 04:20:19 +03: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 13:52:36 +03:00
|
|
|
}
|
2016-02-21 04:20:19 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
m_RouterInfo.DeleteProperty (i2p::data::ROUTER_INFO_PROPERTY_FAMILY);
|
|
|
|
m_RouterInfo.DeleteProperty (i2p::data::ROUTER_INFO_PROPERTY_FAMILY_SIG);
|
2017-10-12 13:52:36 +03:00
|
|
|
}
|
|
|
|
}
|
2015-03-19 18:14:21 +03:00
|
|
|
|
2018-01-06 06:48:51 +03:00
|
|
|
void RouterContext::SetBandwidth (char L)
|
2017-10-05 17:37:28 +03:00
|
|
|
{
|
|
|
|
uint32_t limit = 0;
|
2017-01-26 00:14:01 +03:00
|
|
|
enum { low, high, extra, unlim } type = high;
|
2016-03-31 03:00:00 +03:00
|
|
|
/* detect parameters */
|
2017-10-12 13:52:36 +03:00
|
|
|
switch (L)
|
2016-03-31 04:31:17 +03:00
|
|
|
{
|
2016-03-31 03:00:00 +03: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 17:37:28 +03:00
|
|
|
case i2p::data::CAPS_FLAG_EXTRA_BANDWIDTH2 : limit = 1000000; type = unlim; break; // 1Gbyte/s
|
2016-03-31 04:31:17 +03:00
|
|
|
default:
|
2021-05-15 16:28:16 +03:00
|
|
|
limit = 48; type = low;
|
2016-03-31 03:00:00 +03:00
|
|
|
}
|
|
|
|
/* update caps & flags in RI */
|
|
|
|
auto caps = m_RouterInfo.GetCaps ();
|
|
|
|
caps &= ~i2p::data::RouterInfo::eHighBandwidth;
|
|
|
|
caps &= ~i2p::data::RouterInfo::eExtraBandwidth;
|
2017-10-12 13:52:36 +03:00
|
|
|
switch (type)
|
2016-03-31 04:31:17 +03:00
|
|
|
{
|
2016-03-31 03:00:00 +03:00
|
|
|
case low : /* not set */; break;
|
2017-01-26 00:14:01 +03:00
|
|
|
case extra : caps |= i2p::data::RouterInfo::eExtraBandwidth; break; // 'P'
|
2020-03-01 13:25:50 +03:00
|
|
|
case unlim : caps |= i2p::data::RouterInfo::eExtraBandwidth;
|
|
|
|
#if (__cplusplus >= 201703L) // C++ 17 or higher
|
|
|
|
[[fallthrough]];
|
|
|
|
#endif
|
2021-05-15 16:28:16 +03:00
|
|
|
// no break here, extra + high means 'X'
|
|
|
|
case high : caps |= i2p::data::RouterInfo::eHighBandwidth; break;
|
2016-03-31 03:00:00 +03:00
|
|
|
}
|
2022-01-16 02:54:02 +03:00
|
|
|
m_RouterInfo.UpdateCaps (caps);
|
2016-03-31 03:00:00 +03:00
|
|
|
UpdateRouterInfo ();
|
|
|
|
m_BandwidthLimit = limit;
|
2015-03-19 18:14:21 +03:00
|
|
|
}
|
|
|
|
|
2017-10-12 13:52:36 +03:00
|
|
|
void RouterContext::SetBandwidth (int limit)
|
2016-03-31 04:31:17 +03:00
|
|
|
{
|
2016-03-31 03:00:00 +03: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 06:13:17 +03:00
|
|
|
m_BandwidthLimit = limit; // set precise limit
|
2016-01-03 06:17:04 +03:00
|
|
|
}
|
2016-03-31 03:00:00 +03:00
|
|
|
|
2017-05-04 21:58:12 +03:00
|
|
|
void RouterContext::SetShareRatio (int percents)
|
|
|
|
{
|
|
|
|
if (percents < 0) percents = 0;
|
|
|
|
if (percents > 100) percents = 100;
|
|
|
|
m_ShareRatio = percents;
|
|
|
|
}
|
|
|
|
|
2015-03-01 15:55:03 +03:00
|
|
|
bool RouterContext::IsUnreachable () const
|
|
|
|
{
|
|
|
|
return m_RouterInfo.GetCaps () & i2p::data::RouterInfo::eUnreachable;
|
2017-10-12 13:52:36 +03:00
|
|
|
}
|
|
|
|
|
2020-10-04 05:29:52 +03:00
|
|
|
void RouterContext::RemoveNTCPAddress (bool v4only)
|
2019-04-17 04:04:04 +03:00
|
|
|
{
|
2022-08-14 17:43:16 +03:00
|
|
|
bool updated = false;
|
2019-04-17 04:04:04 +03:00
|
|
|
auto& addresses = m_RouterInfo.GetAddresses ();
|
2020-10-04 05:29:52 +03:00
|
|
|
for (auto it = addresses.begin (); it != addresses.end ();)
|
2019-04-17 04:04:04 +03:00
|
|
|
{
|
2020-10-04 05:29:52 +03:00
|
|
|
if ((*it)->transportStyle == i2p::data::RouterInfo::eTransportNTCP && !(*it)->IsNTCP2 () &&
|
|
|
|
(!v4only || (*it)->host.is_v4 ()))
|
2019-04-17 04:04:04 +03:00
|
|
|
{
|
2020-10-04 05:29:52 +03:00
|
|
|
it = addresses.erase (it);
|
2022-08-14 17:43:16 +03:00
|
|
|
updated = true;
|
2020-10-04 05:29:52 +03:00
|
|
|
if (v4only) break; // otherwise might be more than one address
|
2019-04-17 04:04:04 +03:00
|
|
|
}
|
2020-10-04 05:29:52 +03:00
|
|
|
else
|
|
|
|
++it;
|
2019-05-08 00:45:52 +03:00
|
|
|
}
|
2022-08-14 17:43:16 +03:00
|
|
|
if (updated)
|
|
|
|
m_RouterInfo.UpdateSupportedTransports ();
|
2019-05-08 00:45:52 +03:00
|
|
|
}
|
|
|
|
|
2022-08-10 02:12:11 +03:00
|
|
|
void RouterContext::RemoveSSUAddress ()
|
|
|
|
{
|
2022-08-14 17:43:16 +03:00
|
|
|
bool updated = false;
|
2022-08-10 02:12:11 +03:00
|
|
|
auto& addresses = m_RouterInfo.GetAddresses ();
|
|
|
|
for (auto it = addresses.begin (); it != addresses.end ();)
|
|
|
|
{
|
|
|
|
if ((*it)->transportStyle == i2p::data::RouterInfo::eTransportSSU)
|
2022-10-09 20:24:43 +03:00
|
|
|
{
|
2022-08-10 02:12:11 +03:00
|
|
|
it = addresses.erase (it);
|
2022-08-14 17:43:16 +03:00
|
|
|
updated = true;
|
2022-10-09 20:24:43 +03:00
|
|
|
}
|
2022-08-10 02:12:11 +03:00
|
|
|
else
|
|
|
|
++it;
|
|
|
|
}
|
2022-08-14 17:43:16 +03:00
|
|
|
if (updated)
|
|
|
|
m_RouterInfo.UpdateSupportedTransports ();
|
2022-08-10 02:12:11 +03:00
|
|
|
}
|
2022-10-09 20:24:43 +03:00
|
|
|
|
2021-03-27 22:16:56 +03:00
|
|
|
void RouterContext::SetUnreachable (bool v4, bool v6)
|
2014-09-09 00:43:20 +04:00
|
|
|
{
|
2021-04-21 03:02:30 +03:00
|
|
|
if (v4 || (v6 && !SupportsV4 ()))
|
2021-05-15 16:28:16 +03:00
|
|
|
{
|
2021-03-29 21:44:50 +03:00
|
|
|
// set caps
|
|
|
|
uint8_t caps = m_RouterInfo.GetCaps ();
|
|
|
|
caps &= ~i2p::data::RouterInfo::eReachable;
|
|
|
|
caps |= i2p::data::RouterInfo::eUnreachable;
|
2021-06-19 21:25:50 +03:00
|
|
|
if (v6 || !SupportsV6 ())
|
|
|
|
caps &= ~i2p::data::RouterInfo::eFloodfill; // can't be floodfill
|
2022-01-16 02:54:02 +03:00
|
|
|
m_RouterInfo.UpdateCaps (caps);
|
2021-05-15 16:28:16 +03:00
|
|
|
}
|
2019-05-30 23:11:35 +03:00
|
|
|
uint16_t port = 0;
|
2014-09-09 05:53:55 +04:00
|
|
|
// delete previous introducers
|
2019-04-17 04:04:04 +03:00
|
|
|
auto& addresses = m_RouterInfo.GetAddresses ();
|
2017-10-12 13:52:36 +03:00
|
|
|
for (auto& addr : addresses)
|
2022-11-23 06:03:19 +03:00
|
|
|
if (addr->ssu && ((v4 && addr->IsV4 ()) || (v6 && addr->IsV6 ())))
|
2019-05-30 23:11:35 +03:00
|
|
|
{
|
2021-04-06 04:45:48 +03:00
|
|
|
addr->published = false;
|
2021-02-27 05:02:51 +03:00
|
|
|
addr->caps &= ~i2p::data::RouterInfo::eSSUIntroducer; // can't be introducer
|
2017-01-03 00:36:59 +03:00
|
|
|
addr->ssu->introducers.clear ();
|
2019-05-30 23:11:35 +03:00
|
|
|
port = addr->port;
|
|
|
|
}
|
2021-06-14 19:36:54 +03:00
|
|
|
// unpublish NTCP2 addreeses
|
2020-10-04 01:46:12 +03:00
|
|
|
bool ntcp2; i2p::config::GetOption("ntcp2.enabled", ntcp2);
|
|
|
|
if (ntcp2)
|
2021-03-27 22:16:56 +03:00
|
|
|
PublishNTCP2Address (port, false, v4, v6, false);
|
2014-09-09 00:43:20 +04:00
|
|
|
// update
|
2021-06-17 01:14:33 +03:00
|
|
|
m_RouterInfo.UpdateSupportedTransports ();
|
2014-09-09 00:43:20 +04:00
|
|
|
UpdateRouterInfo ();
|
|
|
|
}
|
2014-10-27 04:32:06 +03:00
|
|
|
|
2021-03-27 22:16:56 +03:00
|
|
|
void RouterContext::SetReachable (bool v4, bool v6)
|
2015-03-01 15:55:03 +03:00
|
|
|
{
|
2021-04-21 03:02:30 +03:00
|
|
|
if (v4 || (v6 && !SupportsV4 ()))
|
2021-05-15 16:28:16 +03:00
|
|
|
{
|
2021-03-29 21:44:50 +03:00
|
|
|
// 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;
|
2022-01-16 02:54:02 +03:00
|
|
|
m_RouterInfo.UpdateCaps (caps);
|
2021-05-15 16:28:16 +03:00
|
|
|
}
|
2019-05-30 23:11:35 +03:00
|
|
|
uint16_t port = 0;
|
2015-03-01 15:55:03 +03:00
|
|
|
// delete previous introducers
|
2022-11-23 06:03:19 +03:00
|
|
|
bool isSSU2Published; i2p::config::GetOption ("ssu2.published", isSSU2Published);
|
2019-04-17 04:04:04 +03:00
|
|
|
auto& addresses = m_RouterInfo.GetAddresses ();
|
2016-08-08 00:52:18 +03:00
|
|
|
for (auto& addr : addresses)
|
2022-11-23 06:03:19 +03:00
|
|
|
if (addr->ssu && isSSU2Published && ((v4 && addr->IsV4 ()) || (v6 && addr->IsV6 ())))
|
2019-05-30 23:11:35 +03:00
|
|
|
{
|
2021-04-06 04:45:48 +03:00
|
|
|
addr->published = true;
|
2021-02-27 05:02:51 +03:00
|
|
|
addr->caps |= i2p::data::RouterInfo::eSSUIntroducer;
|
2017-01-03 00:36:59 +03:00
|
|
|
addr->ssu->introducers.clear ();
|
2022-11-23 06:03:19 +03:00
|
|
|
if (addr->port) port = addr->port;
|
2019-05-30 23:11:35 +03:00
|
|
|
}
|
2021-03-27 22:16:56 +03:00
|
|
|
// publish NTCP2
|
2020-10-04 01:46:12 +03:00
|
|
|
bool ntcp2; i2p::config::GetOption("ntcp2.enabled", ntcp2);
|
|
|
|
if (ntcp2)
|
2019-05-30 23:11:35 +03:00
|
|
|
{
|
2020-10-04 01:46:12 +03:00
|
|
|
bool published; i2p::config::GetOption ("ntcp2.published", published);
|
|
|
|
if (published)
|
2019-05-30 23:11:35 +03:00
|
|
|
{
|
2020-10-04 01:46:12 +03:00
|
|
|
uint16_t ntcp2Port; i2p::config::GetOption ("ntcp2.port", ntcp2Port);
|
|
|
|
if (!ntcp2Port) ntcp2Port = port;
|
2021-05-15 16:28:16 +03:00
|
|
|
PublishNTCP2Address (ntcp2Port, true, v4, v6, false);
|
2019-05-30 23:11:35 +03:00
|
|
|
}
|
|
|
|
}
|
2015-03-01 15:55:03 +03:00
|
|
|
// update
|
2021-06-17 01:14:33 +03:00
|
|
|
m_RouterInfo.UpdateSupportedTransports ();
|
2015-03-01 15:55:03 +03:00
|
|
|
UpdateRouterInfo ();
|
2017-10-12 13:52:36 +03:00
|
|
|
}
|
|
|
|
|
2014-10-27 04:32:06 +03:00
|
|
|
void RouterContext::SetSupportsV6 (bool supportsV6)
|
|
|
|
{
|
|
|
|
if (supportsV6)
|
2019-11-12 23:06:04 +03:00
|
|
|
{
|
2019-05-29 18:36:58 +03:00
|
|
|
// insert v6 addresses if necessary
|
2022-11-23 06:03:19 +03:00
|
|
|
bool foundNTCP2 = false, foundSSU2 = false;
|
2019-05-29 18:36:58 +03:00
|
|
|
uint16_t port = 0;
|
|
|
|
auto& addresses = m_RouterInfo.GetAddresses ();
|
|
|
|
for (auto& addr: addresses)
|
|
|
|
{
|
2021-03-13 00:13:01 +03:00
|
|
|
if (addr->IsV6 () && !i2p::util::net::IsYggdrasilAddress (addr->host))
|
2019-05-29 18:36:58 +03:00
|
|
|
{
|
2022-03-29 20:56:56 +03:00
|
|
|
switch (addr->transportStyle)
|
|
|
|
{
|
|
|
|
case i2p::data::RouterInfo::eTransportNTCP:
|
|
|
|
foundNTCP2 = true;
|
2022-05-20 19:56:05 +03:00
|
|
|
break;
|
2022-03-29 20:56:56 +03:00
|
|
|
case i2p::data::RouterInfo::eTransportSSU2:
|
|
|
|
foundSSU2 = true;
|
2022-05-20 19:56:05 +03:00
|
|
|
break;
|
2022-03-29 20:56:56 +03:00
|
|
|
default: ;
|
2022-05-20 19:56:05 +03:00
|
|
|
}
|
2019-11-12 23:06:04 +03:00
|
|
|
}
|
2019-05-29 18:36:58 +03:00
|
|
|
port = addr->port;
|
|
|
|
}
|
2022-10-09 20:24:43 +03:00
|
|
|
if (!port)
|
|
|
|
{
|
2022-08-11 05:00:11 +03:00
|
|
|
i2p::config::GetOption("port", port);
|
|
|
|
if (!port) port = SelectRandomPort ();
|
2022-10-09 20:24:43 +03:00
|
|
|
}
|
2019-05-29 18:36:58 +03:00
|
|
|
// NTCP2
|
|
|
|
if (!foundNTCP2)
|
|
|
|
{
|
|
|
|
bool ntcp2; i2p::config::GetOption("ntcp2.enabled", ntcp2);
|
|
|
|
bool ntcp2Published; i2p::config::GetOption("ntcp2.published", ntcp2Published);
|
2021-11-27 23:30:35 +03:00
|
|
|
if (ntcp2)
|
2019-05-29 18:36:58 +03:00
|
|
|
{
|
2021-06-19 01:19:05 +03:00
|
|
|
if (ntcp2Published)
|
2021-11-27 23:30:35 +03:00
|
|
|
{
|
2021-06-19 01:19:05 +03:00
|
|
|
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);
|
2021-11-27 23:30:35 +03:00
|
|
|
}
|
2019-05-29 18:36:58 +03:00
|
|
|
else
|
2021-06-19 01:19:05 +03:00
|
|
|
m_RouterInfo.AddNTCP2Address (m_NTCP2Keys->staticPublicKey, m_NTCP2Keys->iv, boost::asio::ip::address(), 0, i2p::data::RouterInfo::eV6);
|
2019-05-29 18:36:58 +03:00
|
|
|
}
|
|
|
|
}
|
2022-03-29 20:56:56 +03:00
|
|
|
// SSU2
|
|
|
|
if (!foundSSU2)
|
|
|
|
{
|
|
|
|
bool ssu2; i2p::config::GetOption("ssu2.enabled", ssu2);
|
2022-04-05 03:37:29 +03:00
|
|
|
if (ssu2)
|
2022-05-20 19:56:05 +03:00
|
|
|
{
|
2022-04-05 03:37:29 +03:00
|
|
|
bool ssu2Published; i2p::config::GetOption("ssu2.published", ssu2Published);
|
|
|
|
if (ssu2Published)
|
|
|
|
{
|
|
|
|
uint16_t ssu2Port; i2p::config::GetOption ("ssu2.port", ssu2Port);
|
2022-11-23 06:03:19 +03:00
|
|
|
if (!ssu2Port) ssu2Port = port;
|
2022-04-05 03:37:29 +03:00
|
|
|
m_RouterInfo.AddSSU2Address (m_SSU2Keys->staticPublicKey, m_SSU2Keys->intro, boost::asio::ip::address::from_string ("::1"), ssu2Port);
|
2022-05-20 19:56:05 +03:00
|
|
|
}
|
2022-04-05 03:37:29 +03:00
|
|
|
else
|
|
|
|
m_RouterInfo.AddSSU2Address (m_SSU2Keys->staticPublicKey, m_SSU2Keys->intro, i2p::data::RouterInfo::eV6);
|
2022-05-20 19:56:05 +03:00
|
|
|
}
|
|
|
|
}
|
2021-03-08 23:57:05 +03:00
|
|
|
m_RouterInfo.EnableV6 ();
|
2019-05-29 18:36:58 +03:00
|
|
|
}
|
2014-10-27 04:32:06 +03:00
|
|
|
else
|
|
|
|
m_RouterInfo.DisableV6 ();
|
2014-10-30 01:46:35 +03:00
|
|
|
UpdateRouterInfo ();
|
2016-03-25 01:44:41 +03:00
|
|
|
}
|
2017-10-12 13:52:36 +03:00
|
|
|
|
2016-03-25 01:44:41 +03:00
|
|
|
void RouterContext::SetSupportsV4 (bool supportsV4)
|
|
|
|
{
|
2021-03-13 00:13:01 +03:00
|
|
|
// check if updates
|
|
|
|
if (supportsV4 && SupportsV4 ()) return;
|
|
|
|
if (!supportsV4 && !SupportsV4 ()) return;
|
|
|
|
// update
|
2016-03-25 01:44:41 +03:00
|
|
|
if (supportsV4)
|
2021-03-13 00:13:01 +03:00
|
|
|
{
|
2022-11-23 06:03:19 +03:00
|
|
|
bool foundNTCP2 = false, foundSSU2 = false;
|
2021-05-15 16:28:16 +03:00
|
|
|
std::string host = "127.0.0.1";
|
2021-03-13 00:13:01 +03:00
|
|
|
uint16_t port = 0;
|
|
|
|
auto& addresses = m_RouterInfo.GetAddresses ();
|
|
|
|
for (auto& addr: addresses)
|
|
|
|
{
|
|
|
|
if (addr->IsV4 ())
|
|
|
|
{
|
2022-03-29 21:56:57 +03:00
|
|
|
switch (addr->transportStyle)
|
|
|
|
{
|
|
|
|
case i2p::data::RouterInfo::eTransportNTCP:
|
|
|
|
foundNTCP2 = true;
|
2022-05-20 19:56:05 +03:00
|
|
|
break;
|
2022-03-29 21:56:57 +03:00
|
|
|
case i2p::data::RouterInfo::eTransportSSU2:
|
|
|
|
foundSSU2 = true;
|
2022-05-20 19:56:05 +03:00
|
|
|
break;
|
2022-03-29 21:56:57 +03:00
|
|
|
default: ;
|
2022-05-20 19:56:05 +03:00
|
|
|
}
|
2021-03-13 00:13:01 +03:00
|
|
|
}
|
|
|
|
if (addr->port) port = addr->port;
|
|
|
|
}
|
2022-10-09 20:24:43 +03:00
|
|
|
if (!port)
|
|
|
|
{
|
2022-08-11 05:00:11 +03:00
|
|
|
i2p::config::GetOption("port", port);
|
|
|
|
if (!port) port = SelectRandomPort ();
|
2022-10-09 20:24:43 +03:00
|
|
|
}
|
2021-03-13 00:13:01 +03:00
|
|
|
// NTCP2
|
|
|
|
if (!foundNTCP2)
|
|
|
|
{
|
|
|
|
bool ntcp2; i2p::config::GetOption("ntcp2.enabled", ntcp2);
|
|
|
|
if (ntcp2)
|
|
|
|
{
|
|
|
|
bool ntcp2Published; i2p::config::GetOption("ntcp2.published", ntcp2Published);
|
|
|
|
if (ntcp2Published)
|
2021-05-15 16:28:16 +03:00
|
|
|
{
|
2021-03-13 00:13:01 +03:00
|
|
|
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);
|
2021-05-15 16:28:16 +03:00
|
|
|
}
|
2021-03-13 00:13:01 +03:00
|
|
|
else
|
2021-06-19 01:19:05 +03:00
|
|
|
m_RouterInfo.AddNTCP2Address (m_NTCP2Keys->staticPublicKey, m_NTCP2Keys->iv, boost::asio::ip::address(), 0, i2p::data::RouterInfo::eV4);
|
2021-05-15 16:28:16 +03:00
|
|
|
}
|
|
|
|
}
|
2022-03-29 21:56:57 +03:00
|
|
|
// SSU2
|
|
|
|
if (!foundSSU2)
|
|
|
|
{
|
|
|
|
bool ssu2; i2p::config::GetOption("ssu2.enabled", ssu2);
|
2022-04-05 03:37:29 +03:00
|
|
|
if (ssu2)
|
2022-03-29 21:56:57 +03:00
|
|
|
{
|
2022-04-05 03:37:29 +03:00
|
|
|
bool ssu2Published; i2p::config::GetOption("ssu2.published", ssu2Published);
|
|
|
|
if (ssu2Published)
|
|
|
|
{
|
|
|
|
uint16_t ssu2Port; i2p::config::GetOption ("ssu2.port", ssu2Port);
|
2022-11-23 06:03:19 +03:00
|
|
|
if (!ssu2Port) ssu2Port = port;
|
2022-04-05 03:37:29 +03:00
|
|
|
m_RouterInfo.AddSSU2Address (m_SSU2Keys->staticPublicKey, m_SSU2Keys->intro, boost::asio::ip::address::from_string ("127.0.0.1"), ssu2Port);
|
2022-05-20 19:56:05 +03:00
|
|
|
}
|
2022-04-05 03:37:29 +03:00
|
|
|
else
|
2022-08-10 22:50:30 +03:00
|
|
|
m_RouterInfo.AddSSU2Address (m_SSU2Keys->staticPublicKey, m_SSU2Keys->intro, i2p::data::RouterInfo::eV4);
|
2022-05-20 19:56:05 +03:00
|
|
|
}
|
|
|
|
}
|
2016-03-25 01:44:41 +03:00
|
|
|
m_RouterInfo.EnableV4 ();
|
2021-05-15 16:28:16 +03:00
|
|
|
}
|
2016-03-25 01:44:41 +03:00
|
|
|
else
|
|
|
|
m_RouterInfo.DisableV4 ();
|
|
|
|
UpdateRouterInfo ();
|
|
|
|
}
|
2017-10-12 13:52:36 +03:00
|
|
|
|
2021-02-01 02:30:53 +03:00
|
|
|
void RouterContext::SetSupportsMesh (bool supportsmesh, const boost::asio::ip::address_v6& host)
|
2021-05-15 16:28:16 +03:00
|
|
|
{
|
2021-02-01 01:25:07 +03:00
|
|
|
if (supportsmesh)
|
2021-05-15 16:28:16 +03:00
|
|
|
{
|
2021-02-01 01:25:07 +03:00
|
|
|
m_RouterInfo.EnableMesh ();
|
2021-02-01 02:30:53 +03:00
|
|
|
uint16_t port = 0;
|
|
|
|
i2p::config::GetOption ("ntcp2.port", port);
|
|
|
|
if (!port) i2p::config::GetOption("port", port);
|
2021-02-01 03:09:38 +03: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-02-01 02:30:53 +03:00
|
|
|
{
|
2021-02-01 03:09:38 +03:00
|
|
|
foundMesh = true;
|
|
|
|
break;
|
2021-05-15 16:28:16 +03:00
|
|
|
}
|
2021-02-01 03:09:38 +03:00
|
|
|
}
|
|
|
|
if (!foundMesh)
|
|
|
|
m_RouterInfo.AddNTCP2Address (m_NTCP2Keys->staticPublicKey, m_NTCP2Keys->iv, host, port);
|
2021-05-15 16:28:16 +03:00
|
|
|
}
|
2021-02-01 01:25:07 +03:00
|
|
|
else
|
|
|
|
m_RouterInfo.DisableMesh ();
|
|
|
|
UpdateRouterInfo ();
|
|
|
|
}
|
2021-05-15 16:28:16 +03:00
|
|
|
|
2022-07-24 22:24:01 +03:00
|
|
|
void RouterContext::SetMTU (int mtu, bool v4)
|
|
|
|
{
|
|
|
|
if (mtu < 1280 || mtu > 1500) return;
|
|
|
|
auto& addresses = m_RouterInfo.GetAddresses ();
|
|
|
|
for (auto& addr: addresses)
|
|
|
|
{
|
|
|
|
if (addr->ssu && ((v4 && addr->IsV4 ()) || (!v4 && addr->IsV6 ())))
|
2022-11-23 06:03:19 +03:00
|
|
|
{
|
|
|
|
addr->ssu->mtu = mtu;
|
|
|
|
LogPrint (eLogDebug, "Router: MTU for ", v4 ? "ipv4" : "ipv6", " address ", addr->host.to_string(), " is set to ", mtu);
|
2022-07-27 13:24:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-27 22:01:43 +03:00
|
|
|
void RouterContext::UpdateNTCP2V6Address (const boost::asio::ip::address& host)
|
|
|
|
{
|
2021-02-01 20:47:41 +03:00
|
|
|
bool isYgg = i2p::util::net::IsYggdrasilAddress (host);
|
2019-05-29 18:36:58 +03:00
|
|
|
bool updated = false;
|
2018-08-27 22:01:43 +03:00
|
|
|
auto& addresses = m_RouterInfo.GetAddresses ();
|
|
|
|
for (auto& addr: addresses)
|
|
|
|
{
|
|
|
|
if (addr->IsPublishedNTCP2 ())
|
|
|
|
{
|
2021-02-01 20:47:41 +03:00
|
|
|
bool isYgg1 = i2p::util::net::IsYggdrasilAddress (addr->host);
|
2021-06-14 19:36:54 +03:00
|
|
|
if (addr->IsV6 () && ((isYgg && isYgg1) || (!isYgg && !isYgg1)))
|
2018-08-27 22:01:43 +03:00
|
|
|
{
|
|
|
|
if (addr->host != host)
|
|
|
|
{
|
|
|
|
addr->host = host;
|
|
|
|
updated = true;
|
|
|
|
}
|
2018-08-28 01:56:57 +03:00
|
|
|
break;
|
2018-08-27 22:01:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-27 23:01:47 +03:00
|
|
|
if (updated)
|
2019-05-08 00:45:52 +03:00
|
|
|
UpdateRouterInfo ();
|
2018-08-27 22:01:43 +03:00
|
|
|
}
|
|
|
|
|
2015-03-18 22:36:07 +03:00
|
|
|
void RouterContext::UpdateStats ()
|
|
|
|
{
|
|
|
|
if (m_IsFloodfill)
|
|
|
|
{
|
|
|
|
// update routers and leasesets
|
2016-11-04 03:00:00 +03:00
|
|
|
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 13:52:36 +03:00
|
|
|
UpdateRouterInfo ();
|
2015-03-18 22:36:07 +03:00
|
|
|
}
|
|
|
|
}
|
2017-10-12 13:52:36 +03:00
|
|
|
|
2018-09-21 17:13:18 +03:00
|
|
|
void RouterContext::UpdateTimestamp (uint64_t ts)
|
|
|
|
{
|
|
|
|
if (ts > m_LastUpdateTime + ROUTER_INFO_UPDATE_INTERVAL)
|
|
|
|
UpdateRouterInfo ();
|
|
|
|
}
|
|
|
|
|
2013-10-23 06:45:40 +04:00
|
|
|
bool RouterContext::Load ()
|
|
|
|
{
|
2015-11-03 21:05:37 +03:00
|
|
|
{
|
2020-11-21 04:31:50 +03: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 21:05:37 +03:00
|
|
|
}
|
2020-11-21 04:31:50 +03:00
|
|
|
std::shared_ptr<const i2p::data::IdentityEx> oldIdentity;
|
2021-05-24 00:27:14 +03:00
|
|
|
if (m_Keys.GetPublic ()->GetSigningKeyType () == i2p::data::SIGNING_KEY_TYPE_DSA_SHA1 ||
|
2022-05-20 19:56:05 +03:00
|
|
|
m_Keys.GetPublic ()->GetCryptoKeyType () == i2p::data::CRYPTO_KEY_TYPE_ELGAMAL)
|
2015-11-03 21:05:37 +03:00
|
|
|
{
|
2020-11-21 04:31:50 +03:00
|
|
|
// update keys
|
|
|
|
LogPrint (eLogInfo, "Router: router keys are obsolete. Creating new");
|
|
|
|
oldIdentity = m_Keys.GetPublic ();
|
2020-12-16 00:06:32 +03: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 04:31:50 +03:00
|
|
|
SaveKeys ();
|
2021-05-15 16:28:16 +03:00
|
|
|
}
|
2018-08-04 04:16:35 +03:00
|
|
|
// read NTCP2 keys if available
|
|
|
|
std::ifstream n2k (i2p::fs::DataDirPath (NTCP2_KEYS), std::ifstream::in | std::ifstream::binary);
|
2019-05-08 00:45:52 +03:00
|
|
|
if (n2k)
|
2018-08-04 04:16:35 +03:00
|
|
|
{
|
|
|
|
n2k.seekg (0, std::ios::end);
|
2020-11-21 04:31:50 +03:00
|
|
|
size_t len = n2k.tellg();
|
2018-08-04 04:16:35 +03:00
|
|
|
n2k.seekg (0, std::ios::beg);
|
|
|
|
if (len == sizeof (NTCP2PrivateKeys))
|
|
|
|
{
|
|
|
|
m_NTCP2Keys.reset (new NTCP2PrivateKeys ());
|
2019-05-08 00:45:52 +03:00
|
|
|
n2k.read ((char *)m_NTCP2Keys.get (), sizeof (NTCP2PrivateKeys));
|
|
|
|
}
|
2018-08-04 04:16:35 +03:00
|
|
|
n2k.close ();
|
|
|
|
}
|
|
|
|
// read RouterInfo
|
2020-11-21 04:31:50 +03:00
|
|
|
m_RouterInfo.SetRouterIdentity (oldIdentity ? oldIdentity : GetIdentity ());
|
2017-10-12 13:52:36 +03:00
|
|
|
i2p::data::RouterInfo routerInfo(i2p::fs::DataDirPath (ROUTER_INFO));
|
2016-04-05 17:22:32 +03:00
|
|
|
if (!routerInfo.IsUnreachable ()) // router.info looks good
|
|
|
|
{
|
|
|
|
m_RouterInfo.Update (routerInfo.GetBuffer (), routerInfo.GetBufferLen ());
|
2020-11-21 04:31:50 +03:00
|
|
|
if (oldIdentity)
|
|
|
|
m_RouterInfo.SetRouterIdentity (GetIdentity ()); // from new keys
|
2016-04-05 17:22:32 +03:00
|
|
|
m_RouterInfo.SetProperty ("router.version", I2P_VERSION);
|
2021-03-05 17:29:28 +03:00
|
|
|
m_RouterInfo.DeleteProperty ("coreVersion"); // TODO: remove later
|
2016-04-05 17:22:32 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, ROUTER_INFO, " is malformed. Creating new");
|
|
|
|
NewRouterInfo ();
|
2017-10-12 13:52:36 +03:00
|
|
|
}
|
2015-03-01 15:55:03 +03:00
|
|
|
|
|
|
|
if (IsUnreachable ())
|
2021-03-27 22:16:56 +03:00
|
|
|
SetReachable (true, true); // we assume reachable until we discover firewall through peer tests
|
2017-10-12 13:52:36 +03:00
|
|
|
|
2018-06-11 22:33:48 +03:00
|
|
|
// read NTCP2
|
2021-05-15 16:28:16 +03:00
|
|
|
bool ntcp2; i2p::config::GetOption("ntcp2.enabled", ntcp2);
|
|
|
|
bool ygg; i2p::config::GetOption("meshnets.yggdrasil", ygg);
|
2021-02-07 03:07:39 +03:00
|
|
|
if (ntcp2 || ygg)
|
2018-06-11 22:33:48 +03:00
|
|
|
{
|
2018-08-04 04:16:35 +03:00
|
|
|
if (!m_NTCP2Keys) NewNTCP2Keys ();
|
|
|
|
UpdateNTCP2Address (true); // enable NTCP2
|
2018-06-11 22:33:48 +03:00
|
|
|
}
|
2018-08-03 21:49:28 +03:00
|
|
|
else
|
2022-05-20 19:56:05 +03:00
|
|
|
UpdateNTCP2Address (false); // disable NTCP2
|
2018-06-11 22:33:48 +03:00
|
|
|
|
2022-03-13 05:40:12 +03:00
|
|
|
// read SSU2
|
|
|
|
bool ssu2; i2p::config::GetOption("ssu2.enabled", ssu2);
|
|
|
|
if (ssu2)
|
|
|
|
{
|
|
|
|
// read SSU2 keys if available
|
|
|
|
std::ifstream s2k (i2p::fs::DataDirPath (SSU2_KEYS), std::ifstream::in | std::ifstream::binary);
|
|
|
|
if (s2k)
|
|
|
|
{
|
|
|
|
s2k.seekg (0, std::ios::end);
|
|
|
|
size_t len = s2k.tellg();
|
|
|
|
s2k.seekg (0, std::ios::beg);
|
|
|
|
if (len == sizeof (SSU2PrivateKeys))
|
|
|
|
{
|
|
|
|
m_SSU2Keys.reset (new SSU2PrivateKeys ());
|
|
|
|
s2k.read ((char *)m_SSU2Keys.get (), sizeof (SSU2PrivateKeys));
|
|
|
|
}
|
|
|
|
s2k.close ();
|
|
|
|
}
|
|
|
|
if (!m_SSU2Keys) NewSSU2Keys ();
|
2022-03-13 05:51:17 +03:00
|
|
|
UpdateSSU2Address (true); // enable SSU2
|
2022-03-13 05:40:12 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
UpdateSSU2Address (false); // disable SSU2
|
2022-05-20 19:56:05 +03:00
|
|
|
|
2013-10-23 06:45:40 +04:00
|
|
|
return true;
|
|
|
|
}
|
2014-08-17 09:09:15 +04:00
|
|
|
|
2014-09-01 00:46:39 +04:00
|
|
|
void RouterContext::SaveKeys ()
|
2017-10-12 13:52:36 +03:00
|
|
|
{
|
2015-11-03 21:05:37 +03:00
|
|
|
// save in the same format as .dat files
|
2016-02-11 03:00:00 +03:00
|
|
|
std::ofstream fk (i2p::fs::DataDirPath (ROUTER_KEYS), std::ofstream::binary | std::ofstream::out);
|
2015-11-03 21:05:37 +03: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-08-17 09:09:15 +04:00
|
|
|
}
|
2014-10-12 05:27:55 +04:00
|
|
|
|
2015-04-05 19:54:15 +03:00
|
|
|
std::shared_ptr<i2p::tunnel::TunnelPool> RouterContext::GetTunnelPool () const
|
|
|
|
{
|
2017-10-12 13:52:36 +03:00
|
|
|
return i2p::tunnel::tunnels.GetExploratoryPool ();
|
|
|
|
}
|
|
|
|
|
2020-01-07 23:20:55 +03:00
|
|
|
void RouterContext::HandleI2NPMessage (const uint8_t * buf, size_t len)
|
2014-10-12 05:27:55 +04:00
|
|
|
{
|
2020-01-07 23:20:55 +03:00
|
|
|
i2p::HandleI2NPMessage (CreateI2NPMessage (buf, GetI2NPMessageLength (buf, len)));
|
2015-02-23 22:41:56 +03:00
|
|
|
}
|
|
|
|
|
2021-07-21 05:00:06 +03:00
|
|
|
bool RouterContext::HandleCloveI2NPMessage (I2NPMessageType typeID, const uint8_t * payload, size_t len, uint32_t msgID)
|
2021-05-15 16:28:16 +03:00
|
|
|
{
|
2021-07-21 05:00:06 +03:00
|
|
|
auto msg = CreateI2NPMessage (typeID, payload, len, msgID);
|
2020-11-05 23:27:37 +03:00
|
|
|
if (!msg) return false;
|
|
|
|
i2p::HandleI2NPMessage (msg);
|
2021-05-15 16:28:16 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-11-05 23:27:37 +03:00
|
|
|
|
2015-06-16 17:14:14 +03:00
|
|
|
void RouterContext::ProcessGarlicMessage (std::shared_ptr<I2NPMessage> msg)
|
2015-06-10 05:14:31 +03:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_GarlicMutex);
|
2021-09-05 18:16:41 +03:00
|
|
|
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 (!HandleECIESx25519TagMessage (buf, len)) // try tag first
|
2021-11-27 23:30:35 +03:00
|
|
|
{
|
2021-09-05 18:16:41 +03:00
|
|
|
// then Noise_N one-time decryption
|
|
|
|
if (m_ECIESSession)
|
|
|
|
m_ECIESSession->HandleNextMessage (buf, len);
|
|
|
|
else
|
|
|
|
LogPrint (eLogError, "Router: Session is not set for ECIES router");
|
2021-11-27 23:30:35 +03:00
|
|
|
}
|
2017-10-12 13:52:36 +03:00
|
|
|
}
|
|
|
|
|
2015-06-16 17:14:14 +03:00
|
|
|
void RouterContext::ProcessDeliveryStatusMessage (std::shared_ptr<I2NPMessage> msg)
|
2015-06-10 05:14:31 +03:00
|
|
|
{
|
2020-11-15 21:06:02 +03:00
|
|
|
if (i2p::data::netdb.GetPublishReplyToken () == bufbe32toh (msg->GetPayload () + DELIVERY_STATUS_MSGID_OFFSET))
|
|
|
|
i2p::data::netdb.PostI2NPMsg (msg);
|
|
|
|
else
|
2021-05-15 16:28:16 +03:00
|
|
|
{
|
2020-11-15 21:06:02 +03:00
|
|
|
std::unique_lock<std::mutex> l(m_GarlicMutex);
|
|
|
|
i2p::garlic::GarlicDestination::ProcessDeliveryStatusMessage (msg);
|
2021-05-15 16:28:16 +03:00
|
|
|
}
|
2017-10-12 13:52:36 +03:00
|
|
|
}
|
2016-07-28 20:24:25 +03:00
|
|
|
|
|
|
|
void RouterContext::CleanupDestination ()
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_GarlicMutex);
|
|
|
|
i2p::garlic::GarlicDestination::CleanupExpiredTags ();
|
|
|
|
}
|
2017-10-12 13:52:36 +03:00
|
|
|
|
2015-02-23 22:41:56 +03:00
|
|
|
uint32_t RouterContext::GetUptime () const
|
|
|
|
{
|
2019-06-19 18:43:04 +03:00
|
|
|
return std::chrono::duration_cast<std::chrono::seconds> (std::chrono::steady_clock::now() - m_StartupTime).count ();
|
2017-10-12 13:52:36 +03:00
|
|
|
}
|
2017-11-08 21:49:48 +03:00
|
|
|
|
2021-09-01 01:51:40 +03:00
|
|
|
bool RouterContext::Decrypt (const uint8_t * encrypted, uint8_t * data, i2p::data::CryptoKeyType preferredCrypto) const
|
2017-11-08 21:49:48 +03:00
|
|
|
{
|
2021-09-01 01:51:40 +03:00
|
|
|
return m_Decryptor ? m_Decryptor->Decrypt (encrypted, data) : false;
|
2018-03-09 22:56:06 +03:00
|
|
|
}
|
|
|
|
|
2021-03-28 19:14:02 +03:00
|
|
|
bool RouterContext::DecryptTunnelBuildRecord (const uint8_t * encrypted, uint8_t * data)
|
2018-03-09 22:56:06 +03:00
|
|
|
{
|
2021-09-05 18:16:41 +03:00
|
|
|
return DecryptECIESTunnelBuildRecord (encrypted, data, ECIES_BUILD_REQUEST_RECORD_CLEAR_TEXT_SIZE);
|
2017-11-08 21:49:48 +03:00
|
|
|
}
|
2018-09-09 05:08:08 +03:00
|
|
|
|
2021-06-03 02:50:29 +03:00
|
|
|
bool RouterContext::DecryptECIESTunnelBuildRecord (const uint8_t * encrypted, uint8_t * data, size_t clearTextSize)
|
2021-11-27 23:30:35 +03:00
|
|
|
{
|
2021-06-03 02:50:29 +03:00
|
|
|
// m_InitialNoiseState is h = SHA256(h || hepk)
|
2021-06-09 19:49:50 +03:00
|
|
|
m_CurrentNoiseState = m_InitialNoiseState;
|
|
|
|
m_CurrentNoiseState.MixHash (encrypted, 32); // h = SHA256(h || sepk)
|
2021-06-03 02:50:29 +03:00
|
|
|
uint8_t sharedSecret[32];
|
2021-09-01 01:51:40 +03:00
|
|
|
if (!m_TunnelDecryptor->Decrypt (encrypted, sharedSecret))
|
2021-06-03 02:50:29 +03:00
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "Router: Incorrect ephemeral public key");
|
|
|
|
return false;
|
|
|
|
}
|
2021-06-09 19:49:50 +03:00
|
|
|
m_CurrentNoiseState.MixKey (sharedSecret);
|
2021-06-03 02:50:29 +03:00
|
|
|
encrypted += 32;
|
|
|
|
uint8_t nonce[12];
|
|
|
|
memset (nonce, 0, 12);
|
2021-11-27 23:30:35 +03:00
|
|
|
if (!i2p::crypto::AEADChaCha20Poly1305 (encrypted, clearTextSize, m_CurrentNoiseState.m_H, 32,
|
2021-06-09 19:49:50 +03:00
|
|
|
m_CurrentNoiseState.m_CK + 32, nonce, data, clearTextSize, false)) // decrypt
|
2021-06-03 02:50:29 +03:00
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "Router: Tunnel record AEAD decryption failed");
|
|
|
|
return false;
|
|
|
|
}
|
2021-06-09 19:49:50 +03:00
|
|
|
m_CurrentNoiseState.MixHash (encrypted, clearTextSize + 16); // h = SHA256(h || ciphertext)
|
2021-06-03 02:50:29 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RouterContext::DecryptTunnelShortRequestRecord (const uint8_t * encrypted, uint8_t * data)
|
|
|
|
{
|
2021-09-05 18:16:41 +03:00
|
|
|
return DecryptECIESTunnelBuildRecord (encrypted, data, SHORT_REQUEST_RECORD_CLEAR_TEXT_SIZE);
|
2021-11-27 23:30:35 +03:00
|
|
|
}
|
|
|
|
|
2022-03-02 05:23:08 +03:00
|
|
|
i2p::crypto::X25519Keys& RouterContext::GetNTCP2StaticKeys ()
|
2018-09-09 05:08:08 +03:00
|
|
|
{
|
2022-03-02 05:23:08 +03:00
|
|
|
if (!m_NTCP2StaticKeys)
|
2019-05-08 00:45:52 +03:00
|
|
|
{
|
2018-09-09 05:08:08 +03:00
|
|
|
if (!m_NTCP2Keys) NewNTCP2Keys ();
|
2018-09-09 15:38:12 +03:00
|
|
|
auto x = new i2p::crypto::X25519Keys (m_NTCP2Keys->staticPrivateKey, m_NTCP2Keys->staticPublicKey);
|
2022-03-02 05:23:08 +03:00
|
|
|
if (!m_NTCP2StaticKeys)
|
|
|
|
m_NTCP2StaticKeys.reset (x);
|
2018-09-09 15:38:12 +03:00
|
|
|
else
|
|
|
|
delete x;
|
2018-09-09 05:08:08 +03:00
|
|
|
}
|
2022-03-02 05:23:08 +03:00
|
|
|
return *m_NTCP2StaticKeys;
|
2019-05-08 00:45:52 +03:00
|
|
|
}
|
2022-03-02 05:23:08 +03:00
|
|
|
|
|
|
|
i2p::crypto::X25519Keys& RouterContext::GetSSU2StaticKeys ()
|
|
|
|
{
|
|
|
|
if (!m_SSU2StaticKeys)
|
|
|
|
{
|
|
|
|
if (!m_SSU2Keys) NewSSU2Keys ();
|
|
|
|
auto x = new i2p::crypto::X25519Keys (m_SSU2Keys->staticPrivateKey, m_SSU2Keys->staticPublicKey);
|
|
|
|
if (!m_SSU2StaticKeys)
|
|
|
|
m_SSU2StaticKeys.reset (x);
|
|
|
|
else
|
|
|
|
delete x;
|
|
|
|
}
|
|
|
|
return *m_SSU2StaticKeys;
|
2022-05-20 19:56:05 +03:00
|
|
|
}
|
2014-08-17 09:09:15 +04:00
|
|
|
}
|