2020-05-22 13:18:41 +00:00
|
|
|
/*
|
2024-01-11 20:39:42 +00:00
|
|
|
* Copyright (c) 2013-2024, The PurpleI2P Project
|
2020-05-22 13:18:41 +00: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-27 15:26:39 +00:00
|
|
|
#include "Log.h"
|
2015-11-03 14:15:49 +00:00
|
|
|
#include "Crypto.h"
|
2013-10-27 15:26:39 +00:00
|
|
|
#include "RouterContext.h"
|
|
|
|
#include "I2NPProtocol.h"
|
2017-04-22 00:04:16 +00:00
|
|
|
#include "NetDb.hpp"
|
2013-10-27 15:26:39 +00:00
|
|
|
#include "Transports.h"
|
2016-10-28 19:57:18 +00:00
|
|
|
#include "Config.h"
|
2017-05-29 05:28:16 +00:00
|
|
|
#include "HTTP.h"
|
2020-12-07 03:22:16 +00:00
|
|
|
#include "util.h"
|
2013-10-27 15:26:39 +00:00
|
|
|
|
|
|
|
using namespace i2p::data;
|
|
|
|
|
|
|
|
namespace i2p
|
2014-10-21 16:25:53 +00:00
|
|
|
{
|
|
|
|
namespace transport
|
2013-10-27 15:26:39 +00:00
|
|
|
{
|
2020-06-30 00:02:09 +00:00
|
|
|
template<typename Keys>
|
|
|
|
EphemeralKeysSupplier<Keys>::EphemeralKeysSupplier (int size):
|
2014-10-20 19:19:56 +00:00
|
|
|
m_QueueSize (size), m_IsRunning (false), m_Thread (nullptr)
|
|
|
|
{
|
2017-05-29 05:28:16 +00:00
|
|
|
}
|
2014-10-20 19:19:56 +00:00
|
|
|
|
2020-10-12 14:36:44 +00:00
|
|
|
template<typename Keys>
|
2020-06-30 00:02:09 +00:00
|
|
|
EphemeralKeysSupplier<Keys>::~EphemeralKeysSupplier ()
|
2014-04-04 20:29:40 +00:00
|
|
|
{
|
|
|
|
Stop ();
|
|
|
|
}
|
|
|
|
|
2020-10-12 14:36:44 +00:00
|
|
|
template<typename Keys>
|
2020-06-30 00:02:09 +00:00
|
|
|
void EphemeralKeysSupplier<Keys>::Start ()
|
2014-04-04 20:29:40 +00:00
|
|
|
{
|
|
|
|
m_IsRunning = true;
|
2020-06-30 00:02:09 +00:00
|
|
|
m_Thread = new std::thread (std::bind (&EphemeralKeysSupplier<Keys>::Run, this));
|
2014-04-04 20:29:40 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 14:36:44 +00:00
|
|
|
template<typename Keys>
|
2020-06-30 00:02:09 +00:00
|
|
|
void EphemeralKeysSupplier<Keys>::Stop ()
|
2014-04-04 20:29:40 +00:00
|
|
|
{
|
2018-11-20 18:57:51 +00:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_AcquiredMutex);
|
|
|
|
m_IsRunning = false;
|
|
|
|
m_Acquired.notify_one ();
|
|
|
|
}
|
2014-04-04 20:29:40 +00:00
|
|
|
if (m_Thread)
|
2017-05-29 05:28:16 +00:00
|
|
|
{
|
|
|
|
m_Thread->join ();
|
2014-04-04 20:29:40 +00:00
|
|
|
delete m_Thread;
|
|
|
|
m_Thread = 0;
|
2017-05-29 05:28:16 +00:00
|
|
|
}
|
2014-04-04 20:29:40 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 14:36:44 +00:00
|
|
|
template<typename Keys>
|
2020-06-30 00:02:09 +00:00
|
|
|
void EphemeralKeysSupplier<Keys>::Run ()
|
2014-04-04 20:29:40 +00:00
|
|
|
{
|
2020-12-07 03:22:16 +00:00
|
|
|
i2p::util::SetThreadName("Ephemerals");
|
2020-12-04 15:36:49 +00:00
|
|
|
|
2014-04-04 20:29:40 +00:00
|
|
|
while (m_IsRunning)
|
|
|
|
{
|
2016-11-07 19:44:32 +00:00
|
|
|
int num, total = 0;
|
2018-11-20 18:57:51 +00:00
|
|
|
while ((num = m_QueueSize - (int)m_Queue.size ()) > 0 && total < 10)
|
2017-05-29 05:28:16 +00:00
|
|
|
{
|
2020-06-30 00:02:09 +00:00
|
|
|
CreateEphemeralKeys (num);
|
2016-11-07 19:44:32 +00:00
|
|
|
total += num;
|
|
|
|
}
|
2018-11-20 18:57:51 +00:00
|
|
|
if (total >= 10)
|
2016-11-07 19:44:32 +00:00
|
|
|
{
|
2020-06-30 00:02:09 +00:00
|
|
|
LogPrint (eLogWarning, "Transports: ", total, " ephemeral keys generated at the time");
|
2016-11-07 19:44:32 +00:00
|
|
|
std::this_thread::sleep_for (std::chrono::seconds(1)); // take a break
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-11-20 18:57:51 +00:00
|
|
|
std::unique_lock<std::mutex> l(m_AcquiredMutex);
|
|
|
|
if (!m_IsRunning) break;
|
2017-12-07 13:26:28 +00:00
|
|
|
m_Acquired.wait (l); // wait for element gets acquired
|
2017-05-29 05:28:16 +00:00
|
|
|
}
|
2014-04-04 20:29:40 +00:00
|
|
|
}
|
2017-05-29 05:28:16 +00:00
|
|
|
}
|
2014-04-04 20:29:40 +00:00
|
|
|
|
2020-10-12 14:36:44 +00:00
|
|
|
template<typename Keys>
|
2020-06-30 00:02:09 +00:00
|
|
|
void EphemeralKeysSupplier<Keys>::CreateEphemeralKeys (int num)
|
2014-04-04 20:29:40 +00:00
|
|
|
{
|
|
|
|
if (num > 0)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < num; i++)
|
|
|
|
{
|
2020-06-30 00:02:09 +00:00
|
|
|
auto pair = std::make_shared<Keys> ();
|
2015-11-03 14:15:49 +00:00
|
|
|
pair->GenerateKeys ();
|
2020-06-30 00:02:09 +00:00
|
|
|
std::unique_lock<std::mutex> l(m_AcquiredMutex);
|
2014-04-04 20:29:40 +00:00
|
|
|
m_Queue.push (pair);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-12 14:36:44 +00:00
|
|
|
template<typename Keys>
|
2020-06-30 00:02:09 +00:00
|
|
|
std::shared_ptr<Keys> EphemeralKeysSupplier<Keys>::Acquire ()
|
2014-04-04 20:29:40 +00:00
|
|
|
{
|
|
|
|
{
|
2020-06-30 00:02:09 +00:00
|
|
|
std::unique_lock<std::mutex> l(m_AcquiredMutex);
|
2016-01-28 03:09:35 +00:00
|
|
|
if (!m_Queue.empty ())
|
|
|
|
{
|
|
|
|
auto pair = m_Queue.front ();
|
|
|
|
m_Queue.pop ();
|
|
|
|
m_Acquired.notify_one ();
|
|
|
|
return pair;
|
2017-05-29 05:28:16 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-28 03:09:35 +00:00
|
|
|
// queue is empty, create new
|
2020-06-30 00:02:09 +00:00
|
|
|
auto pair = std::make_shared<Keys> ();
|
2016-01-28 03:09:35 +00:00
|
|
|
pair->GenerateKeys ();
|
|
|
|
return pair;
|
2014-04-04 20:29:40 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 14:36:44 +00:00
|
|
|
template<typename Keys>
|
2020-06-30 00:02:09 +00:00
|
|
|
void EphemeralKeysSupplier<Keys>::Return (std::shared_ptr<Keys> pair)
|
2014-09-17 15:13:25 +00:00
|
|
|
{
|
2017-08-25 18:45:58 +00:00
|
|
|
if (pair)
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex>l(m_AcquiredMutex);
|
|
|
|
if ((int)m_Queue.size () < 2*m_QueueSize)
|
|
|
|
m_Queue.push (pair);
|
|
|
|
}
|
|
|
|
else
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint(eLogError, "Transports: Return null DHKeys");
|
2014-09-17 15:13:25 +00:00
|
|
|
}
|
|
|
|
|
2017-05-29 05:28:16 +00:00
|
|
|
Transports transports;
|
|
|
|
|
|
|
|
Transports::Transports ():
|
2020-10-12 15:27:25 +00:00
|
|
|
m_IsOnline (true), m_IsRunning (false), m_IsNAT (true), m_CheckReserved(true), m_Thread (nullptr),
|
2020-10-12 14:36:44 +00:00
|
|
|
m_Service (nullptr), m_Work (nullptr), m_PeerCleanupTimer (nullptr), m_PeerTestTimer (nullptr),
|
2022-12-12 05:12:23 +00:00
|
|
|
m_UpdateBandwidthTimer (nullptr), m_SSU2Server (nullptr), m_NTCP2Server (nullptr),
|
2021-05-10 22:55:39 +00:00
|
|
|
m_X25519KeysPairSupplier (15), // 15 pre-generated keys
|
2022-12-12 05:12:23 +00:00
|
|
|
m_TotalSentBytes (0), m_TotalReceivedBytes (0), m_TotalTransitTransmittedBytes (0),
|
|
|
|
m_InBandwidth (0), m_OutBandwidth (0), m_TransitBandwidth (0),
|
|
|
|
m_LastInBandwidthUpdateBytes (0), m_LastOutBandwidthUpdateBytes (0), m_LastTransitBandwidthUpdateBytes (0),
|
|
|
|
m_InBandwidth15s (0), m_OutBandwidth15s (0), m_TransitBandwidth15s (0),
|
|
|
|
m_LastInBandwidth15sUpdateBytes (0), m_LastOutBandwidth15sUpdateBytes (0), m_LastTransitBandwidth15sUpdateBytes (0),
|
|
|
|
m_LastBandwidth15sUpdateTime (0)
|
2017-05-29 05:28:16 +00:00
|
|
|
{
|
2013-10-27 15:26:39 +00:00
|
|
|
}
|
2017-05-29 05:28:16 +00:00
|
|
|
|
|
|
|
Transports::~Transports ()
|
|
|
|
{
|
2013-10-27 15:26:39 +00:00
|
|
|
Stop ();
|
2016-12-22 18:32:06 +00:00
|
|
|
if (m_Service)
|
|
|
|
{
|
|
|
|
delete m_PeerCleanupTimer; m_PeerCleanupTimer = nullptr;
|
|
|
|
delete m_PeerTestTimer; m_PeerTestTimer = nullptr;
|
2022-12-12 05:12:23 +00:00
|
|
|
delete m_UpdateBandwidthTimer; m_UpdateBandwidthTimer = nullptr;
|
2016-12-22 18:32:06 +00:00
|
|
|
delete m_Work; m_Work = nullptr;
|
|
|
|
delete m_Service; m_Service = nullptr;
|
2017-05-29 05:28:16 +00:00
|
|
|
}
|
|
|
|
}
|
2013-10-27 15:26:39 +00:00
|
|
|
|
2022-11-23 00:29:20 +00:00
|
|
|
void Transports::Start (bool enableNTCP2, bool enableSSU2)
|
2013-10-27 15:26:39 +00:00
|
|
|
{
|
2016-12-22 18:32:06 +00:00
|
|
|
if (!m_Service)
|
|
|
|
{
|
|
|
|
m_Service = new boost::asio::io_service ();
|
|
|
|
m_Work = new boost::asio::io_service::work (*m_Service);
|
|
|
|
m_PeerCleanupTimer = new boost::asio::deadline_timer (*m_Service);
|
2018-01-06 04:01:44 +00:00
|
|
|
m_PeerTestTimer = new boost::asio::deadline_timer (*m_Service);
|
2022-12-13 20:25:16 +00:00
|
|
|
m_UpdateBandwidthTimer = new boost::asio::deadline_timer (*m_Service);
|
2016-12-22 18:32:06 +00:00
|
|
|
}
|
|
|
|
|
2022-10-27 19:02:54 +00:00
|
|
|
bool ipv4; i2p::config::GetOption("ipv4", ipv4);
|
|
|
|
bool ipv6; i2p::config::GetOption("ipv6", ipv6);
|
2017-10-30 12:27:55 +00:00
|
|
|
i2p::config::GetOption("nat", m_IsNAT);
|
2020-06-30 00:02:09 +00:00
|
|
|
m_X25519KeysPairSupplier.Start ();
|
2013-12-29 15:48:57 +00:00
|
|
|
m_IsRunning = true;
|
2013-10-27 15:26:39 +00:00
|
|
|
m_Thread = new std::thread (std::bind (&Transports::Run, this));
|
2020-03-01 20:11:54 +00:00
|
|
|
std::string ntcp2proxy; i2p::config::GetOption("ntcp2.proxy", ntcp2proxy);
|
2020-10-12 14:36:44 +00:00
|
|
|
i2p::http::URL proxyurl;
|
2020-03-01 14:35:24 +00:00
|
|
|
// create NTCP2. TODO: move to acceptor
|
2021-03-10 20:36:10 +00:00
|
|
|
if (enableNTCP2 || i2p::context.SupportsMesh ())
|
2018-07-13 19:59:28 +00:00
|
|
|
{
|
2021-03-10 20:36:10 +00:00
|
|
|
if(!ntcp2proxy.empty() && enableNTCP2)
|
2020-03-01 14:35:24 +00:00
|
|
|
{
|
|
|
|
if(proxyurl.parse(ntcp2proxy))
|
|
|
|
{
|
|
|
|
if(proxyurl.schema == "socks" || proxyurl.schema == "http")
|
|
|
|
{
|
|
|
|
m_NTCP2Server = new NTCP2Server ();
|
|
|
|
NTCP2Server::ProxyType proxytype = NTCP2Server::eSocksProxy;
|
|
|
|
|
|
|
|
if (proxyurl.schema == "http")
|
|
|
|
proxytype = NTCP2Server::eHTTPProxy;
|
|
|
|
|
2021-03-11 01:00:21 +00:00
|
|
|
m_NTCP2Server->UseProxy(proxytype, proxyurl.host, proxyurl.port, proxyurl.user, proxyurl.pass);
|
2021-03-06 13:50:47 +00:00
|
|
|
i2p::context.SetStatus (eRouterStatusProxy);
|
2022-10-27 19:02:54 +00:00
|
|
|
if (ipv6)
|
|
|
|
i2p::context.SetStatusV6 (eRouterStatusProxy);
|
2020-03-01 14:35:24 +00:00
|
|
|
}
|
|
|
|
else
|
2023-03-31 11:29:04 +00:00
|
|
|
LogPrint(eLogCritical, "Transports: Unsupported NTCP2 proxy URL ", ntcp2proxy);
|
2020-03-01 14:35:24 +00:00
|
|
|
}
|
|
|
|
else
|
2023-03-31 11:29:04 +00:00
|
|
|
LogPrint(eLogCritical, "Transports: Invalid NTCP2 proxy URL ", ntcp2proxy);
|
2020-03-01 14:35:24 +00:00
|
|
|
}
|
2020-03-01 20:11:54 +00:00
|
|
|
else
|
|
|
|
m_NTCP2Server = new NTCP2Server ();
|
2020-03-01 14:35:24 +00:00
|
|
|
}
|
2017-05-29 05:28:16 +00:00
|
|
|
|
2022-03-14 01:34:11 +00:00
|
|
|
// create SSU2 server
|
2022-12-13 20:25:16 +00:00
|
|
|
if (enableSSU2)
|
|
|
|
{
|
2022-10-26 20:05:40 +00:00
|
|
|
m_SSU2Server = new SSU2Server ();
|
|
|
|
std::string ssu2proxy; i2p::config::GetOption("ssu2.proxy", ssu2proxy);
|
|
|
|
if (!ssu2proxy.empty())
|
|
|
|
{
|
|
|
|
if (proxyurl.parse (ssu2proxy) && proxyurl.schema == "socks")
|
|
|
|
{
|
|
|
|
if (m_SSU2Server->SetProxy (proxyurl.host, proxyurl.port))
|
2022-12-13 20:25:16 +00:00
|
|
|
{
|
2022-10-26 20:05:40 +00:00
|
|
|
i2p::context.SetStatus (eRouterStatusProxy);
|
2022-10-27 19:02:54 +00:00
|
|
|
if (ipv6)
|
|
|
|
i2p::context.SetStatusV6 (eRouterStatusProxy);
|
2022-12-13 20:25:16 +00:00
|
|
|
}
|
2022-10-26 20:05:40 +00:00
|
|
|
else
|
2023-03-31 11:29:04 +00:00
|
|
|
LogPrint(eLogCritical, "Transports: Can't set SSU2 proxy ", ssu2proxy);
|
2022-12-13 20:25:16 +00:00
|
|
|
}
|
2022-10-26 20:05:40 +00:00
|
|
|
else
|
2023-03-31 11:29:04 +00:00
|
|
|
LogPrint(eLogCritical, "Transports: Invalid SSU2 proxy URL ", ssu2proxy);
|
2022-12-13 20:25:16 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-20 16:56:05 +00:00
|
|
|
|
2021-02-27 00:31:38 +00:00
|
|
|
// bind to interfaces
|
|
|
|
if (ipv4)
|
|
|
|
{
|
|
|
|
std::string address; i2p::config::GetOption("address4", address);
|
|
|
|
if (!address.empty ())
|
2021-11-27 20:30:35 +00:00
|
|
|
{
|
2021-02-27 00:31:38 +00:00
|
|
|
boost::system::error_code ec;
|
|
|
|
auto addr = boost::asio::ip::address::from_string (address, ec);
|
2021-02-27 21:13:12 +00:00
|
|
|
if (!ec)
|
2021-11-27 20:30:35 +00:00
|
|
|
{
|
2021-02-27 21:13:12 +00:00
|
|
|
if (m_NTCP2Server) m_NTCP2Server->SetLocalAddress (addr);
|
2022-06-17 19:16:12 +00:00
|
|
|
if (m_SSU2Server) m_SSU2Server->SetLocalAddress (addr);
|
2021-11-27 20:30:35 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-31 22:11:36 +00:00
|
|
|
|
|
|
|
if (enableSSU2)
|
|
|
|
{
|
|
|
|
uint16_t mtu; i2p::config::GetOption ("ssu2.mtu4", mtu);
|
|
|
|
if (mtu)
|
2022-12-13 20:25:16 +00:00
|
|
|
{
|
2022-10-31 22:11:36 +00:00
|
|
|
if (mtu < (int)SSU2_MIN_PACKET_SIZE) mtu = SSU2_MIN_PACKET_SIZE;
|
|
|
|
if (mtu > (int)SSU2_MAX_PACKET_SIZE) mtu = SSU2_MAX_PACKET_SIZE;
|
|
|
|
i2p::context.SetMTU (mtu, true);
|
2022-12-13 20:25:16 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-27 20:30:35 +00:00
|
|
|
}
|
2021-02-27 00:31:38 +00:00
|
|
|
|
|
|
|
if (ipv6)
|
|
|
|
{
|
|
|
|
std::string address; i2p::config::GetOption("address6", address);
|
|
|
|
if (!address.empty ())
|
2021-11-27 20:30:35 +00:00
|
|
|
{
|
2021-02-27 00:31:38 +00:00
|
|
|
boost::system::error_code ec;
|
|
|
|
auto addr = boost::asio::ip::address::from_string (address, ec);
|
2021-11-27 20:30:35 +00:00
|
|
|
if (!ec)
|
|
|
|
{
|
2021-02-27 21:13:12 +00:00
|
|
|
if (m_NTCP2Server) m_NTCP2Server->SetLocalAddress (addr);
|
2022-06-17 19:16:12 +00:00
|
|
|
if (m_SSU2Server) m_SSU2Server->SetLocalAddress (addr);
|
2021-11-27 20:30:35 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-31 22:11:36 +00:00
|
|
|
|
|
|
|
if (enableSSU2)
|
|
|
|
{
|
|
|
|
uint16_t mtu; i2p::config::GetOption ("ssu2.mtu6", mtu);
|
|
|
|
if (mtu)
|
2022-12-13 20:25:16 +00:00
|
|
|
{
|
2022-10-31 22:11:36 +00:00
|
|
|
if (mtu < (int)SSU2_MIN_PACKET_SIZE) mtu = SSU2_MIN_PACKET_SIZE;
|
|
|
|
if (mtu > (int)SSU2_MAX_PACKET_SIZE) mtu = SSU2_MAX_PACKET_SIZE;
|
|
|
|
i2p::context.SetMTU (mtu, false);
|
2022-12-13 20:25:16 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-27 00:31:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ygg; i2p::config::GetOption("meshnets.yggdrasil", ygg);
|
|
|
|
if (ygg)
|
|
|
|
{
|
2021-02-27 01:38:16 +00:00
|
|
|
std::string address; i2p::config::GetOption("meshnets.yggaddress", address);
|
2021-02-27 00:31:38 +00:00
|
|
|
if (!address.empty ())
|
2021-11-27 20:30:35 +00:00
|
|
|
{
|
2021-02-27 00:31:38 +00:00
|
|
|
boost::system::error_code ec;
|
|
|
|
auto addr = boost::asio::ip::address::from_string (address, ec);
|
|
|
|
if (!ec && m_NTCP2Server && i2p::util::net::IsYggdrasilAddress (addr))
|
|
|
|
m_NTCP2Server->SetLocalAddress (addr);
|
2021-11-27 20:30:35 +00:00
|
|
|
}
|
2021-02-27 00:31:38 +00:00
|
|
|
}
|
2021-02-27 15:35:50 +00:00
|
|
|
|
|
|
|
// start servers
|
|
|
|
if (m_NTCP2Server) m_NTCP2Server->Start ();
|
2022-06-02 19:08:38 +00:00
|
|
|
if (m_SSU2Server) m_SSU2Server->Start ();
|
2022-11-23 00:29:20 +00:00
|
|
|
if (m_SSU2Server) DetectExternalIP ();
|
2022-07-27 10:38:28 +00:00
|
|
|
|
2022-12-12 05:12:23 +00:00
|
|
|
m_PeerCleanupTimer->expires_from_now (boost::posix_time::seconds(5 * SESSION_CREATION_TIMEOUT));
|
2016-12-22 18:32:06 +00:00
|
|
|
m_PeerCleanupTimer->async_wait (std::bind (&Transports::HandlePeerCleanupTimer, this, std::placeholders::_1));
|
2017-08-09 14:52:52 +00:00
|
|
|
|
2022-12-13 20:25:16 +00:00
|
|
|
m_UpdateBandwidthTimer->expires_from_now (boost::posix_time::seconds(1));
|
2022-12-12 05:12:23 +00:00
|
|
|
m_UpdateBandwidthTimer->async_wait (std::bind (&Transports::HandleUpdateBandwidthTimer, this, std::placeholders::_1));
|
|
|
|
|
2020-03-20 14:44:53 +00:00
|
|
|
if (m_IsNAT)
|
|
|
|
{
|
|
|
|
m_PeerTestTimer->expires_from_now (boost::posix_time::minutes(PEER_TEST_INTERVAL));
|
|
|
|
m_PeerTestTimer->async_wait (std::bind (&Transports::HandlePeerTestTimer, this, std::placeholders::_1));
|
|
|
|
}
|
2013-10-27 15:26:39 +00:00
|
|
|
}
|
2017-05-29 05:28:16 +00:00
|
|
|
|
2013-10-27 15:26:39 +00:00
|
|
|
void Transports::Stop ()
|
2017-05-29 05:28:16 +00:00
|
|
|
{
|
|
|
|
if (m_PeerCleanupTimer) m_PeerCleanupTimer->cancel ();
|
2016-12-22 18:32:06 +00:00
|
|
|
if (m_PeerTestTimer) m_PeerTestTimer->cancel ();
|
2015-01-13 03:53:35 +00:00
|
|
|
m_Peers.clear ();
|
2022-05-20 16:56:05 +00:00
|
|
|
|
2022-03-11 21:17:44 +00:00
|
|
|
if (m_SSU2Server)
|
|
|
|
{
|
|
|
|
m_SSU2Server->Stop ();
|
|
|
|
delete m_SSU2Server;
|
|
|
|
m_SSU2Server = nullptr;
|
|
|
|
}
|
2022-05-20 16:56:05 +00:00
|
|
|
|
2018-07-13 19:59:28 +00:00
|
|
|
if (m_NTCP2Server)
|
|
|
|
{
|
|
|
|
m_NTCP2Server->Stop ();
|
|
|
|
delete m_NTCP2Server;
|
|
|
|
m_NTCP2Server = nullptr;
|
|
|
|
}
|
|
|
|
|
2020-06-30 00:02:09 +00:00
|
|
|
m_X25519KeysPairSupplier.Stop ();
|
2013-12-29 15:48:57 +00:00
|
|
|
m_IsRunning = false;
|
2016-12-22 18:32:06 +00:00
|
|
|
if (m_Service) m_Service->stop ();
|
2013-10-27 15:26:39 +00:00
|
|
|
if (m_Thread)
|
2017-05-29 05:28:16 +00:00
|
|
|
{
|
|
|
|
m_Thread->join ();
|
2013-10-27 15:26:39 +00:00
|
|
|
delete m_Thread;
|
2014-09-22 17:28:46 +00:00
|
|
|
m_Thread = nullptr;
|
2017-05-29 05:28:16 +00:00
|
|
|
}
|
|
|
|
}
|
2013-10-27 15:26:39 +00:00
|
|
|
|
2017-05-29 05:28:16 +00:00
|
|
|
void Transports::Run ()
|
|
|
|
{
|
2020-12-07 03:22:16 +00:00
|
|
|
i2p::util::SetThreadName("Transports");
|
2020-12-04 15:36:49 +00:00
|
|
|
|
2016-12-22 18:32:06 +00:00
|
|
|
while (m_IsRunning && m_Service)
|
2013-11-29 12:52:09 +00:00
|
|
|
{
|
2013-12-29 15:48:57 +00:00
|
|
|
try
|
2017-05-29 05:28:16 +00:00
|
|
|
{
|
2016-12-22 18:32:06 +00:00
|
|
|
m_Service->run ();
|
2013-12-29 15:48:57 +00:00
|
|
|
}
|
|
|
|
catch (std::exception& ex)
|
|
|
|
{
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint (eLogError, "Transports: Runtime exception: ", ex.what ());
|
2017-05-29 05:28:16 +00:00
|
|
|
}
|
|
|
|
}
|
2013-11-29 12:52:09 +00:00
|
|
|
}
|
2017-05-29 05:28:16 +00:00
|
|
|
|
2022-12-12 05:12:23 +00:00
|
|
|
void Transports::HandleUpdateBandwidthTimer (const boost::system::error_code& ecode)
|
2015-03-17 19:19:38 +00:00
|
|
|
{
|
2022-12-12 05:12:23 +00:00
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
2015-03-17 19:19:38 +00:00
|
|
|
{
|
2022-12-12 05:12:23 +00:00
|
|
|
uint64_t ts = i2p::util::GetMillisecondsSinceEpoch ();
|
|
|
|
|
|
|
|
// updated every second
|
|
|
|
m_InBandwidth = m_TotalReceivedBytes - m_LastInBandwidthUpdateBytes;
|
|
|
|
m_OutBandwidth = m_TotalSentBytes - m_LastOutBandwidthUpdateBytes;
|
|
|
|
m_TransitBandwidth = m_TotalTransitTransmittedBytes - m_LastTransitBandwidthUpdateBytes;
|
2022-12-13 20:25:16 +00:00
|
|
|
|
2022-12-12 05:12:23 +00:00
|
|
|
m_LastInBandwidthUpdateBytes = m_TotalReceivedBytes;
|
|
|
|
m_LastOutBandwidthUpdateBytes = m_TotalSentBytes;
|
|
|
|
m_LastTransitBandwidthUpdateBytes = m_TotalTransitTransmittedBytes;
|
|
|
|
|
|
|
|
// updated every 15 seconds
|
|
|
|
auto delta = ts - m_LastBandwidth15sUpdateTime;
|
|
|
|
if (delta > 15 * 1000)
|
2015-03-17 19:19:38 +00:00
|
|
|
{
|
2022-12-12 05:12:23 +00:00
|
|
|
m_InBandwidth15s = (m_TotalReceivedBytes - m_LastInBandwidth15sUpdateBytes) * 1000 / delta;
|
|
|
|
m_OutBandwidth15s = (m_TotalSentBytes - m_LastOutBandwidth15sUpdateBytes) * 1000 / delta;
|
|
|
|
m_TransitBandwidth15s = (m_TotalTransitTransmittedBytes - m_LastTransitBandwidth15sUpdateBytes) * 1000 / delta;
|
|
|
|
|
|
|
|
m_LastBandwidth15sUpdateTime = ts;
|
|
|
|
m_LastInBandwidth15sUpdateBytes = m_TotalReceivedBytes;
|
|
|
|
m_LastOutBandwidth15sUpdateBytes = m_TotalSentBytes;
|
|
|
|
m_LastTransitBandwidth15sUpdateBytes = m_TotalTransitTransmittedBytes;
|
2017-05-29 05:28:16 +00:00
|
|
|
}
|
2022-12-12 05:12:23 +00:00
|
|
|
|
2022-12-13 20:25:16 +00:00
|
|
|
m_UpdateBandwidthTimer->expires_from_now (boost::posix_time::seconds(1));
|
2022-12-12 05:12:23 +00:00
|
|
|
m_UpdateBandwidthTimer->async_wait (std::bind (&Transports::HandleUpdateBandwidthTimer, this, std::placeholders::_1));
|
2015-03-17 19:19:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-18 17:07:11 +00:00
|
|
|
bool Transports::IsBandwidthExceeded () const
|
|
|
|
{
|
2016-03-31 00:00:00 +00:00
|
|
|
auto limit = i2p::context.GetBandwidthLimit() * 1024; // convert to bytes
|
2022-12-12 15:22:16 +00:00
|
|
|
auto bw = std::max (m_InBandwidth15s, m_OutBandwidth15s);
|
2016-03-31 00:00:00 +00:00
|
|
|
return bw > limit;
|
2015-03-18 17:07:11 +00:00
|
|
|
}
|
2013-10-27 15:26:39 +00:00
|
|
|
|
2017-05-04 18:58:12 +00:00
|
|
|
bool Transports::IsTransitBandwidthExceeded () const
|
|
|
|
{
|
|
|
|
auto limit = i2p::context.GetTransitBandwidthLimit() * 1024; // convert to bytes
|
|
|
|
return m_TransitBandwidth > limit;
|
2017-05-29 05:28:16 +00:00
|
|
|
}
|
2017-05-04 18:58:12 +00:00
|
|
|
|
2015-06-17 15:41:07 +00:00
|
|
|
void Transports::SendMessage (const i2p::data::IdentHash& ident, std::shared_ptr<i2p::I2NPMessage> msg)
|
|
|
|
{
|
2020-10-11 21:51:40 +00:00
|
|
|
if (m_IsOnline)
|
|
|
|
SendMessages (ident, std::vector<std::shared_ptr<i2p::I2NPMessage> > {msg });
|
2017-05-29 05:28:16 +00:00
|
|
|
}
|
2015-06-17 15:41:07 +00:00
|
|
|
|
|
|
|
void Transports::SendMessages (const i2p::data::IdentHash& ident, const std::vector<std::shared_ptr<i2p::I2NPMessage> >& msgs)
|
2015-01-21 02:05:57 +00:00
|
|
|
{
|
2016-12-22 18:32:06 +00:00
|
|
|
m_Service->post (std::bind (&Transports::PostMessages, this, ident, msgs));
|
2017-05-29 05:28:16 +00:00
|
|
|
}
|
2015-01-14 02:31:39 +00:00
|
|
|
|
2015-06-17 15:41:07 +00:00
|
|
|
void Transports::PostMessages (i2p::data::IdentHash ident, std::vector<std::shared_ptr<i2p::I2NPMessage> > msgs)
|
2015-01-21 02:05:57 +00:00
|
|
|
{
|
|
|
|
if (ident == i2p::context.GetRouterInfo ().GetIdentHash ())
|
2017-05-29 05:28:16 +00:00
|
|
|
{
|
2015-01-21 02:05:57 +00:00
|
|
|
// we send it to ourself
|
2016-08-08 22:53:37 +00:00
|
|
|
for (auto& it: msgs)
|
2021-10-17 15:31:37 +00:00
|
|
|
m_LoopbackHandler.PutNextMessage (std::move (it));
|
2016-11-15 19:11:55 +00:00
|
|
|
m_LoopbackHandler.Flush ();
|
2015-01-21 02:05:57 +00:00
|
|
|
return;
|
2016-10-28 16:50:26 +00:00
|
|
|
}
|
2020-05-24 18:14:16 +00:00
|
|
|
if(RoutesRestricted() && !IsRestrictedPeer(ident)) return;
|
2015-01-21 02:05:57 +00:00
|
|
|
auto it = m_Peers.find (ident);
|
|
|
|
if (it == m_Peers.end ())
|
|
|
|
{
|
2024-01-16 19:33:36 +00:00
|
|
|
// check if not known as unreachable
|
|
|
|
auto profile = i2p::data::GetRouterProfile (ident);
|
|
|
|
if (profile && profile->IsUnreachable ()) return; // don't create peer to unreachable router
|
|
|
|
// try to connect
|
2017-05-29 05:28:16 +00:00
|
|
|
bool connected = false;
|
2015-04-14 14:40:46 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = netdb.FindRouter (ident);
|
2021-06-19 18:44:33 +00:00
|
|
|
if (r && (r->IsUnreachable () || !r->IsReachableFrom (i2p::context.GetRouterInfo ()))) return; // router found but non-reachable
|
2016-01-15 21:23:03 +00:00
|
|
|
{
|
2022-06-19 18:21:35 +00:00
|
|
|
auto ts = i2p::util::GetSecondsSinceEpoch ();
|
2020-03-01 10:25:50 +00:00
|
|
|
std::unique_lock<std::mutex> l(m_PeersMutex);
|
2022-09-30 23:24:36 +00:00
|
|
|
it = m_Peers.insert (std::pair<i2p::data::IdentHash, Peer>(ident, {r, ts})).first;
|
2016-01-15 21:23:03 +00:00
|
|
|
}
|
2015-04-14 14:40:46 +00:00
|
|
|
connected = ConnectToPeer (ident, it->second);
|
|
|
|
}
|
|
|
|
catch (std::exception& ex)
|
|
|
|
{
|
2015-12-18 12:57:22 +00:00
|
|
|
LogPrint (eLogError, "Transports: PostMessages exception:", ex.what ());
|
2015-04-14 14:40:46 +00:00
|
|
|
}
|
2015-06-17 15:41:07 +00:00
|
|
|
if (!connected) return;
|
2017-05-29 05:28:16 +00:00
|
|
|
}
|
2024-01-19 00:18:51 +00:00
|
|
|
if (it->second.IsConnected ())
|
2015-06-17 15:41:07 +00:00
|
|
|
it->second.sessions.front ()->SendI2NPMessages (msgs);
|
2015-01-21 02:05:57 +00:00
|
|
|
else
|
2017-05-29 05:28:16 +00:00
|
|
|
{
|
2023-04-16 02:16:31 +00:00
|
|
|
auto sz = it->second.delayedMessages.size ();
|
|
|
|
if (sz < MAX_NUM_DELAYED_MESSAGES)
|
2017-05-29 05:28:16 +00:00
|
|
|
{
|
2023-04-16 12:18:41 +00:00
|
|
|
if (sz < CHECK_PROFILE_NUM_DELAYED_MESSAGES && sz + msgs.size () >= CHECK_PROFILE_NUM_DELAYED_MESSAGES)
|
2023-04-16 02:16:31 +00:00
|
|
|
{
|
|
|
|
auto profile = i2p::data::GetRouterProfile (ident);
|
|
|
|
if (profile && profile->IsUnreachable ())
|
|
|
|
{
|
2023-04-19 00:21:22 +00:00
|
|
|
LogPrint (eLogWarning, "Transports: Peer profile for ", ident.ToBase64 (), " reports unreachable. Dropped");
|
2023-04-16 02:16:31 +00:00
|
|
|
std::unique_lock<std::mutex> l(m_PeersMutex);
|
|
|
|
m_Peers.erase (it);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-08-08 22:53:37 +00:00
|
|
|
for (auto& it1: msgs)
|
2024-02-01 23:55:10 +00:00
|
|
|
if (sz > MAX_NUM_DELAYED_MESSAGES/2 && it1->onDrop)
|
|
|
|
it1->Drop (); // drop earlier because we can handle it
|
|
|
|
else
|
|
|
|
it->second.delayedMessages.push_back (it1);
|
2016-07-12 16:37:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint (eLogWarning, "Transports: Delayed messages queue size to ",
|
2020-05-24 18:14:16 +00:00
|
|
|
ident.ToBase64 (), " exceeds ", MAX_NUM_DELAYED_MESSAGES);
|
2017-05-29 05:28:16 +00:00
|
|
|
std::unique_lock<std::mutex> l(m_PeersMutex);
|
2016-07-12 16:37:39 +00:00
|
|
|
m_Peers.erase (it);
|
2017-05-29 05:28:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-14 02:31:39 +00:00
|
|
|
bool Transports::ConnectToPeer (const i2p::data::IdentHash& ident, Peer& peer)
|
|
|
|
{
|
2020-06-24 15:29:54 +00:00
|
|
|
if (!peer.router) // reconnect
|
2023-02-03 20:59:56 +00:00
|
|
|
peer.SetRouter (netdb.FindRouter (ident)); // try to get new one from netdb
|
2015-01-14 02:31:39 +00:00
|
|
|
if (peer.router) // we have RI already
|
2017-05-29 05:28:16 +00:00
|
|
|
{
|
2022-09-30 23:24:36 +00:00
|
|
|
if (peer.priority.empty ())
|
|
|
|
SetPriority (peer);
|
|
|
|
while (peer.numAttempts < (int)peer.priority.size ())
|
2020-04-30 01:44:13 +00:00
|
|
|
{
|
2022-09-30 23:24:36 +00:00
|
|
|
auto tr = peer.priority[peer.numAttempts];
|
|
|
|
peer.numAttempts++;
|
|
|
|
switch (tr)
|
2021-11-27 20:30:35 +00:00
|
|
|
{
|
2022-09-30 23:24:36 +00:00
|
|
|
case i2p::data::RouterInfo::eNTCP2V4:
|
|
|
|
case i2p::data::RouterInfo::eNTCP2V6:
|
2022-10-09 17:24:43 +00:00
|
|
|
{
|
2022-09-30 23:24:36 +00:00
|
|
|
if (!m_NTCP2Server) continue;
|
|
|
|
std::shared_ptr<const RouterInfo::Address> address = (tr == i2p::data::RouterInfo::eNTCP2V6) ?
|
|
|
|
peer.router->GetPublishedNTCP2V6Address () : peer.router->GetPublishedNTCP2V4Address ();
|
2024-01-11 20:39:42 +00:00
|
|
|
if (address && IsInReservedRange(address->host))
|
2022-09-30 23:24:36 +00:00
|
|
|
address = nullptr;
|
|
|
|
if (address)
|
2021-02-05 02:48:13 +00:00
|
|
|
{
|
2022-09-30 23:24:36 +00:00
|
|
|
auto s = std::make_shared<NTCP2Session> (*m_NTCP2Server, peer.router, address);
|
|
|
|
if( m_NTCP2Server->UsingProxy())
|
|
|
|
m_NTCP2Server->ConnectWithProxy(s);
|
|
|
|
else
|
|
|
|
m_NTCP2Server->Connect (s);
|
|
|
|
return true;
|
2021-02-05 02:48:13 +00:00
|
|
|
}
|
2022-09-30 23:24:36 +00:00
|
|
|
break;
|
2021-02-05 02:48:13 +00:00
|
|
|
}
|
2022-09-30 23:24:36 +00:00
|
|
|
case i2p::data::RouterInfo::eSSU2V4:
|
|
|
|
case i2p::data::RouterInfo::eSSU2V6:
|
2021-02-05 02:48:13 +00:00
|
|
|
{
|
2022-09-30 23:24:36 +00:00
|
|
|
if (!m_SSU2Server) continue;
|
|
|
|
std::shared_ptr<const RouterInfo::Address> address = (tr == i2p::data::RouterInfo::eSSU2V6) ?
|
|
|
|
peer.router->GetSSU2V6Address () : peer.router->GetSSU2V4Address ();
|
2024-01-11 20:39:42 +00:00
|
|
|
if (address && IsInReservedRange(address->host))
|
2022-09-30 23:24:36 +00:00
|
|
|
address = nullptr;
|
|
|
|
if (address && address->IsReachableSSU ())
|
2021-02-05 02:48:13 +00:00
|
|
|
{
|
2022-09-30 23:24:36 +00:00
|
|
|
if (m_SSU2Server->CreateSession (peer.router, address))
|
|
|
|
return true;
|
2021-02-05 02:48:13 +00:00
|
|
|
}
|
2022-09-30 23:24:36 +00:00
|
|
|
break;
|
2022-10-09 17:24:43 +00:00
|
|
|
}
|
2022-09-30 23:24:36 +00:00
|
|
|
case i2p::data::RouterInfo::eNTCP2V6Mesh:
|
2022-03-17 01:11:48 +00:00
|
|
|
{
|
2022-09-30 23:24:36 +00:00
|
|
|
if (!m_NTCP2Server) continue;
|
|
|
|
auto address = peer.router->GetYggdrasilAddress ();
|
|
|
|
if (address)
|
2022-03-17 01:11:48 +00:00
|
|
|
{
|
2022-09-30 23:24:36 +00:00
|
|
|
auto s = std::make_shared<NTCP2Session> (*m_NTCP2Server, peer.router, address);
|
|
|
|
m_NTCP2Server->Connect (s);
|
2022-03-17 01:11:48 +00:00
|
|
|
return true;
|
2022-10-09 17:24:43 +00:00
|
|
|
}
|
2022-09-30 23:24:36 +00:00
|
|
|
break;
|
2022-10-09 17:24:43 +00:00
|
|
|
}
|
2022-09-30 23:24:36 +00:00
|
|
|
default:
|
|
|
|
LogPrint (eLogError, "Transports: Unknown transport ", (int)tr);
|
2022-10-09 17:24:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 23:24:36 +00:00
|
|
|
LogPrint (eLogInfo, "Transports: No compatible addresses available");
|
2023-02-06 19:18:15 +00:00
|
|
|
if (peer.router->IsReachableFrom (i2p::context.GetRouterInfo ()))
|
|
|
|
i2p::data::netdb.SetUnreachable (ident, true); // we are here because all connection attempts failed but router claimed them
|
2015-06-09 15:00:37 +00:00
|
|
|
peer.Done ();
|
2017-05-29 05:28:16 +00:00
|
|
|
std::unique_lock<std::mutex> l(m_PeersMutex);
|
2015-01-14 02:31:39 +00:00
|
|
|
m_Peers.erase (ident);
|
|
|
|
return false;
|
2017-05-29 05:28:16 +00:00
|
|
|
}
|
2015-01-14 02:31:39 +00:00
|
|
|
else // otherwise request RI
|
|
|
|
{
|
2015-12-18 12:57:22 +00:00
|
|
|
LogPrint (eLogInfo, "Transports: RouterInfo for ", ident.ToBase64 (), " not found, requested");
|
2015-01-14 21:37:03 +00:00
|
|
|
i2p::data::netdb.RequestDestination (ident, std::bind (
|
|
|
|
&Transports::RequestComplete, this, std::placeholders::_1, ident));
|
2022-10-09 17:24:43 +00:00
|
|
|
}
|
2015-01-14 02:31:39 +00:00
|
|
|
return true;
|
2022-10-09 17:24:43 +00:00
|
|
|
}
|
|
|
|
|
2022-09-30 23:24:36 +00:00
|
|
|
void Transports::SetPriority (Peer& peer) const
|
|
|
|
{
|
2022-10-09 17:24:43 +00:00
|
|
|
static const std::vector<i2p::data::RouterInfo::SupportedTransports>
|
|
|
|
ntcp2Priority =
|
2022-09-30 23:24:36 +00:00
|
|
|
{
|
|
|
|
i2p::data::RouterInfo::eNTCP2V6,
|
|
|
|
i2p::data::RouterInfo::eNTCP2V4,
|
|
|
|
i2p::data::RouterInfo::eSSU2V6,
|
|
|
|
i2p::data::RouterInfo::eSSU2V4,
|
2022-11-23 00:29:20 +00:00
|
|
|
i2p::data::RouterInfo::eNTCP2V6Mesh
|
2022-10-09 17:24:43 +00:00
|
|
|
},
|
|
|
|
ssu2Priority =
|
2022-10-01 23:39:08 +00:00
|
|
|
{
|
|
|
|
i2p::data::RouterInfo::eSSU2V6,
|
|
|
|
i2p::data::RouterInfo::eSSU2V4,
|
|
|
|
i2p::data::RouterInfo::eNTCP2V6,
|
|
|
|
i2p::data::RouterInfo::eNTCP2V4,
|
2022-11-23 00:29:20 +00:00
|
|
|
i2p::data::RouterInfo::eNTCP2V6Mesh
|
2022-10-09 17:24:43 +00:00
|
|
|
};
|
2022-09-30 23:24:36 +00:00
|
|
|
if (!peer.router) return;
|
|
|
|
auto compatibleTransports = context.GetRouterInfo ().GetCompatibleTransports (false) &
|
|
|
|
peer.router->GetCompatibleTransports (true);
|
|
|
|
peer.numAttempts = 0;
|
|
|
|
peer.priority.clear ();
|
2023-05-06 20:43:09 +00:00
|
|
|
bool ssu2 = peer.router->GetProfile ()->IsReal () ? (rand () & 1) : false; // try NTCP2 if router is not confirmed real
|
2022-10-01 23:39:08 +00:00
|
|
|
const auto& priority = ssu2 ? ssu2Priority : ntcp2Priority;
|
|
|
|
for (auto transport: priority)
|
2022-09-30 23:24:36 +00:00
|
|
|
if (transport & compatibleTransports)
|
2022-10-09 17:24:43 +00:00
|
|
|
peer.priority.push_back (transport);
|
|
|
|
}
|
|
|
|
|
2015-01-14 21:37:03 +00:00
|
|
|
void Transports::RequestComplete (std::shared_ptr<const i2p::data::RouterInfo> r, const i2p::data::IdentHash& ident)
|
|
|
|
{
|
2016-12-22 18:32:06 +00:00
|
|
|
m_Service->post (std::bind (&Transports::HandleRequestComplete, this, r, ident));
|
2017-05-29 05:28:16 +00:00
|
|
|
}
|
|
|
|
|
2015-12-31 16:21:01 +00:00
|
|
|
void Transports::HandleRequestComplete (std::shared_ptr<const i2p::data::RouterInfo> r, i2p::data::IdentHash ident)
|
2014-08-29 01:34:23 +00:00
|
|
|
{
|
2015-01-14 02:31:39 +00:00
|
|
|
auto it = m_Peers.find (ident);
|
|
|
|
if (it != m_Peers.end ())
|
2017-05-29 05:28:16 +00:00
|
|
|
{
|
2015-01-14 02:31:39 +00:00
|
|
|
if (r)
|
|
|
|
{
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint (eLogDebug, "Transports: RouterInfo for ", ident.ToBase64 (), " found, trying to connect");
|
2023-02-03 20:59:56 +00:00
|
|
|
it->second.SetRouter (r);
|
2024-01-19 00:18:51 +00:00
|
|
|
if (!it->second.IsConnected ())
|
|
|
|
ConnectToPeer (ident, it->second);
|
2017-05-29 05:28:16 +00:00
|
|
|
}
|
2015-01-14 02:31:39 +00:00
|
|
|
else
|
|
|
|
{
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint (eLogWarning, "Transports: RouterInfo not found, failed to send messages");
|
2017-05-29 05:28:16 +00:00
|
|
|
std::unique_lock<std::mutex> l(m_PeersMutex);
|
2015-01-14 02:31:39 +00:00
|
|
|
m_Peers.erase (it);
|
2017-05-29 05:28:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-09 02:06:40 +00:00
|
|
|
void Transports::DetectExternalIP ()
|
|
|
|
{
|
2016-10-28 16:50:26 +00:00
|
|
|
if (RoutesRestricted())
|
2018-01-06 04:01:44 +00:00
|
|
|
{
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint(eLogInfo, "Transports: Restricted routes enabled, not detecting IP");
|
2016-10-28 17:11:50 +00:00
|
|
|
i2p::context.SetStatus (eRouterStatusOK);
|
2016-10-28 16:50:26 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-11-23 00:29:20 +00:00
|
|
|
if (m_SSU2Server)
|
2021-03-23 19:36:57 +00:00
|
|
|
PeerTest ();
|
2015-02-26 19:17:16 +00:00
|
|
|
else
|
2022-07-13 23:56:55 +00:00
|
|
|
LogPrint (eLogWarning, "Transports: Can't detect external IP. SSU or SSU2 is not available");
|
2014-02-09 02:06:40 +00:00
|
|
|
}
|
2015-11-03 14:15:49 +00:00
|
|
|
|
2021-05-04 18:59:25 +00:00
|
|
|
void Transports::PeerTest (bool ipv4, bool ipv6)
|
2015-11-03 14:15:49 +00:00
|
|
|
{
|
2022-11-23 00:29:20 +00:00
|
|
|
if (RoutesRestricted() || !m_SSU2Server || m_SSU2Server->UsesProxy ()) return;
|
2021-05-04 18:59:25 +00:00
|
|
|
if (ipv4 && i2p::context.SupportsV4 ())
|
2017-05-29 05:28:16 +00:00
|
|
|
{
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint (eLogInfo, "Transports: Started peer test IPv4");
|
2021-04-17 18:33:53 +00:00
|
|
|
std::set<i2p::data::IdentHash> excluded;
|
2022-02-19 13:15:49 +00:00
|
|
|
excluded.insert (i2p::context.GetIdentHash ()); // don't pick own router
|
2023-11-10 02:56:32 +00:00
|
|
|
int testDelay = 0;
|
2022-11-23 00:29:20 +00:00
|
|
|
for (int i = 0; i < 5; i++)
|
2022-06-02 01:51:02 +00:00
|
|
|
{
|
2022-11-23 00:29:20 +00:00
|
|
|
auto router = i2p::data::netdb.GetRandomSSU2PeerTestRouter (true, excluded); // v4
|
|
|
|
if (router)
|
2022-07-27 10:38:28 +00:00
|
|
|
{
|
2023-07-30 12:29:10 +00:00
|
|
|
if (!i2p::context.GetTesting ())
|
2023-11-10 02:56:32 +00:00
|
|
|
{
|
2023-07-30 12:29:10 +00:00
|
|
|
i2p::context.SetTesting (true);
|
2023-11-10 02:56:32 +00:00
|
|
|
// send first peer test immediately
|
|
|
|
m_SSU2Server->StartPeerTest (router, true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
testDelay += PEER_TEST_DELAY_INTERVAL + rand() % PEER_TEST_DELAY_INTERVAL_VARIANCE;
|
|
|
|
if (m_Service)
|
|
|
|
{
|
|
|
|
auto delayTimer = std::make_shared<boost::asio::deadline_timer>(*m_Service);
|
|
|
|
delayTimer->expires_from_now (boost::posix_time::milliseconds (testDelay));
|
|
|
|
delayTimer->async_wait (
|
|
|
|
[this, router, delayTimer](const boost::system::error_code& ecode)
|
|
|
|
{
|
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
|
|
|
m_SSU2Server->StartPeerTest (router, true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2022-11-23 00:29:20 +00:00
|
|
|
excluded.insert (router->GetIdentHash ());
|
2022-07-27 10:38:28 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-23 00:29:20 +00:00
|
|
|
if (excluded.size () <= 1)
|
|
|
|
LogPrint (eLogWarning, "Transports: Can't find routers for peer test IPv4");
|
2015-11-03 14:15:49 +00:00
|
|
|
}
|
2021-05-04 18:59:25 +00:00
|
|
|
if (ipv6 && i2p::context.SupportsV6 ())
|
2021-03-23 19:36:57 +00:00
|
|
|
{
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint (eLogInfo, "Transports: Started peer test IPv6");
|
2021-04-17 18:33:53 +00:00
|
|
|
std::set<i2p::data::IdentHash> excluded;
|
2022-02-19 13:15:49 +00:00
|
|
|
excluded.insert (i2p::context.GetIdentHash ()); // don't pick own router
|
2023-11-10 02:56:32 +00:00
|
|
|
int testDelay = 0;
|
2022-11-23 00:29:20 +00:00
|
|
|
for (int i = 0; i < 5; i++)
|
2022-07-27 10:38:28 +00:00
|
|
|
{
|
2022-11-23 00:29:20 +00:00
|
|
|
auto router = i2p::data::netdb.GetRandomSSU2PeerTestRouter (false, excluded); // v6
|
|
|
|
if (router)
|
2021-03-23 19:36:57 +00:00
|
|
|
{
|
2023-07-30 12:29:10 +00:00
|
|
|
if (!i2p::context.GetTestingV6 ())
|
2023-11-10 02:56:32 +00:00
|
|
|
{
|
|
|
|
i2p::context.SetTestingV6 (true);
|
|
|
|
// send first peer test immediately
|
|
|
|
m_SSU2Server->StartPeerTest (router, false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
testDelay += PEER_TEST_DELAY_INTERVAL + rand() % PEER_TEST_DELAY_INTERVAL_VARIANCE;
|
|
|
|
if (m_Service)
|
|
|
|
{
|
|
|
|
auto delayTimer = std::make_shared<boost::asio::deadline_timer>(*m_Service);
|
|
|
|
delayTimer->expires_from_now (boost::posix_time::milliseconds (testDelay));
|
|
|
|
delayTimer->async_wait (
|
|
|
|
[this, router, delayTimer](const boost::system::error_code& ecode)
|
|
|
|
{
|
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
|
|
|
m_SSU2Server->StartPeerTest (router, false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2022-11-23 00:29:20 +00:00
|
|
|
excluded.insert (router->GetIdentHash ());
|
2022-07-27 10:38:28 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-23 00:29:20 +00:00
|
|
|
if (excluded.size () <= 1)
|
|
|
|
LogPrint (eLogWarning, "Transports: Can't find routers for peer test IPv6");
|
2021-11-27 20:30:35 +00:00
|
|
|
}
|
2017-05-29 05:28:16 +00:00
|
|
|
}
|
|
|
|
|
2020-06-30 00:02:09 +00:00
|
|
|
std::shared_ptr<i2p::crypto::X25519Keys> Transports::GetNextX25519KeysPair ()
|
|
|
|
{
|
|
|
|
return m_X25519KeysPairSupplier.Acquire ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Transports::ReuseX25519KeysPair (std::shared_ptr<i2p::crypto::X25519Keys> pair)
|
|
|
|
{
|
|
|
|
m_X25519KeysPairSupplier.Return (pair);
|
|
|
|
}
|
2020-10-12 14:36:44 +00:00
|
|
|
|
2015-01-13 03:53:35 +00:00
|
|
|
void Transports::PeerConnected (std::shared_ptr<TransportSession> session)
|
|
|
|
{
|
2016-12-22 18:32:06 +00:00
|
|
|
m_Service->post([session, this]()
|
2017-05-29 05:28:16 +00:00
|
|
|
{
|
|
|
|
auto remoteIdentity = session->GetRemoteIdentity ();
|
2016-02-01 19:19:54 +00:00
|
|
|
if (!remoteIdentity) return;
|
|
|
|
auto ident = remoteIdentity->GetIdentHash ();
|
2016-11-01 14:26:40 +00:00
|
|
|
auto it = m_Peers.find (ident);
|
2015-01-13 03:53:35 +00:00
|
|
|
if (it != m_Peers.end ())
|
|
|
|
{
|
2023-04-18 18:35:13 +00:00
|
|
|
if (it->second.numAttempts > 1)
|
|
|
|
{
|
|
|
|
// exclude failed transports
|
|
|
|
i2p::data::RouterInfo::CompatibleTransports transports = 0;
|
|
|
|
int numExcluded = it->second.numAttempts - 1;
|
|
|
|
if (numExcluded > (int)it->second.priority.size ()) numExcluded = it->second.priority.size ();
|
|
|
|
for (int i = 0; i < numExcluded; i++)
|
|
|
|
transports |= it->second.priority[i];
|
|
|
|
i2p::data::netdb.ExcludeReachableTransports (ident, transports);
|
2023-05-06 20:43:09 +00:00
|
|
|
}
|
2023-06-11 10:48:47 +00:00
|
|
|
if (it->second.router && it->second.numAttempts)
|
2023-05-06 20:43:09 +00:00
|
|
|
{
|
2023-06-11 10:48:47 +00:00
|
|
|
auto transport = it->second.priority[it->second.numAttempts-1];
|
2023-05-06 20:43:09 +00:00
|
|
|
if (transport == i2p::data::RouterInfo::eNTCP2V4 ||
|
|
|
|
transport == i2p::data::RouterInfo::eNTCP2V6 || transport == i2p::data::RouterInfo::eNTCP2V6Mesh)
|
|
|
|
it->second.router->GetProfile ()->Connected (); // outgoing NTCP2 connection if always real
|
2023-07-17 22:44:51 +00:00
|
|
|
i2p::data::netdb.SetUnreachable (ident, false); // clear unreachable
|
2023-04-18 18:35:13 +00:00
|
|
|
}
|
|
|
|
it->second.numAttempts = 0;
|
2020-06-24 15:29:54 +00:00
|
|
|
it->second.router = nullptr; // we don't need RouterInfo after successive connect
|
2016-03-19 02:53:03 +00:00
|
|
|
bool sendDatabaseStore = true;
|
|
|
|
if (it->second.delayedMessages.size () > 0)
|
|
|
|
{
|
|
|
|
// check if first message is our DatabaseStore (publishing)
|
|
|
|
auto firstMsg = it->second.delayedMessages[0];
|
|
|
|
if (firstMsg && firstMsg->GetTypeID () == eI2NPDatabaseStore &&
|
2016-06-13 15:34:44 +00:00
|
|
|
i2p::data::IdentHash(firstMsg->GetPayload () + DATABASE_STORE_KEY_OFFSET) == i2p::context.GetIdentHash ())
|
2016-03-19 02:53:03 +00:00
|
|
|
sendDatabaseStore = false; // we have it in the list already
|
2017-05-29 05:28:16 +00:00
|
|
|
}
|
2016-03-19 02:53:03 +00:00
|
|
|
if (sendDatabaseStore)
|
2018-08-14 15:27:27 +00:00
|
|
|
session->SendLocalRouterInfo ();
|
2016-08-04 14:26:50 +00:00
|
|
|
else
|
|
|
|
session->SetTerminationTimeout (10); // most likely it's publishing, no follow-up messages expected, set timeout to 10 seconds
|
2015-06-09 15:00:37 +00:00
|
|
|
it->second.sessions.push_back (session);
|
|
|
|
session->SendI2NPMessages (it->second.delayedMessages);
|
|
|
|
it->second.delayedMessages.clear ();
|
2015-01-13 03:53:35 +00:00
|
|
|
}
|
2023-04-19 23:48:09 +00:00
|
|
|
else // incoming connection or peer test
|
2016-01-15 21:23:03 +00:00
|
|
|
{
|
2016-10-28 16:50:26 +00:00
|
|
|
if(RoutesRestricted() && ! IsRestrictedPeer(ident)) {
|
|
|
|
// not trusted
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint(eLogWarning, "Transports: Closing untrusted inbound connection from ", ident.ToBase64());
|
2016-10-28 16:50:26 +00:00
|
|
|
session->Done();
|
|
|
|
return;
|
|
|
|
}
|
2023-04-19 23:48:09 +00:00
|
|
|
if (!session->IsOutgoing ()) // incoming
|
|
|
|
session->SendI2NPMessages ({ CreateDatabaseStoreMsg () }); // send DatabaseStore
|
|
|
|
auto r = i2p::data::netdb.FindRouter (ident); // router should be in netdb after SessionConfirmed
|
2023-05-06 20:43:09 +00:00
|
|
|
if (r) r->GetProfile ()->Connected ();
|
2022-06-19 18:21:35 +00:00
|
|
|
auto ts = i2p::util::GetSecondsSinceEpoch ();
|
|
|
|
std::unique_lock<std::mutex> l(m_PeersMutex);
|
2023-04-19 23:48:09 +00:00
|
|
|
auto it = m_Peers.insert (std::make_pair (ident, Peer{ r, ts })).first;
|
2022-12-28 21:47:50 +00:00
|
|
|
it->second.sessions.push_back (session);
|
2023-04-19 23:48:09 +00:00
|
|
|
it->second.router = nullptr;
|
2016-01-15 21:23:03 +00:00
|
|
|
}
|
2016-11-01 14:26:40 +00:00
|
|
|
});
|
2015-01-13 03:53:35 +00:00
|
|
|
}
|
2017-05-29 05:28:16 +00:00
|
|
|
|
2015-01-13 03:53:35 +00:00
|
|
|
void Transports::PeerDisconnected (std::shared_ptr<TransportSession> session)
|
|
|
|
{
|
2016-12-22 18:32:06 +00:00
|
|
|
m_Service->post([session, this]()
|
2016-11-01 14:26:40 +00:00
|
|
|
{
|
2017-05-29 05:28:16 +00:00
|
|
|
auto remoteIdentity = session->GetRemoteIdentity ();
|
2016-02-01 19:19:54 +00:00
|
|
|
if (!remoteIdentity) return;
|
|
|
|
auto ident = remoteIdentity->GetIdentHash ();
|
2015-01-13 03:53:35 +00:00
|
|
|
auto it = m_Peers.find (ident);
|
2015-06-09 15:00:37 +00:00
|
|
|
if (it != m_Peers.end ())
|
2015-01-14 02:31:39 +00:00
|
|
|
{
|
2024-01-19 00:18:51 +00:00
|
|
|
bool wasConnected = it->second.IsConnected ();
|
2015-06-09 15:00:37 +00:00
|
|
|
it->second.sessions.remove (session);
|
2024-01-19 00:18:51 +00:00
|
|
|
if (!it->second.IsConnected ())
|
2017-05-29 05:28:16 +00:00
|
|
|
{
|
2015-06-09 15:00:37 +00:00
|
|
|
if (it->second.delayedMessages.size () > 0)
|
2019-11-12 11:46:08 +00:00
|
|
|
{
|
2024-01-19 00:18:51 +00:00
|
|
|
if (wasConnected) // we had an active session before
|
2019-11-12 14:38:22 +00:00
|
|
|
it->second.numAttempts = 0; // start over
|
2015-06-09 15:00:37 +00:00
|
|
|
ConnectToPeer (ident, it->second);
|
2020-03-01 14:35:24 +00:00
|
|
|
}
|
2015-06-09 15:00:37 +00:00
|
|
|
else
|
2016-01-15 21:23:03 +00:00
|
|
|
{
|
2017-05-29 05:28:16 +00:00
|
|
|
std::unique_lock<std::mutex> l(m_PeersMutex);
|
2015-06-09 15:00:37 +00:00
|
|
|
m_Peers.erase (it);
|
2016-01-15 21:23:03 +00:00
|
|
|
}
|
2015-06-09 15:00:37 +00:00
|
|
|
}
|
2015-01-14 02:31:39 +00:00
|
|
|
}
|
2017-05-29 05:28:16 +00:00
|
|
|
});
|
|
|
|
}
|
2015-02-11 19:45:25 +00:00
|
|
|
|
2015-03-18 00:56:51 +00:00
|
|
|
bool Transports::IsConnected (const i2p::data::IdentHash& ident) const
|
2017-05-29 05:28:16 +00:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_PeersMutex);
|
2015-03-18 00:56:51 +00:00
|
|
|
auto it = m_Peers.find (ident);
|
|
|
|
return it != m_Peers.end ();
|
2017-05-29 05:28:16 +00:00
|
|
|
}
|
|
|
|
|
2015-02-11 19:45:25 +00:00
|
|
|
void Transports::HandlePeerCleanupTimer (const boost::system::error_code& ecode)
|
|
|
|
{
|
2015-02-12 03:48:26 +00:00
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
2015-02-11 19:45:25 +00:00
|
|
|
{
|
2015-02-12 03:48:26 +00:00
|
|
|
auto ts = i2p::util::GetSecondsSinceEpoch ();
|
|
|
|
for (auto it = m_Peers.begin (); it != m_Peers.end (); )
|
2015-02-11 19:45:25 +00:00
|
|
|
{
|
2022-12-11 00:09:37 +00:00
|
|
|
it->second.sessions.remove_if (
|
|
|
|
[](std::shared_ptr<TransportSession> session)->bool
|
2022-12-12 05:12:23 +00:00
|
|
|
{
|
2022-12-11 00:09:37 +00:00
|
|
|
return !session || !session->IsEstablished ();
|
|
|
|
});
|
2024-01-19 00:18:51 +00:00
|
|
|
if (!it->second.IsConnected () && ts > it->second.creationTime + SESSION_CREATION_TIMEOUT)
|
2015-02-12 03:48:26 +00:00
|
|
|
{
|
2015-12-18 12:57:22 +00:00
|
|
|
LogPrint (eLogWarning, "Transports: Session to peer ", it->first.ToBase64 (), " has not been created in ", SESSION_CREATION_TIMEOUT, " seconds");
|
2023-04-20 18:23:41 +00:00
|
|
|
/* if (!it->second.router)
|
2023-04-19 00:21:22 +00:00
|
|
|
{
|
|
|
|
// if router for ident not found mark it unreachable
|
|
|
|
auto profile = i2p::data::GetRouterProfile (it->first);
|
|
|
|
if (profile) profile->Unreachable ();
|
2023-04-20 18:23:41 +00:00
|
|
|
} */
|
2020-03-01 10:25:50 +00:00
|
|
|
std::unique_lock<std::mutex> l(m_PeersMutex);
|
2015-02-12 03:48:26 +00:00
|
|
|
it = m_Peers.erase (it);
|
|
|
|
}
|
|
|
|
else
|
2022-06-19 18:21:35 +00:00
|
|
|
{
|
|
|
|
if (ts > it->second.nextRouterInfoUpdateTime)
|
|
|
|
{
|
|
|
|
auto session = it->second.sessions.front ();
|
|
|
|
if (session)
|
|
|
|
session->SendLocalRouterInfo (true);
|
2022-10-09 17:24:43 +00:00
|
|
|
it->second.nextRouterInfoUpdateTime = ts + PEER_ROUTER_INFO_UPDATE_INTERVAL +
|
2022-06-19 18:21:35 +00:00
|
|
|
rand () % PEER_ROUTER_INFO_UPDATE_INTERVAL_VARIANCE;
|
2022-07-27 10:38:28 +00:00
|
|
|
}
|
2016-08-08 22:53:37 +00:00
|
|
|
++it;
|
2022-07-27 10:38:28 +00:00
|
|
|
}
|
2015-02-11 19:45:25 +00:00
|
|
|
}
|
2023-07-30 12:29:10 +00:00
|
|
|
bool ipv4Testing = i2p::context.GetTesting ();
|
2023-09-28 22:40:51 +00:00
|
|
|
if (!ipv4Testing)
|
|
|
|
ipv4Testing = i2p::context.GetRouterInfo ().IsSSU2V4 () && (i2p::context.GetStatus() == eRouterStatusUnknown);
|
2023-07-30 12:29:10 +00:00
|
|
|
bool ipv6Testing = i2p::context.GetTestingV6 ();
|
2023-09-28 22:40:51 +00:00
|
|
|
if (!ipv6Testing)
|
|
|
|
ipv6Testing = i2p::context.GetRouterInfo ().IsSSU2V6 () && (i2p::context.GetStatusV6() == eRouterStatusUnknown);
|
|
|
|
// if still testing or unknown, repeat peer test
|
2021-05-04 18:59:25 +00:00
|
|
|
if (ipv4Testing || ipv6Testing)
|
|
|
|
PeerTest (ipv4Testing, ipv6Testing);
|
2022-12-12 05:12:23 +00:00
|
|
|
m_PeerCleanupTimer->expires_from_now (boost::posix_time::seconds(3 * SESSION_CREATION_TIMEOUT));
|
2016-12-22 18:32:06 +00:00
|
|
|
m_PeerCleanupTimer->async_wait (std::bind (&Transports::HandlePeerCleanupTimer, this, std::placeholders::_1));
|
2017-05-29 05:28:16 +00:00
|
|
|
}
|
2015-02-11 19:45:25 +00:00
|
|
|
}
|
2015-05-05 14:33:19 +00:00
|
|
|
|
2016-11-13 14:14:05 +00:00
|
|
|
void Transports::HandlePeerTestTimer (const boost::system::error_code& ecode)
|
|
|
|
{
|
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
|
|
|
{
|
|
|
|
PeerTest ();
|
2016-12-22 18:32:06 +00:00
|
|
|
m_PeerTestTimer->expires_from_now (boost::posix_time::minutes(PEER_TEST_INTERVAL));
|
|
|
|
m_PeerTestTimer->async_wait (std::bind (&Transports::HandlePeerTestTimer, this, std::placeholders::_1));
|
2017-05-29 05:28:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-01 21:04:09 +00:00
|
|
|
template<typename Filter>
|
|
|
|
std::shared_ptr<const i2p::data::RouterInfo> Transports::GetRandomPeer (Filter filter) const
|
2015-05-05 14:33:19 +00:00
|
|
|
{
|
2023-02-01 21:04:09 +00:00
|
|
|
if (m_Peers.empty()) return nullptr;
|
|
|
|
bool found = false;
|
2021-03-16 19:23:00 +00:00
|
|
|
i2p::data::IdentHash ident;
|
|
|
|
{
|
2023-02-01 21:04:09 +00:00
|
|
|
uint16_t inds[3];
|
|
|
|
RAND_bytes ((uint8_t *)inds, sizeof (inds));
|
2021-03-16 19:23:00 +00:00
|
|
|
std::unique_lock<std::mutex> l(m_PeersMutex);
|
2023-07-09 00:07:04 +00:00
|
|
|
auto count = m_Peers.size ();
|
|
|
|
if(count == 0) return nullptr;
|
|
|
|
inds[0] %= count;
|
2021-03-16 19:23:00 +00:00
|
|
|
auto it = m_Peers.begin ();
|
2023-02-01 21:04:09 +00:00
|
|
|
std::advance (it, inds[0]);
|
|
|
|
// try random peer
|
|
|
|
if (it != m_Peers.end () && filter (it->second))
|
2023-02-11 06:41:51 +00:00
|
|
|
{
|
2023-02-01 21:04:09 +00:00
|
|
|
ident = it->first;
|
|
|
|
found = true;
|
2023-02-11 06:41:51 +00:00
|
|
|
}
|
2023-02-01 21:04:09 +00:00
|
|
|
else
|
2023-02-11 06:41:51 +00:00
|
|
|
{
|
2023-02-01 21:04:09 +00:00
|
|
|
// try some peers around
|
|
|
|
auto it1 = m_Peers.begin ();
|
|
|
|
if (inds[0])
|
|
|
|
{
|
|
|
|
// before
|
|
|
|
inds[1] %= inds[0];
|
|
|
|
std::advance (it1, (inds[1] + inds[0])/2);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
it1 = it;
|
|
|
|
auto it2 = it;
|
|
|
|
if (inds[0] < m_Peers.size () - 1)
|
|
|
|
{
|
|
|
|
// after
|
|
|
|
inds[2] %= (m_Peers.size () - 1 - inds[0]); inds[2] /= 2;
|
|
|
|
std::advance (it2, inds[2]);
|
|
|
|
}
|
|
|
|
// it1 - from, it2 - to
|
|
|
|
it = it1;
|
|
|
|
while (it != it2 && it != m_Peers.end ())
|
|
|
|
{
|
|
|
|
if (filter (it->second))
|
2023-02-11 06:41:51 +00:00
|
|
|
{
|
2023-02-01 21:04:09 +00:00
|
|
|
ident = it->first;
|
|
|
|
found = true;
|
|
|
|
break;
|
2023-02-11 06:41:51 +00:00
|
|
|
}
|
2023-02-01 21:04:09 +00:00
|
|
|
it++;
|
|
|
|
}
|
|
|
|
if (!found)
|
2023-02-11 06:41:51 +00:00
|
|
|
{
|
2023-02-01 21:04:09 +00:00
|
|
|
// still not found, try from the beginning
|
|
|
|
it = m_Peers.begin ();
|
|
|
|
while (it != it1 && it != m_Peers.end ())
|
|
|
|
{
|
|
|
|
if (filter (it->second))
|
2023-02-11 06:41:51 +00:00
|
|
|
{
|
2023-02-01 21:04:09 +00:00
|
|
|
ident = it->first;
|
|
|
|
found = true;
|
|
|
|
break;
|
2023-02-11 06:41:51 +00:00
|
|
|
}
|
2023-02-01 21:04:09 +00:00
|
|
|
it++;
|
|
|
|
}
|
|
|
|
if (!found)
|
2023-02-11 06:41:51 +00:00
|
|
|
{
|
2023-02-01 21:04:09 +00:00
|
|
|
// still not found, try to the beginning
|
|
|
|
it = it2;
|
|
|
|
while (it != m_Peers.end ())
|
|
|
|
{
|
|
|
|
if (filter (it->second))
|
2023-02-11 06:41:51 +00:00
|
|
|
{
|
2023-02-01 21:04:09 +00:00
|
|
|
ident = it->first;
|
|
|
|
found = true;
|
|
|
|
break;
|
2023-02-11 06:41:51 +00:00
|
|
|
}
|
2023-02-01 21:04:09 +00:00
|
|
|
it++;
|
|
|
|
}
|
2023-02-11 06:41:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-01 21:04:09 +00:00
|
|
|
return found ? i2p::data::netdb.FindRouter (ident) : nullptr;
|
|
|
|
}
|
2023-02-11 06:41:51 +00:00
|
|
|
|
2023-02-03 20:59:56 +00:00
|
|
|
std::shared_ptr<const i2p::data::RouterInfo> Transports::GetRandomPeer (bool isHighBandwidth) const
|
2023-02-01 21:04:09 +00:00
|
|
|
{
|
|
|
|
return GetRandomPeer (
|
2023-02-03 20:59:56 +00:00
|
|
|
[isHighBandwidth](const Peer& peer)->bool
|
2023-02-01 21:04:09 +00:00
|
|
|
{
|
2023-03-29 19:54:53 +00:00
|
|
|
// connected, not overloaded and not slow
|
2024-01-19 00:18:51 +00:00
|
|
|
return !peer.router && peer.IsConnected () && peer.isReachable &&
|
2023-02-03 20:59:56 +00:00
|
|
|
peer.sessions.front ()->GetSendQueueSize () <= PEER_ROUTER_INFO_OVERLOAD_QUEUE_SIZE &&
|
2023-10-15 12:31:55 +00:00
|
|
|
!peer.sessions.front ()->IsSlow () && !peer.sessions.front ()->IsBandwidthExceeded (peer.isHighBandwidth) &&
|
2023-02-03 20:59:56 +00:00
|
|
|
(!isHighBandwidth || peer.isHighBandwidth);
|
2023-02-01 21:04:09 +00:00
|
|
|
});
|
2015-05-05 14:33:19 +00:00
|
|
|
}
|
2022-05-20 16:56:05 +00:00
|
|
|
|
2022-03-24 19:50:20 +00:00
|
|
|
void Transports::RestrictRoutesToFamilies(const std::set<std::string>& families)
|
2016-11-01 14:26:40 +00:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(m_FamilyMutex);
|
|
|
|
m_TrustedFamilies.clear();
|
2022-03-24 19:50:20 +00:00
|
|
|
for (auto fam : families)
|
2022-05-20 16:56:05 +00:00
|
|
|
{
|
2022-03-24 19:50:20 +00:00
|
|
|
boost::to_lower (fam);
|
|
|
|
auto id = i2p::data::netdb.GetFamilies ().GetFamilyID (fam);
|
2022-05-20 16:56:05 +00:00
|
|
|
if (id)
|
2022-03-24 19:50:20 +00:00
|
|
|
m_TrustedFamilies.push_back (id);
|
2022-05-20 16:56:05 +00:00
|
|
|
}
|
2016-11-01 14:26:40 +00:00
|
|
|
}
|
2016-06-17 15:03:33 +00:00
|
|
|
|
2016-10-28 16:50:26 +00:00
|
|
|
void Transports::RestrictRoutesToRouters(std::set<i2p::data::IdentHash> routers)
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(m_TrustedRoutersMutex);
|
|
|
|
m_TrustedRouters.clear();
|
|
|
|
for (const auto & ri : routers )
|
|
|
|
m_TrustedRouters.push_back(ri);
|
|
|
|
}
|
2017-05-29 05:28:16 +00:00
|
|
|
|
2016-11-01 14:26:40 +00:00
|
|
|
bool Transports::RoutesRestricted() const {
|
|
|
|
std::unique_lock<std::mutex> famlock(m_FamilyMutex);
|
2016-10-28 16:50:26 +00:00
|
|
|
std::unique_lock<std::mutex> routerslock(m_TrustedRoutersMutex);
|
2016-11-01 14:26:40 +00:00
|
|
|
return m_TrustedFamilies.size() > 0 || m_TrustedRouters.size() > 0;
|
|
|
|
}
|
2016-06-17 15:03:33 +00:00
|
|
|
|
2016-11-01 14:26:40 +00:00
|
|
|
/** XXX: if routes are not restricted this dies */
|
|
|
|
std::shared_ptr<const i2p::data::RouterInfo> Transports::GetRestrictedPeer() const
|
2016-10-28 16:50:26 +00:00
|
|
|
{
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> l(m_FamilyMutex);
|
2022-03-24 19:50:20 +00:00
|
|
|
i2p::data::FamilyID fam = 0;
|
2016-10-28 16:50:26 +00:00
|
|
|
auto sz = m_TrustedFamilies.size();
|
|
|
|
if(sz > 1)
|
|
|
|
{
|
|
|
|
auto it = m_TrustedFamilies.begin ();
|
|
|
|
std::advance(it, rand() % sz);
|
|
|
|
fam = *it;
|
|
|
|
}
|
|
|
|
else if (sz == 1)
|
|
|
|
{
|
|
|
|
fam = m_TrustedFamilies[0];
|
|
|
|
}
|
2022-03-24 19:50:20 +00:00
|
|
|
if (fam)
|
2016-10-28 16:50:26 +00:00
|
|
|
return i2p::data::netdb.GetRandomRouterInFamily(fam);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_TrustedRoutersMutex);
|
|
|
|
auto sz = m_TrustedRouters.size();
|
|
|
|
if (sz)
|
|
|
|
{
|
|
|
|
if(sz == 1)
|
|
|
|
return i2p::data::netdb.FindRouter(m_TrustedRouters[0]);
|
|
|
|
auto it = m_TrustedRouters.begin();
|
|
|
|
std::advance(it, rand() % sz);
|
|
|
|
return i2p::data::netdb.FindRouter(*it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
2016-11-01 14:26:40 +00:00
|
|
|
}
|
2016-10-28 16:50:26 +00:00
|
|
|
|
|
|
|
bool Transports::IsRestrictedPeer(const i2p::data::IdentHash & ih) const
|
|
|
|
{
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_TrustedRoutersMutex);
|
|
|
|
for (const auto & r : m_TrustedRouters )
|
|
|
|
if ( r == ih ) return true;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_FamilyMutex);
|
|
|
|
auto ri = i2p::data::netdb.FindRouter(ih);
|
|
|
|
for (const auto & fam : m_TrustedFamilies)
|
|
|
|
if(ri->IsFamily(fam)) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2020-10-11 21:51:40 +00:00
|
|
|
|
2020-10-12 14:36:44 +00:00
|
|
|
void Transports::SetOnline (bool online)
|
|
|
|
{
|
2020-10-11 21:51:40 +00:00
|
|
|
if (m_IsOnline != online)
|
2020-10-12 14:36:44 +00:00
|
|
|
{
|
|
|
|
m_IsOnline = online;
|
2020-10-11 21:51:40 +00:00
|
|
|
if (online)
|
|
|
|
PeerTest ();
|
|
|
|
else
|
|
|
|
i2p::context.SetError (eRouterErrorOffline);
|
2020-10-12 14:36:44 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-24 01:27:11 +00:00
|
|
|
|
2024-01-11 20:39:42 +00:00
|
|
|
bool Transports::IsInReservedRange (const boost::asio::ip::address& host) const
|
|
|
|
{
|
|
|
|
return IsCheckReserved () && i2p::util::net::IsInReservedRange (host);
|
|
|
|
}
|
|
|
|
|
2022-10-10 12:43:37 +00:00
|
|
|
void InitAddressFromIface ()
|
2022-09-24 01:27:11 +00:00
|
|
|
{
|
|
|
|
bool ipv6; i2p::config::GetOption("ipv6", ipv6);
|
|
|
|
bool ipv4; i2p::config::GetOption("ipv4", ipv4);
|
|
|
|
|
|
|
|
// ifname -> address
|
|
|
|
std::string ifname; i2p::config::GetOption("ifname", ifname);
|
|
|
|
if (ipv4 && i2p::config::IsDefault ("address4"))
|
|
|
|
{
|
|
|
|
std::string ifname4; i2p::config::GetOption("ifname4", ifname4);
|
|
|
|
if (!ifname4.empty ())
|
|
|
|
i2p::config::SetOption ("address4", i2p::util::net::GetInterfaceAddress(ifname4, false).to_string ()); // v4
|
|
|
|
else if (!ifname.empty ())
|
|
|
|
i2p::config::SetOption ("address4", i2p::util::net::GetInterfaceAddress(ifname, false).to_string ()); // v4
|
|
|
|
}
|
|
|
|
if (ipv6 && i2p::config::IsDefault ("address6"))
|
|
|
|
{
|
|
|
|
std::string ifname6; i2p::config::GetOption("ifname6", ifname6);
|
|
|
|
if (!ifname6.empty ())
|
|
|
|
i2p::config::SetOption ("address6", i2p::util::net::GetInterfaceAddress(ifname6, true).to_string ()); // v6
|
|
|
|
else if (!ifname.empty ())
|
|
|
|
i2p::config::SetOption ("address6", i2p::util::net::GetInterfaceAddress(ifname, true).to_string ()); // v6
|
|
|
|
}
|
2022-10-10 12:43:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InitTransports ()
|
|
|
|
{
|
|
|
|
bool ipv6; i2p::config::GetOption("ipv6", ipv6);
|
|
|
|
bool ipv4; i2p::config::GetOption("ipv4", ipv4);
|
|
|
|
bool ygg; i2p::config::GetOption("meshnets.yggdrasil", ygg);
|
|
|
|
uint16_t port; i2p::config::GetOption("port", port);
|
2022-09-24 01:27:11 +00:00
|
|
|
|
|
|
|
boost::asio::ip::address_v6 yggaddr;
|
|
|
|
if (ygg)
|
|
|
|
{
|
|
|
|
std::string yggaddress; i2p::config::GetOption ("meshnets.yggaddress", yggaddress);
|
|
|
|
if (!yggaddress.empty ())
|
|
|
|
{
|
|
|
|
yggaddr = boost::asio::ip::address_v6::from_string (yggaddress);
|
|
|
|
if (yggaddr.is_unspecified () || !i2p::util::net::IsYggdrasilAddress (yggaddr) ||
|
|
|
|
!i2p::util::net::IsLocalAddress (yggaddr))
|
|
|
|
{
|
|
|
|
LogPrint(eLogWarning, "Transports: Can't find Yggdrasil address ", yggaddress);
|
|
|
|
ygg = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
yggaddr = i2p::util::net::GetYggdrasilAddress ();
|
|
|
|
if (yggaddr.is_unspecified ())
|
|
|
|
{
|
|
|
|
LogPrint(eLogWarning, "Transports: Yggdrasil is not running. Disabled");
|
|
|
|
ygg = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!i2p::config::IsDefault("port"))
|
|
|
|
{
|
|
|
|
LogPrint(eLogInfo, "Transports: Accepting incoming connections at port ", port);
|
|
|
|
i2p::context.UpdatePort (port);
|
|
|
|
}
|
|
|
|
i2p::context.SetSupportsV6 (ipv6);
|
|
|
|
i2p::context.SetSupportsV4 (ipv4);
|
|
|
|
i2p::context.SetSupportsMesh (ygg, yggaddr);
|
|
|
|
|
|
|
|
bool ntcp2; i2p::config::GetOption("ntcp2.enabled", ntcp2);
|
|
|
|
if (ntcp2)
|
|
|
|
{
|
|
|
|
bool published; i2p::config::GetOption("ntcp2.published", published);
|
|
|
|
if (published)
|
|
|
|
{
|
|
|
|
std::string ntcp2proxy; i2p::config::GetOption("ntcp2.proxy", ntcp2proxy);
|
|
|
|
if (!ntcp2proxy.empty ()) published = false;
|
|
|
|
}
|
|
|
|
if (published)
|
|
|
|
{
|
|
|
|
uint16_t ntcp2port; i2p::config::GetOption("ntcp2.port", ntcp2port);
|
|
|
|
if (!ntcp2port) ntcp2port = port; // use standard port
|
|
|
|
i2p::context.PublishNTCP2Address (ntcp2port, true, ipv4, ipv6, false); // publish
|
|
|
|
if (ipv6)
|
|
|
|
{
|
|
|
|
std::string ipv6Addr; i2p::config::GetOption("ntcp2.addressv6", ipv6Addr);
|
|
|
|
auto addr = boost::asio::ip::address_v6::from_string (ipv6Addr);
|
|
|
|
if (!addr.is_unspecified () && addr != boost::asio::ip::address_v6::any ())
|
|
|
|
i2p::context.UpdateNTCP2V6Address (addr); // set ipv6 address if configured
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
i2p::context.PublishNTCP2Address (port, false, ipv4, ipv6, false); // unpublish
|
|
|
|
}
|
|
|
|
if (ygg)
|
|
|
|
{
|
|
|
|
i2p::context.PublishNTCP2Address (port, true, false, false, true);
|
|
|
|
i2p::context.UpdateNTCP2V6Address (yggaddr);
|
|
|
|
if (!ipv4 && !ipv6)
|
|
|
|
i2p::context.SetStatus (eRouterStatusMesh);
|
|
|
|
}
|
|
|
|
bool ssu2; i2p::config::GetOption("ssu2.enabled", ssu2);
|
2022-09-24 20:37:18 +00:00
|
|
|
if (ssu2 && i2p::config::IsDefault ("ssu2.enabled") && !ipv4 && !ipv6)
|
|
|
|
ssu2 = false; // don't enable ssu2 for yggdrasil only router
|
2022-09-24 01:27:11 +00:00
|
|
|
if (ssu2)
|
|
|
|
{
|
|
|
|
uint16_t ssu2port; i2p::config::GetOption("ssu2.port", ssu2port);
|
2022-11-23 20:45:00 +00:00
|
|
|
if (!ssu2port && port) ssu2port = port;
|
2022-09-24 01:27:11 +00:00
|
|
|
bool published; i2p::config::GetOption("ssu2.published", published);
|
|
|
|
if (published)
|
|
|
|
i2p::context.PublishSSU2Address (ssu2port, true, ipv4, ipv6); // publish
|
|
|
|
else
|
|
|
|
i2p::context.PublishSSU2Address (ssu2port, false, ipv4, ipv6); // unpublish
|
|
|
|
}
|
2022-10-09 17:24:43 +00:00
|
|
|
}
|
2013-10-27 15:26:39 +00:00
|
|
|
}
|
2014-10-21 16:25:53 +00:00
|
|
|
}
|