2020-05-22 16:18:41 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013-2020, The PurpleI2P Project
|
|
|
|
*
|
|
|
|
* This file is part of Purple i2pd project and licensed under BSD3
|
|
|
|
*
|
|
|
|
* See full license text in LICENSE file at top of project tree
|
|
|
|
*/
|
|
|
|
|
2013-10-27 19:26:39 +04:00
|
|
|
#include "Log.h"
|
2015-11-03 17:15:49 +03:00
|
|
|
#include "Crypto.h"
|
2013-10-27 19:26:39 +04:00
|
|
|
#include "RouterContext.h"
|
|
|
|
#include "I2NPProtocol.h"
|
2017-04-22 03:04:16 +03:00
|
|
|
#include "NetDb.hpp"
|
2013-10-27 19:26:39 +04:00
|
|
|
#include "Transports.h"
|
2016-10-28 22:57:18 +03:00
|
|
|
#include "Config.h"
|
2017-05-29 08:28:16 +03:00
|
|
|
#include "HTTP.h"
|
2013-10-27 19:26:39 +04:00
|
|
|
|
|
|
|
using namespace i2p::data;
|
|
|
|
|
|
|
|
namespace i2p
|
2014-10-21 20:25:53 +04:00
|
|
|
{
|
|
|
|
namespace transport
|
2013-10-27 19:26:39 +04:00
|
|
|
{
|
2020-06-30 03:02:09 +03:00
|
|
|
template<typename Keys>
|
|
|
|
EphemeralKeysSupplier<Keys>::EphemeralKeysSupplier (int size):
|
2014-10-20 23:19:56 +04:00
|
|
|
m_QueueSize (size), m_IsRunning (false), m_Thread (nullptr)
|
|
|
|
{
|
2017-05-29 08:28:16 +03:00
|
|
|
}
|
2014-10-20 23:19:56 +04:00
|
|
|
|
2020-06-30 03:02:09 +03:00
|
|
|
template<typename Keys>
|
|
|
|
EphemeralKeysSupplier<Keys>::~EphemeralKeysSupplier ()
|
2014-04-05 00:29:40 +04:00
|
|
|
{
|
|
|
|
Stop ();
|
|
|
|
}
|
|
|
|
|
2020-06-30 03:02:09 +03:00
|
|
|
template<typename Keys>
|
|
|
|
void EphemeralKeysSupplier<Keys>::Start ()
|
2014-04-05 00:29:40 +04:00
|
|
|
{
|
|
|
|
m_IsRunning = true;
|
2020-06-30 03:02:09 +03:00
|
|
|
m_Thread = new std::thread (std::bind (&EphemeralKeysSupplier<Keys>::Run, this));
|
2014-04-05 00:29:40 +04:00
|
|
|
}
|
|
|
|
|
2020-06-30 03:02:09 +03:00
|
|
|
template<typename Keys>
|
|
|
|
void EphemeralKeysSupplier<Keys>::Stop ()
|
2014-04-05 00:29:40 +04:00
|
|
|
{
|
2018-11-20 21:57:51 +03:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_AcquiredMutex);
|
|
|
|
m_IsRunning = false;
|
|
|
|
m_Acquired.notify_one ();
|
|
|
|
}
|
2014-04-05 00:29:40 +04:00
|
|
|
if (m_Thread)
|
2017-05-29 08:28:16 +03:00
|
|
|
{
|
|
|
|
m_Thread->join ();
|
2014-04-05 00:29:40 +04:00
|
|
|
delete m_Thread;
|
|
|
|
m_Thread = 0;
|
2017-05-29 08:28:16 +03:00
|
|
|
}
|
2014-04-05 00:29:40 +04:00
|
|
|
}
|
|
|
|
|
2020-06-30 03:02:09 +03:00
|
|
|
template<typename Keys>
|
|
|
|
void EphemeralKeysSupplier<Keys>::Run ()
|
2014-04-05 00:29:40 +04:00
|
|
|
{
|
|
|
|
while (m_IsRunning)
|
|
|
|
{
|
2016-11-07 22:44:32 +03:00
|
|
|
int num, total = 0;
|
2018-11-20 21:57:51 +03:00
|
|
|
while ((num = m_QueueSize - (int)m_Queue.size ()) > 0 && total < 10)
|
2017-05-29 08:28:16 +03:00
|
|
|
{
|
2020-06-30 03:02:09 +03:00
|
|
|
CreateEphemeralKeys (num);
|
2016-11-07 22:44:32 +03:00
|
|
|
total += num;
|
|
|
|
}
|
2018-11-20 21:57:51 +03:00
|
|
|
if (total >= 10)
|
2016-11-07 22:44:32 +03:00
|
|
|
{
|
2020-06-30 03:02:09 +03:00
|
|
|
LogPrint (eLogWarning, "Transports: ", total, " ephemeral keys generated at the time");
|
2016-11-07 22:44:32 +03:00
|
|
|
std::this_thread::sleep_for (std::chrono::seconds(1)); // take a break
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-11-20 21:57:51 +03:00
|
|
|
std::unique_lock<std::mutex> l(m_AcquiredMutex);
|
|
|
|
if (!m_IsRunning) break;
|
2017-12-07 16:26:28 +03:00
|
|
|
m_Acquired.wait (l); // wait for element gets acquired
|
2017-05-29 08:28:16 +03:00
|
|
|
}
|
2014-04-05 00:29:40 +04:00
|
|
|
}
|
2017-05-29 08:28:16 +03:00
|
|
|
}
|
2014-04-05 00:29:40 +04:00
|
|
|
|
2020-06-30 03:02:09 +03:00
|
|
|
template<typename Keys>
|
|
|
|
void EphemeralKeysSupplier<Keys>::CreateEphemeralKeys (int num)
|
2014-04-05 00:29:40 +04:00
|
|
|
{
|
|
|
|
if (num > 0)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < num; i++)
|
|
|
|
{
|
2020-06-30 03:02:09 +03:00
|
|
|
auto pair = std::make_shared<Keys> ();
|
2015-11-03 17:15:49 +03:00
|
|
|
pair->GenerateKeys ();
|
2020-06-30 03:02:09 +03:00
|
|
|
std::unique_lock<std::mutex> l(m_AcquiredMutex);
|
2014-04-05 00:29:40 +04:00
|
|
|
m_Queue.push (pair);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-30 03:02:09 +03:00
|
|
|
template<typename Keys>
|
|
|
|
std::shared_ptr<Keys> EphemeralKeysSupplier<Keys>::Acquire ()
|
2014-04-05 00:29:40 +04:00
|
|
|
{
|
|
|
|
{
|
2020-06-30 03:02:09 +03:00
|
|
|
std::unique_lock<std::mutex> l(m_AcquiredMutex);
|
2016-01-28 06:09:35 +03:00
|
|
|
if (!m_Queue.empty ())
|
|
|
|
{
|
|
|
|
auto pair = m_Queue.front ();
|
|
|
|
m_Queue.pop ();
|
|
|
|
m_Acquired.notify_one ();
|
|
|
|
return pair;
|
2017-05-29 08:28:16 +03:00
|
|
|
}
|
|
|
|
}
|
2016-01-28 06:09:35 +03:00
|
|
|
// queue is empty, create new
|
2020-06-30 03:02:09 +03:00
|
|
|
auto pair = std::make_shared<Keys> ();
|
2016-01-28 06:09:35 +03:00
|
|
|
pair->GenerateKeys ();
|
|
|
|
return pair;
|
2014-04-05 00:29:40 +04:00
|
|
|
}
|
|
|
|
|
2020-06-30 03:02:09 +03:00
|
|
|
template<typename Keys>
|
|
|
|
void EphemeralKeysSupplier<Keys>::Return (std::shared_ptr<Keys> pair)
|
2014-09-17 19:13:25 +04:00
|
|
|
{
|
2017-08-25 21:45:58 +03:00
|
|
|
if (pair)
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex>l(m_AcquiredMutex);
|
|
|
|
if ((int)m_Queue.size () < 2*m_QueueSize)
|
|
|
|
m_Queue.push (pair);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
LogPrint(eLogError, "Transports: return null DHKeys");
|
2014-09-17 19:13:25 +04:00
|
|
|
}
|
|
|
|
|
2017-05-29 08:28:16 +03:00
|
|
|
Transports transports;
|
|
|
|
|
|
|
|
Transports::Transports ():
|
2017-08-09 17:52:52 +03:00
|
|
|
m_IsOnline (true), m_IsRunning (false), m_IsNAT (true), m_Thread (nullptr), m_Service (nullptr),
|
2016-12-22 21:32:06 +03:00
|
|
|
m_Work (nullptr), m_PeerCleanupTimer (nullptr), m_PeerTestTimer (nullptr),
|
2018-07-13 22:59:28 +03:00
|
|
|
m_NTCPServer (nullptr), m_SSUServer (nullptr), m_NTCP2Server (nullptr),
|
2020-06-30 03:02:09 +03:00
|
|
|
m_DHKeysPairSupplier (5), m_X25519KeysPairSupplier (5), // 5 pre-generated keys
|
2017-05-02 21:20:00 +03:00
|
|
|
m_TotalSentBytes(0), m_TotalReceivedBytes(0), m_TotalTransitTransmittedBytes (0),
|
2018-01-06 07:01:44 +03:00
|
|
|
m_InBandwidth (0), m_OutBandwidth (0), m_TransitBandwidth(0),
|
2017-05-02 21:20:00 +03:00
|
|
|
m_LastInBandwidthUpdateBytes (0), m_LastOutBandwidthUpdateBytes (0),
|
2017-05-29 08:28:16 +03:00
|
|
|
m_LastTransitBandwidthUpdateBytes (0), m_LastBandwidthUpdateTime (0)
|
|
|
|
{
|
2013-10-27 19:26:39 +04:00
|
|
|
}
|
2017-05-29 08:28:16 +03:00
|
|
|
|
|
|
|
Transports::~Transports ()
|
|
|
|
{
|
2013-10-27 19:26:39 +04:00
|
|
|
Stop ();
|
2016-12-22 21:32:06 +03:00
|
|
|
if (m_Service)
|
|
|
|
{
|
|
|
|
delete m_PeerCleanupTimer; m_PeerCleanupTimer = nullptr;
|
|
|
|
delete m_PeerTestTimer; m_PeerTestTimer = nullptr;
|
|
|
|
delete m_Work; m_Work = nullptr;
|
|
|
|
delete m_Service; m_Service = nullptr;
|
2017-05-29 08:28:16 +03:00
|
|
|
}
|
|
|
|
}
|
2013-10-27 19:26:39 +04:00
|
|
|
|
2016-06-17 16:02:12 +03:00
|
|
|
void Transports::Start (bool enableNTCP, bool enableSSU)
|
2013-10-27 19:26:39 +04:00
|
|
|
{
|
2016-12-22 21:32:06 +03: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 07:01:44 +03:00
|
|
|
m_PeerTestTimer = new boost::asio::deadline_timer (*m_Service);
|
2016-12-22 21:32:06 +03:00
|
|
|
}
|
|
|
|
|
2017-10-30 15:27:55 +03:00
|
|
|
i2p::config::GetOption("nat", m_IsNAT);
|
2014-04-05 00:29:40 +04:00
|
|
|
m_DHKeysPairSupplier.Start ();
|
2020-06-30 03:02:09 +03:00
|
|
|
m_X25519KeysPairSupplier.Start ();
|
2013-12-29 19:48:57 +04:00
|
|
|
m_IsRunning = true;
|
2013-10-27 19:26:39 +04:00
|
|
|
m_Thread = new std::thread (std::bind (&Transports::Run, this));
|
2017-05-29 08:28:16 +03:00
|
|
|
std::string ntcpproxy; i2p::config::GetOption("ntcpproxy", ntcpproxy);
|
2020-03-01 23:11:54 +03:00
|
|
|
std::string ntcp2proxy; i2p::config::GetOption("ntcp2.proxy", ntcp2proxy);
|
2017-05-29 08:28:16 +03:00
|
|
|
i2p::http::URL proxyurl;
|
2018-02-20 21:18:57 +03:00
|
|
|
uint16_t softLimit, hardLimit, threads;
|
2017-10-30 15:27:55 +03:00
|
|
|
i2p::config::GetOption("limits.ntcpsoft", softLimit);
|
|
|
|
i2p::config::GetOption("limits.ntcphard", hardLimit);
|
2018-02-20 21:18:57 +03:00
|
|
|
i2p::config::GetOption("limits.ntcpthreads", threads);
|
2017-10-30 16:53:41 +03:00
|
|
|
if(softLimit > 0 && hardLimit > 0 && softLimit >= hardLimit)
|
2017-10-30 15:27:55 +03:00
|
|
|
{
|
|
|
|
LogPrint(eLogError, "ntcp soft limit must be less than ntcp hard limit");
|
|
|
|
return;
|
|
|
|
}
|
2017-05-29 08:28:16 +03:00
|
|
|
if(ntcpproxy.size() && enableNTCP)
|
|
|
|
{
|
|
|
|
if(proxyurl.parse(ntcpproxy))
|
|
|
|
{
|
2017-05-29 16:57:30 +03:00
|
|
|
if(proxyurl.schema == "socks" || proxyurl.schema == "http")
|
2017-05-29 08:28:16 +03:00
|
|
|
{
|
2018-02-20 21:18:57 +03:00
|
|
|
m_NTCPServer = new NTCPServer(threads);
|
2017-10-30 15:27:55 +03:00
|
|
|
m_NTCPServer->SetSessionLimits(softLimit, hardLimit);
|
2017-05-29 16:57:30 +03:00
|
|
|
NTCPServer::ProxyType proxytype = NTCPServer::eSocksProxy;
|
|
|
|
|
|
|
|
if (proxyurl.schema == "http")
|
|
|
|
proxytype = NTCPServer::eHTTPProxy;
|
2020-04-30 04:44:13 +03:00
|
|
|
m_NTCPServer->UseProxy(proxytype, proxyurl.host, proxyurl.port);
|
2017-05-29 08:28:16 +03:00
|
|
|
m_NTCPServer->Start();
|
|
|
|
if(!m_NTCPServer->NetworkIsReady())
|
|
|
|
{
|
2017-05-29 16:57:30 +03:00
|
|
|
LogPrint(eLogError, "Transports: NTCP failed to start with proxy");
|
2017-05-29 08:28:16 +03:00
|
|
|
m_NTCPServer->Stop();
|
|
|
|
delete m_NTCPServer;
|
|
|
|
m_NTCPServer = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
LogPrint(eLogError, "Transports: unsupported NTCP proxy URL ", ntcpproxy);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
LogPrint(eLogError, "Transports: invalid NTCP proxy url ", ntcpproxy);
|
|
|
|
return;
|
|
|
|
}
|
2020-03-01 17:35:24 +03:00
|
|
|
// create NTCP2. TODO: move to acceptor
|
2020-04-30 04:44:13 +03:00
|
|
|
bool ntcp2; i2p::config::GetOption("ntcp2.enabled", ntcp2);
|
2018-07-13 22:59:28 +03:00
|
|
|
if (ntcp2)
|
|
|
|
{
|
2020-03-01 23:11:54 +03:00
|
|
|
if(!ntcp2proxy.empty())
|
2020-03-01 17:35:24 +03: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;
|
|
|
|
|
2020-04-30 04:44:13 +03:00
|
|
|
m_NTCP2Server->UseProxy(proxytype, proxyurl.host, proxyurl.port);
|
2020-03-01 17:35:24 +03:00
|
|
|
m_NTCP2Server->Start();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
LogPrint(eLogError, "Transports: unsupported NTCP2 proxy URL ", ntcp2proxy);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
LogPrint(eLogError, "Transports: invalid NTCP2 proxy url ", ntcp2proxy);
|
|
|
|
return;
|
|
|
|
}
|
2020-03-01 23:11:54 +03:00
|
|
|
else
|
2020-03-01 13:25:50 +03:00
|
|
|
{
|
2020-03-01 23:11:54 +03:00
|
|
|
m_NTCP2Server = new NTCP2Server ();
|
|
|
|
m_NTCP2Server->Start ();
|
2020-03-01 13:25:50 +03:00
|
|
|
}
|
2020-03-01 17:35:24 +03:00
|
|
|
}
|
2017-05-29 08:28:16 +03:00
|
|
|
|
2013-10-27 19:26:39 +04:00
|
|
|
// create acceptors
|
2016-03-21 20:02:51 +03:00
|
|
|
auto& addresses = context.GetRouterInfo ().GetAddresses ();
|
2016-08-09 01:53:37 +03:00
|
|
|
for (const auto& address : addresses)
|
2013-10-27 19:26:39 +04:00
|
|
|
{
|
2016-07-22 16:56:17 +03:00
|
|
|
if (!address) continue;
|
2016-07-14 16:23:33 +03:00
|
|
|
if (m_NTCPServer == nullptr && enableNTCP)
|
2016-06-13 18:34:44 +03:00
|
|
|
{
|
2018-02-20 21:18:57 +03:00
|
|
|
m_NTCPServer = new NTCPServer (threads);
|
2017-10-30 15:27:55 +03:00
|
|
|
m_NTCPServer->SetSessionLimits(softLimit, hardLimit);
|
2015-01-12 01:41:56 +03:00
|
|
|
m_NTCPServer->Start ();
|
2016-06-13 18:34:44 +03:00
|
|
|
if (!(m_NTCPServer->IsBoundV6() || m_NTCPServer->IsBoundV4())) {
|
|
|
|
/** failed to bind to NTCP */
|
|
|
|
LogPrint(eLogError, "Transports: failed to bind to TCP");
|
|
|
|
m_NTCPServer->Stop();
|
|
|
|
delete m_NTCPServer;
|
|
|
|
m_NTCPServer = nullptr;
|
|
|
|
}
|
2017-05-29 08:28:16 +03:00
|
|
|
}
|
|
|
|
|
2016-06-27 17:24:37 +03:00
|
|
|
if (address->transportStyle == RouterInfo::eTransportSSU)
|
2014-02-25 04:25:26 +04:00
|
|
|
{
|
2016-07-14 16:23:33 +03:00
|
|
|
if (m_SSUServer == nullptr && enableSSU)
|
2016-06-27 17:24:37 +03:00
|
|
|
{
|
|
|
|
if (address->host.is_v4())
|
|
|
|
m_SSUServer = new SSUServer (address->port);
|
|
|
|
else
|
|
|
|
m_SSUServer = new SSUServer (address->host, address->port);
|
2016-03-21 20:02:51 +03:00
|
|
|
LogPrint (eLogInfo, "Transports: Start listening UDP port ", address->port);
|
2016-06-13 18:34:44 +03:00
|
|
|
try {
|
|
|
|
m_SSUServer->Start ();
|
|
|
|
} catch ( std::exception & ex ) {
|
|
|
|
LogPrint(eLogError, "Transports: Failed to bind to UDP port", address->port);
|
|
|
|
delete m_SSUServer;
|
|
|
|
m_SSUServer = nullptr;
|
|
|
|
continue;
|
|
|
|
}
|
2014-02-25 04:25:26 +04:00
|
|
|
DetectExternalIP ();
|
2014-01-24 01:10:33 +04:00
|
|
|
}
|
|
|
|
else
|
2015-12-18 15:57:22 +03:00
|
|
|
LogPrint (eLogError, "Transports: SSU server already exists");
|
2014-01-24 01:10:33 +04:00
|
|
|
}
|
2017-05-29 08:28:16 +03:00
|
|
|
}
|
2016-12-22 21:32:06 +03:00
|
|
|
m_PeerCleanupTimer->expires_from_now (boost::posix_time::seconds(5*SESSION_CREATION_TIMEOUT));
|
|
|
|
m_PeerCleanupTimer->async_wait (std::bind (&Transports::HandlePeerCleanupTimer, this, std::placeholders::_1));
|
2017-08-09 17:52:52 +03:00
|
|
|
|
2020-03-20 17:44:53 +03: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 19:26:39 +04:00
|
|
|
}
|
2017-05-29 08:28:16 +03:00
|
|
|
|
2013-10-27 19:26:39 +04:00
|
|
|
void Transports::Stop ()
|
2017-05-29 08:28:16 +03:00
|
|
|
{
|
|
|
|
if (m_PeerCleanupTimer) m_PeerCleanupTimer->cancel ();
|
2016-12-22 21:32:06 +03:00
|
|
|
if (m_PeerTestTimer) m_PeerTestTimer->cancel ();
|
2015-01-13 06:53:35 +03:00
|
|
|
m_Peers.clear ();
|
2014-01-24 01:10:33 +04:00
|
|
|
if (m_SSUServer)
|
|
|
|
{
|
|
|
|
m_SSUServer->Stop ();
|
|
|
|
delete m_SSUServer;
|
2014-09-22 21:28:46 +04:00
|
|
|
m_SSUServer = nullptr;
|
2017-05-29 08:28:16 +03:00
|
|
|
}
|
2015-01-12 01:41:56 +03:00
|
|
|
if (m_NTCPServer)
|
|
|
|
{
|
|
|
|
m_NTCPServer->Stop ();
|
|
|
|
delete m_NTCPServer;
|
|
|
|
m_NTCPServer = nullptr;
|
2017-05-29 08:28:16 +03:00
|
|
|
}
|
2014-01-24 01:10:33 +04:00
|
|
|
|
2018-07-13 22:59:28 +03:00
|
|
|
if (m_NTCP2Server)
|
|
|
|
{
|
|
|
|
m_NTCP2Server->Stop ();
|
|
|
|
delete m_NTCP2Server;
|
|
|
|
m_NTCP2Server = nullptr;
|
|
|
|
}
|
|
|
|
|
2014-04-05 00:29:40 +04:00
|
|
|
m_DHKeysPairSupplier.Stop ();
|
2020-06-30 03:02:09 +03:00
|
|
|
m_X25519KeysPairSupplier.Stop ();
|
2013-12-29 19:48:57 +04:00
|
|
|
m_IsRunning = false;
|
2016-12-22 21:32:06 +03:00
|
|
|
if (m_Service) m_Service->stop ();
|
2013-10-27 19:26:39 +04:00
|
|
|
if (m_Thread)
|
2017-05-29 08:28:16 +03:00
|
|
|
{
|
|
|
|
m_Thread->join ();
|
2013-10-27 19:26:39 +04:00
|
|
|
delete m_Thread;
|
2014-09-22 21:28:46 +04:00
|
|
|
m_Thread = nullptr;
|
2017-05-29 08:28:16 +03:00
|
|
|
}
|
|
|
|
}
|
2013-10-27 19:26:39 +04:00
|
|
|
|
2017-05-29 08:28:16 +03:00
|
|
|
void Transports::Run ()
|
|
|
|
{
|
2016-12-22 21:32:06 +03:00
|
|
|
while (m_IsRunning && m_Service)
|
2013-11-29 16:52:09 +04:00
|
|
|
{
|
2013-12-29 19:48:57 +04:00
|
|
|
try
|
2017-05-29 08:28:16 +03:00
|
|
|
{
|
2016-12-22 21:32:06 +03:00
|
|
|
m_Service->run ();
|
2013-12-29 19:48:57 +04:00
|
|
|
}
|
|
|
|
catch (std::exception& ex)
|
|
|
|
{
|
2015-12-18 15:57:22 +03:00
|
|
|
LogPrint (eLogError, "Transports: runtime exception: ", ex.what ());
|
2017-05-29 08:28:16 +03:00
|
|
|
}
|
|
|
|
}
|
2013-11-29 16:52:09 +04:00
|
|
|
}
|
2017-05-29 08:28:16 +03:00
|
|
|
|
2015-03-17 22:19:38 +03:00
|
|
|
void Transports::UpdateBandwidth ()
|
|
|
|
{
|
|
|
|
uint64_t ts = i2p::util::GetMillisecondsSinceEpoch ();
|
|
|
|
if (m_LastBandwidthUpdateTime > 0)
|
|
|
|
{
|
|
|
|
auto delta = ts - m_LastBandwidthUpdateTime;
|
|
|
|
if (delta > 0)
|
|
|
|
{
|
2017-05-29 08:28:16 +03:00
|
|
|
m_InBandwidth = (m_TotalReceivedBytes - m_LastInBandwidthUpdateBytes)*1000/delta; // per second
|
|
|
|
m_OutBandwidth = (m_TotalSentBytes - m_LastOutBandwidthUpdateBytes)*1000/delta; // per second
|
2017-05-02 21:20:00 +03:00
|
|
|
m_TransitBandwidth = (m_TotalTransitTransmittedBytes - m_LastTransitBandwidthUpdateBytes)*1000/delta;
|
2017-05-29 08:28:16 +03:00
|
|
|
}
|
2015-03-17 22:19:38 +03:00
|
|
|
}
|
|
|
|
m_LastBandwidthUpdateTime = ts;
|
2017-05-29 08:28:16 +03:00
|
|
|
m_LastInBandwidthUpdateBytes = m_TotalReceivedBytes;
|
|
|
|
m_LastOutBandwidthUpdateBytes = m_TotalSentBytes;
|
|
|
|
m_LastTransitBandwidthUpdateBytes = m_TotalTransitTransmittedBytes;
|
2015-03-17 22:19:38 +03:00
|
|
|
}
|
|
|
|
|
2015-03-18 20:07:11 +03:00
|
|
|
bool Transports::IsBandwidthExceeded () const
|
|
|
|
{
|
2016-03-31 03:00:00 +03:00
|
|
|
auto limit = i2p::context.GetBandwidthLimit() * 1024; // convert to bytes
|
2016-01-03 06:17:04 +03:00
|
|
|
auto bw = std::max (m_InBandwidth, m_OutBandwidth);
|
2016-03-31 03:00:00 +03:00
|
|
|
return bw > limit;
|
2015-03-18 20:07:11 +03:00
|
|
|
}
|
2013-10-27 19:26:39 +04:00
|
|
|
|
2017-05-04 21:58:12 +03:00
|
|
|
bool Transports::IsTransitBandwidthExceeded () const
|
|
|
|
{
|
|
|
|
auto limit = i2p::context.GetTransitBandwidthLimit() * 1024; // convert to bytes
|
|
|
|
return m_TransitBandwidth > limit;
|
2017-05-29 08:28:16 +03:00
|
|
|
}
|
2017-05-04 21:58:12 +03:00
|
|
|
|
2015-06-17 18:41:07 +03:00
|
|
|
void Transports::SendMessage (const i2p::data::IdentHash& ident, std::shared_ptr<i2p::I2NPMessage> msg)
|
|
|
|
{
|
2017-05-29 08:28:16 +03:00
|
|
|
SendMessages (ident, std::vector<std::shared_ptr<i2p::I2NPMessage> > {msg });
|
|
|
|
}
|
2015-06-17 18:41:07 +03:00
|
|
|
|
|
|
|
void Transports::SendMessages (const i2p::data::IdentHash& ident, const std::vector<std::shared_ptr<i2p::I2NPMessage> >& msgs)
|
2015-01-21 05:05:57 +03:00
|
|
|
{
|
2016-12-22 21:32:06 +03:00
|
|
|
m_Service->post (std::bind (&Transports::PostMessages, this, ident, msgs));
|
2017-05-29 08:28:16 +03:00
|
|
|
}
|
2015-01-14 05:31:39 +03:00
|
|
|
|
2015-06-17 18:41:07 +03:00
|
|
|
void Transports::PostMessages (i2p::data::IdentHash ident, std::vector<std::shared_ptr<i2p::I2NPMessage> > msgs)
|
2015-01-21 05:05:57 +03:00
|
|
|
{
|
|
|
|
if (ident == i2p::context.GetRouterInfo ().GetIdentHash ())
|
2017-05-29 08:28:16 +03:00
|
|
|
{
|
2015-01-21 05:05:57 +03:00
|
|
|
// we send it to ourself
|
2016-08-09 01:53:37 +03:00
|
|
|
for (auto& it: msgs)
|
2016-11-15 22:11:55 +03:00
|
|
|
m_LoopbackHandler.PutNextMessage (it);
|
|
|
|
m_LoopbackHandler.Flush ();
|
2015-01-21 05:05:57 +03:00
|
|
|
return;
|
2016-10-28 19:50:26 +03:00
|
|
|
}
|
2020-05-24 21:14:16 +03:00
|
|
|
if(RoutesRestricted() && !IsRestrictedPeer(ident)) return;
|
2015-01-21 05:05:57 +03:00
|
|
|
auto it = m_Peers.find (ident);
|
|
|
|
if (it == m_Peers.end ())
|
|
|
|
{
|
2017-05-29 08:28:16 +03:00
|
|
|
bool connected = false;
|
2015-04-14 17:40:46 +03:00
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = netdb.FindRouter (ident);
|
2016-01-16 00:23:03 +03:00
|
|
|
{
|
2020-03-01 13:25:50 +03:00
|
|
|
std::unique_lock<std::mutex> l(m_PeersMutex);
|
2016-01-16 00:23:03 +03:00
|
|
|
it = m_Peers.insert (std::pair<i2p::data::IdentHash, Peer>(ident, { 0, r, {},
|
|
|
|
i2p::util::GetSecondsSinceEpoch (), {} })).first;
|
|
|
|
}
|
2015-04-14 17:40:46 +03:00
|
|
|
connected = ConnectToPeer (ident, it->second);
|
|
|
|
}
|
|
|
|
catch (std::exception& ex)
|
|
|
|
{
|
2015-12-18 15:57:22 +03:00
|
|
|
LogPrint (eLogError, "Transports: PostMessages exception:", ex.what ());
|
2015-04-14 17:40:46 +03:00
|
|
|
}
|
2015-06-17 18:41:07 +03:00
|
|
|
if (!connected) return;
|
2017-05-29 08:28:16 +03:00
|
|
|
}
|
2015-06-09 18:00:37 +03:00
|
|
|
if (!it->second.sessions.empty ())
|
2015-06-17 18:41:07 +03:00
|
|
|
it->second.sessions.front ()->SendI2NPMessages (msgs);
|
2015-01-21 05:05:57 +03:00
|
|
|
else
|
2017-05-29 08:28:16 +03:00
|
|
|
{
|
2016-07-12 19:37:39 +03:00
|
|
|
if (it->second.delayedMessages.size () < MAX_NUM_DELAYED_MESSAGES)
|
2017-05-29 08:28:16 +03:00
|
|
|
{
|
2016-08-09 01:53:37 +03:00
|
|
|
for (auto& it1: msgs)
|
2016-07-12 19:37:39 +03:00
|
|
|
it->second.delayedMessages.push_back (it1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-24 21:14:16 +03:00
|
|
|
LogPrint (eLogWarning, "Transports: delayed messages queue size to ",
|
|
|
|
ident.ToBase64 (), " exceeds ", MAX_NUM_DELAYED_MESSAGES);
|
2017-05-29 08:28:16 +03:00
|
|
|
std::unique_lock<std::mutex> l(m_PeersMutex);
|
2016-07-12 19:37:39 +03:00
|
|
|
m_Peers.erase (it);
|
2017-05-29 08:28:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-14 05:31:39 +03:00
|
|
|
bool Transports::ConnectToPeer (const i2p::data::IdentHash& ident, Peer& peer)
|
|
|
|
{
|
2020-06-24 18:29:54 +03:00
|
|
|
if (!peer.router) // reconnect
|
|
|
|
peer.router = netdb.FindRouter (ident); // try to get new one from netdb
|
2015-01-14 05:31:39 +03:00
|
|
|
if (peer.router) // we have RI already
|
2017-05-29 08:28:16 +03:00
|
|
|
{
|
2020-04-30 04:44:13 +03:00
|
|
|
if (!peer.numAttempts) // NTCP2
|
|
|
|
{
|
|
|
|
peer.numAttempts++;
|
|
|
|
if (m_NTCP2Server) // we support NTCP2
|
|
|
|
{
|
|
|
|
// NTCP2 have priority over NTCP
|
|
|
|
auto address = peer.router->GetNTCP2Address (true, !context.SupportsV6 ()); // published only
|
2020-06-23 05:32:18 +03:00
|
|
|
if (address && !peer.router->IsUnreachable ())
|
2020-04-30 04:44:13 +03:00
|
|
|
{
|
|
|
|
auto s = std::make_shared<NTCP2Session> (*m_NTCP2Server, peer.router);
|
|
|
|
|
|
|
|
if(m_NTCP2Server->UsingProxy())
|
|
|
|
{
|
|
|
|
NTCP2Server::RemoteAddressType remote = NTCP2Server::eIP4Address;
|
|
|
|
std::string addr = address->host.to_string();
|
|
|
|
|
|
|
|
if(address->host.is_v6())
|
|
|
|
remote = NTCP2Server::eIP6Address;
|
|
|
|
|
|
|
|
m_NTCP2Server->ConnectWithProxy(addr, address->port, remote, s);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
m_NTCP2Server->Connect (address->host, address->port, s);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-14 18:27:27 +03:00
|
|
|
if (peer.numAttempts == 1) // NTCP1
|
|
|
|
{
|
2020-04-30 04:44:13 +03:00
|
|
|
peer.numAttempts++;
|
2018-08-04 20:48:09 +03:00
|
|
|
auto address = peer.router->GetNTCPAddress (!context.SupportsV6 ());
|
2016-03-12 03:27:43 +03:00
|
|
|
if (address && m_NTCPServer)
|
2015-01-16 23:25:44 +03:00
|
|
|
{
|
2019-02-25 02:26:58 +03:00
|
|
|
if (!peer.router->UsesIntroducer () && !peer.router->IsUnreachable ())
|
2015-01-16 23:25:44 +03:00
|
|
|
{
|
2019-02-25 02:26:58 +03:00
|
|
|
if(!m_NTCPServer->ShouldLimit())
|
2017-05-29 08:28:16 +03:00
|
|
|
{
|
2019-02-25 02:26:58 +03:00
|
|
|
auto s = std::make_shared<NTCPSession> (*m_NTCPServer, peer.router);
|
|
|
|
if(m_NTCPServer->UsingProxy())
|
2017-05-29 08:28:16 +03:00
|
|
|
{
|
2019-02-25 02:26:58 +03:00
|
|
|
NTCPServer::RemoteAddressType remote = NTCPServer::eIP4Address;
|
|
|
|
std::string addr = address->host.to_string();
|
2017-10-30 15:27:55 +03:00
|
|
|
|
2019-02-25 02:26:58 +03:00
|
|
|
if(address->host.is_v6())
|
|
|
|
remote = NTCPServer::eIP6Address;
|
2017-10-30 15:27:55 +03:00
|
|
|
|
2019-02-25 02:26:58 +03:00
|
|
|
m_NTCPServer->ConnectWithProxy(addr, address->port, remote, s);
|
2017-05-29 08:28:16 +03:00
|
|
|
}
|
|
|
|
else
|
2019-02-25 02:26:58 +03:00
|
|
|
m_NTCPServer->Connect (address->host, address->port, s);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LogPrint(eLogWarning, "Transports: NTCP Limit hit falling back to SSU");
|
2015-01-16 23:25:44 +03:00
|
|
|
}
|
|
|
|
}
|
2017-05-29 08:28:16 +03:00
|
|
|
}
|
2015-12-02 20:48:10 +03:00
|
|
|
else
|
2016-06-13 18:34:44 +03:00
|
|
|
LogPrint (eLogDebug, "Transports: NTCP address is not present for ", i2p::data::GetIdentHashAbbreviation (ident), ", trying SSU");
|
2013-12-29 19:48:57 +04:00
|
|
|
}
|
2018-08-14 18:27:27 +03:00
|
|
|
if (peer.numAttempts == 2)// SSU
|
2014-01-19 19:05:54 +04:00
|
|
|
{
|
2015-01-14 05:31:39 +03:00
|
|
|
peer.numAttempts++;
|
2015-12-02 20:48:10 +03:00
|
|
|
if (m_SSUServer && peer.router->IsSSU (!context.SupportsV6 ()))
|
|
|
|
{
|
|
|
|
auto address = peer.router->GetSSUAddress (!context.SupportsV6 ());
|
2019-02-25 02:26:58 +03:00
|
|
|
m_SSUServer->CreateSession (peer.router, address->host, address->port);
|
|
|
|
return true;
|
2015-01-14 05:31:39 +03:00
|
|
|
}
|
2017-05-29 08:28:16 +03:00
|
|
|
}
|
2017-01-02 17:03:12 +03:00
|
|
|
LogPrint (eLogInfo, "Transports: No NTCP or SSU addresses available");
|
2019-11-12 18:03:33 +03:00
|
|
|
i2p::data::netdb.SetUnreachable (ident, true); // we are here because all connection attempts failed
|
2015-06-09 18:00:37 +03:00
|
|
|
peer.Done ();
|
2017-05-29 08:28:16 +03:00
|
|
|
std::unique_lock<std::mutex> l(m_PeersMutex);
|
2015-01-14 05:31:39 +03:00
|
|
|
m_Peers.erase (ident);
|
|
|
|
return false;
|
2017-05-29 08:28:16 +03:00
|
|
|
}
|
2015-01-14 05:31:39 +03:00
|
|
|
else // otherwise request RI
|
|
|
|
{
|
2015-12-18 15:57:22 +03:00
|
|
|
LogPrint (eLogInfo, "Transports: RouterInfo for ", ident.ToBase64 (), " not found, requested");
|
2015-01-15 00:37:03 +03:00
|
|
|
i2p::data::netdb.RequestDestination (ident, std::bind (
|
|
|
|
&Transports::RequestComplete, this, std::placeholders::_1, ident));
|
2017-05-29 08:28:16 +03:00
|
|
|
}
|
2015-01-14 05:31:39 +03:00
|
|
|
return true;
|
2017-05-29 08:28:16 +03:00
|
|
|
}
|
|
|
|
|
2015-01-15 00:37:03 +03:00
|
|
|
void Transports::RequestComplete (std::shared_ptr<const i2p::data::RouterInfo> r, const i2p::data::IdentHash& ident)
|
|
|
|
{
|
2016-12-22 21:32:06 +03:00
|
|
|
m_Service->post (std::bind (&Transports::HandleRequestComplete, this, r, ident));
|
2017-05-29 08:28:16 +03:00
|
|
|
}
|
|
|
|
|
2015-12-31 19:21:01 +03:00
|
|
|
void Transports::HandleRequestComplete (std::shared_ptr<const i2p::data::RouterInfo> r, i2p::data::IdentHash ident)
|
2014-08-29 05:34:23 +04:00
|
|
|
{
|
2015-01-14 05:31:39 +03:00
|
|
|
auto it = m_Peers.find (ident);
|
|
|
|
if (it != m_Peers.end ())
|
2017-05-29 08:28:16 +03:00
|
|
|
{
|
2015-01-14 05:31:39 +03:00
|
|
|
if (r)
|
|
|
|
{
|
2015-12-18 15:57:22 +03:00
|
|
|
LogPrint (eLogDebug, "Transports: RouterInfo for ", ident.ToBase64 (), " found, Trying to connect");
|
2015-01-14 05:31:39 +03:00
|
|
|
it->second.router = r;
|
|
|
|
ConnectToPeer (ident, it->second);
|
2017-05-29 08:28:16 +03:00
|
|
|
}
|
2015-01-14 05:31:39 +03:00
|
|
|
else
|
|
|
|
{
|
2017-01-31 04:36:35 +03:00
|
|
|
LogPrint (eLogWarning, "Transports: RouterInfo not found, Failed to send messages");
|
2017-05-29 08:28:16 +03:00
|
|
|
std::unique_lock<std::mutex> l(m_PeersMutex);
|
2015-01-14 05:31:39 +03:00
|
|
|
m_Peers.erase (it);
|
2017-05-29 08:28:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-09 06:06:40 +04:00
|
|
|
void Transports::DetectExternalIP ()
|
|
|
|
{
|
2016-10-28 19:50:26 +03:00
|
|
|
if (RoutesRestricted())
|
2018-01-06 07:01:44 +03:00
|
|
|
{
|
2016-10-28 19:50:26 +03:00
|
|
|
LogPrint(eLogInfo, "Transports: restricted routes enabled, not detecting ip");
|
2016-10-28 20:11:50 +03:00
|
|
|
i2p::context.SetStatus (eRouterStatusOK);
|
2016-10-28 19:50:26 +03:00
|
|
|
return;
|
|
|
|
}
|
2015-02-26 22:17:16 +03:00
|
|
|
if (m_SSUServer)
|
2014-02-09 06:06:40 +04:00
|
|
|
{
|
2016-12-02 19:17:22 +03:00
|
|
|
bool isv4 = i2p::context.SupportsV4 ();
|
2017-08-09 17:52:52 +03:00
|
|
|
if (m_IsNAT && isv4)
|
2016-11-01 17:26:40 +03:00
|
|
|
i2p::context.SetStatus (eRouterStatusTesting);
|
2015-02-26 22:17:16 +03:00
|
|
|
for (int i = 0; i < 5; i++)
|
|
|
|
{
|
2016-12-02 19:17:22 +03:00
|
|
|
auto router = i2p::data::netdb.GetRandomPeerTestRouter (isv4); // v4 only if v4
|
|
|
|
if (router)
|
2020-03-01 13:25:50 +03:00
|
|
|
m_SSUServer->CreateSession (router, true, isv4); // peer test
|
2015-02-26 22:17:16 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// if not peer test capable routers found pick any
|
|
|
|
router = i2p::data::netdb.GetRandomRouter ();
|
|
|
|
if (router && router->IsSSU ())
|
2020-03-01 13:25:50 +03:00
|
|
|
m_SSUServer->CreateSession (router); // no peer test
|
2015-02-26 22:17:16 +03:00
|
|
|
}
|
2017-05-29 08:28:16 +03:00
|
|
|
}
|
2019-05-23 22:59:44 +03:00
|
|
|
if (i2p::context.SupportsV6 ())
|
|
|
|
{
|
|
|
|
// try to connect to few v6 addresses to get our address back
|
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
{
|
|
|
|
auto router = i2p::data::netdb.GetRandomSSUV6Router ();
|
|
|
|
if (router)
|
|
|
|
{
|
|
|
|
auto addr = router->GetSSUV6Address ();
|
|
|
|
if (addr)
|
2019-06-14 22:43:03 +03:00
|
|
|
m_SSUServer->GetServiceV6 ().post ([this, router, addr]
|
|
|
|
{
|
2020-03-01 13:25:50 +03:00
|
|
|
m_SSUServer->CreateDirectSession (router, { addr->host, (uint16_t)addr->port }, false);
|
2019-06-14 22:43:03 +03:00
|
|
|
});
|
2019-05-23 22:59:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-26 22:17:16 +03:00
|
|
|
}
|
|
|
|
else
|
2015-12-18 15:57:22 +03:00
|
|
|
LogPrint (eLogError, "Transports: Can't detect external IP. SSU is not available");
|
2014-02-09 06:06:40 +04:00
|
|
|
}
|
2015-11-03 17:15:49 +03:00
|
|
|
|
|
|
|
void Transports::PeerTest ()
|
|
|
|
{
|
2016-12-02 19:17:22 +03:00
|
|
|
if (RoutesRestricted() || !i2p::context.SupportsV4 ()) return;
|
2015-11-03 17:15:49 +03:00
|
|
|
if (m_SSUServer)
|
2017-05-29 08:28:16 +03:00
|
|
|
{
|
2020-03-20 17:44:53 +03:00
|
|
|
LogPrint (eLogInfo, "Transports: Started peer test");
|
2015-11-03 17:15:49 +03:00
|
|
|
bool statusChanged = false;
|
|
|
|
for (int i = 0; i < 5; i++)
|
|
|
|
{
|
2016-12-02 19:17:22 +03:00
|
|
|
auto router = i2p::data::netdb.GetRandomPeerTestRouter (true); // v4 only
|
|
|
|
if (router)
|
2017-05-29 08:28:16 +03:00
|
|
|
{
|
2015-11-03 17:15:49 +03:00
|
|
|
if (!statusChanged)
|
2017-05-29 08:28:16 +03:00
|
|
|
{
|
2015-11-03 17:15:49 +03:00
|
|
|
statusChanged = true;
|
|
|
|
i2p::context.SetStatus (eRouterStatusTesting); // first time only
|
2017-05-29 08:28:16 +03:00
|
|
|
}
|
|
|
|
m_SSUServer->CreateSession (router, true, true); // peer test v4
|
|
|
|
}
|
2016-12-02 19:17:22 +03:00
|
|
|
}
|
|
|
|
if (!statusChanged)
|
2020-03-20 17:44:53 +03:00
|
|
|
LogPrint (eLogWarning, "Transports: Can't find routers for peer test");
|
2015-11-03 17:15:49 +03:00
|
|
|
}
|
2017-05-29 08:28:16 +03:00
|
|
|
}
|
|
|
|
|
2015-11-03 17:15:49 +03:00
|
|
|
std::shared_ptr<i2p::crypto::DHKeys> Transports::GetNextDHKeysPair ()
|
2014-04-04 21:30:13 +04:00
|
|
|
{
|
2014-04-05 00:29:40 +04:00
|
|
|
return m_DHKeysPairSupplier.Acquire ();
|
2014-04-04 21:30:13 +04:00
|
|
|
}
|
2014-09-17 19:13:25 +04:00
|
|
|
|
2015-11-03 17:15:49 +03:00
|
|
|
void Transports::ReuseDHKeysPair (std::shared_ptr<i2p::crypto::DHKeys> pair)
|
2014-09-17 19:13:25 +04:00
|
|
|
{
|
|
|
|
m_DHKeysPairSupplier.Return (pair);
|
|
|
|
}
|
2015-01-13 06:53:35 +03:00
|
|
|
|
2020-06-30 03:02:09 +03: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);
|
|
|
|
}
|
|
|
|
|
2015-01-13 06:53:35 +03:00
|
|
|
void Transports::PeerConnected (std::shared_ptr<TransportSession> session)
|
|
|
|
{
|
2016-12-22 21:32:06 +03:00
|
|
|
m_Service->post([session, this]()
|
2017-05-29 08:28:16 +03:00
|
|
|
{
|
|
|
|
auto remoteIdentity = session->GetRemoteIdentity ();
|
2016-02-01 22:19:54 +03:00
|
|
|
if (!remoteIdentity) return;
|
|
|
|
auto ident = remoteIdentity->GetIdentHash ();
|
2016-11-01 17:26:40 +03:00
|
|
|
auto it = m_Peers.find (ident);
|
2015-01-13 06:53:35 +03:00
|
|
|
if (it != m_Peers.end ())
|
|
|
|
{
|
2020-06-24 18:29:54 +03:00
|
|
|
it->second.router = nullptr; // we don't need RouterInfo after successive connect
|
2016-03-19 05:53:03 +03: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 18:34:44 +03:00
|
|
|
i2p::data::IdentHash(firstMsg->GetPayload () + DATABASE_STORE_KEY_OFFSET) == i2p::context.GetIdentHash ())
|
2016-03-19 05:53:03 +03:00
|
|
|
sendDatabaseStore = false; // we have it in the list already
|
2017-05-29 08:28:16 +03:00
|
|
|
}
|
2016-03-19 05:53:03 +03:00
|
|
|
if (sendDatabaseStore)
|
2018-08-14 18:27:27 +03:00
|
|
|
session->SendLocalRouterInfo ();
|
2016-08-04 17:26:50 +03:00
|
|
|
else
|
|
|
|
session->SetTerminationTimeout (10); // most likely it's publishing, no follow-up messages expected, set timeout to 10 seconds
|
2015-06-09 18:00:37 +03:00
|
|
|
it->second.sessions.push_back (session);
|
|
|
|
session->SendI2NPMessages (it->second.delayedMessages);
|
|
|
|
it->second.delayedMessages.clear ();
|
2015-01-13 06:53:35 +03:00
|
|
|
}
|
2015-01-14 05:31:39 +03:00
|
|
|
else // incoming connection
|
2016-01-16 00:23:03 +03:00
|
|
|
{
|
2016-10-28 19:50:26 +03:00
|
|
|
if(RoutesRestricted() && ! IsRestrictedPeer(ident)) {
|
|
|
|
// not trusted
|
|
|
|
LogPrint(eLogWarning, "Transports: closing untrusted inbound connection from ", ident.ToBase64());
|
|
|
|
session->Done();
|
|
|
|
return;
|
|
|
|
}
|
2016-03-19 05:53:03 +03:00
|
|
|
session->SendI2NPMessages ({ CreateDatabaseStoreMsg () }); // send DatabaseStore
|
2017-05-29 08:28:16 +03:00
|
|
|
std::unique_lock<std::mutex> l(m_PeersMutex);
|
2015-12-24 03:55:53 +03:00
|
|
|
m_Peers.insert (std::make_pair (ident, Peer{ 0, nullptr, { session }, i2p::util::GetSecondsSinceEpoch (), {} }));
|
2016-01-16 00:23:03 +03:00
|
|
|
}
|
2016-11-01 17:26:40 +03:00
|
|
|
});
|
2015-01-13 06:53:35 +03:00
|
|
|
}
|
2017-05-29 08:28:16 +03:00
|
|
|
|
2015-01-13 06:53:35 +03:00
|
|
|
void Transports::PeerDisconnected (std::shared_ptr<TransportSession> session)
|
|
|
|
{
|
2016-12-22 21:32:06 +03:00
|
|
|
m_Service->post([session, this]()
|
2016-11-01 17:26:40 +03:00
|
|
|
{
|
2017-05-29 08:28:16 +03:00
|
|
|
auto remoteIdentity = session->GetRemoteIdentity ();
|
2016-02-01 22:19:54 +03:00
|
|
|
if (!remoteIdentity) return;
|
|
|
|
auto ident = remoteIdentity->GetIdentHash ();
|
2015-01-13 06:53:35 +03:00
|
|
|
auto it = m_Peers.find (ident);
|
2015-06-09 18:00:37 +03:00
|
|
|
if (it != m_Peers.end ())
|
2015-01-14 05:31:39 +03:00
|
|
|
{
|
2019-11-12 17:38:22 +03:00
|
|
|
auto before = it->second.sessions.size ();
|
2015-06-09 18:00:37 +03:00
|
|
|
it->second.sessions.remove (session);
|
2020-03-01 17:35:24 +03:00
|
|
|
if (it->second.sessions.empty ())
|
2017-05-29 08:28:16 +03:00
|
|
|
{
|
2015-06-09 18:00:37 +03:00
|
|
|
if (it->second.delayedMessages.size () > 0)
|
2019-11-12 14:46:08 +03:00
|
|
|
{
|
2019-11-12 17:38:22 +03:00
|
|
|
if (before > 0) // we had an active session before
|
|
|
|
it->second.numAttempts = 0; // start over
|
2015-06-09 18:00:37 +03:00
|
|
|
ConnectToPeer (ident, it->second);
|
2020-03-01 17:35:24 +03:00
|
|
|
}
|
2015-06-09 18:00:37 +03:00
|
|
|
else
|
2016-01-16 00:23:03 +03:00
|
|
|
{
|
2017-05-29 08:28:16 +03:00
|
|
|
std::unique_lock<std::mutex> l(m_PeersMutex);
|
2015-06-09 18:00:37 +03:00
|
|
|
m_Peers.erase (it);
|
2016-01-16 00:23:03 +03:00
|
|
|
}
|
2015-06-09 18:00:37 +03:00
|
|
|
}
|
2015-01-14 05:31:39 +03:00
|
|
|
}
|
2017-05-29 08:28:16 +03:00
|
|
|
});
|
|
|
|
}
|
2015-02-11 22:45:25 +03:00
|
|
|
|
2015-03-18 03:56:51 +03:00
|
|
|
bool Transports::IsConnected (const i2p::data::IdentHash& ident) const
|
2017-05-29 08:28:16 +03:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_PeersMutex);
|
2015-03-18 03:56:51 +03:00
|
|
|
auto it = m_Peers.find (ident);
|
|
|
|
return it != m_Peers.end ();
|
2017-05-29 08:28:16 +03:00
|
|
|
}
|
|
|
|
|
2015-02-11 22:45:25 +03:00
|
|
|
void Transports::HandlePeerCleanupTimer (const boost::system::error_code& ecode)
|
|
|
|
{
|
2015-02-12 06:48:26 +03:00
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
2015-02-11 22:45:25 +03:00
|
|
|
{
|
2015-02-12 06:48:26 +03:00
|
|
|
auto ts = i2p::util::GetSecondsSinceEpoch ();
|
|
|
|
for (auto it = m_Peers.begin (); it != m_Peers.end (); )
|
2015-02-11 22:45:25 +03:00
|
|
|
{
|
2015-06-09 18:00:37 +03:00
|
|
|
if (it->second.sessions.empty () && ts > it->second.creationTime + SESSION_CREATION_TIMEOUT)
|
2015-02-12 06:48:26 +03:00
|
|
|
{
|
2015-12-18 15:57:22 +03:00
|
|
|
LogPrint (eLogWarning, "Transports: Session to peer ", it->first.ToBase64 (), " has not been created in ", SESSION_CREATION_TIMEOUT, " seconds");
|
2016-06-28 21:43:55 +03:00
|
|
|
auto profile = i2p::data::GetRouterProfile(it->first);
|
|
|
|
if (profile)
|
|
|
|
{
|
|
|
|
profile->TunnelNonReplied();
|
|
|
|
}
|
2020-03-01 13:25:50 +03:00
|
|
|
std::unique_lock<std::mutex> l(m_PeersMutex);
|
2015-02-12 06:48:26 +03:00
|
|
|
it = m_Peers.erase (it);
|
|
|
|
}
|
|
|
|
else
|
2016-08-09 01:53:37 +03:00
|
|
|
++it;
|
2015-02-11 22:45:25 +03:00
|
|
|
}
|
2015-03-17 22:19:38 +03:00
|
|
|
UpdateBandwidth (); // TODO: use separate timer(s) for it
|
2016-06-13 18:34:44 +03:00
|
|
|
if (i2p::context.GetStatus () == eRouterStatusTesting) // if still testing, repeat peer test
|
2015-03-28 21:57:39 +03:00
|
|
|
DetectExternalIP ();
|
2016-12-22 21:32:06 +03:00
|
|
|
m_PeerCleanupTimer->expires_from_now (boost::posix_time::seconds(5*SESSION_CREATION_TIMEOUT));
|
|
|
|
m_PeerCleanupTimer->async_wait (std::bind (&Transports::HandlePeerCleanupTimer, this, std::placeholders::_1));
|
2017-05-29 08:28:16 +03:00
|
|
|
}
|
2015-02-11 22:45:25 +03:00
|
|
|
}
|
2015-05-05 17:33:19 +03:00
|
|
|
|
2016-11-13 17:14:05 +03:00
|
|
|
void Transports::HandlePeerTestTimer (const boost::system::error_code& ecode)
|
|
|
|
{
|
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
|
|
|
{
|
|
|
|
PeerTest ();
|
2016-12-22 21:32:06 +03: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 08:28:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-05 17:33:19 +03:00
|
|
|
std::shared_ptr<const i2p::data::RouterInfo> Transports::GetRandomPeer () const
|
|
|
|
{
|
2016-06-01 03:00:00 +03:00
|
|
|
if (m_Peers.empty ()) return nullptr;
|
2017-05-29 08:28:16 +03:00
|
|
|
std::unique_lock<std::mutex> l(m_PeersMutex);
|
2015-05-05 17:33:19 +03:00
|
|
|
auto it = m_Peers.begin ();
|
2017-05-29 08:28:16 +03:00
|
|
|
std::advance (it, rand () % m_Peers.size ());
|
2015-05-05 17:33:19 +03:00
|
|
|
return it != m_Peers.end () ? it->second.router : nullptr;
|
|
|
|
}
|
2016-11-01 17:26:40 +03:00
|
|
|
void Transports::RestrictRoutesToFamilies(std::set<std::string> families)
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(m_FamilyMutex);
|
|
|
|
m_TrustedFamilies.clear();
|
|
|
|
for ( const auto& fam : families )
|
|
|
|
m_TrustedFamilies.push_back(fam);
|
|
|
|
}
|
2016-06-17 18:03:33 +03:00
|
|
|
|
2016-10-28 19:50:26 +03: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 08:28:16 +03:00
|
|
|
|
2016-11-01 17:26:40 +03:00
|
|
|
bool Transports::RoutesRestricted() const {
|
|
|
|
std::unique_lock<std::mutex> famlock(m_FamilyMutex);
|
2016-10-28 19:50:26 +03:00
|
|
|
std::unique_lock<std::mutex> routerslock(m_TrustedRoutersMutex);
|
2016-11-01 17:26:40 +03:00
|
|
|
return m_TrustedFamilies.size() > 0 || m_TrustedRouters.size() > 0;
|
|
|
|
}
|
2016-06-17 18:03:33 +03:00
|
|
|
|
2016-11-01 17:26:40 +03:00
|
|
|
/** XXX: if routes are not restricted this dies */
|
|
|
|
std::shared_ptr<const i2p::data::RouterInfo> Transports::GetRestrictedPeer() const
|
2016-10-28 19:50:26 +03:00
|
|
|
{
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> l(m_FamilyMutex);
|
|
|
|
std::string fam;
|
|
|
|
auto sz = m_TrustedFamilies.size();
|
|
|
|
if(sz > 1)
|
|
|
|
{
|
|
|
|
auto it = m_TrustedFamilies.begin ();
|
|
|
|
std::advance(it, rand() % sz);
|
|
|
|
fam = *it;
|
|
|
|
boost::to_lower(fam);
|
|
|
|
}
|
|
|
|
else if (sz == 1)
|
|
|
|
{
|
|
|
|
fam = m_TrustedFamilies[0];
|
|
|
|
}
|
|
|
|
if (fam.size())
|
|
|
|
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 17:26:40 +03:00
|
|
|
}
|
2016-10-28 19:50:26 +03: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;
|
|
|
|
}
|
2013-10-27 19:26:39 +04:00
|
|
|
}
|
2014-10-21 20:25:53 +04:00
|
|
|
}
|