mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-03-07 06:09:42 +00:00
Compare commits
78 Commits
8c555fe592
...
openssl
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fe3e7b1f6e | ||
|
|
66a52a17c6 | ||
|
|
c113241ccd | ||
|
|
9c97909e04 | ||
|
|
c816d3e4cc | ||
|
|
4f82fe24da | ||
|
|
a1794ccd22 | ||
|
|
5f762845f0 | ||
|
|
539e7e988e | ||
|
|
2a4403f1e0 | ||
|
|
dcd15cc244 | ||
|
|
9432202fad | ||
|
|
bf050ac465 | ||
|
|
81dae1997d | ||
|
|
7e3d9649de | ||
|
|
9ba016259d | ||
|
|
7791b3952e | ||
|
|
b97f09cc95 | ||
|
|
aedf59d11a | ||
|
|
d09367d686 | ||
|
|
70f99ccc21 | ||
|
|
ef72ba3f34 | ||
|
|
251605e2b8 | ||
|
|
fa2178ca3e | ||
|
|
3d19fa12f6 | ||
|
|
48aaecacce | ||
|
|
4bb86b6a86 | ||
|
|
0588116489 | ||
|
|
78a37cc00f | ||
|
|
fb90b01f6c | ||
|
|
ea55215668 | ||
|
|
58a86fa2dc | ||
|
|
8a7c4040b6 | ||
|
|
ea279ef45a | ||
|
|
8aa18add4b | ||
|
|
5ff52e6c93 | ||
|
|
ef19a85fc0 | ||
|
|
2ce3145195 | ||
|
|
b8d74dab47 | ||
|
|
e8f5efd156 | ||
|
|
57aa8b3de8 | ||
|
|
972b66f9a5 | ||
|
|
eadeea76e7 | ||
|
|
da7d3c55b0 | ||
|
|
ff0b6a6a6a | ||
|
|
60d3e4d963 | ||
|
|
adc230acde | ||
|
|
e4ba07a540 | ||
|
|
93ec5ac5c4 | ||
|
|
774c606b09 | ||
|
|
1bff42042d | ||
|
|
daeb177579 | ||
|
|
5d7a062f1b | ||
|
|
35f7bd5127 | ||
|
|
d411da451a | ||
|
|
45bab06f37 | ||
|
|
588855c6a7 | ||
|
|
c3fa0ae8cc | ||
|
|
bf85a69a2f | ||
|
|
72ff0b9fbb | ||
|
|
b9c9988ff4 | ||
|
|
1bb5ad22af | ||
|
|
4fa5cec0dc | ||
|
|
1e7254dfaa | ||
|
|
ca0818af7e | ||
|
|
b3d09513b8 | ||
|
|
2857a163e9 | ||
|
|
cba7e5350d | ||
|
|
29a5effabb | ||
|
|
39e07ac265 | ||
|
|
57986bd348 | ||
|
|
5e301937f2 | ||
|
|
4edde333ad | ||
|
|
c600b834e3 | ||
|
|
b6319d78bf | ||
|
|
e4fc2789fe | ||
|
|
4c5a1e064d | ||
|
|
4bb82110ab |
23
ChangeLog
23
ChangeLog
@@ -1,6 +1,29 @@
|
|||||||
# for this file format description,
|
# for this file format description,
|
||||||
# see https://github.com/olivierlacan/keep-a-changelog
|
# see https://github.com/olivierlacan/keep-a-changelog
|
||||||
|
|
||||||
|
## [2.56.0] - 2025-02-11
|
||||||
|
### Added
|
||||||
|
- Config params for shared local destination
|
||||||
|
- AddressBook full addresses cache
|
||||||
|
- Decline transit tunnel to duplicated router
|
||||||
|
- Recreate tunnels in random order
|
||||||
|
### Changed
|
||||||
|
- Exclude disk operations from SSU2 and NTCP2 threads
|
||||||
|
- Set minimal version for peer test to 0.9.62
|
||||||
|
- Send ack requested flag after second SSU2 resend attempt
|
||||||
|
- Shorter ECIESx25519 ack request interval for datagram and I2CP sessions
|
||||||
|
- Don't change datagram routing path too often if unidirectional data stream
|
||||||
|
- Reduce LeaseSet and local RouterInfo publishing confirmation intervals
|
||||||
|
- Don't delete buffer of connected routers or if an update received
|
||||||
|
- Smaller RouterInfo request timeout if sent directly
|
||||||
|
- Persist local RouterInfo in separate thread
|
||||||
|
- Don't recalculate and process ranges for every SSU2 Ack block
|
||||||
|
- Reseeds list
|
||||||
|
### Fixed
|
||||||
|
- Termination deadlock if SAM session is active
|
||||||
|
- Race condition at tunnel endpoint
|
||||||
|
- Inbound tunnel build encryption
|
||||||
|
|
||||||
## [2.55.0] - 2024-12-30
|
## [2.55.0] - 2024-12-30
|
||||||
### Added
|
### Added
|
||||||
- Support boost 1.87
|
- Support boost 1.87
|
||||||
|
|||||||
3
Makefile
3
Makefile
@@ -69,6 +69,9 @@ else ifneq (, $(findstring freebsd, $(SYS))$(findstring openbsd, $(SYS)))
|
|||||||
else ifneq (, $(findstring haiku, $(SYS)))
|
else ifneq (, $(findstring haiku, $(SYS)))
|
||||||
DAEMON_SRC += $(DAEMON_SRC_DIR)/UnixDaemon.cpp
|
DAEMON_SRC += $(DAEMON_SRC_DIR)/UnixDaemon.cpp
|
||||||
include Makefile.haiku
|
include Makefile.haiku
|
||||||
|
else ifneq (, $(findstring solaris, $(SYS)))
|
||||||
|
DAEMON_SRC += $(DAEMON_SRC_DIR)/UnixDaemon.cpp
|
||||||
|
include Makefile.solaris
|
||||||
else # not supported
|
else # not supported
|
||||||
$(error Not supported platform)
|
$(error Not supported platform)
|
||||||
endif
|
endif
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ CXXFLAGS ?= ${CXX_DEBUG} -Wall -Wextra -Wno-unused-parameter -pedantic -Wno-misl
|
|||||||
DEFINES = -D_GLIBCXX_USE_NANOSLEEP=1
|
DEFINES = -D_GLIBCXX_USE_NANOSLEEP=1
|
||||||
INCFLAGS = -I/usr/include/ -I/usr/local/include/
|
INCFLAGS = -I/usr/include/ -I/usr/local/include/
|
||||||
LDFLAGS = ${LD_DEBUG} -Wl,-rpath,/usr/local/lib -L/usr/local/lib
|
LDFLAGS = ${LD_DEBUG} -Wl,-rpath,/usr/local/lib -L/usr/local/lib
|
||||||
LDLIBS = -lcrypto -lssl -lz -lpthread -lboost_system -lboost_program_options
|
LDLIBS = -lssl -lcrypto -lz -lpthread -lboost_system -lboost_program_options
|
||||||
|
|
||||||
## NOTE: NEEDED_CXXFLAGS is here so that custom CXXFLAGS can be specified at build time
|
## NOTE: NEEDED_CXXFLAGS is here so that custom CXXFLAGS can be specified at build time
|
||||||
## **without** overwriting the CXXFLAGS which we need in order to build.
|
## **without** overwriting the CXXFLAGS which we need in order to build.
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ CXX = g++
|
|||||||
CXXFLAGS := -Wall -std=c++17
|
CXXFLAGS := -Wall -std=c++17
|
||||||
INCFLAGS = -I/system/develop/headers
|
INCFLAGS = -I/system/develop/headers
|
||||||
DEFINES = -D_DEFAULT_SOURCE -D_GNU_SOURCE
|
DEFINES = -D_DEFAULT_SOURCE -D_GNU_SOURCE
|
||||||
LDLIBS = -lbe -lbsd -lnetwork -lz -lcrypto -lssl -lboost_system -lboost_program_options -lpthread
|
LDLIBS = -lbe -lbsd -lnetwork -lz -lssl -lcrypto -lboost_system -lboost_program_options -lpthread
|
||||||
|
|
||||||
ifeq ($(USE_UPNP),yes)
|
ifeq ($(USE_UPNP),yes)
|
||||||
DEFINES += -DUSE_UPNP
|
DEFINES += -DUSE_UPNP
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ endif
|
|||||||
LDLIBS += -lpthread -ldl
|
LDLIBS += -lpthread -ldl
|
||||||
else
|
else
|
||||||
LDFLAGS += -L${SSLROOT}/lib -L${BOOSTROOT}/lib
|
LDFLAGS += -L${SSLROOT}/lib -L${BOOSTROOT}/lib
|
||||||
LDLIBS = -lz -lcrypto -lssl -lboost_system -lboost_filesystem -lboost_program_options -lpthread
|
LDLIBS = -lz -lssl -lcrypto -lboost_system -lboost_filesystem -lboost_program_options -lpthread
|
||||||
ifeq ($(USE_UPNP),yes)
|
ifeq ($(USE_UPNP),yes)
|
||||||
LDFLAGS += -L${UPNPROOT}/lib
|
LDFLAGS += -L${UPNPROOT}/lib
|
||||||
LDLIBS += -lminiupnpc
|
LDLIBS += -lminiupnpc
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ ifeq ($(USE_UPNP),yes)
|
|||||||
endif
|
endif
|
||||||
LDLIBS += -lpthread -ldl
|
LDLIBS += -lpthread -ldl
|
||||||
else
|
else
|
||||||
LDLIBS += -lcrypto -lssl -lz -lboost_program_options -lpthread -latomic
|
LDLIBS += -lssl -lcrypto -lz -lboost_program_options -lpthread -latomic
|
||||||
ifeq ($(USE_UPNP),yes)
|
ifeq ($(USE_UPNP),yes)
|
||||||
LDLIBS += -lminiupnpc
|
LDLIBS += -lminiupnpc
|
||||||
endif
|
endif
|
||||||
|
|||||||
@@ -7,9 +7,9 @@ LDFLAGS += -Wl,-dead_strip
|
|||||||
LDFLAGS += -Wl,-dead_strip_dylibs
|
LDFLAGS += -Wl,-dead_strip_dylibs
|
||||||
|
|
||||||
ifeq ($(USE_STATIC),yes)
|
ifeq ($(USE_STATIC),yes)
|
||||||
LDLIBS = -lz /usr/local/lib/libcrypto.a /usr/local/lib/libssl.a /usr/local/lib/libboost_system.a /usr/local/lib/libboost_filesystem.a /usr/local/lib/libboost_program_options.a -lpthread
|
LDLIBS = -lz /usr/local/lib/libssl.a /usr/local/lib/libcrypto.a /usr/local/lib/libboost_system.a /usr/local/lib/libboost_filesystem.a /usr/local/lib/libboost_program_options.a -lpthread
|
||||||
else
|
else
|
||||||
LDLIBS = -lz -lcrypto -lssl -lboost_system -lboost_filesystem -lboost_program_options -lpthread
|
LDLIBS = -lz -lssl -lcrypto -lboost_system -lboost_filesystem -lboost_program_options -lpthread
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifeq ($(USE_UPNP),yes)
|
ifeq ($(USE_UPNP),yes)
|
||||||
|
|||||||
9
Makefile.solaris
Normal file
9
Makefile.solaris
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
CXX = g++
|
||||||
|
INCFLAGS = -I/usr/openssl/3/include
|
||||||
|
CXXFLAGS := -Wall -std=c++20
|
||||||
|
LDLIBS = -L/usr/openssl/3/lib/64 -lssl -lcrypto -lboost_program_options -lz -lpthread -lsocket
|
||||||
|
|
||||||
|
ifeq ($(USE_UPNP),yes)
|
||||||
|
DEFINES += -DUSE_UPNP
|
||||||
|
LDLIBS += -lminiupnpc
|
||||||
|
endif
|
||||||
@@ -59,7 +59,7 @@ get_filename_component(_gitdescmoddir ${CMAKE_CURRENT_LIST_FILE} PATH)
|
|||||||
# function returns an empty string via _git_dir_var.
|
# function returns an empty string via _git_dir_var.
|
||||||
#
|
#
|
||||||
# Example: Given a path C:/bla/foo/bar and assuming C:/bla/.git exists and
|
# Example: Given a path C:/bla/foo/bar and assuming C:/bla/.git exists and
|
||||||
# neither foo nor bar contain a file/directory .git. This wil return
|
# neither foo nor bar contain a file/directory .git. This will return
|
||||||
# C:/bla/.git
|
# C:/bla/.git
|
||||||
#
|
#
|
||||||
function(_git_find_closest_git_dir _start_dir _git_dir_var)
|
function(_git_find_closest_git_dir _start_dir _git_dir_var)
|
||||||
|
|||||||
@@ -243,7 +243,7 @@ verify = true
|
|||||||
## Default: reg.i2p at "mainline" I2P Network
|
## Default: reg.i2p at "mainline" I2P Network
|
||||||
# defaulturl = http://shx5vqsw7usdaunyzr2qmes2fq37oumybpudrd4jjj4e4vk4uusa.b32.i2p/hosts.txt
|
# defaulturl = http://shx5vqsw7usdaunyzr2qmes2fq37oumybpudrd4jjj4e4vk4uusa.b32.i2p/hosts.txt
|
||||||
## Optional subscriptions URLs, separated by comma
|
## Optional subscriptions URLs, separated by comma
|
||||||
# subscriptions = http://reg.i2p/hosts.txt,http://identiguy.i2p/hosts.txt,http://stats.i2p/cgi-bin/newhosts.txt,http://rus.i2p/hosts.txt
|
# subscriptions = http://reg.i2p/hosts.txt,http://identiguy.i2p/hosts.txt,http://stats.i2p/cgi-bin/newhosts.txt
|
||||||
|
|
||||||
[limits]
|
[limits]
|
||||||
## Maximum active transit sessions (default: 5000)
|
## Maximum active transit sessions (default: 5000)
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
%define git_hash %(git rev-parse HEAD | cut -c -7)
|
%define git_hash %(git rev-parse HEAD | cut -c -7)
|
||||||
|
|
||||||
Name: i2pd-git
|
Name: i2pd-git
|
||||||
Version: 2.55.0
|
Version: 2.56.0
|
||||||
Release: git%{git_hash}%{?dist}
|
Release: git%{git_hash}%{?dist}
|
||||||
Summary: I2P router written in C++
|
Summary: I2P router written in C++
|
||||||
Conflicts: i2pd
|
Conflicts: i2pd
|
||||||
@@ -148,6 +148,9 @@ getent passwd i2pd >/dev/null || \
|
|||||||
|
|
||||||
|
|
||||||
%changelog
|
%changelog
|
||||||
|
* Tue Feb 11 2025 orignal <orignal@i2pmail.org> - 2.56.0
|
||||||
|
- update to 2.56.0
|
||||||
|
|
||||||
* Mon Dec 30 2024 orignal <orignal@i2pmail.org> - 2.55.0
|
* Mon Dec 30 2024 orignal <orignal@i2pmail.org> - 2.55.0
|
||||||
- update to 2.55.0
|
- update to 2.55.0
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
Name: i2pd
|
Name: i2pd
|
||||||
Version: 2.55.0
|
Version: 2.56.0
|
||||||
Release: 1%{?dist}
|
Release: 1%{?dist}
|
||||||
Summary: I2P router written in C++
|
Summary: I2P router written in C++
|
||||||
Conflicts: i2pd-git
|
Conflicts: i2pd-git
|
||||||
@@ -146,6 +146,9 @@ getent passwd i2pd >/dev/null || \
|
|||||||
|
|
||||||
|
|
||||||
%changelog
|
%changelog
|
||||||
|
* Tue Feb 11 2025 orignal <orignal@i2pmail.org> - 2.56.0
|
||||||
|
- update to 2.56.0
|
||||||
|
|
||||||
* Mon Dec 30 2024 orignal <orignal@i2pmail.org> - 2.55.0
|
* Mon Dec 30 2024 orignal <orignal@i2pmail.org> - 2.55.0
|
||||||
- update to 2.55.0
|
- update to 2.55.0
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ port = 6668
|
|||||||
destination = irc.ilita.i2p
|
destination = irc.ilita.i2p
|
||||||
destinationport = 6667
|
destinationport = 6667
|
||||||
keys = irc-keys.dat
|
keys = irc-keys.dat
|
||||||
|
i2p.streaming.profile=2
|
||||||
|
|
||||||
#[IRC-IRC2P]
|
#[IRC-IRC2P]
|
||||||
#type = client
|
#type = client
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2013-2024, The PurpleI2P Project
|
* Copyright (c) 2013-2025, The PurpleI2P Project
|
||||||
*
|
*
|
||||||
* This file is part of Purple i2pd project and licensed under BSD3
|
* This file is part of Purple i2pd project and licensed under BSD3
|
||||||
*
|
*
|
||||||
@@ -15,7 +15,6 @@
|
|||||||
// Use global placeholders from boost introduced when local_time.hpp is loaded
|
// Use global placeholders from boost introduced when local_time.hpp is loaded
|
||||||
#define BOOST_BIND_GLOBAL_PLACEHOLDERS
|
#define BOOST_BIND_GLOBAL_PLACEHOLDERS
|
||||||
#include <boost/property_tree/json_parser.hpp>
|
#include <boost/property_tree/json_parser.hpp>
|
||||||
#include <boost/lexical_cast.hpp>
|
|
||||||
|
|
||||||
#include "FS.h"
|
#include "FS.h"
|
||||||
#include "Log.h"
|
#include "Log.h"
|
||||||
@@ -30,11 +29,24 @@ namespace i2p
|
|||||||
namespace client
|
namespace client
|
||||||
{
|
{
|
||||||
I2PControlService::I2PControlService (const std::string& address, int port):
|
I2PControlService::I2PControlService (const std::string& address, int port):
|
||||||
m_IsRunning (false), m_Thread (nullptr),
|
m_IsRunning (false),
|
||||||
m_Acceptor (m_Service, boost::asio::ip::tcp::endpoint(boost::asio::ip::make_address(address), port)),
|
|
||||||
m_SSLContext (boost::asio::ssl::context::sslv23),
|
m_SSLContext (boost::asio::ssl::context::sslv23),
|
||||||
m_ShutdownTimer (m_Service)
|
m_ShutdownTimer (m_Service)
|
||||||
{
|
{
|
||||||
|
if (port)
|
||||||
|
m_Acceptor = std::make_unique<boost::asio::ip::tcp::acceptor>(m_Service,
|
||||||
|
boost::asio::ip::tcp::endpoint(boost::asio::ip::make_address(address), port));
|
||||||
|
else
|
||||||
|
#if defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
|
||||||
|
{
|
||||||
|
std::remove (address.c_str ()); // just in case
|
||||||
|
m_LocalAcceptor = std::make_unique<boost::asio::local::stream_protocol::acceptor>(m_Service,
|
||||||
|
boost::asio::local::stream_protocol::endpoint(address));
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
LogPrint(eLogError, "I2PControl: Local sockets are not supported");
|
||||||
|
#endif
|
||||||
|
|
||||||
i2p::config::GetOption("i2pcontrol.password", m_Password);
|
i2p::config::GetOption("i2pcontrol.password", m_Password);
|
||||||
|
|
||||||
// certificate / keys
|
// certificate / keys
|
||||||
@@ -98,7 +110,7 @@ namespace client
|
|||||||
{
|
{
|
||||||
Accept ();
|
Accept ();
|
||||||
m_IsRunning = true;
|
m_IsRunning = true;
|
||||||
m_Thread = new std::thread (std::bind (&I2PControlService::Run, this));
|
m_Thread = std::make_unique<std::thread>(std::bind (&I2PControlService::Run, this));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,12 +119,19 @@ namespace client
|
|||||||
if (m_IsRunning)
|
if (m_IsRunning)
|
||||||
{
|
{
|
||||||
m_IsRunning = false;
|
m_IsRunning = false;
|
||||||
m_Acceptor.cancel ();
|
if (m_Acceptor) m_Acceptor->cancel ();
|
||||||
|
#if defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
|
||||||
|
if (m_LocalAcceptor)
|
||||||
|
{
|
||||||
|
auto path = m_LocalAcceptor->local_endpoint().path();
|
||||||
|
m_LocalAcceptor->cancel ();
|
||||||
|
std::remove (path.c_str ());
|
||||||
|
}
|
||||||
|
#endif
|
||||||
m_Service.stop ();
|
m_Service.stop ();
|
||||||
if (m_Thread)
|
if (m_Thread)
|
||||||
{
|
{
|
||||||
m_Thread->join ();
|
m_Thread->join ();
|
||||||
delete m_Thread;
|
|
||||||
m_Thread = nullptr;
|
m_Thread = nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -134,40 +153,60 @@ namespace client
|
|||||||
|
|
||||||
void I2PControlService::Accept ()
|
void I2PControlService::Accept ()
|
||||||
{
|
{
|
||||||
auto newSocket = std::make_shared<ssl_socket> (m_Service, m_SSLContext);
|
if (m_Acceptor)
|
||||||
m_Acceptor.async_accept (newSocket->lowest_layer(), std::bind (&I2PControlService::HandleAccept, this,
|
{
|
||||||
std::placeholders::_1, newSocket));
|
auto newSocket = std::make_shared<boost::asio::ssl::stream<boost::asio::ip::tcp::socket> > (m_Service, m_SSLContext);
|
||||||
|
m_Acceptor->async_accept (newSocket->lowest_layer(),
|
||||||
|
[this, newSocket](const boost::system::error_code& ecode)
|
||||||
|
{
|
||||||
|
HandleAccepted (ecode, newSocket);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
#if defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
|
||||||
|
else if (m_LocalAcceptor)
|
||||||
|
{
|
||||||
|
auto newSocket = std::make_shared<boost::asio::ssl::stream<boost::asio::local::stream_protocol::socket> > (m_Service, m_SSLContext);
|
||||||
|
m_LocalAcceptor->async_accept (newSocket->lowest_layer(),
|
||||||
|
[this, newSocket](const boost::system::error_code& ecode)
|
||||||
|
{
|
||||||
|
HandleAccepted (ecode, newSocket);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void I2PControlService::HandleAccept(const boost::system::error_code& ecode, std::shared_ptr<ssl_socket> socket)
|
template<typename ssl_socket>
|
||||||
|
void I2PControlService::HandleAccepted (const boost::system::error_code& ecode,
|
||||||
|
std::shared_ptr<ssl_socket> newSocket)
|
||||||
{
|
{
|
||||||
if (ecode != boost::asio::error::operation_aborted)
|
if (ecode != boost::asio::error::operation_aborted)
|
||||||
Accept ();
|
Accept ();
|
||||||
|
|
||||||
if (ecode) {
|
if (ecode)
|
||||||
|
{
|
||||||
LogPrint (eLogError, "I2PControl: Accept error: ", ecode.message ());
|
LogPrint (eLogError, "I2PControl: Accept error: ", ecode.message ());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
LogPrint (eLogDebug, "I2PControl: New request from ", socket->lowest_layer ().remote_endpoint ());
|
LogPrint (eLogDebug, "I2PControl: New request from ", newSocket->lowest_layer ().remote_endpoint ());
|
||||||
Handshake (socket);
|
Handshake (newSocket);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename ssl_socket>
|
||||||
void I2PControlService::Handshake (std::shared_ptr<ssl_socket> socket)
|
void I2PControlService::Handshake (std::shared_ptr<ssl_socket> socket)
|
||||||
{
|
{
|
||||||
socket->async_handshake(boost::asio::ssl::stream_base::server,
|
socket->async_handshake(boost::asio::ssl::stream_base::server,
|
||||||
std::bind( &I2PControlService::HandleHandshake, this, std::placeholders::_1, socket));
|
[this, socket](const boost::system::error_code& ecode)
|
||||||
}
|
{
|
||||||
|
if (ecode)
|
||||||
void I2PControlService::HandleHandshake (const boost::system::error_code& ecode, std::shared_ptr<ssl_socket> socket)
|
|
||||||
{
|
{
|
||||||
if (ecode) {
|
|
||||||
LogPrint (eLogError, "I2PControl: Handshake error: ", ecode.message ());
|
LogPrint (eLogError, "I2PControl: Handshake error: ", ecode.message ());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
//std::this_thread::sleep_for (std::chrono::milliseconds(5));
|
|
||||||
ReadRequest (socket);
|
ReadRequest (socket);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename ssl_socket>
|
||||||
void I2PControlService::ReadRequest (std::shared_ptr<ssl_socket> socket)
|
void I2PControlService::ReadRequest (std::shared_ptr<ssl_socket> socket)
|
||||||
{
|
{
|
||||||
auto request = std::make_shared<I2PControlBuffer>();
|
auto request = std::make_shared<I2PControlBuffer>();
|
||||||
@@ -177,10 +216,13 @@ namespace client
|
|||||||
#else
|
#else
|
||||||
boost::asio::buffer (request->data (), request->size ()),
|
boost::asio::buffer (request->data (), request->size ()),
|
||||||
#endif
|
#endif
|
||||||
std::bind(&I2PControlService::HandleRequestReceived, this,
|
[this, socket, request](const boost::system::error_code& ecode, size_t bytes_transferred)
|
||||||
std::placeholders::_1, std::placeholders::_2, socket, request));
|
{
|
||||||
|
HandleRequestReceived (ecode, bytes_transferred, socket, request);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename ssl_socket>
|
||||||
void I2PControlService::HandleRequestReceived (const boost::system::error_code& ecode,
|
void I2PControlService::HandleRequestReceived (const boost::system::error_code& ecode,
|
||||||
size_t bytes_transferred, std::shared_ptr<ssl_socket> socket,
|
size_t bytes_transferred, std::shared_ptr<ssl_socket> socket,
|
||||||
std::shared_ptr<I2PControlBuffer> buf)
|
std::shared_ptr<I2PControlBuffer> buf)
|
||||||
@@ -258,6 +300,7 @@ namespace client
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename ssl_socket>
|
||||||
void I2PControlService::SendResponse (std::shared_ptr<ssl_socket> socket,
|
void I2PControlService::SendResponse (std::shared_ptr<ssl_socket> socket,
|
||||||
std::shared_ptr<I2PControlBuffer> buf, std::ostringstream& response, bool isHtml)
|
std::shared_ptr<I2PControlBuffer> buf, std::ostringstream& response, bool isHtml)
|
||||||
{
|
{
|
||||||
@@ -267,7 +310,7 @@ namespace client
|
|||||||
std::ostringstream header;
|
std::ostringstream header;
|
||||||
header << "HTTP/1.1 200 OK\r\n";
|
header << "HTTP/1.1 200 OK\r\n";
|
||||||
header << "Connection: close\r\n";
|
header << "Connection: close\r\n";
|
||||||
header << "Content-Length: " << boost::lexical_cast<std::string>(len) << "\r\n";
|
header << "Content-Length: " << std::to_string(len) << "\r\n";
|
||||||
header << "Content-Type: application/json\r\n";
|
header << "Content-Type: application/json\r\n";
|
||||||
header << "Date: ";
|
header << "Date: ";
|
||||||
std::time_t t = std::time (nullptr);
|
std::time_t t = std::time (nullptr);
|
||||||
@@ -280,16 +323,11 @@ namespace client
|
|||||||
memcpy (buf->data () + offset, response.str ().c_str (), len);
|
memcpy (buf->data () + offset, response.str ().c_str (), len);
|
||||||
boost::asio::async_write (*socket, boost::asio::buffer (buf->data (), offset + len),
|
boost::asio::async_write (*socket, boost::asio::buffer (buf->data (), offset + len),
|
||||||
boost::asio::transfer_all (),
|
boost::asio::transfer_all (),
|
||||||
std::bind(&I2PControlService::HandleResponseSent, this,
|
[socket, buf](const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
||||||
std::placeholders::_1, std::placeholders::_2, socket, buf));
|
|
||||||
}
|
|
||||||
|
|
||||||
void I2PControlService::HandleResponseSent (const boost::system::error_code& ecode, std::size_t bytes_transferred,
|
|
||||||
std::shared_ptr<ssl_socket> socket, std::shared_ptr<I2PControlBuffer> buf)
|
|
||||||
{
|
{
|
||||||
if (ecode) {
|
if (ecode)
|
||||||
LogPrint (eLogError, "I2PControl: Write error: ", ecode.message ());
|
LogPrint (eLogError, "I2PControl: Write error: ", ecode.message ());
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// handlers
|
// handlers
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2013-2024, The PurpleI2P Project
|
* Copyright (c) 2013-2025, The PurpleI2P Project
|
||||||
*
|
*
|
||||||
* This file is part of Purple i2pd project and licensed under BSD3
|
* This file is part of Purple i2pd project and licensed under BSD3
|
||||||
*
|
*
|
||||||
@@ -35,8 +35,6 @@ namespace client
|
|||||||
|
|
||||||
class I2PControlService: public I2PControlHandlers
|
class I2PControlService: public I2PControlHandlers
|
||||||
{
|
{
|
||||||
typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> ssl_socket;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
I2PControlService (const std::string& address, int port);
|
I2PControlService (const std::string& address, int port);
|
||||||
@@ -49,16 +47,18 @@ namespace client
|
|||||||
|
|
||||||
void Run ();
|
void Run ();
|
||||||
void Accept ();
|
void Accept ();
|
||||||
void HandleAccept(const boost::system::error_code& ecode, std::shared_ptr<ssl_socket> socket);
|
template<typename ssl_socket>
|
||||||
|
void HandleAccepted (const boost::system::error_code& ecode, std::shared_ptr<ssl_socket> newSocket);
|
||||||
|
template<typename ssl_socket>
|
||||||
void Handshake (std::shared_ptr<ssl_socket> socket);
|
void Handshake (std::shared_ptr<ssl_socket> socket);
|
||||||
void HandleHandshake (const boost::system::error_code& ecode, std::shared_ptr<ssl_socket> socket);
|
template<typename ssl_socket>
|
||||||
void ReadRequest (std::shared_ptr<ssl_socket> socket);
|
void ReadRequest (std::shared_ptr<ssl_socket> socket);
|
||||||
|
template<typename ssl_socket>
|
||||||
void HandleRequestReceived (const boost::system::error_code& ecode, size_t bytes_transferred,
|
void HandleRequestReceived (const boost::system::error_code& ecode, size_t bytes_transferred,
|
||||||
std::shared_ptr<ssl_socket> socket, std::shared_ptr<I2PControlBuffer> buf);
|
std::shared_ptr<ssl_socket> socket, std::shared_ptr<I2PControlBuffer> buf);
|
||||||
|
template<typename ssl_socket>
|
||||||
void SendResponse (std::shared_ptr<ssl_socket> socket,
|
void SendResponse (std::shared_ptr<ssl_socket> socket,
|
||||||
std::shared_ptr<I2PControlBuffer> buf, std::ostringstream& response, bool isHtml);
|
std::shared_ptr<I2PControlBuffer> buf, std::ostringstream& response, bool isHtml);
|
||||||
void HandleResponseSent (const boost::system::error_code& ecode, std::size_t bytes_transferred,
|
|
||||||
std::shared_ptr<ssl_socket> socket, std::shared_ptr<I2PControlBuffer> buf);
|
|
||||||
|
|
||||||
void CreateCertificate (const char *crt_path, const char *key_path);
|
void CreateCertificate (const char *crt_path, const char *key_path);
|
||||||
|
|
||||||
@@ -86,10 +86,13 @@ namespace client
|
|||||||
|
|
||||||
std::string m_Password;
|
std::string m_Password;
|
||||||
bool m_IsRunning;
|
bool m_IsRunning;
|
||||||
std::thread * m_Thread;
|
std::unique_ptr<std::thread> m_Thread;
|
||||||
|
|
||||||
boost::asio::io_context m_Service;
|
boost::asio::io_context m_Service;
|
||||||
boost::asio::ip::tcp::acceptor m_Acceptor;
|
std::unique_ptr<boost::asio::ip::tcp::acceptor> m_Acceptor;
|
||||||
|
#if defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
|
||||||
|
std::unique_ptr<boost::asio::local::stream_protocol::acceptor> m_LocalAcceptor;
|
||||||
|
#endif
|
||||||
boost::asio::ssl::context m_SSLContext;
|
boost::asio::ssl::context m_SSLContext;
|
||||||
boost::asio::deadline_timer m_ShutdownTimer;
|
boost::asio::deadline_timer m_ShutdownTimer;
|
||||||
std::set<std::string> m_Tokens;
|
std::set<std::string> m_Tokens;
|
||||||
|
|||||||
6
debian/changelog
vendored
6
debian/changelog
vendored
@@ -1,3 +1,9 @@
|
|||||||
|
i2pd (2.56.0-1) unstable; urgency=medium
|
||||||
|
|
||||||
|
* updated to version 2.56.0/0.9.65
|
||||||
|
|
||||||
|
-- orignal <orignal@i2pmail.org> Tue, 11 Feb 2025 16:00:00 +0000
|
||||||
|
|
||||||
i2pd (2.55.0-1) unstable; urgency=medium
|
i2pd (2.55.0-1) unstable; urgency=medium
|
||||||
|
|
||||||
* updated to version 2.55.0
|
* updated to version 2.55.0
|
||||||
|
|||||||
@@ -238,7 +238,7 @@ namespace config {
|
|||||||
"https://reseed.onion.im/,"
|
"https://reseed.onion.im/,"
|
||||||
"https://i2pseed.creativecowpat.net:8443/,"
|
"https://i2pseed.creativecowpat.net:8443/,"
|
||||||
"https://reseed.i2pgit.org/,"
|
"https://reseed.i2pgit.org/,"
|
||||||
"https://banana.incognet.io/,"
|
"https://coconut.incognet.io/,"
|
||||||
"https://reseed-pl.i2pd.xyz/,"
|
"https://reseed-pl.i2pd.xyz/,"
|
||||||
"https://www2.mk16.de/,"
|
"https://www2.mk16.de/,"
|
||||||
"https://i2p.ghativega.in/,"
|
"https://i2p.ghativega.in/,"
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2013-2024, The PurpleI2P Project
|
* Copyright (c) 2013-2025, The PurpleI2P Project
|
||||||
*
|
*
|
||||||
* This file is part of Purple i2pd project and licensed under BSD3
|
* This file is part of Purple i2pd project and licensed under BSD3
|
||||||
*
|
*
|
||||||
@@ -711,18 +711,40 @@ namespace crypto
|
|||||||
return AEADChaCha20Poly1305 (m_Ctx, msg, msgLen, ad, adLen, key, nonce, buf, len, false);
|
return AEADChaCha20Poly1305 (m_Ctx, msg, msgLen, ad, adLen, key, nonce, buf, len, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ChaCha20 (const uint8_t * msg, size_t msgLen, const uint8_t * key, const uint8_t * nonce, uint8_t * out)
|
static void ChaCha20 (EVP_CIPHER_CTX *ctx, const uint8_t * msg, size_t msgLen, const uint8_t * key, const uint8_t * nonce, uint8_t * out)
|
||||||
{
|
{
|
||||||
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new ();
|
|
||||||
uint32_t iv[4];
|
uint32_t iv[4];
|
||||||
iv[0] = htole32 (1); memcpy (iv + 1, nonce, 12); // counter | nonce
|
iv[0] = htole32 (1); memcpy (iv + 1, nonce, 12); // counter | nonce
|
||||||
EVP_EncryptInit_ex(ctx, EVP_chacha20 (), NULL, key, (const uint8_t *)iv);
|
EVP_EncryptInit_ex(ctx, EVP_chacha20 (), NULL, key, (const uint8_t *)iv);
|
||||||
int outlen = 0;
|
int outlen = 0;
|
||||||
EVP_EncryptUpdate(ctx, out, &outlen, msg, msgLen);
|
EVP_EncryptUpdate(ctx, out, &outlen, msg, msgLen);
|
||||||
EVP_EncryptFinal_ex(ctx, NULL, &outlen);
|
EVP_EncryptFinal_ex(ctx, NULL, &outlen);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ChaCha20 (const uint8_t * msg, size_t msgLen, const uint8_t * key, const uint8_t * nonce, uint8_t * out)
|
||||||
|
{
|
||||||
|
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new ();
|
||||||
|
ChaCha20 (ctx, msg, msgLen, key, nonce, out);
|
||||||
EVP_CIPHER_CTX_free (ctx);
|
EVP_CIPHER_CTX_free (ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
ChaCha20Context::ChaCha20Context ()
|
||||||
|
{
|
||||||
|
m_Ctx = EVP_CIPHER_CTX_new ();
|
||||||
|
}
|
||||||
|
|
||||||
|
ChaCha20Context::~ChaCha20Context ()
|
||||||
|
{
|
||||||
|
if (m_Ctx)
|
||||||
|
EVP_CIPHER_CTX_free (m_Ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ChaCha20Context::operator ()(const uint8_t * msg, size_t msgLen, const uint8_t * key, const uint8_t * nonce, uint8_t * out)
|
||||||
|
{
|
||||||
|
ChaCha20 (m_Ctx, msg, msgLen, key, nonce, out);
|
||||||
|
}
|
||||||
|
|
||||||
void HKDF (const uint8_t * salt, const uint8_t * key, size_t keyLen, const std::string& info,
|
void HKDF (const uint8_t * salt, const uint8_t * key, size_t keyLen, const std::string& info,
|
||||||
uint8_t * out, size_t outLen)
|
uint8_t * out, size_t outLen)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2013-2024, The PurpleI2P Project
|
* Copyright (c) 2013-2025, The PurpleI2P Project
|
||||||
*
|
*
|
||||||
* This file is part of Purple i2pd project and licensed under BSD3
|
* This file is part of Purple i2pd project and licensed under BSD3
|
||||||
*
|
*
|
||||||
@@ -226,6 +226,19 @@ namespace crypto
|
|||||||
// ChaCha20
|
// ChaCha20
|
||||||
void ChaCha20 (const uint8_t * msg, size_t msgLen, const uint8_t * key, const uint8_t * nonce, uint8_t * out);
|
void ChaCha20 (const uint8_t * msg, size_t msgLen, const uint8_t * key, const uint8_t * nonce, uint8_t * out);
|
||||||
|
|
||||||
|
class ChaCha20Context
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
ChaCha20Context ();
|
||||||
|
~ChaCha20Context ();
|
||||||
|
void operator ()(const uint8_t * msg, size_t msgLen, const uint8_t * key, const uint8_t * nonce, uint8_t * out);
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
EVP_CIPHER_CTX * m_Ctx;
|
||||||
|
};
|
||||||
|
|
||||||
// HKDF
|
// HKDF
|
||||||
|
|
||||||
void HKDF (const uint8_t * salt, const uint8_t * key, size_t keyLen, const std::string& info, uint8_t * out, size_t outLen = 64); // salt - 32, out - 32 or 64, info <= 32
|
void HKDF (const uint8_t * salt, const uint8_t * key, size_t keyLen, const std::string& info, uint8_t * out, size_t outLen = 64); // salt - 32, out - 32 or 64, info <= 32
|
||||||
|
|||||||
@@ -195,7 +195,7 @@ namespace client
|
|||||||
m_IsPublic = itr->second != "true";
|
m_IsPublic = itr->second != "true";
|
||||||
}
|
}
|
||||||
|
|
||||||
int inLen, outLen, inQuant, outQuant, numTags, minLatency, maxLatency;
|
int inLen = 0, outLen = 0, inQuant = 0, outQuant = 0, numTags = 0, minLatency = 0, maxLatency = 0;
|
||||||
std::map<std::string, int&> intOpts = {
|
std::map<std::string, int&> intOpts = {
|
||||||
{I2CP_PARAM_INBOUND_TUNNEL_LENGTH, inLen},
|
{I2CP_PARAM_INBOUND_TUNNEL_LENGTH, inLen},
|
||||||
{I2CP_PARAM_OUTBOUND_TUNNEL_LENGTH, outLen},
|
{I2CP_PARAM_OUTBOUND_TUNNEL_LENGTH, outLen},
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2013-2024, The PurpleI2P Project
|
* Copyright (c) 2013-2025, The PurpleI2P Project
|
||||||
*
|
*
|
||||||
* This file is part of Purple i2pd project and licensed under BSD3
|
* This file is part of Purple i2pd project and licensed under BSD3
|
||||||
*
|
*
|
||||||
@@ -90,10 +90,10 @@ namespace data
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool Families::VerifyFamily (const std::string& family, const IdentHash& ident,
|
bool Families::VerifyFamily (const std::string& family, const IdentHash& ident,
|
||||||
const char * signature, const char * key) const
|
std::string_view signature, const char * key) const
|
||||||
{
|
{
|
||||||
uint8_t buf[100], signatureBuf[64];
|
uint8_t buf[100], signatureBuf[64];
|
||||||
size_t len = family.length (), signatureLen = strlen (signature);
|
size_t len = family.length ();
|
||||||
if (len + 32 > 100)
|
if (len + 32 > 100)
|
||||||
{
|
{
|
||||||
LogPrint (eLogError, "Family: ", family, " is too long");
|
LogPrint (eLogError, "Family: ", family, " is too long");
|
||||||
@@ -105,7 +105,7 @@ namespace data
|
|||||||
memcpy (buf, family.c_str (), len);
|
memcpy (buf, family.c_str (), len);
|
||||||
memcpy (buf + len, (const uint8_t *)ident, 32);
|
memcpy (buf + len, (const uint8_t *)ident, 32);
|
||||||
len += 32;
|
len += 32;
|
||||||
auto signatureBufLen = Base64ToByteStream (signature, signatureLen, signatureBuf, 64);
|
auto signatureBufLen = Base64ToByteStream (signature.data (), signature.length (), signatureBuf, 64);
|
||||||
if (signatureBufLen)
|
if (signatureBufLen)
|
||||||
{
|
{
|
||||||
EVP_MD_CTX * ctx = EVP_MD_CTX_create ();
|
EVP_MD_CTX * ctx = EVP_MD_CTX_create ();
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2013-2024, The PurpleI2P Project
|
* Copyright (c) 2013-2025, The PurpleI2P Project
|
||||||
*
|
*
|
||||||
* This file is part of Purple i2pd project and licensed under BSD3
|
* This file is part of Purple i2pd project and licensed under BSD3
|
||||||
*
|
*
|
||||||
@@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <string_view>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <openssl/evp.h>
|
#include <openssl/evp.h>
|
||||||
#include "Identity.h"
|
#include "Identity.h"
|
||||||
@@ -28,7 +29,7 @@ namespace data
|
|||||||
~Families ();
|
~Families ();
|
||||||
void LoadCertificates ();
|
void LoadCertificates ();
|
||||||
bool VerifyFamily (const std::string& family, const IdentHash& ident,
|
bool VerifyFamily (const std::string& family, const IdentHash& ident,
|
||||||
const char * signature, const char * key = nullptr) const;
|
std::string_view signature, const char * key = nullptr) const;
|
||||||
FamilyID GetFamilyID (const std::string& family) const;
|
FamilyID GetFamilyID (const std::string& family) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -27,18 +27,15 @@ namespace data
|
|||||||
|
|
||||||
size_t Identity::FromBuffer (const uint8_t * buf, size_t len)
|
size_t Identity::FromBuffer (const uint8_t * buf, size_t len)
|
||||||
{
|
{
|
||||||
if ( len < DEFAULT_IDENTITY_SIZE ) {
|
if (len < DEFAULT_IDENTITY_SIZE) return 0; // buffer too small, don't overflow
|
||||||
// buffer too small, don't overflow
|
memcpy (this, buf, DEFAULT_IDENTITY_SIZE);
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
memcpy (publicKey, buf, DEFAULT_IDENTITY_SIZE);
|
|
||||||
return DEFAULT_IDENTITY_SIZE;
|
return DEFAULT_IDENTITY_SIZE;
|
||||||
}
|
}
|
||||||
|
|
||||||
IdentHash Identity::Hash () const
|
IdentHash Identity::Hash () const
|
||||||
{
|
{
|
||||||
IdentHash hash;
|
IdentHash hash;
|
||||||
SHA256(publicKey, DEFAULT_IDENTITY_SIZE, hash);
|
SHA256((const uint8_t *)this, DEFAULT_IDENTITY_SIZE, hash);
|
||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -402,12 +399,8 @@ namespace data
|
|||||||
return std::make_shared<i2p::crypto::ECIESX25519AEADRatchetEncryptor>(key);
|
return std::make_shared<i2p::crypto::ECIESX25519AEADRatchetEncryptor>(key);
|
||||||
break;
|
break;
|
||||||
case CRYPTO_KEY_TYPE_ECIES_P256_SHA256_AES256CBC:
|
case CRYPTO_KEY_TYPE_ECIES_P256_SHA256_AES256CBC:
|
||||||
case CRYPTO_KEY_TYPE_ECIES_P256_SHA256_AES256CBC_TEST:
|
|
||||||
return std::make_shared<i2p::crypto::ECIESP256Encryptor>(key);
|
return std::make_shared<i2p::crypto::ECIESP256Encryptor>(key);
|
||||||
break;
|
break;
|
||||||
case CRYPTO_KEY_TYPE_ECIES_GOSTR3410_CRYPTO_PRO_A_SHA256_AES256CBC:
|
|
||||||
return std::make_shared<i2p::crypto::ECIESGOSTR3410Encryptor>(key);
|
|
||||||
break;
|
|
||||||
default:
|
default:
|
||||||
LogPrint (eLogError, "Identity: Unknown crypto key type ", (int)keyType);
|
LogPrint (eLogError, "Identity: Unknown crypto key type ", (int)keyType);
|
||||||
};
|
};
|
||||||
@@ -676,12 +669,8 @@ namespace data
|
|||||||
return std::make_shared<i2p::crypto::ECIESX25519AEADRatchetDecryptor>(key);
|
return std::make_shared<i2p::crypto::ECIESX25519AEADRatchetDecryptor>(key);
|
||||||
break;
|
break;
|
||||||
case CRYPTO_KEY_TYPE_ECIES_P256_SHA256_AES256CBC:
|
case CRYPTO_KEY_TYPE_ECIES_P256_SHA256_AES256CBC:
|
||||||
case CRYPTO_KEY_TYPE_ECIES_P256_SHA256_AES256CBC_TEST:
|
|
||||||
return std::make_shared<i2p::crypto::ECIESP256Decryptor>(key);
|
return std::make_shared<i2p::crypto::ECIESP256Decryptor>(key);
|
||||||
break;
|
break;
|
||||||
case CRYPTO_KEY_TYPE_ECIES_GOSTR3410_CRYPTO_PRO_A_SHA256_AES256CBC:
|
|
||||||
return std::make_shared<i2p::crypto::ECIESGOSTR3410Decryptor>(key);
|
|
||||||
break;
|
|
||||||
default:
|
default:
|
||||||
LogPrint (eLogError, "Identity: Unknown crypto key type ", (int)cryptoType);
|
LogPrint (eLogError, "Identity: Unknown crypto key type ", (int)cryptoType);
|
||||||
};
|
};
|
||||||
@@ -756,12 +745,8 @@ namespace data
|
|||||||
i2p::crypto::GenerateElGamalKeyPair(priv, pub);
|
i2p::crypto::GenerateElGamalKeyPair(priv, pub);
|
||||||
break;
|
break;
|
||||||
case CRYPTO_KEY_TYPE_ECIES_P256_SHA256_AES256CBC:
|
case CRYPTO_KEY_TYPE_ECIES_P256_SHA256_AES256CBC:
|
||||||
case CRYPTO_KEY_TYPE_ECIES_P256_SHA256_AES256CBC_TEST:
|
|
||||||
i2p::crypto::CreateECIESP256RandomKeys (priv, pub);
|
i2p::crypto::CreateECIESP256RandomKeys (priv, pub);
|
||||||
break;
|
break;
|
||||||
case CRYPTO_KEY_TYPE_ECIES_GOSTR3410_CRYPTO_PRO_A_SHA256_AES256CBC:
|
|
||||||
i2p::crypto::CreateECIESGOSTR3410RandomKeys (priv, pub);
|
|
||||||
break;
|
|
||||||
case CRYPTO_KEY_TYPE_ECIES_X25519_AEAD:
|
case CRYPTO_KEY_TYPE_ECIES_X25519_AEAD:
|
||||||
i2p::crypto::CreateECIESX25519AEADRatchetRandomKeys (priv, pub);
|
i2p::crypto::CreateECIESX25519AEADRatchetRandomKeys (priv, pub);
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -64,8 +64,6 @@ namespace data
|
|||||||
const uint16_t CRYPTO_KEY_TYPE_ELGAMAL = 0;
|
const uint16_t CRYPTO_KEY_TYPE_ELGAMAL = 0;
|
||||||
const uint16_t CRYPTO_KEY_TYPE_ECIES_P256_SHA256_AES256CBC = 1;
|
const uint16_t CRYPTO_KEY_TYPE_ECIES_P256_SHA256_AES256CBC = 1;
|
||||||
const uint16_t CRYPTO_KEY_TYPE_ECIES_X25519_AEAD = 4;
|
const uint16_t CRYPTO_KEY_TYPE_ECIES_X25519_AEAD = 4;
|
||||||
const uint16_t CRYPTO_KEY_TYPE_ECIES_P256_SHA256_AES256CBC_TEST = 65280; // TODO: remove later
|
|
||||||
const uint16_t CRYPTO_KEY_TYPE_ECIES_GOSTR3410_CRYPTO_PRO_A_SHA256_AES256CBC = 65281; // TODO: use GOST R 34.11 instead SHA256 and GOST 28147-89 instead AES
|
|
||||||
|
|
||||||
const uint16_t SIGNING_KEY_TYPE_DSA_SHA1 = 0;
|
const uint16_t SIGNING_KEY_TYPE_DSA_SHA1 = 0;
|
||||||
const uint16_t SIGNING_KEY_TYPE_ECDSA_SHA256_P256 = 1;
|
const uint16_t SIGNING_KEY_TYPE_ECDSA_SHA256_P256 = 1;
|
||||||
@@ -75,7 +73,7 @@ namespace data
|
|||||||
const uint16_t SIGNING_KEY_TYPE_RSA_SHA384_3072 = 5;
|
const uint16_t SIGNING_KEY_TYPE_RSA_SHA384_3072 = 5;
|
||||||
const uint16_t SIGNING_KEY_TYPE_RSA_SHA512_4096 = 6;
|
const uint16_t SIGNING_KEY_TYPE_RSA_SHA512_4096 = 6;
|
||||||
const uint16_t SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519 = 7;
|
const uint16_t SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519 = 7;
|
||||||
const uint16_t SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519ph = 8; // not implemented
|
const uint16_t SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519ph = 8; // since openssl 3.0.0
|
||||||
const uint16_t SIGNING_KEY_TYPE_GOSTR3410_CRYPTO_PRO_A_GOSTR3411_256 = 9;
|
const uint16_t SIGNING_KEY_TYPE_GOSTR3410_CRYPTO_PRO_A_GOSTR3411_256 = 9;
|
||||||
const uint16_t SIGNING_KEY_TYPE_GOSTR3410_TC26_A_512_GOSTR3411_512 = 10; // approved by FSB
|
const uint16_t SIGNING_KEY_TYPE_GOSTR3410_TC26_A_512_GOSTR3411_512 = 10; // approved by FSB
|
||||||
const uint16_t SIGNING_KEY_TYPE_REDDSA_SHA512_ED25519 = 11; // for LeaseSet2 only
|
const uint16_t SIGNING_KEY_TYPE_REDDSA_SHA512_ED25519 = 11; // for LeaseSet2 only
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2013-2024, The PurpleI2P Project
|
* Copyright (c) 2013-2025, The PurpleI2P Project
|
||||||
*
|
*
|
||||||
* This file is part of Purple i2pd project and licensed under BSD3
|
* This file is part of Purple i2pd project and licensed under BSD3
|
||||||
*
|
*
|
||||||
@@ -819,17 +819,22 @@ namespace transport
|
|||||||
Terminate ();
|
Terminate ();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
std::shared_ptr<i2p::data::RouterProfile> profile; // not null if older
|
|
||||||
|
bool isOlder = false;
|
||||||
if (ri.GetTimestamp () + i2p::data::NETDB_EXPIRATION_TIMEOUT_THRESHOLD*1000LL < ri1->GetTimestamp ())
|
if (ri.GetTimestamp () + i2p::data::NETDB_EXPIRATION_TIMEOUT_THRESHOLD*1000LL < ri1->GetTimestamp ())
|
||||||
{
|
{
|
||||||
// received RouterInfo is older than one in netdb
|
// received RouterInfo is older than one in netdb
|
||||||
profile = i2p::data::GetRouterProfile (ri1->GetIdentHash ()); // retrieve profile
|
isOlder = true;
|
||||||
|
if (ri1->HasProfile ())
|
||||||
|
{
|
||||||
|
auto profile = i2p::data::GetRouterProfile (ri1->GetIdentHash ()); // retrieve profile
|
||||||
if (profile && profile->IsDuplicated ())
|
if (profile && profile->IsDuplicated ())
|
||||||
{
|
{
|
||||||
SendTerminationAndTerminate (eNTCP2Banned);
|
SendTerminationAndTerminate (eNTCP2Banned);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
auto addr = m_RemoteEndpoint.address ().is_v4 () ? ri1->GetNTCP2V4Address () :
|
auto addr = m_RemoteEndpoint.address ().is_v4 () ? ri1->GetNTCP2V4Address () :
|
||||||
(i2p::util::net::IsYggdrasilAddress (m_RemoteEndpoint.address ()) ? ri1->GetYggdrasilAddress () : ri1->GetNTCP2V6Address ());
|
(i2p::util::net::IsYggdrasilAddress (m_RemoteEndpoint.address ()) ? ri1->GetYggdrasilAddress () : ri1->GetNTCP2V6Address ());
|
||||||
@@ -844,8 +849,12 @@ namespace transport
|
|||||||
memcmp (m_RemoteEndpoint.address ().to_v6 ().to_bytes ().data () + 1, addr->host.to_v6 ().to_bytes ().data () + 1, 7) : // from the same yggdrasil subnet
|
memcmp (m_RemoteEndpoint.address ().to_v6 ().to_bytes ().data () + 1, addr->host.to_v6 ().to_bytes ().data () + 1, 7) : // from the same yggdrasil subnet
|
||||||
memcmp (m_RemoteEndpoint.address ().to_v6 ().to_bytes ().data (), addr->host.to_v6 ().to_bytes ().data (), 8)))) // temporary address
|
memcmp (m_RemoteEndpoint.address ().to_v6 ().to_bytes ().data (), addr->host.to_v6 ().to_bytes ().data (), 8)))) // temporary address
|
||||||
{
|
{
|
||||||
if (profile) // older router?
|
if (isOlder) // older router?
|
||||||
profile->Duplicated (); // mark router as duplicated in profile
|
i2p::data::UpdateRouterProfile (ri1->GetIdentHash (),
|
||||||
|
[](std::shared_ptr<i2p::data::RouterProfile> profile)
|
||||||
|
{
|
||||||
|
if (profile) profile->Duplicated (); // mark router as duplicated in profile
|
||||||
|
});
|
||||||
else
|
else
|
||||||
LogPrint (eLogInfo, "NTCP2: Host mismatch between published address ", addr->host, " and actual endpoint ", m_RemoteEndpoint.address ());
|
LogPrint (eLogInfo, "NTCP2: Host mismatch between published address ", addr->host, " and actual endpoint ", m_RemoteEndpoint.address ());
|
||||||
SendTerminationAndTerminate (eNTCP2Banned);
|
SendTerminationAndTerminate (eNTCP2Banned);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2013-2024, The PurpleI2P Project
|
* Copyright (c) 2013-2025, The PurpleI2P Project
|
||||||
*
|
*
|
||||||
* This file is part of Purple i2pd project and licensed under BSD3
|
* This file is part of Purple i2pd project and licensed under BSD3
|
||||||
*
|
*
|
||||||
@@ -10,7 +10,6 @@
|
|||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <random>
|
|
||||||
#include <boost/asio.hpp>
|
#include <boost/asio.hpp>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
@@ -40,7 +39,7 @@ namespace data
|
|||||||
|
|
||||||
NetDb::NetDb (): m_IsRunning (false), m_Thread (nullptr), m_Reseeder (nullptr),
|
NetDb::NetDb (): m_IsRunning (false), m_Thread (nullptr), m_Reseeder (nullptr),
|
||||||
m_Storage("netDb", "r", "routerInfo-", "dat"), m_PersistProfiles (true),
|
m_Storage("netDb", "r", "routerInfo-", "dat"), m_PersistProfiles (true),
|
||||||
m_LastExploratorySelectionUpdateTime (0)
|
m_LastExploratorySelectionUpdateTime (0), m_Rng(i2p::util::GetMonotonicMicroseconds () % 1000000LL)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -119,8 +118,9 @@ namespace data
|
|||||||
i2p::util::SetThreadName("NetDB");
|
i2p::util::SetThreadName("NetDB");
|
||||||
|
|
||||||
uint64_t lastManage = 0;
|
uint64_t lastManage = 0;
|
||||||
uint64_t lastProfilesCleanup = i2p::util::GetMonotonicMilliseconds (), lastObsoleteProfilesCleanup = lastProfilesCleanup;
|
uint64_t lastProfilesCleanup = i2p::util::GetMonotonicMilliseconds (),
|
||||||
int16_t profilesCleanupVariance = 0, obsoleteProfilesCleanVariance = 0;
|
lastObsoleteProfilesCleanup = lastProfilesCleanup, lastApplyingProfileUpdates = lastProfilesCleanup;
|
||||||
|
int16_t profilesCleanupVariance = 0, obsoleteProfilesCleanVariance = 0, applyingProfileUpdatesVariance = 0;
|
||||||
|
|
||||||
std::list<std::shared_ptr<const I2NPMessage> > msgs;
|
std::list<std::shared_ptr<const I2NPMessage> > msgs;
|
||||||
while (m_IsRunning)
|
while (m_IsRunning)
|
||||||
@@ -181,7 +181,7 @@ namespace data
|
|||||||
LogPrint (eLogWarning, "NetDb: Can't persist profiles. Profiles are being saved to disk");
|
LogPrint (eLogWarning, "NetDb: Can't persist profiles. Profiles are being saved to disk");
|
||||||
}
|
}
|
||||||
lastProfilesCleanup = mts;
|
lastProfilesCleanup = mts;
|
||||||
profilesCleanupVariance = rand () % i2p::data::PEER_PROFILE_AUTOCLEAN_VARIANCE;
|
profilesCleanupVariance = m_Rng () % i2p::data::PEER_PROFILE_AUTOCLEAN_VARIANCE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mts >= lastObsoleteProfilesCleanup + (uint64_t)(i2p::data::PEER_PROFILE_OBSOLETE_PROFILES_CLEAN_TIMEOUT + obsoleteProfilesCleanVariance)*1000)
|
if (mts >= lastObsoleteProfilesCleanup + (uint64_t)(i2p::data::PEER_PROFILE_OBSOLETE_PROFILES_CLEAN_TIMEOUT + obsoleteProfilesCleanVariance)*1000)
|
||||||
@@ -197,7 +197,20 @@ namespace data
|
|||||||
else
|
else
|
||||||
LogPrint (eLogWarning, "NetDb: Can't delete profiles. Profiles are being deleted from disk");
|
LogPrint (eLogWarning, "NetDb: Can't delete profiles. Profiles are being deleted from disk");
|
||||||
lastObsoleteProfilesCleanup = mts;
|
lastObsoleteProfilesCleanup = mts;
|
||||||
obsoleteProfilesCleanVariance = rand () % i2p::data::PEER_PROFILE_OBSOLETE_PROFILES_CLEAN_VARIANCE;
|
obsoleteProfilesCleanVariance = m_Rng () % i2p::data::PEER_PROFILE_OBSOLETE_PROFILES_CLEAN_VARIANCE;
|
||||||
|
}
|
||||||
|
if (mts >= lastApplyingProfileUpdates + i2p::data::PEER_PROFILE_APPLY_POSTPONED_TIMEOUT + applyingProfileUpdatesVariance)
|
||||||
|
{
|
||||||
|
bool isApplying = m_ApplyingProfileUpdates.valid ();
|
||||||
|
if (isApplying && m_ApplyingProfileUpdates.wait_for(std::chrono::seconds(0)) == std::future_status::ready) // still active?
|
||||||
|
{
|
||||||
|
m_ApplyingProfileUpdates.get ();
|
||||||
|
isApplying = false;
|
||||||
|
}
|
||||||
|
if (!isApplying)
|
||||||
|
m_ApplyingProfileUpdates = i2p::data::FlushPostponedRouterProfileUpdates ();
|
||||||
|
lastApplyingProfileUpdates = mts;
|
||||||
|
applyingProfileUpdatesVariance = m_Rng () % i2p::data::PEER_PROFILE_APPLY_POSTPONED_TIMEOUT_VARIANCE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (std::exception& ex)
|
catch (std::exception& ex)
|
||||||
@@ -281,6 +294,7 @@ namespace data
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
r->CancelBufferToDelete (); // since an update received
|
||||||
if (CheckLogLevel (eLogDebug))
|
if (CheckLogLevel (eLogDebug))
|
||||||
LogPrint (eLogDebug, "NetDb: RouterInfo is older: ", ident.ToBase64());
|
LogPrint (eLogDebug, "NetDb: RouterInfo is older: ", ident.ToBase64());
|
||||||
updated = false;
|
updated = false;
|
||||||
@@ -557,7 +571,7 @@ namespace data
|
|||||||
while(n > 0)
|
while(n > 0)
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(m_RouterInfosMutex);
|
std::lock_guard<std::mutex> lock(m_RouterInfosMutex);
|
||||||
uint32_t idx = rand () % m_RouterInfos.size ();
|
uint32_t idx = m_Rng () % m_RouterInfos.size ();
|
||||||
uint32_t i = 0;
|
uint32_t i = 0;
|
||||||
for (const auto & it : m_RouterInfos) {
|
for (const auto & it : m_RouterInfos) {
|
||||||
if(i >= idx) // are we at the random start point?
|
if(i >= idx) // are we at the random start point?
|
||||||
@@ -660,15 +674,20 @@ namespace data
|
|||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> l(m_RouterInfosMutex); // possible collision between DeleteBuffer and Update
|
std::lock_guard<std::mutex> l(m_RouterInfosMutex); // possible collision between DeleteBuffer and Update
|
||||||
buffer = r->CopyBuffer ();
|
buffer = r->CopyBuffer ();
|
||||||
r->ScheduleBufferToDelete ();
|
|
||||||
}
|
}
|
||||||
|
if (!i2p::transport::transports.IsConnected (ident))
|
||||||
|
r->ScheduleBufferToDelete ();
|
||||||
if (buffer)
|
if (buffer)
|
||||||
saveToDisk.push_back(std::make_pair(ident.ToBase64 (), buffer));
|
saveToDisk.emplace_back(ident.ToBase64 (), buffer);
|
||||||
}
|
}
|
||||||
r->SetUpdated (false);
|
r->SetUpdated (false);
|
||||||
updatedCount++;
|
updatedCount++;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
else if (r->GetBuffer () && ts > r->GetTimestamp () + NETDB_MIN_EXPIRATION_TIMEOUT*1000LL)
|
||||||
|
// since update was long time ago we assume that router is not connected anymore
|
||||||
|
r->ScheduleBufferToDelete ();
|
||||||
|
|
||||||
if (r->GetProfile ()->IsUnreachable ())
|
if (r->GetProfile ()->IsUnreachable ())
|
||||||
r->SetUnreachable (true);
|
r->SetUnreachable (true);
|
||||||
// make router reachable back if too few routers or floodfills
|
// make router reachable back if too few routers or floodfills
|
||||||
@@ -704,7 +723,7 @@ namespace data
|
|||||||
{
|
{
|
||||||
if (r->IsFloodfill ()) deletedFloodfillsCount++;
|
if (r->IsFloodfill ()) deletedFloodfillsCount++;
|
||||||
// delete RI file
|
// delete RI file
|
||||||
removeFromDisk.push_back (ident.ToBase64());
|
removeFromDisk.emplace_back (ident.ToBase64());
|
||||||
deletedCount++;
|
deletedCount++;
|
||||||
if (total - deletedCount < NETDB_MIN_ROUTERS) checkForExpiration = false;
|
if (total - deletedCount < NETDB_MIN_ROUTERS) checkForExpiration = false;
|
||||||
}
|
}
|
||||||
@@ -1331,7 +1350,7 @@ namespace data
|
|||||||
if (eligible.size () > NETDB_MAX_EXPLORATORY_SELECTION_SIZE)
|
if (eligible.size () > NETDB_MAX_EXPLORATORY_SELECTION_SIZE)
|
||||||
{
|
{
|
||||||
std::sample (eligible.begin(), eligible.end(), std::back_inserter(m_ExploratorySelection),
|
std::sample (eligible.begin(), eligible.end(), std::back_inserter(m_ExploratorySelection),
|
||||||
NETDB_MAX_EXPLORATORY_SELECTION_SIZE, std::mt19937(ts));
|
NETDB_MAX_EXPLORATORY_SELECTION_SIZE, m_Rng);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
std::swap (m_ExploratorySelection, eligible);
|
std::swap (m_ExploratorySelection, eligible);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2013-2024, The PurpleI2P Project
|
* Copyright (c) 2013-2025, The PurpleI2P Project
|
||||||
*
|
*
|
||||||
* This file is part of Purple i2pd project and licensed under BSD3
|
* This file is part of Purple i2pd project and licensed under BSD3
|
||||||
*
|
*
|
||||||
@@ -16,6 +16,7 @@
|
|||||||
#include <thread>
|
#include <thread>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <future>
|
#include <future>
|
||||||
|
#include <random>
|
||||||
|
|
||||||
#include "Base.h"
|
#include "Base.h"
|
||||||
#include "Gzip.h"
|
#include "Gzip.h"
|
||||||
@@ -52,6 +53,7 @@ namespace data
|
|||||||
const int NETDB_MIN_HIGHBANDWIDTH_VERSION = MAKE_VERSION_NUMBER(0, 9, 58); // 0.9.58
|
const int NETDB_MIN_HIGHBANDWIDTH_VERSION = MAKE_VERSION_NUMBER(0, 9, 58); // 0.9.58
|
||||||
const int NETDB_MIN_FLOODFILL_VERSION = MAKE_VERSION_NUMBER(0, 9, 59); // 0.9.59
|
const int NETDB_MIN_FLOODFILL_VERSION = MAKE_VERSION_NUMBER(0, 9, 59); // 0.9.59
|
||||||
const int NETDB_MIN_SHORT_TUNNEL_BUILD_VERSION = MAKE_VERSION_NUMBER(0, 9, 51); // 0.9.51
|
const int NETDB_MIN_SHORT_TUNNEL_BUILD_VERSION = MAKE_VERSION_NUMBER(0, 9, 51); // 0.9.51
|
||||||
|
const int NETDB_MIN_PEER_TEST_VERSION = MAKE_VERSION_NUMBER(0, 9, 62); // 0.9.62
|
||||||
const size_t NETDB_MAX_NUM_SEARCH_REPLY_PEER_HASHES = 16;
|
const size_t NETDB_MAX_NUM_SEARCH_REPLY_PEER_HASHES = 16;
|
||||||
const size_t NETDB_MAX_EXPLORATORY_SELECTION_SIZE = 500;
|
const size_t NETDB_MAX_EXPLORATORY_SELECTION_SIZE = 500;
|
||||||
const int NETDB_EXPLORATORY_SELECTION_UPDATE_INTERVAL = 82; // in seconds. for floodfill
|
const int NETDB_EXPLORATORY_SELECTION_UPDATE_INTERVAL = 82; // in seconds. for floodfill
|
||||||
@@ -185,10 +187,11 @@ namespace data
|
|||||||
std::shared_ptr<NetDbRequests> m_Requests;
|
std::shared_ptr<NetDbRequests> m_Requests;
|
||||||
|
|
||||||
bool m_PersistProfiles;
|
bool m_PersistProfiles;
|
||||||
std::future<void> m_SavingProfiles, m_DeletingProfiles, m_PersistingRouters;
|
std::future<void> m_SavingProfiles, m_DeletingProfiles, m_ApplyingProfileUpdates, m_PersistingRouters;
|
||||||
|
|
||||||
std::vector<std::shared_ptr<const RouterInfo> > m_ExploratorySelection;
|
std::vector<std::shared_ptr<const RouterInfo> > m_ExploratorySelection;
|
||||||
uint64_t m_LastExploratorySelectionUpdateTime; // in monotonic seconds
|
uint64_t m_LastExploratorySelectionUpdateTime; // in monotonic seconds
|
||||||
|
std::mt19937 m_Rng;
|
||||||
|
|
||||||
i2p::util::MemoryPoolMt<RouterInfo::Buffer> m_RouterInfoBuffersPool;
|
i2p::util::MemoryPoolMt<RouterInfo::Buffer> m_RouterInfoBuffersPool;
|
||||||
i2p::util::MemoryPoolMt<RouterInfo::Address> m_RouterInfoAddressesPool;
|
i2p::util::MemoryPoolMt<RouterInfo::Address> m_RouterInfoAddressesPool;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2013-2024, The PurpleI2P Project
|
* Copyright (c) 2013-2025, The PurpleI2P Project
|
||||||
*
|
*
|
||||||
* This file is part of Purple i2pd project and licensed under BSD3
|
* This file is part of Purple i2pd project and licensed under BSD3
|
||||||
*
|
*
|
||||||
@@ -20,8 +20,10 @@ namespace i2p
|
|||||||
namespace data
|
namespace data
|
||||||
{
|
{
|
||||||
RequestedDestination::RequestedDestination (const IdentHash& destination, bool isExploratory, bool direct):
|
RequestedDestination::RequestedDestination (const IdentHash& destination, bool isExploratory, bool direct):
|
||||||
m_Destination (destination), m_IsExploratory (isExploratory), m_IsDirect (direct), m_IsActive (true),
|
m_Destination (destination), m_IsExploratory (isExploratory), m_IsDirect (direct),
|
||||||
m_CreationTime (i2p::util::GetMillisecondsSinceEpoch ()), m_LastRequestTime (0), m_NumAttempts (0)
|
m_IsActive (true), m_IsSentDirectly (false),
|
||||||
|
m_CreationTime (i2p::util::GetMillisecondsSinceEpoch ()),
|
||||||
|
m_LastRequestTime (0), m_NumAttempts (0)
|
||||||
{
|
{
|
||||||
if (i2p::context.IsFloodfill ())
|
if (i2p::context.IsFloodfill ())
|
||||||
m_ExcludedPeers.insert (i2p::context.GetIdentHash ()); // exclude self if floodfill
|
m_ExcludedPeers.insert (i2p::context.GetIdentHash ()); // exclude self if floodfill
|
||||||
@@ -46,6 +48,7 @@ namespace data
|
|||||||
m_ExcludedPeers.insert (router->GetIdentHash ());
|
m_ExcludedPeers.insert (router->GetIdentHash ());
|
||||||
m_LastRequestTime = i2p::util::GetMillisecondsSinceEpoch ();
|
m_LastRequestTime = i2p::util::GetMillisecondsSinceEpoch ();
|
||||||
m_NumAttempts++;
|
m_NumAttempts++;
|
||||||
|
m_IsSentDirectly = false;
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,6 +59,7 @@ namespace data
|
|||||||
m_ExcludedPeers.insert (floodfill);
|
m_ExcludedPeers.insert (floodfill);
|
||||||
m_NumAttempts++;
|
m_NumAttempts++;
|
||||||
m_LastRequestTime = i2p::util::GetMillisecondsSinceEpoch ();
|
m_LastRequestTime = i2p::util::GetMillisecondsSinceEpoch ();
|
||||||
|
m_IsSentDirectly = true;
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -222,7 +226,8 @@ namespace data
|
|||||||
bool done = false;
|
bool done = false;
|
||||||
if (ts < dest->GetCreationTime () + MAX_REQUEST_TIME)
|
if (ts < dest->GetCreationTime () + MAX_REQUEST_TIME)
|
||||||
{
|
{
|
||||||
if (ts > dest->GetLastRequestTime () + MIN_REQUEST_TIME) // try next floodfill if no response after min interval
|
if (ts > dest->GetLastRequestTime () + (dest->IsSentDirectly () ? MIN_DIRECT_REQUEST_TIME : MIN_REQUEST_TIME))
|
||||||
|
// try next floodfill if no response after min interval
|
||||||
done = !SendNextRequest (dest);
|
done = !SendNextRequest (dest);
|
||||||
}
|
}
|
||||||
else // request is expired
|
else // request is expired
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2013-2024, The PurpleI2P Project
|
* Copyright (c) 2013-2025, The PurpleI2P Project
|
||||||
*
|
*
|
||||||
* This file is part of Purple i2pd project and licensed under BSD3
|
* This file is part of Purple i2pd project and licensed under BSD3
|
||||||
*
|
*
|
||||||
@@ -28,6 +28,7 @@ namespace data
|
|||||||
const uint64_t MANAGE_REQUESTS_INTERVAL_VARIANCE = 300; // in milliseconds
|
const uint64_t MANAGE_REQUESTS_INTERVAL_VARIANCE = 300; // in milliseconds
|
||||||
const uint64_t MIN_REQUEST_TIME = 1200; // in milliseconds
|
const uint64_t MIN_REQUEST_TIME = 1200; // in milliseconds
|
||||||
const uint64_t MAX_REQUEST_TIME = MAX_NUM_REQUEST_ATTEMPTS * (MIN_REQUEST_TIME + MANAGE_REQUESTS_INTERVAL + MANAGE_REQUESTS_INTERVAL_VARIANCE);
|
const uint64_t MAX_REQUEST_TIME = MAX_NUM_REQUEST_ATTEMPTS * (MIN_REQUEST_TIME + MANAGE_REQUESTS_INTERVAL + MANAGE_REQUESTS_INTERVAL_VARIANCE);
|
||||||
|
const uint64_t MIN_DIRECT_REQUEST_TIME = 600; // in milliseconds
|
||||||
const uint64_t EXPLORATORY_REQUEST_INTERVAL = 55; // in seconds
|
const uint64_t EXPLORATORY_REQUEST_INTERVAL = 55; // in seconds
|
||||||
const uint64_t EXPLORATORY_REQUEST_INTERVAL_VARIANCE = 170; // in seconds
|
const uint64_t EXPLORATORY_REQUEST_INTERVAL_VARIANCE = 170; // in seconds
|
||||||
const uint64_t DISCOVERED_REQUEST_INTERVAL = 360; // in milliseconds
|
const uint64_t DISCOVERED_REQUEST_INTERVAL = 360; // in milliseconds
|
||||||
@@ -52,6 +53,7 @@ namespace data
|
|||||||
bool IsExploratory () const { return m_IsExploratory; };
|
bool IsExploratory () const { return m_IsExploratory; };
|
||||||
bool IsDirect () const { return m_IsDirect; };
|
bool IsDirect () const { return m_IsDirect; };
|
||||||
bool IsActive () const { return m_IsActive; };
|
bool IsActive () const { return m_IsActive; };
|
||||||
|
bool IsSentDirectly () const { return m_IsSentDirectly; };
|
||||||
bool IsExcluded (const IdentHash& ident) const;
|
bool IsExcluded (const IdentHash& ident) const;
|
||||||
uint64_t GetCreationTime () const { return m_CreationTime; };
|
uint64_t GetCreationTime () const { return m_CreationTime; };
|
||||||
uint64_t GetLastRequestTime () const { return m_LastRequestTime; };
|
uint64_t GetLastRequestTime () const { return m_LastRequestTime; };
|
||||||
@@ -70,7 +72,7 @@ namespace data
|
|||||||
private:
|
private:
|
||||||
|
|
||||||
IdentHash m_Destination;
|
IdentHash m_Destination;
|
||||||
bool m_IsExploratory, m_IsDirect, m_IsActive;
|
bool m_IsExploratory, m_IsDirect, m_IsActive, m_IsSentDirectly;
|
||||||
std::unordered_set<IdentHash> m_ExcludedPeers;
|
std::unordered_set<IdentHash> m_ExcludedPeers;
|
||||||
uint64_t m_CreationTime, m_LastRequestTime; // in milliseconds
|
uint64_t m_CreationTime, m_LastRequestTime; // in milliseconds
|
||||||
std::list<RequestComplete> m_RequestComplete;
|
std::list<RequestComplete> m_RequestComplete;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2013-2024, The PurpleI2P Project
|
* Copyright (c) 2013-2025, The PurpleI2P Project
|
||||||
*
|
*
|
||||||
* This file is part of Purple i2pd project and licensed under BSD3
|
* This file is part of Purple i2pd project and licensed under BSD3
|
||||||
*
|
*
|
||||||
@@ -27,13 +27,15 @@ namespace data
|
|||||||
static i2p::fs::HashedStorage g_ProfilesStorage("peerProfiles", "p", "profile-", "txt");
|
static i2p::fs::HashedStorage g_ProfilesStorage("peerProfiles", "p", "profile-", "txt");
|
||||||
static std::unordered_map<i2p::data::IdentHash, std::shared_ptr<RouterProfile> > g_Profiles;
|
static std::unordered_map<i2p::data::IdentHash, std::shared_ptr<RouterProfile> > g_Profiles;
|
||||||
static std::mutex g_ProfilesMutex;
|
static std::mutex g_ProfilesMutex;
|
||||||
|
static std::list<std::pair<i2p::data::IdentHash, std::function<void (std::shared_ptr<RouterProfile>)> > > g_PostponedUpdates;
|
||||||
|
static std::mutex g_PostponedUpdatesMutex;
|
||||||
|
|
||||||
RouterProfile::RouterProfile ():
|
RouterProfile::RouterProfile ():
|
||||||
m_IsUpdated (false), m_LastDeclineTime (0), m_LastUnreachableTime (0),
|
m_IsUpdated (false), m_LastDeclineTime (0), m_LastUnreachableTime (0),
|
||||||
m_LastUpdateTime (i2p::util::GetSecondsSinceEpoch ()),
|
m_LastUpdateTime (i2p::util::GetSecondsSinceEpoch ()), m_LastAccessTime (0),
|
||||||
m_NumTunnelsAgreed (0), m_NumTunnelsDeclined (0), m_NumTunnelsNonReplied (0),
|
m_LastPersistTime (0), m_NumTunnelsAgreed (0), m_NumTunnelsDeclined (0),
|
||||||
m_NumTimesTaken (0), m_NumTimesRejected (0), m_HasConnected (false),
|
m_NumTunnelsNonReplied (0),m_NumTimesTaken (0), m_NumTimesRejected (0),
|
||||||
m_IsDuplicated (false)
|
m_HasConnected (false), m_IsDuplicated (false)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,6 +80,7 @@ namespace data
|
|||||||
|
|
||||||
void RouterProfile::Load (const IdentHash& identHash)
|
void RouterProfile::Load (const IdentHash& identHash)
|
||||||
{
|
{
|
||||||
|
m_IsUpdated = false;
|
||||||
std::string ident = identHash.ToBase64 ();
|
std::string ident = identHash.ToBase64 ();
|
||||||
std::string path = g_ProfilesStorage.Path(ident);
|
std::string path = g_ProfilesStorage.Path(ident);
|
||||||
boost::property_tree::ptree pt;
|
boost::property_tree::ptree pt;
|
||||||
@@ -255,24 +258,36 @@ namespace data
|
|||||||
std::unique_lock<std::mutex> l(g_ProfilesMutex);
|
std::unique_lock<std::mutex> l(g_ProfilesMutex);
|
||||||
auto it = g_Profiles.find (identHash);
|
auto it = g_Profiles.find (identHash);
|
||||||
if (it != g_Profiles.end ())
|
if (it != g_Profiles.end ())
|
||||||
|
{
|
||||||
|
it->second->SetLastAccessTime (i2p::util::GetSecondsSinceEpoch ());
|
||||||
return it->second;
|
return it->second;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
auto profile = netdb.NewRouterProfile ();
|
auto profile = netdb.NewRouterProfile ();
|
||||||
profile->Load (identHash); // if possible
|
profile->Load (identHash); // if possible
|
||||||
std::unique_lock<std::mutex> l(g_ProfilesMutex);
|
std::lock_guard<std::mutex> l(g_ProfilesMutex);
|
||||||
g_Profiles.emplace (identHash, profile);
|
g_Profiles.emplace (identHash, profile);
|
||||||
return profile;
|
return profile;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool IsRouterBanned (const IdentHash& identHash)
|
bool IsRouterBanned (const IdentHash& identHash)
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> l(g_ProfilesMutex);
|
std::lock_guard<std::mutex> l(g_ProfilesMutex);
|
||||||
auto it = g_Profiles.find (identHash);
|
auto it = g_Profiles.find (identHash);
|
||||||
if (it != g_Profiles.end ())
|
if (it != g_Profiles.end ())
|
||||||
return it->second->IsUnreachable ();
|
return it->second->IsUnreachable ();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool IsRouterDuplicated (const IdentHash& identHash)
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> l(g_ProfilesMutex);
|
||||||
|
auto it = g_Profiles.find (identHash);
|
||||||
|
if (it != g_Profiles.end ())
|
||||||
|
return it->second->IsDuplicated ();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
void InitProfilesStorage ()
|
void InitProfilesStorage ()
|
||||||
{
|
{
|
||||||
g_ProfilesStorage.SetPlace(i2p::fs::GetDataDir());
|
g_ProfilesStorage.SetPlace(i2p::fs::GetDataDir());
|
||||||
@@ -290,15 +305,17 @@ namespace data
|
|||||||
auto ts = i2p::util::GetSecondsSinceEpoch ();
|
auto ts = i2p::util::GetSecondsSinceEpoch ();
|
||||||
std::list<std::pair<i2p::data::IdentHash, std::shared_ptr<RouterProfile> > > tmp;
|
std::list<std::pair<i2p::data::IdentHash, std::shared_ptr<RouterProfile> > > tmp;
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> l(g_ProfilesMutex);
|
std::lock_guard<std::mutex> l(g_ProfilesMutex);
|
||||||
for (auto it = g_Profiles.begin (); it != g_Profiles.end ();)
|
for (auto it = g_Profiles.begin (); it != g_Profiles.end ();)
|
||||||
{
|
{
|
||||||
if (ts - it->second->GetLastUpdateTime () > PEER_PROFILE_PERSIST_INTERVAL)
|
if (it->second->IsUpdated () && ts > it->second->GetLastPersistTime () + PEER_PROFILE_PERSIST_INTERVAL)
|
||||||
{
|
{
|
||||||
if (it->second->IsUpdated ())
|
tmp.push_back (*it);
|
||||||
tmp.push_back (std::make_pair (it->first, it->second));
|
it->second->SetLastPersistTime (ts);
|
||||||
it = g_Profiles.erase (it);
|
it->second->SetUpdated (false);
|
||||||
}
|
}
|
||||||
|
if (!it->second->IsUpdated () && ts > std::max (it->second->GetLastUpdateTime (), it->second->GetLastAccessTime ()) + PEER_PROFILE_PERSIST_INTERVAL)
|
||||||
|
it = g_Profiles.erase (it);
|
||||||
else
|
else
|
||||||
it++;
|
it++;
|
||||||
}
|
}
|
||||||
@@ -312,7 +329,7 @@ namespace data
|
|||||||
{
|
{
|
||||||
std::unordered_map<i2p::data::IdentHash, std::shared_ptr<RouterProfile> > tmp;
|
std::unordered_map<i2p::data::IdentHash, std::shared_ptr<RouterProfile> > tmp;
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> l(g_ProfilesMutex);
|
std::lock_guard<std::mutex> l(g_ProfilesMutex);
|
||||||
std::swap (tmp, g_Profiles);
|
std::swap (tmp, g_Profiles);
|
||||||
}
|
}
|
||||||
auto ts = i2p::util::GetSecondsSinceEpoch ();
|
auto ts = i2p::util::GetSecondsSinceEpoch ();
|
||||||
@@ -347,7 +364,7 @@ namespace data
|
|||||||
{
|
{
|
||||||
{
|
{
|
||||||
auto ts = i2p::util::GetSecondsSinceEpoch ();
|
auto ts = i2p::util::GetSecondsSinceEpoch ();
|
||||||
std::unique_lock<std::mutex> l(g_ProfilesMutex);
|
std::lock_guard<std::mutex> l(g_ProfilesMutex);
|
||||||
for (auto it = g_Profiles.begin (); it != g_Profiles.end ();)
|
for (auto it = g_Profiles.begin (); it != g_Profiles.end ();)
|
||||||
{
|
{
|
||||||
if (ts - it->second->GetLastUpdateTime () >= PEER_PROFILE_EXPIRATION_TIMEOUT)
|
if (ts - it->second->GetLastUpdateTime () >= PEER_PROFILE_EXPIRATION_TIMEOUT)
|
||||||
@@ -359,5 +376,47 @@ namespace data
|
|||||||
|
|
||||||
return std::async (std::launch::async, DeleteFilesFromDisk);
|
return std::async (std::launch::async, DeleteFilesFromDisk);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool UpdateRouterProfile (const IdentHash& identHash, std::function<void (std::shared_ptr<RouterProfile>)> update)
|
||||||
|
{
|
||||||
|
if (!update) return true;
|
||||||
|
std::shared_ptr<RouterProfile> profile;
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> l(g_ProfilesMutex);
|
||||||
|
auto it = g_Profiles.find (identHash);
|
||||||
|
if (it != g_Profiles.end ())
|
||||||
|
profile = it->second;
|
||||||
|
}
|
||||||
|
if (profile)
|
||||||
|
{
|
||||||
|
update (profile);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
// postpone
|
||||||
|
std::lock_guard<std::mutex> l(g_PostponedUpdatesMutex);
|
||||||
|
g_PostponedUpdates.emplace_back (identHash, update);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ApplyPostponedUpdates (std::list<std::pair<i2p::data::IdentHash, std::function<void (std::shared_ptr<RouterProfile>)> > >&& updates)
|
||||||
|
{
|
||||||
|
for (const auto& [ident, update] : updates)
|
||||||
|
{
|
||||||
|
auto profile = GetRouterProfile (ident);
|
||||||
|
update (profile);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::future<void> FlushPostponedRouterProfileUpdates ()
|
||||||
|
{
|
||||||
|
if (g_PostponedUpdates.empty ()) return std::future<void>();
|
||||||
|
|
||||||
|
std::list<std::pair<i2p::data::IdentHash, std::function<void (std::shared_ptr<RouterProfile>)> > > updates;
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> l(g_PostponedUpdatesMutex);
|
||||||
|
g_PostponedUpdates.swap (updates);
|
||||||
|
}
|
||||||
|
return std::async (std::launch::async, ApplyPostponedUpdates, std::move (updates));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2013-2024, The PurpleI2P Project
|
* Copyright (c) 2013-2025, The PurpleI2P Project
|
||||||
*
|
*
|
||||||
* This file is part of Purple i2pd project and licensed under BSD3
|
* This file is part of Purple i2pd project and licensed under BSD3
|
||||||
*
|
*
|
||||||
@@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <future>
|
#include <future>
|
||||||
|
#include <functional>
|
||||||
#include <boost/asio.hpp>
|
#include <boost/asio.hpp>
|
||||||
#include "Identity.h"
|
#include "Identity.h"
|
||||||
|
|
||||||
@@ -40,10 +41,12 @@ namespace data
|
|||||||
const int PEER_PROFILE_OBSOLETE_PROFILES_CLEAN_VARIANCE = 2400; // in seconds (40 minutes)
|
const int PEER_PROFILE_OBSOLETE_PROFILES_CLEAN_VARIANCE = 2400; // in seconds (40 minutes)
|
||||||
const int PEER_PROFILE_DECLINED_RECENTLY_INTERVAL = 330; // in seconds (5.5 minutes)
|
const int PEER_PROFILE_DECLINED_RECENTLY_INTERVAL = 330; // in seconds (5.5 minutes)
|
||||||
const int PEER_PROFILE_MAX_DECLINED_INTERVAL = 4400; // in second (1.5 hours)
|
const int PEER_PROFILE_MAX_DECLINED_INTERVAL = 4400; // in second (1.5 hours)
|
||||||
const int PEER_PROFILE_PERSIST_INTERVAL = 3300; // in seconds (55 minutes)
|
const int PEER_PROFILE_PERSIST_INTERVAL = 1320; // in seconds (22 minutes)
|
||||||
const int PEER_PROFILE_UNREACHABLE_INTERVAL = 480; // in seconds (8 minutes)
|
const int PEER_PROFILE_UNREACHABLE_INTERVAL = 480; // in seconds (8 minutes)
|
||||||
const int PEER_PROFILE_USEFUL_THRESHOLD = 3;
|
const int PEER_PROFILE_USEFUL_THRESHOLD = 3;
|
||||||
const int PEER_PROFILE_ALWAYS_DECLINING_NUM = 5; // num declines in row to consider always declined
|
const int PEER_PROFILE_ALWAYS_DECLINING_NUM = 5; // num declines in row to consider always declined
|
||||||
|
const int PEER_PROFILE_APPLY_POSTPONED_TIMEOUT = 2100; // in milliseconds
|
||||||
|
const int PEER_PROFILE_APPLY_POSTPONED_TIMEOUT_VARIANCE = 500; // in milliseconds
|
||||||
|
|
||||||
class RouterProfile
|
class RouterProfile
|
||||||
{
|
{
|
||||||
@@ -67,6 +70,11 @@ namespace data
|
|||||||
|
|
||||||
uint64_t GetLastUpdateTime () const { return m_LastUpdateTime; };
|
uint64_t GetLastUpdateTime () const { return m_LastUpdateTime; };
|
||||||
bool IsUpdated () const { return m_IsUpdated; };
|
bool IsUpdated () const { return m_IsUpdated; };
|
||||||
|
void SetUpdated (bool updated) { m_IsUpdated = updated; }
|
||||||
|
uint64_t GetLastAccessTime () const { return m_LastAccessTime; };
|
||||||
|
void SetLastAccessTime (uint64_t ts) { m_LastAccessTime = ts; };
|
||||||
|
uint64_t GetLastPersistTime () const { return m_LastPersistTime; };
|
||||||
|
void SetLastPersistTime (uint64_t ts) { m_LastPersistTime = ts; };
|
||||||
|
|
||||||
bool IsUseful() const;
|
bool IsUseful() const;
|
||||||
bool IsDuplicated () const { return m_IsDuplicated; };
|
bool IsDuplicated () const { return m_IsDuplicated; };
|
||||||
@@ -88,7 +96,8 @@ namespace data
|
|||||||
private:
|
private:
|
||||||
|
|
||||||
bool m_IsUpdated;
|
bool m_IsUpdated;
|
||||||
uint64_t m_LastDeclineTime, m_LastUnreachableTime, m_LastUpdateTime; // in seconds
|
uint64_t m_LastDeclineTime, m_LastUnreachableTime, m_LastUpdateTime,
|
||||||
|
m_LastAccessTime, m_LastPersistTime; // in seconds
|
||||||
// participation
|
// participation
|
||||||
uint32_t m_NumTunnelsAgreed;
|
uint32_t m_NumTunnelsAgreed;
|
||||||
uint32_t m_NumTunnelsDeclined;
|
uint32_t m_NumTunnelsDeclined;
|
||||||
@@ -104,10 +113,13 @@ namespace data
|
|||||||
|
|
||||||
std::shared_ptr<RouterProfile> GetRouterProfile (const IdentHash& identHash);
|
std::shared_ptr<RouterProfile> GetRouterProfile (const IdentHash& identHash);
|
||||||
bool IsRouterBanned (const IdentHash& identHash); // check only existing profiles
|
bool IsRouterBanned (const IdentHash& identHash); // check only existing profiles
|
||||||
|
bool IsRouterDuplicated (const IdentHash& identHash); // check only existing profiles
|
||||||
void InitProfilesStorage ();
|
void InitProfilesStorage ();
|
||||||
std::future<void> DeleteObsoleteProfiles ();
|
std::future<void> DeleteObsoleteProfiles ();
|
||||||
void SaveProfiles ();
|
void SaveProfiles ();
|
||||||
std::future<void> PersistProfiles ();
|
std::future<void> PersistProfiles ();
|
||||||
|
bool UpdateRouterProfile (const IdentHash& identHash, std::function<void (std::shared_ptr<RouterProfile>)> update); // return true if updated immediately, and false if postponed
|
||||||
|
std::future<void> FlushPostponedRouterProfileUpdates ();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -33,13 +33,14 @@ namespace i2p
|
|||||||
m_ShareRatio (100), m_Status (eRouterStatusUnknown), m_StatusV6 (eRouterStatusUnknown),
|
m_ShareRatio (100), m_Status (eRouterStatusUnknown), m_StatusV6 (eRouterStatusUnknown),
|
||||||
m_Error (eRouterErrorNone), m_ErrorV6 (eRouterErrorNone),
|
m_Error (eRouterErrorNone), m_ErrorV6 (eRouterErrorNone),
|
||||||
m_Testing (false), m_TestingV6 (false), m_NetID (I2PD_NET_ID),
|
m_Testing (false), m_TestingV6 (false), m_NetID (I2PD_NET_ID),
|
||||||
m_PublishReplyToken (0), m_IsHiddenMode (false)
|
m_PublishReplyToken (0), m_IsHiddenMode (false),
|
||||||
|
m_Rng(i2p::util::GetMonotonicMicroseconds () % 1000000LL), m_IsSaving (false)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void RouterContext::Init ()
|
void RouterContext::Init ()
|
||||||
{
|
{
|
||||||
srand (i2p::util::GetMillisecondsSinceEpoch () % 1000);
|
srand (m_Rng () % 1000);
|
||||||
m_StartupTime = i2p::util::GetMonotonicSeconds ();
|
m_StartupTime = i2p::util::GetMonotonicSeconds ();
|
||||||
|
|
||||||
if (!Load ())
|
if (!Load ())
|
||||||
@@ -253,11 +254,36 @@ namespace i2p
|
|||||||
|
|
||||||
void RouterContext::UpdateRouterInfo ()
|
void RouterContext::UpdateRouterInfo ()
|
||||||
{
|
{
|
||||||
|
std::shared_ptr<i2p::data::RouterInfo::Buffer> buffer;
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> l(m_RouterInfoMutex);
|
std::lock_guard<std::mutex> l(m_RouterInfoMutex);
|
||||||
m_RouterInfo.CreateBuffer (m_Keys);
|
m_RouterInfo.CreateBuffer (m_Keys);
|
||||||
|
buffer = m_RouterInfo.CopyBuffer ();
|
||||||
|
}
|
||||||
|
{
|
||||||
|
// update save buffer to latest
|
||||||
|
std::lock_guard<std::mutex> l(m_SaveBufferMutex);
|
||||||
|
m_SaveBuffer = buffer;
|
||||||
|
}
|
||||||
|
bool isSaving = false;
|
||||||
|
if (m_IsSaving.compare_exchange_strong (isSaving, true)) // try to save only if not being saved
|
||||||
|
{
|
||||||
|
auto savingRouterInfo = std::async (std::launch::async, [this]()
|
||||||
|
{
|
||||||
|
std::shared_ptr<i2p::data::RouterInfo::Buffer> buffer;
|
||||||
|
while (m_SaveBuffer)
|
||||||
|
{
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> l(m_SaveBufferMutex);
|
||||||
|
buffer = m_SaveBuffer;
|
||||||
|
m_SaveBuffer = nullptr;
|
||||||
|
}
|
||||||
|
if (buffer)
|
||||||
|
i2p::data::RouterInfo::SaveToFile (i2p::fs::DataDirPath (ROUTER_INFO), buffer);
|
||||||
|
}
|
||||||
|
m_IsSaving = false;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
m_RouterInfo.SaveToFile (i2p::fs::DataDirPath (ROUTER_INFO));
|
|
||||||
m_LastUpdateTime = i2p::util::GetSecondsSinceEpoch ();
|
m_LastUpdateTime = i2p::util::GetSecondsSinceEpoch ();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -649,11 +675,11 @@ namespace i2p
|
|||||||
|
|
||||||
void RouterContext::SetBandwidth (int limit)
|
void RouterContext::SetBandwidth (int limit)
|
||||||
{
|
{
|
||||||
if (limit > 2000) { SetBandwidth('X'); }
|
if (limit > (int)i2p::data::EXTRA_BANDWIDTH_LIMIT) { SetBandwidth('X'); }
|
||||||
else if (limit > 256) { SetBandwidth('P'); }
|
else if (limit > (int)i2p::data::HIGH_BANDWIDTH_LIMIT) { SetBandwidth('P'); }
|
||||||
else if (limit > 128) { SetBandwidth('O'); }
|
else if (limit > 128) { SetBandwidth('O'); }
|
||||||
else if (limit > 64) { SetBandwidth('N'); }
|
else if (limit > 64) { SetBandwidth('N'); }
|
||||||
else if (limit > 48) { SetBandwidth('M'); }
|
else if (limit > (int)i2p::data::LOW_BANDWIDTH_LIMIT) { SetBandwidth('M'); }
|
||||||
else if (limit > 12) { SetBandwidth('L'); }
|
else if (limit > 12) { SetBandwidth('L'); }
|
||||||
else { SetBandwidth('K'); }
|
else { SetBandwidth('K'); }
|
||||||
m_BandwidthLimit = limit; // set precise limit
|
m_BandwidthLimit = limit; // set precise limit
|
||||||
@@ -1359,7 +1385,7 @@ namespace i2p
|
|||||||
{
|
{
|
||||||
m_PublishTimer->cancel ();
|
m_PublishTimer->cancel ();
|
||||||
m_PublishTimer->expires_from_now (boost::posix_time::seconds(ROUTER_INFO_PUBLISH_INTERVAL +
|
m_PublishTimer->expires_from_now (boost::posix_time::seconds(ROUTER_INFO_PUBLISH_INTERVAL +
|
||||||
rand () % ROUTER_INFO_PUBLISH_INTERVAL_VARIANCE));
|
m_Rng () % ROUTER_INFO_PUBLISH_INTERVAL_VARIANCE));
|
||||||
m_PublishTimer->async_wait (std::bind (&RouterContext::HandlePublishTimer,
|
m_PublishTimer->async_wait (std::bind (&RouterContext::HandlePublishTimer,
|
||||||
this, std::placeholders::_1));
|
this, std::placeholders::_1));
|
||||||
}
|
}
|
||||||
@@ -1471,7 +1497,8 @@ namespace i2p
|
|||||||
if (m_CongestionUpdateTimer)
|
if (m_CongestionUpdateTimer)
|
||||||
{
|
{
|
||||||
m_CongestionUpdateTimer->cancel ();
|
m_CongestionUpdateTimer->cancel ();
|
||||||
m_CongestionUpdateTimer->expires_from_now (boost::posix_time::seconds(ROUTER_INFO_CONGESTION_UPDATE_INTERVAL));
|
m_CongestionUpdateTimer->expires_from_now (boost::posix_time::seconds(
|
||||||
|
ROUTER_INFO_CONGESTION_UPDATE_INTERVAL + m_Rng () % ROUTER_INFO_CONGESTION_UPDATE_INTERVAL_VARIANCE));
|
||||||
m_CongestionUpdateTimer->async_wait (std::bind (&RouterContext::HandleCongestionUpdateTimer,
|
m_CongestionUpdateTimer->async_wait (std::bind (&RouterContext::HandleCongestionUpdateTimer,
|
||||||
this, std::placeholders::_1));
|
this, std::placeholders::_1));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@
|
|||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <random>
|
||||||
#include <unordered_set>
|
#include <unordered_set>
|
||||||
#include <boost/asio.hpp>
|
#include <boost/asio.hpp>
|
||||||
#include "Identity.h"
|
#include "Identity.h"
|
||||||
@@ -36,7 +37,8 @@ namespace garlic
|
|||||||
const int ROUTER_INFO_PUBLISH_INTERVAL_VARIANCE = 105;// in seconds
|
const int ROUTER_INFO_PUBLISH_INTERVAL_VARIANCE = 105;// in seconds
|
||||||
const int ROUTER_INFO_CONFIRMATION_TIMEOUT = 1600; // in milliseconds
|
const int ROUTER_INFO_CONFIRMATION_TIMEOUT = 1600; // in milliseconds
|
||||||
const int ROUTER_INFO_MAX_PUBLISH_EXCLUDED_FLOODFILLS = 15;
|
const int ROUTER_INFO_MAX_PUBLISH_EXCLUDED_FLOODFILLS = 15;
|
||||||
const int ROUTER_INFO_CONGESTION_UPDATE_INTERVAL = 12*60; // in seconds
|
const int ROUTER_INFO_CONGESTION_UPDATE_INTERVAL = 11*60; // in seconds
|
||||||
|
const int ROUTER_INFO_CONGESTION_UPDATE_INTERVAL_VARIANCE = 130; // in seconds
|
||||||
const int ROUTER_INFO_CLEANUP_INTERVAL = 102; // in seconds
|
const int ROUTER_INFO_CLEANUP_INTERVAL = 102; // in seconds
|
||||||
|
|
||||||
enum RouterStatus
|
enum RouterStatus
|
||||||
@@ -263,6 +265,10 @@ namespace garlic
|
|||||||
uint32_t m_PublishReplyToken;
|
uint32_t m_PublishReplyToken;
|
||||||
bool m_IsHiddenMode; // not publish
|
bool m_IsHiddenMode; // not publish
|
||||||
mutable std::mutex m_RouterInfoMutex;
|
mutable std::mutex m_RouterInfoMutex;
|
||||||
|
std::mt19937 m_Rng;
|
||||||
|
std::shared_ptr<i2p::data::RouterInfo::Buffer> m_SaveBuffer;
|
||||||
|
std::mutex m_SaveBufferMutex; // TODO: make m_SaveBuffer atomic
|
||||||
|
std::atomic<bool> m_IsSaving;
|
||||||
};
|
};
|
||||||
|
|
||||||
extern RouterContext context;
|
extern RouterContext context;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2013-2024, The PurpleI2P Project
|
* Copyright (c) 2013-2025, The PurpleI2P Project
|
||||||
*
|
*
|
||||||
* This file is part of Purple i2pd project and licensed under BSD3
|
* This file is part of Purple i2pd project and licensed under BSD3
|
||||||
*
|
*
|
||||||
@@ -11,7 +11,7 @@
|
|||||||
#include "I2PEndian.h"
|
#include "I2PEndian.h"
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <boost/lexical_cast.hpp>
|
#include <charconv>
|
||||||
#include <boost/algorithm/string.hpp> // for boost::to_lower
|
#include <boost/algorithm/string.hpp> // for boost::to_lower
|
||||||
#ifndef __cpp_lib_atomic_shared_ptr
|
#ifndef __cpp_lib_atomic_shared_ptr
|
||||||
#include <boost/atomic.hpp>
|
#include <boost/atomic.hpp>
|
||||||
@@ -106,8 +106,7 @@ namespace data
|
|||||||
// skip identity
|
// skip identity
|
||||||
size_t identityLen = m_RouterIdentity->GetFullLen ();
|
size_t identityLen = m_RouterIdentity->GetFullLen ();
|
||||||
// read new RI
|
// read new RI
|
||||||
std::stringstream str (std::string ((char *)buf + identityLen, len - identityLen));
|
ReadFromBuffer (buf + identityLen, len - identityLen);
|
||||||
ReadFromStream (str);
|
|
||||||
if (!m_IsUnreachable)
|
if (!m_IsUnreachable)
|
||||||
UpdateBuffer (buf, len); // save buffer
|
UpdateBuffer (buf, len); // save buffer
|
||||||
// don't delete buffer until saved to the file
|
// don't delete buffer until saved to the file
|
||||||
@@ -195,39 +194,34 @@ namespace data
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// parse RI
|
// parse RI
|
||||||
std::stringstream str;
|
if (!ReadFromBuffer (m_Buffer->data () + identityLen, bufferLen - identityLen))
|
||||||
str.write ((const char *)m_Buffer->data () + identityLen, bufferLen - identityLen);
|
|
||||||
ReadFromStream (str);
|
|
||||||
if (!str)
|
|
||||||
{
|
{
|
||||||
LogPrint (eLogError, "RouterInfo: Malformed message");
|
LogPrint (eLogError, "RouterInfo: Malformed message");
|
||||||
m_IsUnreachable = true;
|
m_IsUnreachable = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void RouterInfo::ReadFromStream (std::istream& s)
|
bool RouterInfo::ReadFromBuffer (const uint8_t * buf, size_t len)
|
||||||
{
|
{
|
||||||
if (!s) return;
|
if (len < 9) return false;
|
||||||
m_Caps = 0; m_Congestion = eLowCongestion;
|
m_Caps = 0; m_Congestion = eLowCongestion;
|
||||||
s.read ((char *)&m_Timestamp, sizeof (m_Timestamp));
|
m_Timestamp = bufbe64toh (buf);
|
||||||
m_Timestamp = be64toh (m_Timestamp);
|
size_t offset = 8; // timestamp
|
||||||
// read addresses
|
// read addresses
|
||||||
auto addresses = NewAddresses ();
|
auto addresses = NewAddresses ();
|
||||||
uint8_t numAddresses;
|
uint8_t numAddresses = buf[offset]; offset++;
|
||||||
s.read ((char *)&numAddresses, sizeof (numAddresses));
|
|
||||||
for (int i = 0; i < numAddresses; i++)
|
for (int i = 0; i < numAddresses; i++)
|
||||||
{
|
{
|
||||||
|
if (offset + 9 > len) return false; // 1 byte cost + 8 bytes date
|
||||||
uint8_t supportedTransports = 0;
|
uint8_t supportedTransports = 0;
|
||||||
auto address = NewAddress ();
|
auto address = NewAddress ();
|
||||||
uint8_t cost; // ignore
|
offset++; // cost, ignore
|
||||||
s.read ((char *)&cost, sizeof (cost));
|
address->date = bufbe64toh (buf + offset); offset += 8; // date
|
||||||
s.read ((char *)&address->date, sizeof (address->date));
|
|
||||||
bool isHost = false, isStaticKey = false, isV2 = false, isIntroKey = false;
|
bool isHost = false, isStaticKey = false, isV2 = false, isIntroKey = false;
|
||||||
char transportStyle[6];
|
auto transportStyle = ExtractString (buf + offset, len - offset); offset += transportStyle.length () + 1;
|
||||||
ReadString (transportStyle, 6, s);
|
if (!transportStyle.compare (0, 4, "NTCP")) // NTCP or NTCP2
|
||||||
if (!strncmp (transportStyle, "NTCP", 4)) // NTCP or NTCP2
|
|
||||||
address->transportStyle = eTransportNTCP2;
|
address->transportStyle = eTransportNTCP2;
|
||||||
else if (!strncmp (transportStyle, "SSU", 3)) // SSU or SSU2
|
else if (!transportStyle.compare (0, 3, "SSU")) // SSU or SSU2
|
||||||
{
|
{
|
||||||
address->transportStyle = eTransportSSU2;
|
address->transportStyle = eTransportSSU2;
|
||||||
address->ssu.reset (new SSUExt ());
|
address->ssu.reset (new SSUExt ());
|
||||||
@@ -237,24 +231,22 @@ namespace data
|
|||||||
address->transportStyle = eTransportUnknown;
|
address->transportStyle = eTransportUnknown;
|
||||||
address->caps = 0;
|
address->caps = 0;
|
||||||
address->port = 0;
|
address->port = 0;
|
||||||
uint16_t size, r = 0;
|
if (offset + 2 > len) return false;
|
||||||
s.read ((char *)&size, sizeof (size)); if (!s) return;
|
uint16_t size = bufbe16toh (buf + offset); offset += 2; // size
|
||||||
size = be16toh (size);
|
if (offset + size >= len) return false;
|
||||||
if (address->transportStyle == eTransportUnknown)
|
if (address->transportStyle == eTransportUnknown)
|
||||||
{
|
{
|
||||||
// skip unknown address
|
// skip unknown address
|
||||||
s.seekg (size, std::ios_base::cur);
|
offset += size;
|
||||||
if (s) continue; else return;
|
continue;
|
||||||
}
|
}
|
||||||
|
size_t r = 0;
|
||||||
while (r < size)
|
while (r < size)
|
||||||
{
|
{
|
||||||
char key[255], value[255];
|
auto [key, value, sz] = ExtractParam (buf + offset, len - offset);
|
||||||
r += ReadString (key, 255, s);
|
r += sz; offset += sz;
|
||||||
s.seekg (1, std::ios_base::cur); r++; // =
|
if (key.empty ()) continue;
|
||||||
r += ReadString (value, 255, s);
|
if (key == "host")
|
||||||
s.seekg (1, std::ios_base::cur); r++; // ;
|
|
||||||
if (!s) return;
|
|
||||||
if (!strcmp (key, "host"))
|
|
||||||
{
|
{
|
||||||
boost::system::error_code ecode;
|
boost::system::error_code ecode;
|
||||||
address->host = boost::asio::ip::make_address (value, ecode);
|
address->host = boost::asio::ip::make_address (value, ecode);
|
||||||
@@ -268,63 +260,53 @@ namespace data
|
|||||||
address->transportStyle = eTransportUnknown;
|
address->transportStyle = eTransportUnknown;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (!strcmp (key, "port"))
|
else if (key == "port")
|
||||||
{
|
{
|
||||||
try
|
auto res = std::from_chars(value.data(), value.data() + value.size(), address->port);
|
||||||
{
|
if (res.ec != std::errc())
|
||||||
address->port = boost::lexical_cast<int>(value);
|
LogPrint (eLogWarning, "RouterInfo: 'port' conversion error: ", std::make_error_code (res.ec).message ());
|
||||||
}
|
}
|
||||||
catch (std::exception& ex)
|
else if (key == "mtu")
|
||||||
{
|
|
||||||
LogPrint (eLogWarning, "RouterInfo: 'port' exception ", ex.what ());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (!strcmp (key, "mtu"))
|
|
||||||
{
|
{
|
||||||
if (address->ssu)
|
if (address->ssu)
|
||||||
{
|
{
|
||||||
try
|
auto res = std::from_chars(value.data(), value.data() + value.size(), address->ssu->mtu);
|
||||||
{
|
if (res.ec != std::errc())
|
||||||
address->ssu->mtu = boost::lexical_cast<int>(value);
|
LogPrint (eLogWarning, "RouterInfo: 'mtu' conversion error: ", std::make_error_code (res.ec).message ());
|
||||||
}
|
|
||||||
catch (std::exception& ex)
|
|
||||||
{
|
|
||||||
LogPrint (eLogWarning, "RouterInfo: 'mtu' exception ", ex.what ());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
LogPrint (eLogWarning, "RouterInfo: Unexpected field 'mtu' for NTCP2");
|
LogPrint (eLogWarning, "RouterInfo: Unexpected field 'mtu' for NTCP2");
|
||||||
}
|
}
|
||||||
else if (!strcmp (key, "caps"))
|
else if (key == "caps")
|
||||||
address->caps = ExtractAddressCaps (value);
|
address->caps = ExtractAddressCaps (value);
|
||||||
else if (!strcmp (key, "s")) // ntcp2 or ssu2 static key
|
else if (key == "s") // ntcp2 or ssu2 static key
|
||||||
{
|
{
|
||||||
if (Base64ToByteStream (value, strlen (value), address->s, 32) == 32 &&
|
if (Base64ToByteStream (value.data (), value.length (), address->s, 32) == 32 &&
|
||||||
!(address->s[31] & 0x80)) // check if x25519 public key
|
!(address->s[31] & 0x80)) // check if x25519 public key
|
||||||
isStaticKey = true;
|
isStaticKey = true;
|
||||||
else
|
else
|
||||||
address->transportStyle = eTransportUnknown; // invalid address
|
address->transportStyle = eTransportUnknown; // invalid address
|
||||||
}
|
}
|
||||||
else if (!strcmp (key, "i")) // ntcp2 iv or ssu2 intro
|
else if (key == "i") // ntcp2 iv or ssu2 intro
|
||||||
{
|
{
|
||||||
if (address->IsNTCP2 ())
|
if (address->IsNTCP2 ())
|
||||||
{
|
{
|
||||||
if (Base64ToByteStream (value, strlen (value), address->i, 16) == 16)
|
if (Base64ToByteStream (value.data (), value.length (), address->i, 16) == 16)
|
||||||
address->published = true; // presence of "i" means "published" NTCP2
|
address->published = true; // presence of "i" means "published" NTCP2
|
||||||
else
|
else
|
||||||
address->transportStyle = eTransportUnknown; // invalid address
|
address->transportStyle = eTransportUnknown; // invalid address
|
||||||
}
|
}
|
||||||
else if (address->IsSSU2 ())
|
else if (address->IsSSU2 ())
|
||||||
{
|
{
|
||||||
if (Base64ToByteStream (value, strlen (value), address->i, 32) == 32)
|
if (Base64ToByteStream (value.data (), value.length (), address->i, 32) == 32)
|
||||||
isIntroKey = true;
|
isIntroKey = true;
|
||||||
else
|
else
|
||||||
address->transportStyle = eTransportUnknown; // invalid address
|
address->transportStyle = eTransportUnknown; // invalid address
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (!strcmp (key, "v"))
|
else if (key == "v")
|
||||||
{
|
{
|
||||||
if (!strcmp (value, "2"))
|
if (value == "2")
|
||||||
isV2 = true;
|
isV2 = true;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -340,13 +322,11 @@ namespace data
|
|||||||
LogPrint (eLogError, "RouterInfo: Introducer is presented for non-SSU address. Skipped");
|
LogPrint (eLogError, "RouterInfo: Introducer is presented for non-SSU address. Skipped");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
size_t l = strlen(key);
|
unsigned char index = key[key.length () - 1] - '0'; // TODO:
|
||||||
unsigned char index = key[l-1] - '0'; // TODO:
|
|
||||||
key[l-1] = 0;
|
|
||||||
if (index > 9)
|
if (index > 9)
|
||||||
{
|
{
|
||||||
LogPrint (eLogError, "RouterInfo: Unexpected introducer's index ", index, " skipped");
|
LogPrint (eLogError, "RouterInfo: Unexpected introducer's index ", index, " skipped");
|
||||||
if (s) continue; else return;
|
continue;
|
||||||
}
|
}
|
||||||
if (index >= address->ssu->introducers.size ())
|
if (index >= address->ssu->introducers.size ())
|
||||||
{
|
{
|
||||||
@@ -355,34 +335,23 @@ namespace data
|
|||||||
address->ssu->introducers.resize (index + 1);
|
address->ssu->introducers.resize (index + 1);
|
||||||
}
|
}
|
||||||
Introducer& introducer = address->ssu->introducers.at (index);
|
Introducer& introducer = address->ssu->introducers.at (index);
|
||||||
if (!strcmp (key, "itag"))
|
auto key1 = key.substr(0, key.length () - 1);
|
||||||
|
if (key1 == "itag")
|
||||||
{
|
{
|
||||||
try
|
auto res = std::from_chars(value.data(), value.data() + value.size(), introducer.iTag);
|
||||||
|
if (res.ec != std::errc())
|
||||||
|
LogPrint (eLogWarning, "RouterInfo: 'itag' conversion error: ", std::make_error_code (res.ec).message ());
|
||||||
|
}
|
||||||
|
else if (key1 == "ih")
|
||||||
|
Base64ToByteStream (value.data (), value.length (), introducer.iH, 32);
|
||||||
|
else if (key1 == "iexp")
|
||||||
{
|
{
|
||||||
introducer.iTag = boost::lexical_cast<uint32_t>(value);
|
auto res = std::from_chars(value.data(), value.data() + value.size(), introducer.iExp);
|
||||||
}
|
if (res.ec != std::errc())
|
||||||
catch (std::exception& ex)
|
LogPrint (eLogWarning, "RouterInfo: 'iexp' conversion error: ", std::make_error_code (res.ec).message ());
|
||||||
{
|
|
||||||
LogPrint (eLogWarning, "RouterInfo: 'itag' exception ", ex.what ());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (!strcmp (key, "ih"))
|
|
||||||
Base64ToByteStream (value, strlen (value), introducer.iH, 32);
|
|
||||||
else if (!strcmp (key, "iexp"))
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
introducer.iExp = boost::lexical_cast<uint32_t>(value);
|
|
||||||
}
|
|
||||||
catch (std::exception& ex)
|
|
||||||
{
|
|
||||||
LogPrint (eLogWarning, "RouterInfo: 'iexp' exception ", ex.what ());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!s) return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (address->transportStyle == eTransportNTCP2)
|
if (address->transportStyle == eTransportNTCP2)
|
||||||
{
|
{
|
||||||
if (isStaticKey)
|
if (isStaticKey)
|
||||||
@@ -446,66 +415,73 @@ namespace data
|
|||||||
boost::atomic_store (&m_Addresses, addresses);
|
boost::atomic_store (&m_Addresses, addresses);
|
||||||
#endif
|
#endif
|
||||||
// read peers
|
// read peers
|
||||||
uint8_t numPeers;
|
if (offset + 1 > len) return false;
|
||||||
s.read ((char *)&numPeers, sizeof (numPeers)); if (!s) return;
|
uint8_t numPeers = buf[offset]; offset++; // num peers
|
||||||
s.seekg (numPeers*32, std::ios_base::cur); // TODO: read peers
|
offset += numPeers*32; // TODO: read peers
|
||||||
// read properties
|
// read properties
|
||||||
|
if (offset + 2 > len) return false;
|
||||||
m_Version = 0;
|
m_Version = 0;
|
||||||
bool isNetId = false;
|
bool isNetId = false;
|
||||||
std::string family;
|
std::string family;
|
||||||
uint16_t size, r = 0;
|
uint16_t size = bufbe16toh (buf + offset); offset += 2; // size
|
||||||
s.read ((char *)&size, sizeof (size)); if (!s) return;
|
if (offset + size > len) return false;
|
||||||
size = be16toh (size);
|
size_t r = 0;
|
||||||
while (r < size)
|
while (r < size)
|
||||||
{
|
{
|
||||||
char key[255], value[255];
|
auto [key, value, sz] = ExtractParam (buf + offset, len - offset);
|
||||||
r += ReadString (key, 255, s);
|
r += sz; offset += sz;
|
||||||
s.seekg (1, std::ios_base::cur); r++; // =
|
if (key.empty ()) continue;
|
||||||
r += ReadString (value, 255, s);
|
|
||||||
s.seekg (1, std::ios_base::cur); r++; // ;
|
|
||||||
if (!s) return;
|
|
||||||
SetProperty (key, value);
|
SetProperty (key, value);
|
||||||
|
|
||||||
// extract caps
|
// extract caps
|
||||||
if (!strcmp (key, "caps"))
|
if (key == "caps")
|
||||||
{
|
{
|
||||||
ExtractCaps (value);
|
ExtractCaps (value);
|
||||||
m_IsFloodfill = IsDeclaredFloodfill ();
|
m_IsFloodfill = IsDeclaredFloodfill ();
|
||||||
}
|
}
|
||||||
// extract version
|
// extract version
|
||||||
else if (!strcmp (key, ROUTER_INFO_PROPERTY_VERSION))
|
else if (key == ROUTER_INFO_PROPERTY_VERSION)
|
||||||
{
|
{
|
||||||
m_Version = 0;
|
m_Version = 0;
|
||||||
char * ch = value;
|
for (auto ch: value)
|
||||||
while (*ch)
|
|
||||||
{
|
{
|
||||||
if (*ch >= '0' && *ch <= '9')
|
if (ch >= '0' && ch <= '9')
|
||||||
{
|
{
|
||||||
m_Version *= 10;
|
m_Version *= 10;
|
||||||
m_Version += (*ch - '0');
|
m_Version += (ch - '0');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (m_Version < NETDB_MIN_PEER_TEST_VERSION && (m_SupportedTransports & (eSSU2V4 | eSSU2V6)))
|
||||||
|
{
|
||||||
|
auto addresses = GetAddresses ();
|
||||||
|
if (addresses)
|
||||||
|
{
|
||||||
|
if ((*addresses)[eSSU2V4Idx]) (*addresses)[eSSU2V4Idx]->caps &= ~eSSUTesting;
|
||||||
|
if ((*addresses)[eSSU2V6Idx]) (*addresses)[eSSU2V6Idx]->caps &= ~eSSUTesting;
|
||||||
}
|
}
|
||||||
ch++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// check netId
|
// check netId
|
||||||
else if (!strcmp (key, ROUTER_INFO_PROPERTY_NETID))
|
else if (key == ROUTER_INFO_PROPERTY_NETID)
|
||||||
{
|
{
|
||||||
isNetId = true;
|
isNetId = true;
|
||||||
if (atoi (value) != i2p::context.GetNetID ())
|
int netID;
|
||||||
|
auto res = std::from_chars(value.data(), value.data() + value.size(), netID);
|
||||||
|
if (res.ec != std::errc() || netID != i2p::context.GetNetID ())
|
||||||
{
|
{
|
||||||
LogPrint (eLogError, "RouterInfo: Unexpected ", ROUTER_INFO_PROPERTY_NETID, "=", value);
|
LogPrint (eLogError, "RouterInfo: Unexpected ", ROUTER_INFO_PROPERTY_NETID, "=", value);
|
||||||
m_IsUnreachable = true;
|
m_IsUnreachable = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// family
|
// family
|
||||||
else if (!strcmp (key, ROUTER_INFO_PROPERTY_FAMILY))
|
else if (key == ROUTER_INFO_PROPERTY_FAMILY)
|
||||||
{
|
{
|
||||||
family = value;
|
family = value;
|
||||||
boost::to_lower (family);
|
boost::to_lower (family);
|
||||||
}
|
}
|
||||||
else if (!strcmp (key, ROUTER_INFO_PROPERTY_FAMILY_SIG))
|
else if (key == ROUTER_INFO_PROPERTY_FAMILY_SIG)
|
||||||
{
|
{
|
||||||
if (netdb.GetFamilies ().VerifyFamily (family, GetIdentHash (), value))
|
if (netdb.GetFamilies ().VerifyFamily (family, GetIdentHash (), value)) // TODO
|
||||||
m_FamilyID = netdb.GetFamilies ().GetFamilyID (family);
|
m_FamilyID = netdb.GetFamilies ().GetFamilyID (family);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -513,12 +489,12 @@ namespace data
|
|||||||
SetUnreachable (true);
|
SetUnreachable (true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!s) return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!m_SupportedTransports || !isNetId || !m_Version)
|
if (!m_SupportedTransports || !isNetId || !m_Version)
|
||||||
SetUnreachable (true);
|
SetUnreachable (true);
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool RouterInfo::IsFamily (FamilyID famid) const
|
bool RouterInfo::IsFamily (FamilyID famid) const
|
||||||
@@ -526,12 +502,11 @@ namespace data
|
|||||||
return m_FamilyID == famid;
|
return m_FamilyID == famid;
|
||||||
}
|
}
|
||||||
|
|
||||||
void RouterInfo::ExtractCaps (const char * value)
|
void RouterInfo::ExtractCaps (std::string_view value)
|
||||||
{
|
{
|
||||||
const char * cap = value;
|
for (auto cap: value)
|
||||||
while (*cap)
|
|
||||||
{
|
{
|
||||||
switch (*cap)
|
switch (cap)
|
||||||
{
|
{
|
||||||
case CAPS_FLAG_FLOODFILL:
|
case CAPS_FLAG_FLOODFILL:
|
||||||
m_Caps |= Caps::eFloodfill;
|
m_Caps |= Caps::eFloodfill;
|
||||||
@@ -540,16 +515,16 @@ namespace data
|
|||||||
case CAPS_FLAG_LOW_BANDWIDTH2:
|
case CAPS_FLAG_LOW_BANDWIDTH2:
|
||||||
case CAPS_FLAG_LOW_BANDWIDTH3:
|
case CAPS_FLAG_LOW_BANDWIDTH3:
|
||||||
case CAPS_FLAG_LOW_BANDWIDTH4:
|
case CAPS_FLAG_LOW_BANDWIDTH4:
|
||||||
m_BandwidthCap = *cap;
|
m_BandwidthCap = cap;
|
||||||
break;
|
break;
|
||||||
case CAPS_FLAG_HIGH_BANDWIDTH:
|
case CAPS_FLAG_HIGH_BANDWIDTH:
|
||||||
m_Caps |= Caps::eHighBandwidth;
|
m_Caps |= Caps::eHighBandwidth;
|
||||||
m_BandwidthCap = *cap;
|
m_BandwidthCap = cap;
|
||||||
break;
|
break;
|
||||||
case CAPS_FLAG_EXTRA_BANDWIDTH1:
|
case CAPS_FLAG_EXTRA_BANDWIDTH1:
|
||||||
case CAPS_FLAG_EXTRA_BANDWIDTH2:
|
case CAPS_FLAG_EXTRA_BANDWIDTH2:
|
||||||
m_Caps |= Caps::eExtraBandwidth | Caps::eHighBandwidth;
|
m_Caps |= Caps::eExtraBandwidth | Caps::eHighBandwidth;
|
||||||
m_BandwidthCap = *cap;
|
m_BandwidthCap = cap;
|
||||||
break;
|
break;
|
||||||
case CAPS_FLAG_HIDDEN:
|
case CAPS_FLAG_HIDDEN:
|
||||||
m_Caps |= Caps::eHidden;
|
m_Caps |= Caps::eHidden;
|
||||||
@@ -571,17 +546,15 @@ namespace data
|
|||||||
break;
|
break;
|
||||||
default: ;
|
default: ;
|
||||||
}
|
}
|
||||||
cap++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t RouterInfo::ExtractAddressCaps (const char * value) const
|
uint8_t RouterInfo::ExtractAddressCaps (std::string_view value) const
|
||||||
{
|
{
|
||||||
uint8_t caps = 0;
|
uint8_t caps = 0;
|
||||||
const char * cap = value;
|
for (auto cap: value)
|
||||||
while (*cap)
|
|
||||||
{
|
{
|
||||||
switch (*cap)
|
switch (cap)
|
||||||
{
|
{
|
||||||
case CAPS_FLAG_V4:
|
case CAPS_FLAG_V4:
|
||||||
caps |= AddressCaps::eV4;
|
caps |= AddressCaps::eV4;
|
||||||
@@ -597,7 +570,6 @@ namespace data
|
|||||||
break;
|
break;
|
||||||
default: ;
|
default: ;
|
||||||
}
|
}
|
||||||
cap++;
|
|
||||||
}
|
}
|
||||||
return caps;
|
return caps;
|
||||||
}
|
}
|
||||||
@@ -661,23 +633,39 @@ namespace data
|
|||||||
return SaveToFile (fullPath, m_Buffer);
|
return SaveToFile (fullPath, m_Buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t RouterInfo::ReadString (char * str, size_t len, std::istream& s) const
|
std::string_view RouterInfo::ExtractString (const uint8_t * buf, size_t len) const
|
||||||
{
|
{
|
||||||
uint8_t l;
|
uint8_t l = buf[0];
|
||||||
s.read ((char *)&l, 1);
|
if (l > len)
|
||||||
if (l < len)
|
|
||||||
{
|
|
||||||
s.read (str, l);
|
|
||||||
if (!s) l = 0; // failed, return empty string
|
|
||||||
str[l] = 0;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
LogPrint (eLogWarning, "RouterInfo: String length ", (int)l, " exceeds buffer size ", len);
|
LogPrint (eLogWarning, "RouterInfo: String length ", (int)l, " exceeds buffer size ", len);
|
||||||
s.seekg (l, std::ios::cur); // skip
|
l = len;
|
||||||
str[0] = 0;
|
|
||||||
}
|
}
|
||||||
return l+1;
|
return { (const char *)(buf + 1), l };
|
||||||
|
}
|
||||||
|
|
||||||
|
std::tuple<std::string_view, std::string_view, size_t> RouterInfo::ExtractParam (const uint8_t * buf, size_t len) const
|
||||||
|
{
|
||||||
|
auto key = ExtractString (buf, len);
|
||||||
|
size_t offset = key.length () + 1;
|
||||||
|
if (offset >= len) return { std::string_view(), std::string_view(), len };
|
||||||
|
if (buf[offset] != '=')
|
||||||
|
{
|
||||||
|
LogPrint (eLogWarning, "RouterInfo: Unexpected character ", buf[offset], " instead '=' after ", key);
|
||||||
|
key = std::string_view();
|
||||||
|
}
|
||||||
|
offset++;
|
||||||
|
if (offset >= len) return { key, std::string_view(), len };
|
||||||
|
auto value = ExtractString (buf + offset, len - offset);
|
||||||
|
offset += value.length () + 1;
|
||||||
|
if (offset >= len) return { key, std::string_view(), len };
|
||||||
|
if (buf[offset] != ';')
|
||||||
|
{
|
||||||
|
LogPrint (eLogWarning, "RouterInfo: Unexpected character ", buf[offset], " instead ';' after ", value);
|
||||||
|
value = std::string_view();
|
||||||
|
}
|
||||||
|
offset++;
|
||||||
|
return { key, value, offset };
|
||||||
}
|
}
|
||||||
|
|
||||||
void RouterInfo::AddNTCP2Address (const uint8_t * staticKey, const uint8_t * iv,int port, uint8_t caps)
|
void RouterInfo::AddNTCP2Address (const uint8_t * staticKey, const uint8_t * iv,int port, uint8_t caps)
|
||||||
@@ -1136,12 +1124,12 @@ namespace data
|
|||||||
|
|
||||||
void RouterInfo::UpdateBuffer (const uint8_t * buf, size_t len)
|
void RouterInfo::UpdateBuffer (const uint8_t * buf, size_t len)
|
||||||
{
|
{
|
||||||
|
m_IsBufferScheduledToDelete = false;
|
||||||
if (!m_Buffer)
|
if (!m_Buffer)
|
||||||
m_Buffer = NewBuffer ();
|
m_Buffer = NewBuffer ();
|
||||||
if (len > m_Buffer->size ()) len = m_Buffer->size ();
|
if (len > m_Buffer->size ()) len = m_Buffer->size ();
|
||||||
memcpy (m_Buffer->data (), buf, len);
|
memcpy (m_Buffer->data (), buf, len);
|
||||||
m_Buffer->SetBufferLen (len);
|
m_Buffer->SetBufferLen (len);
|
||||||
m_IsBufferScheduledToDelete = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<RouterInfo::Buffer> RouterInfo::CopyBuffer () const
|
std::shared_ptr<RouterInfo::Buffer> RouterInfo::CopyBuffer () const
|
||||||
@@ -1393,9 +1381,9 @@ namespace data
|
|||||||
if (!introducer.iTag) continue;
|
if (!introducer.iTag) continue;
|
||||||
if (introducer.iExp) // expiration is specified
|
if (introducer.iExp) // expiration is specified
|
||||||
{
|
{
|
||||||
WriteString ("iexp" + boost::lexical_cast<std::string>(i), properties);
|
WriteString ("iexp" + std::to_string(i), properties);
|
||||||
properties << '=';
|
properties << '=';
|
||||||
WriteString (boost::lexical_cast<std::string>(introducer.iExp), properties);
|
WriteString (std::to_string(introducer.iExp), properties);
|
||||||
properties << ';';
|
properties << ';';
|
||||||
}
|
}
|
||||||
i++;
|
i++;
|
||||||
@@ -1404,7 +1392,7 @@ namespace data
|
|||||||
for (const auto& introducer: address.ssu->introducers)
|
for (const auto& introducer: address.ssu->introducers)
|
||||||
{
|
{
|
||||||
if (!introducer.iTag) continue;
|
if (!introducer.iTag) continue;
|
||||||
WriteString ("ih" + boost::lexical_cast<std::string>(i), properties);
|
WriteString ("ih" + std::to_string(i), properties);
|
||||||
properties << '=';
|
properties << '=';
|
||||||
char value[64];
|
char value[64];
|
||||||
size_t l = ByteStreamToBase64 (introducer.iH, 32, value, 64);
|
size_t l = ByteStreamToBase64 (introducer.iH, 32, value, 64);
|
||||||
@@ -1417,9 +1405,9 @@ namespace data
|
|||||||
for (const auto& introducer: address.ssu->introducers)
|
for (const auto& introducer: address.ssu->introducers)
|
||||||
{
|
{
|
||||||
if (!introducer.iTag) continue;
|
if (!introducer.iTag) continue;
|
||||||
WriteString ("itag" + boost::lexical_cast<std::string>(i), properties);
|
WriteString ("itag" + std::to_string(i), properties);
|
||||||
properties << '=';
|
properties << '=';
|
||||||
WriteString (boost::lexical_cast<std::string>(introducer.iTag), properties);
|
WriteString (std::to_string(introducer.iTag), properties);
|
||||||
properties << ';';
|
properties << ';';
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
@@ -1433,7 +1421,7 @@ namespace data
|
|||||||
{
|
{
|
||||||
WriteString ("mtu", properties);
|
WriteString ("mtu", properties);
|
||||||
properties << '=';
|
properties << '=';
|
||||||
WriteString (boost::lexical_cast<std::string>(address.ssu->mtu), properties);
|
WriteString (std::to_string(address.ssu->mtu), properties);
|
||||||
properties << ';';
|
properties << ';';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1441,7 +1429,7 @@ namespace data
|
|||||||
{
|
{
|
||||||
WriteString ("port", properties);
|
WriteString ("port", properties);
|
||||||
properties << '=';
|
properties << '=';
|
||||||
WriteString (boost::lexical_cast<std::string>(address.port), properties);
|
WriteString (std::to_string(address.port), properties);
|
||||||
properties << ';';
|
properties << ';';
|
||||||
}
|
}
|
||||||
if (address.IsNTCP2 () || address.IsSSU2 ())
|
if (address.IsNTCP2 () || address.IsSSU2 ())
|
||||||
@@ -1476,9 +1464,11 @@ namespace data
|
|||||||
s.write (properties.str ().c_str (), properties.str ().size ());
|
s.write (properties.str ().c_str (), properties.str ().size ());
|
||||||
}
|
}
|
||||||
|
|
||||||
void LocalRouterInfo::SetProperty (const std::string& key, const std::string& value)
|
void LocalRouterInfo::SetProperty (std::string_view key, std::string_view value)
|
||||||
{
|
{
|
||||||
m_Properties[key] = value;
|
auto [it, inserted] = m_Properties.emplace (key, value);
|
||||||
|
if (!inserted)
|
||||||
|
it->second = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
void LocalRouterInfo::DeleteProperty (const std::string& key)
|
void LocalRouterInfo::DeleteProperty (const std::string& key)
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2013-2024, The PurpleI2P Project
|
* Copyright (c) 2013-2025, The PurpleI2P Project
|
||||||
*
|
*
|
||||||
* This file is part of Purple i2pd project and licensed under BSD3
|
* This file is part of Purple i2pd project and licensed under BSD3
|
||||||
*
|
*
|
||||||
@@ -11,6 +11,8 @@
|
|||||||
|
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <string_view>
|
||||||
|
#include <tuple>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <array>
|
#include <array>
|
||||||
@@ -219,7 +221,7 @@ namespace data
|
|||||||
std::string GetIdentHashBase64 () const { return GetIdentHash ().ToBase64 (); };
|
std::string GetIdentHashBase64 () const { return GetIdentHash ().ToBase64 (); };
|
||||||
uint64_t GetTimestamp () const { return m_Timestamp; };
|
uint64_t GetTimestamp () const { return m_Timestamp; };
|
||||||
int GetVersion () const { return m_Version; };
|
int GetVersion () const { return m_Version; };
|
||||||
virtual void SetProperty (const std::string& key, const std::string& value) {};
|
virtual void SetProperty (std::string_view key, std::string_view value) {};
|
||||||
virtual void ClearProperties () {};
|
virtual void ClearProperties () {};
|
||||||
AddressesPtr GetAddresses () const; // should be called for local RI only, otherwise must return shared_ptr
|
AddressesPtr GetAddresses () const; // should be called for local RI only, otherwise must return shared_ptr
|
||||||
std::shared_ptr<const Address> GetNTCP2V4Address () const;
|
std::shared_ptr<const Address> GetNTCP2V4Address () const;
|
||||||
@@ -294,6 +296,7 @@ namespace data
|
|||||||
std::shared_ptr<Buffer> GetSharedBuffer () const { return m_Buffer; };
|
std::shared_ptr<Buffer> GetSharedBuffer () const { return m_Buffer; };
|
||||||
std::shared_ptr<Buffer> CopyBuffer () const;
|
std::shared_ptr<Buffer> CopyBuffer () const;
|
||||||
void ScheduleBufferToDelete () { m_IsBufferScheduledToDelete = true; };
|
void ScheduleBufferToDelete () { m_IsBufferScheduledToDelete = true; };
|
||||||
|
void CancelBufferToDelete () { m_IsBufferScheduledToDelete = false; };
|
||||||
bool IsBufferScheduledToDelete () const { return m_IsBufferScheduledToDelete; };
|
bool IsBufferScheduledToDelete () const { return m_IsBufferScheduledToDelete; };
|
||||||
|
|
||||||
bool IsUpdated () const { return m_IsUpdated; };
|
bool IsUpdated () const { return m_IsUpdated; };
|
||||||
@@ -332,11 +335,12 @@ namespace data
|
|||||||
|
|
||||||
bool LoadFile (const std::string& fullPath);
|
bool LoadFile (const std::string& fullPath);
|
||||||
void ReadFromFile (const std::string& fullPath);
|
void ReadFromFile (const std::string& fullPath);
|
||||||
void ReadFromStream (std::istream& s);
|
bool ReadFromBuffer (const uint8_t * buf, size_t len); // return false if malformed
|
||||||
void ReadFromBuffer (bool verifySignature);
|
void ReadFromBuffer (bool verifySignature);
|
||||||
size_t ReadString (char* str, size_t len, std::istream& s) const;
|
std::string_view ExtractString (const uint8_t * buf, size_t len) const;
|
||||||
void ExtractCaps (const char * value);
|
std::tuple<std::string_view, std::string_view, size_t> ExtractParam (const uint8_t * buf, size_t len) const;
|
||||||
uint8_t ExtractAddressCaps (const char * value) const;
|
void ExtractCaps (std::string_view value);
|
||||||
|
uint8_t ExtractAddressCaps (std::string_view value) const;
|
||||||
void UpdateIntroducers (std::shared_ptr<Address> address, uint64_t ts);
|
void UpdateIntroducers (std::shared_ptr<Address> address, uint64_t ts);
|
||||||
template<typename Filter>
|
template<typename Filter>
|
||||||
std::shared_ptr<const Address> GetAddress (Filter filter) const;
|
std::shared_ptr<const Address> GetAddress (Filter filter) const;
|
||||||
@@ -378,7 +382,7 @@ namespace data
|
|||||||
void UpdateCaps (uint8_t caps);
|
void UpdateCaps (uint8_t caps);
|
||||||
bool UpdateCongestion (Congestion c); // returns true if updated
|
bool UpdateCongestion (Congestion c); // returns true if updated
|
||||||
|
|
||||||
void SetProperty (const std::string& key, const std::string& value) override;
|
void SetProperty (std::string_view key, std::string_view value) override;
|
||||||
void DeleteProperty (const std::string& key);
|
void DeleteProperty (const std::string& key);
|
||||||
std::string GetProperty (const std::string& key) const;
|
std::string GetProperty (const std::string& key) const;
|
||||||
void ClearProperties () override { m_Properties.clear (); };
|
void ClearProperties () override { m_Properties.clear (); };
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2022-2024, The PurpleI2P Project
|
* Copyright (c) 2022-2025, The PurpleI2P Project
|
||||||
*
|
*
|
||||||
* This file is part of Purple i2pd project and licensed under BSD3
|
* This file is part of Purple i2pd project and licensed under BSD3
|
||||||
*
|
*
|
||||||
@@ -890,7 +890,7 @@ namespace transport
|
|||||||
}
|
}
|
||||||
|
|
||||||
auto session = std::make_shared<SSU2Session> (*this, router, address);
|
auto session = std::make_shared<SSU2Session> (*this, router, address);
|
||||||
if (!isValidEndpoint && router->GetProfile ()->HasLastEndpoint (address->IsV4 ()))
|
if (!isValidEndpoint && router->HasProfile () && router->GetProfile ()->HasLastEndpoint (address->IsV4 ()))
|
||||||
{
|
{
|
||||||
// router doesn't publish endpoint, but we connected before and hole punch might be alive
|
// router doesn't publish endpoint, but we connected before and hole punch might be alive
|
||||||
auto ep = router->GetProfile ()->GetLastEndpoint ();
|
auto ep = router->GetProfile ()->GetLastEndpoint ();
|
||||||
@@ -1251,18 +1251,21 @@ namespace transport
|
|||||||
}
|
}
|
||||||
uint64_t token;
|
uint64_t token;
|
||||||
RAND_bytes ((uint8_t *)&token, 8);
|
RAND_bytes ((uint8_t *)&token, 8);
|
||||||
m_IncomingTokens.emplace (ep, std::make_pair (token, uint32_t(ts + SSU2_TOKEN_EXPIRATION_TIMEOUT)));
|
if (!token) token = 1; // token can't be zero
|
||||||
|
m_IncomingTokens.try_emplace (ep, token, uint32_t(ts + SSU2_TOKEN_EXPIRATION_TIMEOUT));
|
||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::pair<uint64_t, uint32_t> SSU2Server::NewIncomingToken (const boost::asio::ip::udp::endpoint& ep)
|
std::pair<uint64_t, uint32_t> SSU2Server::NewIncomingToken (const boost::asio::ip::udp::endpoint& ep)
|
||||||
{
|
{
|
||||||
m_IncomingTokens.erase (ep); // drop previous
|
|
||||||
uint64_t token;
|
uint64_t token;
|
||||||
RAND_bytes ((uint8_t *)&token, 8);
|
RAND_bytes ((uint8_t *)&token, 8);
|
||||||
auto ret = std::make_pair (token, uint32_t(i2p::util::GetSecondsSinceEpoch () + SSU2_NEXT_TOKEN_EXPIRATION_TIMEOUT));
|
if (!token) token = 1; // token can't be zero
|
||||||
m_IncomingTokens.emplace (ep, ret);
|
uint32_t expires = i2p::util::GetSecondsSinceEpoch () + SSU2_NEXT_TOKEN_EXPIRATION_TIMEOUT;
|
||||||
return ret;
|
auto [it, inserted] = m_IncomingTokens.try_emplace (ep, token, expires);
|
||||||
|
if (!inserted)
|
||||||
|
it->second = { token, expires }; // override
|
||||||
|
return it->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::shared_ptr<SSU2Session> > SSU2Server::FindIntroducers (int maxNumIntroducers,
|
std::vector<std::shared_ptr<SSU2Session> > SSU2Server::FindIntroducers (int maxNumIntroducers,
|
||||||
@@ -1528,6 +1531,11 @@ namespace transport
|
|||||||
return m_Decryptor.Decrypt (msg, msgLen, ad, adLen, key, nonce, buf, len);
|
return m_Decryptor.Decrypt (msg, msgLen, ad, adLen, key, nonce, buf, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SSU2Server::ChaCha20 (const uint8_t * msg, size_t msgLen, const uint8_t * key, const uint8_t * nonce, uint8_t * out)
|
||||||
|
{
|
||||||
|
m_ChaCha20 (msg, msgLen, key, nonce, out);
|
||||||
|
}
|
||||||
|
|
||||||
void SSU2Server::SendThroughProxy (const uint8_t * header, size_t headerLen, const uint8_t * headerX, size_t headerXLen,
|
void SSU2Server::SendThroughProxy (const uint8_t * header, size_t headerLen, const uint8_t * headerX, size_t headerXLen,
|
||||||
const uint8_t * payload, size_t payloadLen, const boost::asio::ip::udp::endpoint& to)
|
const uint8_t * payload, size_t payloadLen, const boost::asio::ip::udp::endpoint& to)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2022-2024, The PurpleI2P Project
|
* Copyright (c) 2022-2025, The PurpleI2P Project
|
||||||
*
|
*
|
||||||
* This file is part of Purple i2pd project and licensed under BSD3
|
* This file is part of Purple i2pd project and licensed under BSD3
|
||||||
*
|
*
|
||||||
@@ -86,6 +86,7 @@ namespace transport
|
|||||||
const uint8_t * key, const uint8_t * nonce, uint8_t * buf, size_t len);
|
const uint8_t * key, const uint8_t * nonce, uint8_t * buf, size_t len);
|
||||||
bool AEADChaCha20Poly1305Decrypt (const uint8_t * msg, size_t msgLen, const uint8_t * ad, size_t adLen,
|
bool AEADChaCha20Poly1305Decrypt (const uint8_t * msg, size_t msgLen, const uint8_t * ad, size_t adLen,
|
||||||
const uint8_t * key, const uint8_t * nonce, uint8_t * buf, size_t len);
|
const uint8_t * key, const uint8_t * nonce, uint8_t * buf, size_t len);
|
||||||
|
void ChaCha20 (const uint8_t * msg, size_t msgLen, const uint8_t * key, const uint8_t * nonce, uint8_t * out);
|
||||||
bool IsMaxNumIntroducers (bool v4) const { return (v4 ? m_Introducers.size () : m_IntroducersV6.size ()) >= SSU2_MAX_NUM_INTRODUCERS; }
|
bool IsMaxNumIntroducers (bool v4) const { return (v4 ? m_Introducers.size () : m_IntroducersV6.size ()) >= SSU2_MAX_NUM_INTRODUCERS; }
|
||||||
bool IsSyncClockFromPeers () const { return m_IsSyncClockFromPeers; };
|
bool IsSyncClockFromPeers () const { return m_IsSyncClockFromPeers; };
|
||||||
void AdjustTimeOffset (int64_t offset, std::shared_ptr<const i2p::data::IdentityEx> from);
|
void AdjustTimeOffset (int64_t offset, std::shared_ptr<const i2p::data::IdentityEx> from);
|
||||||
@@ -206,6 +207,7 @@ namespace transport
|
|||||||
mutable std::mutex m_ReceivedPacketsQueueMutex;
|
mutable std::mutex m_ReceivedPacketsQueueMutex;
|
||||||
i2p::crypto::AEADChaCha20Poly1305Encryptor m_Encryptor;
|
i2p::crypto::AEADChaCha20Poly1305Encryptor m_Encryptor;
|
||||||
i2p::crypto::AEADChaCha20Poly1305Decryptor m_Decryptor;
|
i2p::crypto::AEADChaCha20Poly1305Decryptor m_Decryptor;
|
||||||
|
i2p::crypto::ChaCha20Context m_ChaCha20;
|
||||||
|
|
||||||
// proxy
|
// proxy
|
||||||
bool m_IsThroughProxy;
|
bool m_IsThroughProxy;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2024, The PurpleI2P Project
|
* Copyright (c) 2024-2025, The PurpleI2P Project
|
||||||
*
|
*
|
||||||
* This file is part of Purple i2pd project and licensed under BSD3
|
* This file is part of Purple i2pd project and licensed under BSD3
|
||||||
*
|
*
|
||||||
@@ -46,7 +46,7 @@ namespace transport
|
|||||||
}
|
}
|
||||||
uint8_t nonce[12] = {0};
|
uint8_t nonce[12] = {0};
|
||||||
uint64_t headerX[2]; // sourceConnID, token
|
uint64_t headerX[2]; // sourceConnID, token
|
||||||
i2p::crypto::ChaCha20 (buf + 16, 16, i2p::context.GetSSU2IntroKey (), nonce, (uint8_t *)headerX);
|
GetServer ().ChaCha20 (buf + 16, 16, i2p::context.GetSSU2IntroKey (), nonce, (uint8_t *)headerX);
|
||||||
SetDestConnID (headerX[0]);
|
SetDestConnID (headerX[0]);
|
||||||
// decrypt and handle payload
|
// decrypt and handle payload
|
||||||
uint8_t * payload = buf + 32;
|
uint8_t * payload = buf + 32;
|
||||||
@@ -183,7 +183,7 @@ namespace transport
|
|||||||
header.ll[0] ^= CreateHeaderMask (addr->i, payload + (payloadSize - 24));
|
header.ll[0] ^= CreateHeaderMask (addr->i, payload + (payloadSize - 24));
|
||||||
header.ll[1] ^= CreateHeaderMask (addr->i, payload + (payloadSize - 12));
|
header.ll[1] ^= CreateHeaderMask (addr->i, payload + (payloadSize - 12));
|
||||||
memset (n, 0, 12);
|
memset (n, 0, 12);
|
||||||
i2p::crypto::ChaCha20 (h + 16, 16, addr->i, n, h + 16);
|
GetServer ().ChaCha20 (h + 16, 16, addr->i, n, h + 16);
|
||||||
// send
|
// send
|
||||||
GetServer ().Send (header.buf, 16, h + 16, 16, payload, payloadSize, GetRemoteEndpoint ());
|
GetServer ().Send (header.buf, 16, h + 16, 16, payload, payloadSize, GetRemoteEndpoint ());
|
||||||
UpdateNumSentBytes (payloadSize + 32);
|
UpdateNumSentBytes (payloadSize + 32);
|
||||||
@@ -191,12 +191,7 @@ namespace transport
|
|||||||
|
|
||||||
void SSU2PeerTestSession::SendPeerTest (uint8_t msg, const uint8_t * signedData, size_t signedDataLen, bool delayed)
|
void SSU2PeerTestSession::SendPeerTest (uint8_t msg, const uint8_t * signedData, size_t signedDataLen, bool delayed)
|
||||||
{
|
{
|
||||||
#if __cplusplus >= 202002L // C++20
|
|
||||||
m_SignedData.assign (signedData, signedData + signedDataLen);
|
m_SignedData.assign (signedData, signedData + signedDataLen);
|
||||||
#else
|
|
||||||
m_SignedData.resize (signedDataLen);
|
|
||||||
memcpy (m_SignedData.data (), signedData, signedDataLen);
|
|
||||||
#endif
|
|
||||||
if (!delayed)
|
if (!delayed)
|
||||||
SendPeerTest (msg);
|
SendPeerTest (msg);
|
||||||
// schedule resend for msgs 5 or 6
|
// schedule resend for msgs 5 or 6
|
||||||
@@ -257,7 +252,7 @@ namespace transport
|
|||||||
{
|
{
|
||||||
// we are Charlie
|
// we are Charlie
|
||||||
uint64_t destConnID = htobe64 (((uint64_t)nonce << 32) | nonce); // dest id
|
uint64_t destConnID = htobe64 (((uint64_t)nonce << 32) | nonce); // dest id
|
||||||
uint32_t sourceConnID = ~destConnID;
|
uint64_t sourceConnID = ~destConnID;
|
||||||
SetSourceConnID (sourceConnID);
|
SetSourceConnID (sourceConnID);
|
||||||
SetDestConnID (destConnID);
|
SetDestConnID (destConnID);
|
||||||
SetState (eSSU2SessionStateHolePunch);
|
SetState (eSSU2SessionStateHolePunch);
|
||||||
@@ -305,7 +300,7 @@ namespace transport
|
|||||||
header.ll[0] ^= CreateHeaderMask (addr->i, payload + (payloadSize - 24));
|
header.ll[0] ^= CreateHeaderMask (addr->i, payload + (payloadSize - 24));
|
||||||
header.ll[1] ^= CreateHeaderMask (addr->i, payload + (payloadSize - 12));
|
header.ll[1] ^= CreateHeaderMask (addr->i, payload + (payloadSize - 12));
|
||||||
memset (n, 0, 12);
|
memset (n, 0, 12);
|
||||||
i2p::crypto::ChaCha20 (h + 16, 16, addr->i, n, h + 16);
|
GetServer ().ChaCha20 (h + 16, 16, addr->i, n, h + 16);
|
||||||
// send
|
// send
|
||||||
GetServer ().Send (header.buf, 16, h + 16, 16, payload, payloadSize, ep);
|
GetServer ().Send (header.buf, 16, h + 16, 16, payload, payloadSize, ep);
|
||||||
UpdateNumSentBytes (payloadSize + 32);
|
UpdateNumSentBytes (payloadSize + 32);
|
||||||
@@ -313,12 +308,7 @@ namespace transport
|
|||||||
|
|
||||||
void SSU2HolePunchSession::SendHolePunch (const uint8_t * relayResponseBlock, size_t relayResponseBlockLen)
|
void SSU2HolePunchSession::SendHolePunch (const uint8_t * relayResponseBlock, size_t relayResponseBlockLen)
|
||||||
{
|
{
|
||||||
#if __cplusplus >= 202002L // C++20
|
|
||||||
m_RelayResponseBlock.assign (relayResponseBlock, relayResponseBlock + relayResponseBlockLen);
|
m_RelayResponseBlock.assign (relayResponseBlock, relayResponseBlock + relayResponseBlockLen);
|
||||||
#else
|
|
||||||
m_RelayResponseBlock.resize (relayResponseBlockLen);
|
|
||||||
memcpy (m_RelayResponseBlock.data (), relayResponseBlock, relayResponseBlockLen);
|
|
||||||
#endif
|
|
||||||
SendHolePunch ();
|
SendHolePunch ();
|
||||||
ScheduleResend ();
|
ScheduleResend ();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -189,7 +189,7 @@ namespace transport
|
|||||||
if (!asz) return false;
|
if (!asz) return false;
|
||||||
payload[17] = asz;
|
payload[17] = asz;
|
||||||
packet->payloadSize = asz + 18;
|
packet->payloadSize = asz + 18;
|
||||||
SignedData s;
|
SignedData<128> s;
|
||||||
s.Insert ((const uint8_t *)"RelayRequestData", 16); // prologue
|
s.Insert ((const uint8_t *)"RelayRequestData", 16); // prologue
|
||||||
s.Insert (GetRemoteIdentity ()->GetIdentHash (), 32); // bhash
|
s.Insert (GetRemoteIdentity ()->GetIdentHash (), 32); // bhash
|
||||||
s.Insert (session->GetRemoteIdentity ()->GetIdentHash (), 32); // chash
|
s.Insert (session->GetRemoteIdentity ()->GetIdentHash (), 32); // chash
|
||||||
@@ -623,7 +623,8 @@ namespace transport
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
uint32_t packetNum = SendData (it->second->payload, it->second->payloadSize);
|
uint32_t packetNum = SendData (it->second->payload, it->second->payloadSize,
|
||||||
|
it->second->numResends > 1 ? SSU2_FLAG_IMMEDIATE_ACK_REQUESTED : 0);
|
||||||
it->second->numResends++;
|
it->second->numResends++;
|
||||||
it->second->sendTime = ts;
|
it->second->sendTime = ts;
|
||||||
resentPackets.emplace (packetNum, it->second);
|
resentPackets.emplace (packetNum, it->second);
|
||||||
@@ -682,7 +683,7 @@ namespace transport
|
|||||||
}
|
}
|
||||||
const uint8_t nonce[12] = {0};
|
const uint8_t nonce[12] = {0};
|
||||||
uint64_t headerX[2];
|
uint64_t headerX[2];
|
||||||
i2p::crypto::ChaCha20 (buf + 16, 16, i2p::context.GetSSU2IntroKey (), nonce, (uint8_t *)headerX);
|
m_Server.ChaCha20 (buf + 16, 16, i2p::context.GetSSU2IntroKey (), nonce, (uint8_t *)headerX);
|
||||||
LogPrint (eLogWarning, "SSU2: Unexpected PeerTest message SourceConnID=", connID, " DestConnID=", headerX[0]);
|
LogPrint (eLogWarning, "SSU2: Unexpected PeerTest message SourceConnID=", connID, " DestConnID=", headerX[0]);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -748,7 +749,7 @@ namespace transport
|
|||||||
payloadSize += 16;
|
payloadSize += 16;
|
||||||
header.ll[0] ^= CreateHeaderMask (m_Address->i, payload + (payloadSize - 24));
|
header.ll[0] ^= CreateHeaderMask (m_Address->i, payload + (payloadSize - 24));
|
||||||
header.ll[1] ^= CreateHeaderMask (m_Address->i, payload + (payloadSize - 12));
|
header.ll[1] ^= CreateHeaderMask (m_Address->i, payload + (payloadSize - 12));
|
||||||
i2p::crypto::ChaCha20 (headerX, 48, m_Address->i, nonce, headerX);
|
m_Server.ChaCha20 (headerX, 48, m_Address->i, nonce, headerX);
|
||||||
m_NoiseState->MixHash (payload, payloadSize); // h = SHA256(h || encrypted payload from Session Request) for SessionCreated
|
m_NoiseState->MixHash (payload, payloadSize); // h = SHA256(h || encrypted payload from Session Request) for SessionCreated
|
||||||
m_SentHandshakePacket->payloadSize = payloadSize;
|
m_SentHandshakePacket->payloadSize = payloadSize;
|
||||||
// send
|
// send
|
||||||
@@ -775,7 +776,7 @@ namespace transport
|
|||||||
}
|
}
|
||||||
const uint8_t nonce[12] = {0};
|
const uint8_t nonce[12] = {0};
|
||||||
uint8_t headerX[48];
|
uint8_t headerX[48];
|
||||||
i2p::crypto::ChaCha20 (buf + 16, 48, i2p::context.GetSSU2IntroKey (), nonce, headerX);
|
m_Server.ChaCha20 (buf + 16, 48, i2p::context.GetSSU2IntroKey (), nonce, headerX);
|
||||||
memcpy (&m_DestConnID, headerX, 8);
|
memcpy (&m_DestConnID, headerX, 8);
|
||||||
uint64_t token;
|
uint64_t token;
|
||||||
memcpy (&token, headerX + 8, 8);
|
memcpy (&token, headerX + 8, 8);
|
||||||
@@ -874,7 +875,7 @@ namespace transport
|
|||||||
m_NoiseState->MixHash (payload, payloadSize); // h = SHA256(h || encrypted Noise payload from Session Created)
|
m_NoiseState->MixHash (payload, payloadSize); // h = SHA256(h || encrypted Noise payload from Session Created)
|
||||||
header.ll[0] ^= CreateHeaderMask (i2p::context.GetSSU2IntroKey (), payload + (payloadSize - 24));
|
header.ll[0] ^= CreateHeaderMask (i2p::context.GetSSU2IntroKey (), payload + (payloadSize - 24));
|
||||||
header.ll[1] ^= CreateHeaderMask (kh2, payload + (payloadSize - 12));
|
header.ll[1] ^= CreateHeaderMask (kh2, payload + (payloadSize - 12));
|
||||||
i2p::crypto::ChaCha20 (headerX, 48, kh2, nonce, headerX);
|
m_Server.ChaCha20 (headerX, 48, kh2, nonce, headerX);
|
||||||
m_State = eSSU2SessionStateSessionCreatedSent;
|
m_State = eSSU2SessionStateSessionCreatedSent;
|
||||||
m_SentHandshakePacket->payloadSize = payloadSize;
|
m_SentHandshakePacket->payloadSize = payloadSize;
|
||||||
// send
|
// send
|
||||||
@@ -902,7 +903,7 @@ namespace transport
|
|||||||
m_HandshakeInterval = i2p::util::GetMillisecondsSinceEpoch () - m_HandshakeInterval;
|
m_HandshakeInterval = i2p::util::GetMillisecondsSinceEpoch () - m_HandshakeInterval;
|
||||||
const uint8_t nonce[12] = {0};
|
const uint8_t nonce[12] = {0};
|
||||||
uint8_t headerX[48];
|
uint8_t headerX[48];
|
||||||
i2p::crypto::ChaCha20 (buf + 16, 48, kh2, nonce, headerX);
|
m_Server.ChaCha20 (buf + 16, 48, kh2, nonce, headerX);
|
||||||
// KDF for SessionCreated
|
// KDF for SessionCreated
|
||||||
m_NoiseState->MixHash ( { {header.buf, 16}, {headerX, 16} } ); // h = SHA256(h || header)
|
m_NoiseState->MixHash ( { {header.buf, 16}, {headerX, 16} } ); // h = SHA256(h || header)
|
||||||
m_NoiseState->MixHash (headerX + 16, 32); // h = SHA256(h || bepk);
|
m_NoiseState->MixHash (headerX + 16, 32); // h = SHA256(h || bepk);
|
||||||
@@ -1178,14 +1179,19 @@ namespace transport
|
|||||||
LogPrint (eLogError, "SSU2: Couldn't update RouterInfo from SessionConfirmed in netdb");
|
LogPrint (eLogError, "SSU2: Couldn't update RouterInfo from SessionConfirmed in netdb");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
std::shared_ptr<i2p::data::RouterProfile> profile; // not null if older
|
|
||||||
|
bool isOlder = false;
|
||||||
if (ri->GetTimestamp () + i2p::data::NETDB_EXPIRATION_TIMEOUT_THRESHOLD*1000LL < ri1->GetTimestamp ())
|
if (ri->GetTimestamp () + i2p::data::NETDB_EXPIRATION_TIMEOUT_THRESHOLD*1000LL < ri1->GetTimestamp ())
|
||||||
{
|
{
|
||||||
// received RouterInfo is older than one in netdb
|
// received RouterInfo is older than one in netdb
|
||||||
profile = i2p::data::GetRouterProfile (ri->GetIdentHash ()); // retrieve profile
|
isOlder = true;
|
||||||
|
if (ri->HasProfile ())
|
||||||
|
{
|
||||||
|
auto profile = i2p::data::GetRouterProfile (ri->GetIdentHash ()); // retrieve profile
|
||||||
if (profile && profile->IsDuplicated ())
|
if (profile && profile->IsDuplicated ())
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
ri = ri1;
|
ri = ri1;
|
||||||
|
|
||||||
m_Address = m_RemoteEndpoint.address ().is_v6 () ? ri->GetSSU2V6Address () : ri->GetSSU2V4Address ();
|
m_Address = m_RemoteEndpoint.address ().is_v6 () ? ri->GetSSU2V6Address () : ri->GetSSU2V4Address ();
|
||||||
@@ -1198,15 +1204,28 @@ namespace transport
|
|||||||
(!m_RemoteEndpoint.address ().is_v6 () ||
|
(!m_RemoteEndpoint.address ().is_v6 () ||
|
||||||
memcmp (m_RemoteEndpoint.address ().to_v6 ().to_bytes ().data (), m_Address->host.to_v6 ().to_bytes ().data (), 8))) // temporary address
|
memcmp (m_RemoteEndpoint.address ().to_v6 ().to_bytes ().data (), m_Address->host.to_v6 ().to_bytes ().data (), 8))) // temporary address
|
||||||
{
|
{
|
||||||
if (profile) // older router?
|
if (isOlder) // older router?
|
||||||
profile->Duplicated (); // mark router as duplicated in profile
|
i2p::data::UpdateRouterProfile (ri->GetIdentHash (),
|
||||||
|
[](std::shared_ptr<i2p::data::RouterProfile> profile)
|
||||||
|
{
|
||||||
|
if (profile) profile->Duplicated (); // mark router as duplicated in profile
|
||||||
|
});
|
||||||
else
|
else
|
||||||
LogPrint (eLogInfo, "SSU2: Host mismatch between published address ", m_Address->host,
|
LogPrint (eLogInfo, "SSU2: Host mismatch between published address ", m_Address->host,
|
||||||
" and actual endpoint ", m_RemoteEndpoint.address (), " from ", i2p::data::GetIdentHashAbbreviation (ri->GetIdentHash ()));
|
" and actual endpoint ", m_RemoteEndpoint.address (), " from ", i2p::data::GetIdentHashAbbreviation (ri->GetIdentHash ()));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (!m_Address->published)
|
if (!m_Address->published)
|
||||||
|
{
|
||||||
|
if (ri->HasProfile ())
|
||||||
ri->GetProfile ()->SetLastEndpoint (m_RemoteEndpoint);
|
ri->GetProfile ()->SetLastEndpoint (m_RemoteEndpoint);
|
||||||
|
else
|
||||||
|
i2p::data::UpdateRouterProfile (ri->GetIdentHash (),
|
||||||
|
[ep = m_RemoteEndpoint](std::shared_ptr<i2p::data::RouterProfile> profile)
|
||||||
|
{
|
||||||
|
if (profile) profile->SetLastEndpoint (ep);
|
||||||
|
});
|
||||||
|
}
|
||||||
SetRemoteIdentity (ri->GetRouterIdentity ());
|
SetRemoteIdentity (ri->GetRouterIdentity ());
|
||||||
AdjustMaxPayloadSize ();
|
AdjustMaxPayloadSize ();
|
||||||
m_Server.AddSessionByRouterHash (shared_from_this ()); // we know remote router now
|
m_Server.AddSessionByRouterHash (shared_from_this ()); // we know remote router now
|
||||||
@@ -1264,7 +1283,7 @@ namespace transport
|
|||||||
header.ll[0] ^= CreateHeaderMask (m_Address->i, payload + (payloadSize - 24));
|
header.ll[0] ^= CreateHeaderMask (m_Address->i, payload + (payloadSize - 24));
|
||||||
header.ll[1] ^= CreateHeaderMask (m_Address->i, payload + (payloadSize - 12));
|
header.ll[1] ^= CreateHeaderMask (m_Address->i, payload + (payloadSize - 12));
|
||||||
memset (nonce, 0, 12);
|
memset (nonce, 0, 12);
|
||||||
i2p::crypto::ChaCha20 (h + 16, 16, m_Address->i, nonce, h + 16);
|
m_Server.ChaCha20 (h + 16, 16, m_Address->i, nonce, h + 16);
|
||||||
// send
|
// send
|
||||||
if (m_Server.AddPendingOutgoingSession (shared_from_this ()))
|
if (m_Server.AddPendingOutgoingSession (shared_from_this ()))
|
||||||
m_Server.Send (header.buf, 16, h + 16, 16, payload, payloadSize, m_RemoteEndpoint);
|
m_Server.Send (header.buf, 16, h + 16, 16, payload, payloadSize, m_RemoteEndpoint);
|
||||||
@@ -1286,7 +1305,7 @@ namespace transport
|
|||||||
uint8_t nonce[12] = {0};
|
uint8_t nonce[12] = {0};
|
||||||
uint8_t h[32];
|
uint8_t h[32];
|
||||||
memcpy (h, header.buf, 16);
|
memcpy (h, header.buf, 16);
|
||||||
i2p::crypto::ChaCha20 (buf + 16, 16, i2p::context.GetSSU2IntroKey (), nonce, h + 16);
|
m_Server.ChaCha20 (buf + 16, 16, i2p::context.GetSSU2IntroKey (), nonce, h + 16);
|
||||||
memcpy (&m_DestConnID, h + 16, 8);
|
memcpy (&m_DestConnID, h + 16, 8);
|
||||||
// decrypt
|
// decrypt
|
||||||
CreateNonce (be32toh (header.h.packetNum), nonce);
|
CreateNonce (be32toh (header.h.packetNum), nonce);
|
||||||
@@ -1338,7 +1357,7 @@ namespace transport
|
|||||||
header.ll[0] ^= CreateHeaderMask (i2p::context.GetSSU2IntroKey (), payload + (payloadSize - 24));
|
header.ll[0] ^= CreateHeaderMask (i2p::context.GetSSU2IntroKey (), payload + (payloadSize - 24));
|
||||||
header.ll[1] ^= CreateHeaderMask (i2p::context.GetSSU2IntroKey (), payload + (payloadSize - 12));
|
header.ll[1] ^= CreateHeaderMask (i2p::context.GetSSU2IntroKey (), payload + (payloadSize - 12));
|
||||||
memset (nonce, 0, 12);
|
memset (nonce, 0, 12);
|
||||||
i2p::crypto::ChaCha20 (h + 16, 16, i2p::context.GetSSU2IntroKey (), nonce, h + 16);
|
m_Server.ChaCha20 (h + 16, 16, i2p::context.GetSSU2IntroKey (), nonce, h + 16);
|
||||||
// send
|
// send
|
||||||
m_Server.Send (header.buf, 16, h + 16, 16, payload, payloadSize, m_RemoteEndpoint);
|
m_Server.Send (header.buf, 16, h + 16, 16, payload, payloadSize, m_RemoteEndpoint);
|
||||||
}
|
}
|
||||||
@@ -1362,7 +1381,7 @@ namespace transport
|
|||||||
}
|
}
|
||||||
uint8_t nonce[12] = {0};
|
uint8_t nonce[12] = {0};
|
||||||
uint64_t headerX[2]; // sourceConnID, token
|
uint64_t headerX[2]; // sourceConnID, token
|
||||||
i2p::crypto::ChaCha20 (buf + 16, 16, m_Address->i, nonce, (uint8_t *)headerX);
|
m_Server.ChaCha20 (buf + 16, 16, m_Address->i, nonce, (uint8_t *)headerX);
|
||||||
uint64_t token = headerX[1];
|
uint64_t token = headerX[1];
|
||||||
if (token)
|
if (token)
|
||||||
m_Server.UpdateOutgoingToken (m_RemoteEndpoint, token, i2p::util::GetSecondsSinceEpoch () + SSU2_TOKEN_EXPIRATION_TIMEOUT);
|
m_Server.UpdateOutgoingToken (m_RemoteEndpoint, token, i2p::util::GetSecondsSinceEpoch () + SSU2_TOKEN_EXPIRATION_TIMEOUT);
|
||||||
@@ -1411,7 +1430,7 @@ namespace transport
|
|||||||
}
|
}
|
||||||
uint8_t nonce[12] = {0};
|
uint8_t nonce[12] = {0};
|
||||||
uint64_t headerX[2]; // sourceConnID, token
|
uint64_t headerX[2]; // sourceConnID, token
|
||||||
i2p::crypto::ChaCha20 (buf + 16, 16, i2p::context.GetSSU2IntroKey (), nonce, (uint8_t *)headerX);
|
m_Server.ChaCha20 (buf + 16, 16, i2p::context.GetSSU2IntroKey (), nonce, (uint8_t *)headerX);
|
||||||
m_DestConnID = headerX[0];
|
m_DestConnID = headerX[0];
|
||||||
// decrypt and handle payload
|
// decrypt and handle payload
|
||||||
uint8_t * payload = buf + 32;
|
uint8_t * payload = buf + 32;
|
||||||
@@ -1946,6 +1965,7 @@ namespace transport
|
|||||||
void SSU2Session::HandleRelayRequest (const uint8_t * buf, size_t len)
|
void SSU2Session::HandleRelayRequest (const uint8_t * buf, size_t len)
|
||||||
{
|
{
|
||||||
// we are Bob
|
// we are Bob
|
||||||
|
if (len < 9) return;
|
||||||
auto mts = i2p::util::GetMillisecondsSinceEpoch ();
|
auto mts = i2p::util::GetMillisecondsSinceEpoch ();
|
||||||
uint32_t nonce = bufbe32toh (buf + 1); // nonce
|
uint32_t nonce = bufbe32toh (buf + 1); // nonce
|
||||||
uint32_t relayTag = bufbe32toh (buf + 5); // relay tag
|
uint32_t relayTag = bufbe32toh (buf + 5); // relay tag
|
||||||
@@ -1994,18 +2014,24 @@ namespace transport
|
|||||||
void SSU2Session::HandleRelayIntro (const uint8_t * buf, size_t len, int attempts)
|
void SSU2Session::HandleRelayIntro (const uint8_t * buf, size_t len, int attempts)
|
||||||
{
|
{
|
||||||
// we are Charlie
|
// we are Charlie
|
||||||
|
if (len < 47) return;
|
||||||
SSU2RelayResponseCode code = eSSU2RelayResponseCodeAccept;
|
SSU2RelayResponseCode code = eSSU2RelayResponseCodeAccept;
|
||||||
boost::asio::ip::udp::endpoint ep;
|
boost::asio::ip::udp::endpoint ep;
|
||||||
std::shared_ptr<const i2p::data::RouterInfo::Address> addr;
|
std::shared_ptr<const i2p::data::RouterInfo::Address> addr;
|
||||||
auto r = i2p::data::netdb.FindRouter (buf + 1); // Alice
|
auto r = i2p::data::netdb.FindRouter (buf + 1); // Alice
|
||||||
if (r)
|
if (r)
|
||||||
{
|
{
|
||||||
SignedData s;
|
SignedData<128> s;
|
||||||
s.Insert ((const uint8_t *)"RelayRequestData", 16); // prologue
|
s.Insert ((const uint8_t *)"RelayRequestData", 16); // prologue
|
||||||
s.Insert (GetRemoteIdentity ()->GetIdentHash (), 32); // bhash
|
s.Insert (GetRemoteIdentity ()->GetIdentHash (), 32); // bhash
|
||||||
s.Insert (i2p::context.GetIdentHash (), 32); // chash
|
s.Insert (i2p::context.GetIdentHash (), 32); // chash
|
||||||
s.Insert (buf + 33, 14); // nonce, relay tag, timestamp, ver, asz
|
s.Insert (buf + 33, 14); // nonce, relay tag, timestamp, ver, asz
|
||||||
uint8_t asz = buf[46];
|
uint8_t asz = buf[46];
|
||||||
|
if (asz + 47 + r->GetIdentity ()->GetSignatureLen () > len)
|
||||||
|
{
|
||||||
|
LogPrint (eLogWarning, "SSU2: Malformed RelayIntro len=", len);
|
||||||
|
return;
|
||||||
|
}
|
||||||
s.Insert (buf + 47, asz); // Alice Port, Alice IP
|
s.Insert (buf + 47, asz); // Alice Port, Alice IP
|
||||||
if (s.Verify (r->GetIdentity (), buf + 47 + asz))
|
if (s.Verify (r->GetIdentity (), buf + 47 + asz))
|
||||||
{
|
{
|
||||||
@@ -2094,6 +2120,7 @@ namespace transport
|
|||||||
|
|
||||||
void SSU2Session::HandleRelayResponse (const uint8_t * buf, size_t len)
|
void SSU2Session::HandleRelayResponse (const uint8_t * buf, size_t len)
|
||||||
{
|
{
|
||||||
|
if (len < 6) return;
|
||||||
uint32_t nonce = bufbe32toh (buf + 2);
|
uint32_t nonce = bufbe32toh (buf + 2);
|
||||||
if (m_State == eSSU2SessionStateIntroduced)
|
if (m_State == eSSU2SessionStateIntroduced)
|
||||||
{
|
{
|
||||||
@@ -2114,7 +2141,9 @@ namespace transport
|
|||||||
auto it = m_RelaySessions.find (nonce);
|
auto it = m_RelaySessions.find (nonce);
|
||||||
if (it != m_RelaySessions.end ())
|
if (it != m_RelaySessions.end ())
|
||||||
{
|
{
|
||||||
if (it->second.first && it->second.first->IsEstablished ())
|
auto relaySession = it->second.first;
|
||||||
|
m_RelaySessions.erase (it);
|
||||||
|
if (relaySession && relaySession->IsEstablished ())
|
||||||
{
|
{
|
||||||
// we are Bob, message from Charlie
|
// we are Bob, message from Charlie
|
||||||
auto packet = m_Server.GetSentPacketsPool ().AcquireShared ();
|
auto packet = m_Server.GetSentPacketsPool ().AcquireShared ();
|
||||||
@@ -2124,12 +2153,12 @@ namespace transport
|
|||||||
memcpy (payload + 3, buf, len); // forward to Alice as is
|
memcpy (payload + 3, buf, len); // forward to Alice as is
|
||||||
packet->payloadSize = len + 3;
|
packet->payloadSize = len + 3;
|
||||||
packet->payloadSize += CreatePaddingBlock (payload + packet->payloadSize, m_MaxPayloadSize - packet->payloadSize);
|
packet->payloadSize += CreatePaddingBlock (payload + packet->payloadSize, m_MaxPayloadSize - packet->payloadSize);
|
||||||
uint32_t packetNum = it->second.first->SendData (packet->payload, packet->payloadSize);
|
uint32_t packetNum = relaySession->SendData (packet->payload, packet->payloadSize);
|
||||||
if (m_RemoteVersion >= SSU2_MIN_RELAY_RESPONSE_RESEND_VERSION)
|
if (m_RemoteVersion >= SSU2_MIN_RELAY_RESPONSE_RESEND_VERSION)
|
||||||
{
|
{
|
||||||
// sometimes Alice doesn't ack this RelayResponse in older versions
|
// sometimes Alice doesn't ack this RelayResponse in older versions
|
||||||
packet->sendTime = i2p::util::GetMillisecondsSinceEpoch ();
|
packet->sendTime = i2p::util::GetMillisecondsSinceEpoch ();
|
||||||
it->second.first->m_SentPackets.emplace (packetNum, packet);
|
relaySession->m_SentPackets.emplace (packetNum, packet);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -2138,25 +2167,31 @@ namespace transport
|
|||||||
if (!buf[1]) // status code accepted?
|
if (!buf[1]) // status code accepted?
|
||||||
{
|
{
|
||||||
// verify signature
|
// verify signature
|
||||||
uint8_t csz = buf[11];
|
uint8_t csz = (len >= 12) ? buf[11] : 0;
|
||||||
SignedData s;
|
if (csz + 12 + relaySession->GetRemoteIdentity ()->GetSignatureLen () > len)
|
||||||
|
{
|
||||||
|
LogPrint (eLogWarning, "SSU2: Malformed RelayResponse len=", len);
|
||||||
|
relaySession->Done ();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
SignedData<128> s;
|
||||||
s.Insert ((const uint8_t *)"RelayAgreementOK", 16); // prologue
|
s.Insert ((const uint8_t *)"RelayAgreementOK", 16); // prologue
|
||||||
s.Insert (GetRemoteIdentity ()->GetIdentHash (), 32); // bhash
|
s.Insert (GetRemoteIdentity ()->GetIdentHash (), 32); // bhash
|
||||||
s.Insert (buf + 2, 10 + csz); // nonce, timestamp, ver, csz and Charlie's endpoint
|
s.Insert (buf + 2, 10 + csz); // nonce, timestamp, ver, csz and Charlie's endpoint
|
||||||
if (s.Verify (it->second.first->GetRemoteIdentity (), buf + 12 + csz))
|
if (s.Verify (relaySession->GetRemoteIdentity (), buf + 12 + csz))
|
||||||
{
|
{
|
||||||
if (it->second.first->m_State == eSSU2SessionStateIntroduced) // HolePunch not received yet
|
if (relaySession->m_State == eSSU2SessionStateIntroduced) // HolePunch not received yet
|
||||||
{
|
{
|
||||||
// update Charlie's endpoint
|
// update Charlie's endpoint
|
||||||
if (ExtractEndpoint (buf + 12, csz, it->second.first->m_RemoteEndpoint))
|
if (ExtractEndpoint (buf + 12, csz, relaySession->m_RemoteEndpoint))
|
||||||
{
|
{
|
||||||
// update token
|
// update token
|
||||||
uint64_t token;
|
uint64_t token;
|
||||||
memcpy (&token, buf + len - 8, 8);
|
memcpy (&token, buf + len - 8, 8);
|
||||||
m_Server.UpdateOutgoingToken (it->second.first->m_RemoteEndpoint,
|
m_Server.UpdateOutgoingToken (relaySession->m_RemoteEndpoint,
|
||||||
token, i2p::util::GetSecondsSinceEpoch () + SSU2_TOKEN_EXPIRATION_TIMEOUT);
|
token, i2p::util::GetSecondsSinceEpoch () + SSU2_TOKEN_EXPIRATION_TIMEOUT);
|
||||||
// connect to Charlie, HolePunch will be ignored
|
// connect to Charlie, HolePunch will be ignored
|
||||||
it->second.first->ConnectAfterIntroduction ();
|
relaySession->ConnectAfterIntroduction ();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
LogPrint (eLogWarning, "SSU2: RelayResponse can't extract endpoint");
|
LogPrint (eLogWarning, "SSU2: RelayResponse can't extract endpoint");
|
||||||
@@ -2165,16 +2200,15 @@ namespace transport
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
LogPrint (eLogWarning, "SSU2: RelayResponse signature verification failed");
|
LogPrint (eLogWarning, "SSU2: RelayResponse signature verification failed");
|
||||||
it->second.first->Done ();
|
relaySession->Done ();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
LogPrint (eLogInfo, "SSU2: RelayResponse status code=", (int)buf[1], " nonce=", bufbe32toh (buf + 2));
|
LogPrint (eLogInfo, "SSU2: RelayResponse status code=", (int)buf[1], " nonce=", bufbe32toh (buf + 2));
|
||||||
it->second.first->Done ();
|
relaySession->Done ();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
m_RelaySessions.erase (it);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
LogPrint (eLogDebug, "SSU2: RelayResponse unknown nonce ", bufbe32toh (buf + 2));
|
LogPrint (eLogDebug, "SSU2: RelayResponse unknown nonce ", bufbe32toh (buf + 2));
|
||||||
@@ -2243,10 +2277,13 @@ namespace transport
|
|||||||
case 2: // Charlie from Bob
|
case 2: // Charlie from Bob
|
||||||
{
|
{
|
||||||
// sign with Charlie's key
|
// sign with Charlie's key
|
||||||
|
if (len < offset + 9) return;
|
||||||
uint8_t asz = buf[offset + 9];
|
uint8_t asz = buf[offset + 9];
|
||||||
std::vector<uint8_t> newSignedData (asz + 10 + i2p::context.GetIdentity ()->GetSignatureLen ());
|
size_t l = asz + 10 + i2p::context.GetIdentity ()->GetSignatureLen ();
|
||||||
|
if (len < offset + l) return;
|
||||||
|
std::vector<uint8_t> newSignedData (l);
|
||||||
memcpy (newSignedData.data (), buf + offset, asz + 10);
|
memcpy (newSignedData.data (), buf + offset, asz + 10);
|
||||||
SignedData s;
|
SignedData<128> s;
|
||||||
s.Insert ((const uint8_t *)"PeerTestValidate", 16); // prologue
|
s.Insert ((const uint8_t *)"PeerTestValidate", 16); // prologue
|
||||||
s.Insert (GetRemoteIdentity ()->GetIdentHash (), 32); // bhash
|
s.Insert (GetRemoteIdentity ()->GetIdentHash (), 32); // bhash
|
||||||
s.Insert (buf + 3, 32); // ahash
|
s.Insert (buf + 3, 32); // ahash
|
||||||
@@ -2354,10 +2391,16 @@ namespace transport
|
|||||||
if (GetRouterStatus () == eRouterStatusUnknown)
|
if (GetRouterStatus () == eRouterStatusUnknown)
|
||||||
SetTestingState (true);
|
SetTestingState (true);
|
||||||
auto r = i2p::data::netdb.FindRouter (buf + 3); // find Charlie
|
auto r = i2p::data::netdb.FindRouter (buf + 3); // find Charlie
|
||||||
if (r)
|
if (r && len >= offset + 9)
|
||||||
{
|
{
|
||||||
uint8_t asz = buf[offset + 9];
|
uint8_t asz = buf[offset + 9];
|
||||||
SignedData s;
|
if (len < offset + asz + 10 + r->GetIdentity ()->GetSignatureLen ())
|
||||||
|
{
|
||||||
|
LogPrint (eLogWarning, "Malformed PeerTest 4 len=", len);
|
||||||
|
session->Done ();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
SignedData<128> s;
|
||||||
s.Insert ((const uint8_t *)"PeerTestValidate", 16); // prologue
|
s.Insert ((const uint8_t *)"PeerTestValidate", 16); // prologue
|
||||||
s.Insert (GetRemoteIdentity ()->GetIdentHash (), 32); // bhash
|
s.Insert (GetRemoteIdentity ()->GetIdentHash (), 32); // bhash
|
||||||
s.Insert (i2p::context.GetIdentity ()->GetIdentHash (), 32); // ahash
|
s.Insert (i2p::context.GetIdentity ()->GetIdentHash (), 32); // ahash
|
||||||
@@ -2743,7 +2786,7 @@ namespace transport
|
|||||||
size_t SSU2Session::CreatePaddingBlock (uint8_t * buf, size_t len, size_t minSize)
|
size_t SSU2Session::CreatePaddingBlock (uint8_t * buf, size_t len, size_t minSize)
|
||||||
{
|
{
|
||||||
if (len < 3 || len < minSize) return 0;
|
if (len < 3 || len < minSize) return 0;
|
||||||
size_t paddingSize = m_Server.GetRng ()() & 0x0F; // 0 - 15
|
size_t paddingSize = m_Server.GetRng ()() & 0x1F; // 0 - 31
|
||||||
if (paddingSize + 3 > len) paddingSize = len - 3;
|
if (paddingSize + 3 > len) paddingSize = len - 3;
|
||||||
else if (paddingSize + 3 < minSize) paddingSize = minSize - 3;
|
else if (paddingSize + 3 < minSize) paddingSize = minSize - 3;
|
||||||
buf[0] = eSSU2BlkPadding;
|
buf[0] = eSSU2BlkPadding;
|
||||||
@@ -2845,7 +2888,7 @@ namespace transport
|
|||||||
LogPrint (eLogError, "SSU2: Buffer for RelayResponse signature is too small ", len);
|
LogPrint (eLogError, "SSU2: Buffer for RelayResponse signature is too small ", len);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
SignedData s;
|
SignedData<128> s;
|
||||||
s.Insert ((const uint8_t *)"RelayAgreementOK", 16); // prologue
|
s.Insert ((const uint8_t *)"RelayAgreementOK", 16); // prologue
|
||||||
if (code == eSSU2RelayResponseCodeAccept || code >= 64) // Charlie
|
if (code == eSSU2RelayResponseCodeAccept || code >= 64) // Charlie
|
||||||
s.Insert (GetRemoteIdentity ()->GetIdentHash (), 32); // bhash
|
s.Insert (GetRemoteIdentity ()->GetIdentHash (), 32); // bhash
|
||||||
@@ -2907,7 +2950,7 @@ namespace transport
|
|||||||
size_t asz = CreateEndpoint (signedData + 10, 86, boost::asio::ip::udp::endpoint (localAddress->host, localAddress->port));
|
size_t asz = CreateEndpoint (signedData + 10, 86, boost::asio::ip::udp::endpoint (localAddress->host, localAddress->port));
|
||||||
signedData[9] = asz;
|
signedData[9] = asz;
|
||||||
// signature
|
// signature
|
||||||
SignedData s;
|
SignedData<128> s;
|
||||||
s.Insert ((const uint8_t *)"PeerTestValidate", 16); // prologue
|
s.Insert ((const uint8_t *)"PeerTestValidate", 16); // prologue
|
||||||
s.Insert (GetRemoteIdentity ()->GetIdentHash (), 32); // bhash
|
s.Insert (GetRemoteIdentity ()->GetIdentHash (), 32); // bhash
|
||||||
s.Insert (signedData, 10 + asz); // ver, nonce, ts, asz, Alice's endpoint
|
s.Insert (signedData, 10 + asz); // ver, nonce, ts, asz, Alice's endpoint
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2013-2023, The PurpleI2P Project
|
* Copyright (c) 2013-2025, The PurpleI2P Project
|
||||||
*
|
*
|
||||||
* This file is part of Purple i2pd project and licensed under BSD3
|
* This file is part of Purple i2pd project and licensed under BSD3
|
||||||
*
|
*
|
||||||
@@ -149,5 +149,56 @@ namespace crypto
|
|||||||
LogPrint (eLogError, "EdDSA signing key is not set");
|
LogPrint (eLogError, "EdDSA signing key is not set");
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if (OPENSSL_VERSION_NUMBER >= 0x030000000)
|
||||||
|
static const OSSL_PARAM EDDSA25519phParams[] =
|
||||||
|
{
|
||||||
|
OSSL_PARAM_utf8_string ("instance", (char *)"Ed25519ph", 9),
|
||||||
|
OSSL_PARAM_END
|
||||||
|
};
|
||||||
|
|
||||||
|
bool EDDSA25519phVerifier::Verify (const uint8_t * buf, size_t len, const uint8_t * signature) const
|
||||||
|
{
|
||||||
|
auto pkey = GetPkey ();
|
||||||
|
if (pkey)
|
||||||
|
{
|
||||||
|
uint8_t digest[64];
|
||||||
|
SHA512 (buf, len, digest);
|
||||||
|
EVP_MD_CTX * ctx = EVP_MD_CTX_create ();
|
||||||
|
EVP_DigestVerifyInit_ex (ctx, NULL, NULL, NULL, NULL, pkey, EDDSA25519phParams);
|
||||||
|
auto ret = EVP_DigestVerify (ctx, signature, 64, digest, 64);
|
||||||
|
EVP_MD_CTX_destroy (ctx);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
LogPrint (eLogError, "EdDSA verification key is not set");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
EDDSA25519phSigner::EDDSA25519phSigner (const uint8_t * signingPrivateKey):
|
||||||
|
EDDSA25519Signer (signingPrivateKey)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void EDDSA25519phSigner::Sign (const uint8_t * buf, int len, uint8_t * signature) const
|
||||||
|
{
|
||||||
|
auto pkey = GetPkey ();
|
||||||
|
if (pkey)
|
||||||
|
{
|
||||||
|
uint8_t digest[64];
|
||||||
|
SHA512 (buf, len, digest);
|
||||||
|
EVP_MD_CTX * ctx = EVP_MD_CTX_create ();
|
||||||
|
size_t l = 64;
|
||||||
|
uint8_t sig[64];
|
||||||
|
EVP_DigestSignInit_ex (ctx, NULL, NULL, NULL, NULL, pkey, EDDSA25519phParams);
|
||||||
|
if (!EVP_DigestSign (ctx, sig, &l, digest, 64))
|
||||||
|
LogPrint (eLogError, "EdDSA signing failed");
|
||||||
|
memcpy (signature, sig, 64);
|
||||||
|
EVP_MD_CTX_destroy (ctx);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
LogPrint (eLogError, "EdDSA signing key is not set");
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2013-2023, The PurpleI2P Project
|
* Copyright (c) 2013-2025, The PurpleI2P Project
|
||||||
*
|
*
|
||||||
* This file is part of Purple i2pd project and licensed under BSD3
|
* This file is part of Purple i2pd project and licensed under BSD3
|
||||||
*
|
*
|
||||||
@@ -304,13 +304,27 @@ namespace crypto
|
|||||||
private:
|
private:
|
||||||
|
|
||||||
#if OPENSSL_EDDSA
|
#if OPENSSL_EDDSA
|
||||||
|
|
||||||
EVP_PKEY * m_Pkey;
|
EVP_PKEY * m_Pkey;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
|
EVP_PKEY * GetPkey () const { return m_Pkey; };
|
||||||
#else
|
#else
|
||||||
EDDSAPoint m_PublicKey;
|
EDDSAPoint m_PublicKey;
|
||||||
uint8_t m_PublicKeyEncoded[EDDSA25519_PUBLIC_KEY_LENGTH];
|
uint8_t m_PublicKeyEncoded[EDDSA25519_PUBLIC_KEY_LENGTH];
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#if (OPENSSL_VERSION_NUMBER >= 0x030000000) // since 3.0.0
|
||||||
|
class EDDSA25519phVerifier: public EDDSA25519Verifier
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
bool Verify (const uint8_t * buf, size_t len, const uint8_t * signature) const;
|
||||||
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
class EDDSA25519SignerCompat: public Signer
|
class EDDSA25519SignerCompat: public Signer
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -339,6 +353,10 @@ namespace crypto
|
|||||||
|
|
||||||
void Sign (const uint8_t * buf, int len, uint8_t * signature) const;
|
void Sign (const uint8_t * buf, int len, uint8_t * signature) const;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
|
EVP_PKEY * GetPkey () const { return m_Pkey; };
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
EVP_PKEY * m_Pkey;
|
EVP_PKEY * m_Pkey;
|
||||||
@@ -348,6 +366,18 @@ namespace crypto
|
|||||||
|
|
||||||
typedef EDDSA25519SignerCompat EDDSA25519Signer;
|
typedef EDDSA25519SignerCompat EDDSA25519Signer;
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if (OPENSSL_VERSION_NUMBER >= 0x030000000) // since 3.0.0
|
||||||
|
class EDDSA25519phSigner: public EDDSA25519Signer
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
EDDSA25519phSigner (const uint8_t * signingPrivateKey);
|
||||||
|
|
||||||
|
void Sign (const uint8_t * buf, int len, uint8_t * signature) const;
|
||||||
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
inline void CreateEDDSA25519RandomKeys (uint8_t * signingPrivateKey, uint8_t * signingPublicKey)
|
inline void CreateEDDSA25519RandomKeys (uint8_t * signingPrivateKey, uint8_t * signingPublicKey)
|
||||||
|
|||||||
@@ -613,10 +613,8 @@ namespace stream
|
|||||||
if (wasInitial)
|
if (wasInitial)
|
||||||
ScheduleResend ();
|
ScheduleResend ();
|
||||||
}
|
}
|
||||||
if (m_IsClientChoked && ackThrough > m_DropWindowDelaySequenceNumber)
|
if (m_IsClientChoked && ackThrough >= m_DropWindowDelaySequenceNumber)
|
||||||
{
|
|
||||||
m_IsClientChoked = false;
|
m_IsClientChoked = false;
|
||||||
}
|
|
||||||
if (m_IsWinDropped && ackThrough > m_DropWindowDelaySequenceNumber)
|
if (m_IsWinDropped && ackThrough > m_DropWindowDelaySequenceNumber)
|
||||||
{
|
{
|
||||||
m_IsFirstRttSample = true;
|
m_IsFirstRttSample = true;
|
||||||
@@ -1297,7 +1295,7 @@ namespace stream
|
|||||||
m_NumPacketsToSend = 1; m_PacingTimeRem = 0;
|
m_NumPacketsToSend = 1; m_PacingTimeRem = 0;
|
||||||
}
|
}
|
||||||
m_IsSendTime = true;
|
m_IsSendTime = true;
|
||||||
if (m_WindowIncCounter && (m_WindowSize < MAX_WINDOW_SIZE || m_WindowDropTargetSize) && !m_SendBuffer.IsEmpty () && m_PacingTime > m_MinPacingTime)
|
if (m_WindowIncCounter && (m_WindowSize < MAX_WINDOW_SIZE || m_WindowDropTargetSize) && !m_SendBuffer.IsEmpty () && m_PacingTime > m_MinPacingTime && m_RTT <= m_SlowRTT)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < m_NumPacketsToSend; i++)
|
for (int i = 0; i < m_NumPacketsToSend; i++)
|
||||||
{
|
{
|
||||||
@@ -1307,7 +1305,7 @@ namespace stream
|
|||||||
{
|
{
|
||||||
if (m_LastWindowDropSize && (m_LastWindowDropSize >= m_WindowDropTargetSize))
|
if (m_LastWindowDropSize && (m_LastWindowDropSize >= m_WindowDropTargetSize))
|
||||||
m_WindowDropTargetSize += 1 - (1 / ((m_LastWindowDropSize + PREV_SPEED_KEEP_TIME_COEFF) / m_WindowDropTargetSize)); // some magic here
|
m_WindowDropTargetSize += 1 - (1 / ((m_LastWindowDropSize + PREV_SPEED_KEEP_TIME_COEFF) / m_WindowDropTargetSize)); // some magic here
|
||||||
else if (m_LastWindowDropSize && (m_LastWindowDropSize < m_WindowSize))
|
else if (m_LastWindowDropSize && (m_LastWindowDropSize < m_WindowDropTargetSize))
|
||||||
m_WindowDropTargetSize += (m_WindowDropTargetSize - (m_LastWindowDropSize - PREV_SPEED_KEEP_TIME_COEFF)) / m_WindowDropTargetSize; // some magic here
|
m_WindowDropTargetSize += (m_WindowDropTargetSize - (m_LastWindowDropSize - PREV_SPEED_KEEP_TIME_COEFF)) / m_WindowDropTargetSize; // some magic here
|
||||||
else
|
else
|
||||||
m_WindowDropTargetSize += (m_WindowDropTargetSize - (1 - PREV_SPEED_KEEP_TIME_COEFF)) / m_WindowDropTargetSize;
|
m_WindowDropTargetSize += (m_WindowDropTargetSize - (1 - PREV_SPEED_KEEP_TIME_COEFF)) / m_WindowDropTargetSize;
|
||||||
@@ -1646,14 +1644,22 @@ namespace stream
|
|||||||
|
|
||||||
void Stream::ProcessWindowDrop ()
|
void Stream::ProcessWindowDrop ()
|
||||||
{
|
{
|
||||||
if (m_WindowSize > m_LastWindowDropSize)
|
if (m_WindowDropTargetSize)
|
||||||
|
m_WindowDropTargetSize = (m_WindowDropTargetSize / 2) * 0.75; // congestion window size and -25% to drain queue
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (m_WindowSize < m_LastWindowDropSize)
|
||||||
|
{
|
||||||
|
m_LastWindowDropSize = m_WindowSize - (m_LastWindowDropSize - m_WindowSize);
|
||||||
|
if (m_LastWindowDropSize < MIN_WINDOW_SIZE) m_LastWindowDropSize = MIN_WINDOW_SIZE;
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
m_LastWindowDropSize = (m_LastWindowDropSize + m_WindowSize + m_WindowSizeTail) / 2;
|
m_LastWindowDropSize = (m_LastWindowDropSize + m_WindowSize + m_WindowSizeTail) / 2;
|
||||||
if (m_LastWindowDropSize > MAX_WINDOW_SIZE) m_LastWindowDropSize = MAX_WINDOW_SIZE;
|
if (m_LastWindowDropSize > MAX_WINDOW_SIZE) m_LastWindowDropSize = MAX_WINDOW_SIZE;
|
||||||
}
|
}
|
||||||
else
|
m_WindowDropTargetSize = m_LastWindowDropSize * 0.75; // -25% to drain queue
|
||||||
m_LastWindowDropSize = m_WindowSize;
|
}
|
||||||
m_WindowDropTargetSize = m_LastWindowDropSize - (m_LastWindowDropSize / 4); // -25%;
|
|
||||||
if (m_WindowDropTargetSize < MIN_WINDOW_SIZE)
|
if (m_WindowDropTargetSize < MIN_WINDOW_SIZE)
|
||||||
m_WindowDropTargetSize = MIN_WINDOW_SIZE;
|
m_WindowDropTargetSize = MIN_WINDOW_SIZE;
|
||||||
m_WindowIncCounter = 0; // disable window growth
|
m_WindowIncCounter = 0; // disable window growth
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2013-2024, The PurpleI2P Project
|
* Copyright (c) 2013-2025, The PurpleI2P Project
|
||||||
*
|
*
|
||||||
* This file is part of Purple i2pd project and licensed under BSD3
|
* This file is part of Purple i2pd project and licensed under BSD3
|
||||||
*
|
*
|
||||||
@@ -101,13 +101,13 @@ namespace tunnel
|
|||||||
TunnelMessageBlock block;
|
TunnelMessageBlock block;
|
||||||
block.deliveryType = eDeliveryTypeLocal;
|
block.deliveryType = eDeliveryTypeLocal;
|
||||||
block.data = msg;
|
block.data = msg;
|
||||||
std::unique_lock<std::mutex> l(m_SendMutex);
|
std::lock_guard<std::mutex> l(m_SendMutex);
|
||||||
m_Gateway.PutTunnelDataMsg (block);
|
m_Gateway.PutTunnelDataMsg (block);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TransitTunnelGateway::FlushTunnelDataMsgs ()
|
void TransitTunnelGateway::FlushTunnelDataMsgs ()
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> l(m_SendMutex);
|
std::lock_guard<std::mutex> l(m_SendMutex);
|
||||||
m_Gateway.SendBuffer ();
|
m_Gateway.SendBuffer ();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,20 +130,36 @@ namespace tunnel
|
|||||||
EncryptTunnelMsg (tunnelMsg, newMsg);
|
EncryptTunnelMsg (tunnelMsg, newMsg);
|
||||||
|
|
||||||
LogPrint (eLogDebug, "TransitTunnel: handle msg for endpoint ", GetTunnelID ());
|
LogPrint (eLogDebug, "TransitTunnel: handle msg for endpoint ", GetTunnelID ());
|
||||||
m_Endpoint.HandleDecryptedTunnelDataMsg (newMsg);
|
std::lock_guard<std::mutex> l(m_HandleMutex);
|
||||||
|
if (!m_Endpoint) m_Endpoint = std::make_unique<TunnelEndpoint>(false); // transit endpoint is always outbound
|
||||||
|
m_Endpoint->HandleDecryptedTunnelDataMsg (newMsg);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TransitTunnelEndpoint::FlushTunnelDataMsgs ()
|
void TransitTunnelEndpoint::FlushTunnelDataMsgs ()
|
||||||
{
|
{
|
||||||
m_Endpoint.FlushI2NPMsgs ();
|
if (m_Endpoint)
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> l(m_HandleMutex);
|
||||||
|
m_Endpoint->FlushI2NPMsgs ();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void TransitTunnelEndpoint::Cleanup ()
|
||||||
|
{
|
||||||
|
if (m_Endpoint)
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> l(m_HandleMutex);
|
||||||
|
m_Endpoint->Cleanup ();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string TransitTunnelEndpoint::GetNextPeerName () const
|
std::string TransitTunnelEndpoint::GetNextPeerName () const
|
||||||
{
|
{
|
||||||
auto hash = m_Endpoint.GetCurrentHash ();
|
if (!m_Endpoint) return "";
|
||||||
|
auto hash = m_Endpoint->GetCurrentHash ();
|
||||||
if (hash)
|
if (hash)
|
||||||
{
|
{
|
||||||
const auto& sender = m_Endpoint.GetSender ();
|
const auto& sender = m_Endpoint->GetSender ();
|
||||||
if (sender)
|
if (sender)
|
||||||
{
|
{
|
||||||
auto transport = sender->GetCurrentTransport ();
|
auto transport = sender->GetCurrentTransport ();
|
||||||
@@ -180,7 +196,7 @@ namespace tunnel
|
|||||||
}
|
}
|
||||||
|
|
||||||
TransitTunnels::TransitTunnels ():
|
TransitTunnels::TransitTunnels ():
|
||||||
m_IsRunning (false)
|
m_IsRunning (false), m_Rng(i2p::util::GetMonotonicMicroseconds ()%1000000LL)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -320,21 +336,46 @@ namespace tunnel
|
|||||||
// check if we accept this tunnel
|
// check if we accept this tunnel
|
||||||
std::shared_ptr<i2p::tunnel::TransitTunnel> transitTunnel;
|
std::shared_ptr<i2p::tunnel::TransitTunnel> transitTunnel;
|
||||||
uint8_t retCode = 0;
|
uint8_t retCode = 0;
|
||||||
if (!i2p::context.AcceptsTunnels () || i2p::context.GetCongestionLevel (false) >= CONGESTION_LEVEL_FULL)
|
if (i2p::context.AcceptsTunnels ())
|
||||||
|
{
|
||||||
|
auto congestionLevel = i2p::context.GetCongestionLevel (false);
|
||||||
|
if (congestionLevel < CONGESTION_LEVEL_FULL)
|
||||||
|
{
|
||||||
|
if (congestionLevel >= CONGESTION_LEVEL_MEDIUM)
|
||||||
|
{
|
||||||
|
// random reject depending on congestion level
|
||||||
|
int level = m_Rng () % (CONGESTION_LEVEL_FULL - CONGESTION_LEVEL_MEDIUM) + CONGESTION_LEVEL_MEDIUM;
|
||||||
|
if (congestionLevel > level)
|
||||||
retCode = 30;
|
retCode = 30;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
retCode = 30;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
retCode = 30;
|
||||||
|
|
||||||
if (!retCode)
|
if (!retCode)
|
||||||
|
{
|
||||||
|
i2p::data::IdentHash nextIdent(clearText + SHORT_REQUEST_RECORD_NEXT_IDENT_OFFSET);
|
||||||
|
bool isEndpoint = clearText[SHORT_REQUEST_RECORD_FLAG_OFFSET] & TUNNEL_BUILD_RECORD_ENDPOINT_FLAG;
|
||||||
|
if (isEndpoint || !i2p::data::IsRouterDuplicated (nextIdent))
|
||||||
{
|
{
|
||||||
// create new transit tunnel
|
// create new transit tunnel
|
||||||
transitTunnel = i2p::tunnel::CreateTransitTunnel (
|
transitTunnel = CreateTransitTunnel (
|
||||||
bufbe32toh (clearText + SHORT_REQUEST_RECORD_RECEIVE_TUNNEL_OFFSET),
|
bufbe32toh (clearText + SHORT_REQUEST_RECORD_RECEIVE_TUNNEL_OFFSET),
|
||||||
clearText + SHORT_REQUEST_RECORD_NEXT_IDENT_OFFSET,
|
nextIdent,
|
||||||
bufbe32toh (clearText + SHORT_REQUEST_RECORD_NEXT_TUNNEL_OFFSET),
|
bufbe32toh (clearText + SHORT_REQUEST_RECORD_NEXT_TUNNEL_OFFSET),
|
||||||
layerKey, ivKey,
|
layerKey, ivKey,
|
||||||
clearText[SHORT_REQUEST_RECORD_FLAG_OFFSET] & TUNNEL_BUILD_RECORD_GATEWAY_FLAG,
|
clearText[SHORT_REQUEST_RECORD_FLAG_OFFSET] & TUNNEL_BUILD_RECORD_GATEWAY_FLAG,
|
||||||
clearText[SHORT_REQUEST_RECORD_FLAG_OFFSET] & TUNNEL_BUILD_RECORD_ENDPOINT_FLAG);
|
isEndpoint);
|
||||||
if (!AddTransitTunnel (transitTunnel))
|
if (!AddTransitTunnel (transitTunnel))
|
||||||
retCode = 30;
|
retCode = 30;
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
// decline tunnel going to duplicated router
|
||||||
|
retCode = 30;
|
||||||
|
}
|
||||||
|
|
||||||
// encrypt reply
|
// encrypt reply
|
||||||
uint8_t nonce[12];
|
uint8_t nonce[12];
|
||||||
@@ -444,7 +485,7 @@ namespace tunnel
|
|||||||
if (congestionLevel < CONGESTION_LEVEL_FULL)
|
if (congestionLevel < CONGESTION_LEVEL_FULL)
|
||||||
{
|
{
|
||||||
// random reject depending on congestion level
|
// random reject depending on congestion level
|
||||||
int level = i2p::tunnel::tunnels.GetRng ()() % (CONGESTION_LEVEL_FULL - CONGESTION_LEVEL_MEDIUM) + CONGESTION_LEVEL_MEDIUM;
|
int level = m_Rng () % (CONGESTION_LEVEL_FULL - CONGESTION_LEVEL_MEDIUM) + CONGESTION_LEVEL_MEDIUM;
|
||||||
if (congestionLevel > level)
|
if (congestionLevel > level)
|
||||||
accept = false;
|
accept = false;
|
||||||
}
|
}
|
||||||
@@ -452,23 +493,32 @@ namespace tunnel
|
|||||||
accept = false;
|
accept = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// replace record to reply
|
|
||||||
if (accept)
|
if (accept)
|
||||||
{
|
{
|
||||||
auto transitTunnel = i2p::tunnel::CreateTransitTunnel (
|
i2p::data::IdentHash nextIdent(clearText + ECIES_BUILD_REQUEST_RECORD_NEXT_IDENT_OFFSET);
|
||||||
|
bool isEndpoint = clearText[ECIES_BUILD_REQUEST_RECORD_FLAG_OFFSET] & TUNNEL_BUILD_RECORD_ENDPOINT_FLAG;
|
||||||
|
if (isEndpoint || !i2p::data::IsRouterDuplicated (nextIdent))
|
||||||
|
{
|
||||||
|
auto transitTunnel = CreateTransitTunnel (
|
||||||
bufbe32toh (clearText + ECIES_BUILD_REQUEST_RECORD_RECEIVE_TUNNEL_OFFSET),
|
bufbe32toh (clearText + ECIES_BUILD_REQUEST_RECORD_RECEIVE_TUNNEL_OFFSET),
|
||||||
clearText + ECIES_BUILD_REQUEST_RECORD_NEXT_IDENT_OFFSET,
|
nextIdent,
|
||||||
bufbe32toh (clearText + ECIES_BUILD_REQUEST_RECORD_NEXT_TUNNEL_OFFSET),
|
bufbe32toh (clearText + ECIES_BUILD_REQUEST_RECORD_NEXT_TUNNEL_OFFSET),
|
||||||
clearText + ECIES_BUILD_REQUEST_RECORD_LAYER_KEY_OFFSET,
|
clearText + ECIES_BUILD_REQUEST_RECORD_LAYER_KEY_OFFSET,
|
||||||
clearText + ECIES_BUILD_REQUEST_RECORD_IV_KEY_OFFSET,
|
clearText + ECIES_BUILD_REQUEST_RECORD_IV_KEY_OFFSET,
|
||||||
clearText[ECIES_BUILD_REQUEST_RECORD_FLAG_OFFSET] & TUNNEL_BUILD_RECORD_GATEWAY_FLAG,
|
clearText[ECIES_BUILD_REQUEST_RECORD_FLAG_OFFSET] & TUNNEL_BUILD_RECORD_GATEWAY_FLAG,
|
||||||
clearText[ECIES_BUILD_REQUEST_RECORD_FLAG_OFFSET] & TUNNEL_BUILD_RECORD_ENDPOINT_FLAG);
|
isEndpoint);
|
||||||
if (!AddTransitTunnel (transitTunnel))
|
if (!AddTransitTunnel (transitTunnel))
|
||||||
retCode = 30;
|
retCode = 30;
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
// decline tunnel going to duplicated router
|
||||||
|
retCode = 30;
|
||||||
|
}
|
||||||
else
|
else
|
||||||
retCode = 30; // always reject with bandwidth reason (30)
|
retCode = 30; // always reject with bandwidth reason (30)
|
||||||
|
|
||||||
|
// replace record to reply
|
||||||
memset (record + ECIES_BUILD_RESPONSE_RECORD_OPTIONS_OFFSET, 0, 2); // no options
|
memset (record + ECIES_BUILD_RESPONSE_RECORD_OPTIONS_OFFSET, 0, 2); // no options
|
||||||
record[ECIES_BUILD_RESPONSE_RECORD_RET_OFFSET] = retCode;
|
record[ECIES_BUILD_RESPONSE_RECORD_RET_OFFSET] = retCode;
|
||||||
// encrypt reply
|
// encrypt reply
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2013-2024, The PurpleI2P Project
|
* Copyright (c) 2013-2025, The PurpleI2P Project
|
||||||
*
|
*
|
||||||
* This file is part of Purple i2pd project and licensed under BSD3
|
* This file is part of Purple i2pd project and licensed under BSD3
|
||||||
*
|
*
|
||||||
@@ -97,19 +97,19 @@ namespace tunnel
|
|||||||
TransitTunnelEndpoint (uint32_t receiveTunnelID,
|
TransitTunnelEndpoint (uint32_t receiveTunnelID,
|
||||||
const i2p::data::IdentHash& nextIdent, uint32_t nextTunnelID,
|
const i2p::data::IdentHash& nextIdent, uint32_t nextTunnelID,
|
||||||
const i2p::crypto::AESKey& layerKey, const i2p::crypto::AESKey& ivKey):
|
const i2p::crypto::AESKey& layerKey, const i2p::crypto::AESKey& ivKey):
|
||||||
TransitTunnel (receiveTunnelID, nextIdent, nextTunnelID, layerKey, ivKey),
|
TransitTunnel (receiveTunnelID, nextIdent, nextTunnelID, layerKey, ivKey) {};
|
||||||
m_Endpoint (false) {}; // transit endpoint is always outbound
|
|
||||||
|
|
||||||
void Cleanup () override { m_Endpoint.Cleanup (); }
|
void Cleanup () override;
|
||||||
|
|
||||||
void HandleTunnelDataMsg (std::shared_ptr<i2p::I2NPMessage>&& tunnelMsg) override;
|
void HandleTunnelDataMsg (std::shared_ptr<i2p::I2NPMessage>&& tunnelMsg) override;
|
||||||
void FlushTunnelDataMsgs () override;
|
void FlushTunnelDataMsgs () override;
|
||||||
size_t GetNumTransmittedBytes () const override { return m_Endpoint.GetNumReceivedBytes (); }
|
size_t GetNumTransmittedBytes () const override { return m_Endpoint ? m_Endpoint->GetNumReceivedBytes () : 0; }
|
||||||
std::string GetNextPeerName () const override;
|
std::string GetNextPeerName () const override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
TunnelEndpoint m_Endpoint;
|
std::mutex m_HandleMutex;
|
||||||
|
std::unique_ptr<TunnelEndpoint> m_Endpoint;
|
||||||
};
|
};
|
||||||
|
|
||||||
std::shared_ptr<TransitTunnel> CreateTransitTunnel (uint32_t receiveTunnelID,
|
std::shared_ptr<TransitTunnel> CreateTransitTunnel (uint32_t receiveTunnelID,
|
||||||
@@ -151,6 +151,7 @@ namespace tunnel
|
|||||||
std::unique_ptr<std::thread> m_Thread;
|
std::unique_ptr<std::thread> m_Thread;
|
||||||
std::list<std::shared_ptr<TransitTunnel> > m_TransitTunnels;
|
std::list<std::shared_ptr<TransitTunnel> > m_TransitTunnels;
|
||||||
i2p::util::Queue<std::shared_ptr<I2NPMessage> > m_TunnelBuildMsgQueue;
|
i2p::util::Queue<std::shared_ptr<I2NPMessage> > m_TunnelBuildMsgQueue;
|
||||||
|
std::mt19937 m_Rng;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2013-2024, The PurpleI2P Project
|
* Copyright (c) 2013-2025, The PurpleI2P Project
|
||||||
*
|
*
|
||||||
* This file is part of Purple i2pd project and licensed under BSD3
|
* This file is part of Purple i2pd project and licensed under BSD3
|
||||||
*
|
*
|
||||||
@@ -10,7 +10,7 @@
|
|||||||
#define TRANSPORT_SESSION_H__
|
#define TRANSPORT_SESSION_H__
|
||||||
|
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <iostream>
|
#include <string.h>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
@@ -28,45 +28,51 @@ namespace transport
|
|||||||
const size_t IPV6_HEADER_SIZE = 40;
|
const size_t IPV6_HEADER_SIZE = 40;
|
||||||
const size_t UDP_HEADER_SIZE = 8;
|
const size_t UDP_HEADER_SIZE = 8;
|
||||||
|
|
||||||
|
template<size_t sz>
|
||||||
class SignedData
|
class SignedData
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
SignedData () {}
|
SignedData (): m_Size(0) {}
|
||||||
SignedData (const SignedData& other)
|
SignedData (const SignedData& other)
|
||||||
{
|
{
|
||||||
m_Stream << other.m_Stream.rdbuf ();
|
m_Size = other.m_Size;
|
||||||
|
memcpy (m_Buf, other.m_Buf, m_Size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Reset ()
|
void Reset ()
|
||||||
{
|
{
|
||||||
m_Stream.str("");
|
m_Size = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Insert (const uint8_t * buf, size_t len)
|
size_t Insert (const uint8_t * buf, size_t len)
|
||||||
{
|
{
|
||||||
m_Stream.write ((char *)buf, len);
|
if (m_Size + len > sz) len = sz - m_Size;
|
||||||
|
memcpy (m_Buf + m_Size, buf, len);
|
||||||
|
m_Size += len;
|
||||||
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void Insert (T t)
|
void Insert (T t)
|
||||||
{
|
{
|
||||||
m_Stream.write ((char *)&t, sizeof (T));
|
Insert ((const uint8_t *)&t, sizeof (T));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Verify (std::shared_ptr<const i2p::data::IdentityEx> ident, const uint8_t * signature) const
|
bool Verify (std::shared_ptr<const i2p::data::IdentityEx> ident, const uint8_t * signature) const
|
||||||
{
|
{
|
||||||
return ident->Verify ((const uint8_t *)m_Stream.str ().c_str (), m_Stream.str ().size (), signature);
|
return ident->Verify (m_Buf, m_Size, signature);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Sign (const i2p::data::PrivateKeys& keys, uint8_t * signature) const
|
void Sign (const i2p::data::PrivateKeys& keys, uint8_t * signature) const
|
||||||
{
|
{
|
||||||
keys.Sign ((const uint8_t *)m_Stream.str ().c_str (), m_Stream.str ().size (), signature);
|
keys.Sign (m_Buf, m_Size, signature);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
std::stringstream m_Stream;
|
uint8_t m_Buf[sz];
|
||||||
|
size_t m_Size;
|
||||||
};
|
};
|
||||||
|
|
||||||
const int64_t TRANSPORT_SESSION_SLOWNESS_THRESHOLD = 500; // in milliseconds
|
const int64_t TRANSPORT_SESSION_SLOWNESS_THRESHOLD = 500; // in milliseconds
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2013-2024, The PurpleI2P Project
|
* Copyright (c) 2013-2025, The PurpleI2P Project
|
||||||
*
|
*
|
||||||
* This file is part of Purple i2pd project and licensed under BSD3
|
* This file is part of Purple i2pd project and licensed under BSD3
|
||||||
*
|
*
|
||||||
@@ -160,7 +160,8 @@ namespace transport
|
|||||||
m_TotalSentBytes (0), m_TotalReceivedBytes (0), m_TotalTransitTransmittedBytes (0),
|
m_TotalSentBytes (0), m_TotalReceivedBytes (0), m_TotalTransitTransmittedBytes (0),
|
||||||
m_InBandwidth (0), m_OutBandwidth (0), m_TransitBandwidth (0),
|
m_InBandwidth (0), m_OutBandwidth (0), m_TransitBandwidth (0),
|
||||||
m_InBandwidth15s (0), m_OutBandwidth15s (0), m_TransitBandwidth15s (0),
|
m_InBandwidth15s (0), m_OutBandwidth15s (0), m_TransitBandwidth15s (0),
|
||||||
m_InBandwidth5m (0), m_OutBandwidth5m (0), m_TransitBandwidth5m (0)
|
m_InBandwidth5m (0), m_OutBandwidth5m (0), m_TransitBandwidth5m (0),
|
||||||
|
m_Rng(i2p::util::GetMonotonicMicroseconds () % 1000000LL)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -338,7 +339,7 @@ namespace transport
|
|||||||
|
|
||||||
if (m_IsNAT)
|
if (m_IsNAT)
|
||||||
{
|
{
|
||||||
m_PeerTestTimer->expires_from_now (boost::posix_time::minutes(PEER_TEST_INTERVAL));
|
m_PeerTestTimer->expires_from_now (boost::posix_time::seconds(PEER_TEST_INTERVAL + m_Rng() % PEER_TEST_INTERVAL_VARIANCE));
|
||||||
m_PeerTestTimer->async_wait (std::bind (&Transports::HandlePeerTestTimer, this, std::placeholders::_1));
|
m_PeerTestTimer->async_wait (std::bind (&Transports::HandlePeerTestTimer, this, std::placeholders::_1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -560,7 +561,14 @@ namespace transport
|
|||||||
bool Transports::ConnectToPeer (const i2p::data::IdentHash& ident, std::shared_ptr<Peer> peer)
|
bool Transports::ConnectToPeer (const i2p::data::IdentHash& ident, std::shared_ptr<Peer> peer)
|
||||||
{
|
{
|
||||||
if (!peer->router) // reconnect
|
if (!peer->router) // reconnect
|
||||||
peer->SetRouter (netdb.FindRouter (ident)); // try to get new one from netdb
|
{
|
||||||
|
auto r = netdb.FindRouter (ident); // try to get new one from netdb
|
||||||
|
if (r)
|
||||||
|
{
|
||||||
|
peer->SetRouter (r);
|
||||||
|
r->CancelBufferToDelete ();
|
||||||
|
}
|
||||||
|
}
|
||||||
if (peer->router) // we have RI already
|
if (peer->router) // we have RI already
|
||||||
{
|
{
|
||||||
if (peer->priority.empty ())
|
if (peer->priority.empty ())
|
||||||
@@ -647,7 +655,7 @@ namespace transport
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Transports::SetPriority (std::shared_ptr<Peer> peer) const
|
void Transports::SetPriority (std::shared_ptr<Peer> peer)
|
||||||
{
|
{
|
||||||
static const std::vector<i2p::data::RouterInfo::SupportedTransports>
|
static const std::vector<i2p::data::RouterInfo::SupportedTransports>
|
||||||
ntcp2Priority =
|
ntcp2Priority =
|
||||||
@@ -672,8 +680,21 @@ namespace transport
|
|||||||
auto directTransports = compatibleTransports & peer->router->GetPublishedTransports ();
|
auto directTransports = compatibleTransports & peer->router->GetPublishedTransports ();
|
||||||
peer->numAttempts = 0;
|
peer->numAttempts = 0;
|
||||||
peer->priority.clear ();
|
peer->priority.clear ();
|
||||||
bool isReal = peer->router->GetProfile ()->IsReal ();
|
|
||||||
bool ssu2 = isReal ? (rand () & 1) : false; // try NTCP2 if router is not confirmed real
|
std::shared_ptr<RouterProfile> profile;
|
||||||
|
if (peer->router->HasProfile ()) profile = peer->router->GetProfile (); // only if in memory
|
||||||
|
bool ssu2 = false; // NTCP2 by default
|
||||||
|
bool isReal = profile ? profile->IsReal () : true;
|
||||||
|
if (isReal)
|
||||||
|
{
|
||||||
|
ssu2 = m_Rng () & 1; // 1/2
|
||||||
|
if (ssu2 && !profile)
|
||||||
|
{
|
||||||
|
profile = peer->router->GetProfile (); // load profile if necessary
|
||||||
|
isReal = profile->IsReal ();
|
||||||
|
if (!isReal) ssu2 = false; // try NTCP2 if router is not confirmed real
|
||||||
|
}
|
||||||
|
}
|
||||||
const auto& priority = ssu2 ? ssu2Priority : ntcp2Priority;
|
const auto& priority = ssu2 ? ssu2Priority : ntcp2Priority;
|
||||||
if (directTransports)
|
if (directTransports)
|
||||||
{
|
{
|
||||||
@@ -701,7 +722,8 @@ namespace transport
|
|||||||
// try recently connected SSU2 if any
|
// try recently connected SSU2 if any
|
||||||
auto supportedTransports = context.GetRouterInfo ().GetCompatibleTransports (false) &
|
auto supportedTransports = context.GetRouterInfo ().GetCompatibleTransports (false) &
|
||||||
peer->router->GetCompatibleTransports (false);
|
peer->router->GetCompatibleTransports (false);
|
||||||
if (supportedTransports & (i2p::data::RouterInfo::eSSU2V4 | i2p::data::RouterInfo::eSSU2V6))
|
if ((supportedTransports & (i2p::data::RouterInfo::eSSU2V4 | i2p::data::RouterInfo::eSSU2V6)) &&
|
||||||
|
peer->router->HasProfile ())
|
||||||
{
|
{
|
||||||
auto ep = peer->router->GetProfile ()->GetLastEndpoint ();
|
auto ep = peer->router->GetProfile ()->GetLastEndpoint ();
|
||||||
if (!ep.address ().is_unspecified () && ep.port ())
|
if (!ep.address ().is_unspecified () && ep.port ())
|
||||||
@@ -791,7 +813,7 @@ namespace transport
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
testDelay += PEER_TEST_DELAY_INTERVAL + rand() % PEER_TEST_DELAY_INTERVAL_VARIANCE;
|
testDelay += PEER_TEST_DELAY_INTERVAL + m_Rng() % PEER_TEST_DELAY_INTERVAL_VARIANCE;
|
||||||
if (m_Service)
|
if (m_Service)
|
||||||
{
|
{
|
||||||
auto delayTimer = std::make_shared<boost::asio::deadline_timer>(*m_Service);
|
auto delayTimer = std::make_shared<boost::asio::deadline_timer>(*m_Service);
|
||||||
@@ -829,7 +851,7 @@ namespace transport
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
testDelay += PEER_TEST_DELAY_INTERVAL + rand() % PEER_TEST_DELAY_INTERVAL_VARIANCE;
|
testDelay += PEER_TEST_DELAY_INTERVAL + m_Rng() % PEER_TEST_DELAY_INTERVAL_VARIANCE;
|
||||||
if (m_Service)
|
if (m_Service)
|
||||||
{
|
{
|
||||||
auto delayTimer = std::make_shared<boost::asio::deadline_timer>(*m_Service);
|
auto delayTimer = std::make_shared<boost::asio::deadline_timer>(*m_Service);
|
||||||
@@ -886,7 +908,11 @@ namespace transport
|
|||||||
auto transport = peer->priority[peer->numAttempts-1];
|
auto transport = peer->priority[peer->numAttempts-1];
|
||||||
if (transport == i2p::data::RouterInfo::eNTCP2V4 ||
|
if (transport == i2p::data::RouterInfo::eNTCP2V4 ||
|
||||||
transport == i2p::data::RouterInfo::eNTCP2V6 || transport == i2p::data::RouterInfo::eNTCP2V6Mesh)
|
transport == i2p::data::RouterInfo::eNTCP2V6 || transport == i2p::data::RouterInfo::eNTCP2V6Mesh)
|
||||||
peer->router->GetProfile ()->Connected (); // outgoing NTCP2 connection if always real
|
i2p::data::UpdateRouterProfile (ident,
|
||||||
|
[](std::shared_ptr<i2p::data::RouterProfile> profile)
|
||||||
|
{
|
||||||
|
if (profile) profile->Connected (); // outgoing NTCP2 connection if always real
|
||||||
|
});
|
||||||
i2p::data::netdb.SetUnreachable (ident, false); // clear unreachable
|
i2p::data::netdb.SetUnreachable (ident, false); // clear unreachable
|
||||||
}
|
}
|
||||||
peer->numAttempts = 0;
|
peer->numAttempts = 0;
|
||||||
@@ -921,7 +947,11 @@ namespace transport
|
|||||||
session->SendI2NPMessages (msgs); // send DatabaseStore
|
session->SendI2NPMessages (msgs); // send DatabaseStore
|
||||||
}
|
}
|
||||||
auto r = i2p::data::netdb.FindRouter (ident); // router should be in netdb after SessionConfirmed
|
auto r = i2p::data::netdb.FindRouter (ident); // router should be in netdb after SessionConfirmed
|
||||||
if (r) r->GetProfile ()->Connected ();
|
i2p::data::UpdateRouterProfile (ident,
|
||||||
|
[](std::shared_ptr<i2p::data::RouterProfile> profile)
|
||||||
|
{
|
||||||
|
if (profile) profile->Connected ();
|
||||||
|
});
|
||||||
auto ts = i2p::util::GetSecondsSinceEpoch ();
|
auto ts = i2p::util::GetSecondsSinceEpoch ();
|
||||||
auto peer = std::make_shared<Peer>(r, ts);
|
auto peer = std::make_shared<Peer>(r, ts);
|
||||||
peer->sessions.push_back (session);
|
peer->sessions.push_back (session);
|
||||||
@@ -954,10 +984,15 @@ namespace transport
|
|||||||
ConnectToPeer (ident, peer);
|
ConnectToPeer (ident, peer);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> l(m_PeersMutex);
|
std::lock_guard<std::mutex> l(m_PeersMutex);
|
||||||
m_Peers.erase (it);
|
m_Peers.erase (it);
|
||||||
}
|
}
|
||||||
|
// delete buffer of just disconnected router
|
||||||
|
auto r = i2p::data::netdb.FindRouter (ident);
|
||||||
|
if (r && !r->IsUpdated ()) r->ScheduleBufferToDelete ();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -1006,7 +1041,7 @@ namespace transport
|
|||||||
if (session)
|
if (session)
|
||||||
session->SendLocalRouterInfo (true);
|
session->SendLocalRouterInfo (true);
|
||||||
it->second->nextRouterInfoUpdateTime = ts + PEER_ROUTER_INFO_UPDATE_INTERVAL +
|
it->second->nextRouterInfoUpdateTime = ts + PEER_ROUTER_INFO_UPDATE_INTERVAL +
|
||||||
rand () % PEER_ROUTER_INFO_UPDATE_INTERVAL_VARIANCE;
|
m_Rng() % PEER_ROUTER_INFO_UPDATE_INTERVAL_VARIANCE;
|
||||||
}
|
}
|
||||||
++it;
|
++it;
|
||||||
}
|
}
|
||||||
@@ -1020,7 +1055,7 @@ namespace transport
|
|||||||
// if still testing or unknown, repeat peer test
|
// if still testing or unknown, repeat peer test
|
||||||
if (ipv4Testing || ipv6Testing)
|
if (ipv4Testing || ipv6Testing)
|
||||||
PeerTest (ipv4Testing, ipv6Testing);
|
PeerTest (ipv4Testing, ipv6Testing);
|
||||||
m_PeerCleanupTimer->expires_from_now (boost::posix_time::seconds(3 * SESSION_CREATION_TIMEOUT));
|
m_PeerCleanupTimer->expires_from_now (boost::posix_time::seconds(2 * SESSION_CREATION_TIMEOUT + m_Rng() % SESSION_CREATION_TIMEOUT));
|
||||||
m_PeerCleanupTimer->async_wait (std::bind (&Transports::HandlePeerCleanupTimer, this, std::placeholders::_1));
|
m_PeerCleanupTimer->async_wait (std::bind (&Transports::HandlePeerCleanupTimer, this, std::placeholders::_1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1030,7 +1065,7 @@ namespace transport
|
|||||||
if (ecode != boost::asio::error::operation_aborted)
|
if (ecode != boost::asio::error::operation_aborted)
|
||||||
{
|
{
|
||||||
PeerTest ();
|
PeerTest ();
|
||||||
m_PeerTestTimer->expires_from_now (boost::posix_time::minutes(PEER_TEST_INTERVAL));
|
m_PeerTestTimer->expires_from_now (boost::posix_time::seconds(PEER_TEST_INTERVAL + m_Rng() % PEER_TEST_INTERVAL_VARIANCE));
|
||||||
m_PeerTestTimer->async_wait (std::bind (&Transports::HandlePeerTestTimer, this, std::placeholders::_1));
|
m_PeerTestTimer->async_wait (std::bind (&Transports::HandlePeerTestTimer, this, std::placeholders::_1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1177,7 +1212,7 @@ namespace transport
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** XXX: if routes are not restricted this dies */
|
/** XXX: if routes are not restricted this dies */
|
||||||
std::shared_ptr<const i2p::data::RouterInfo> Transports::GetRestrictedPeer() const
|
std::shared_ptr<const i2p::data::RouterInfo> Transports::GetRestrictedPeer()
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> l(m_FamilyMutex);
|
std::lock_guard<std::mutex> l(m_FamilyMutex);
|
||||||
@@ -1186,7 +1221,7 @@ namespace transport
|
|||||||
if(sz > 1)
|
if(sz > 1)
|
||||||
{
|
{
|
||||||
auto it = m_TrustedFamilies.begin ();
|
auto it = m_TrustedFamilies.begin ();
|
||||||
std::advance(it, rand() % sz);
|
std::advance(it, m_Rng() % sz);
|
||||||
fam = *it;
|
fam = *it;
|
||||||
}
|
}
|
||||||
else if (sz == 1)
|
else if (sz == 1)
|
||||||
@@ -1204,7 +1239,7 @@ namespace transport
|
|||||||
if(sz == 1)
|
if(sz == 1)
|
||||||
return i2p::data::netdb.FindRouter(m_TrustedRouters[0]);
|
return i2p::data::netdb.FindRouter(m_TrustedRouters[0]);
|
||||||
auto it = m_TrustedRouters.begin();
|
auto it = m_TrustedRouters.begin();
|
||||||
std::advance(it, rand() % sz);
|
std::advance(it, m_Rng() % sz);
|
||||||
return i2p::data::netdb.FindRouter(*it);
|
return i2p::data::netdb.FindRouter(*it);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2013-2024, The PurpleI2P Project
|
* Copyright (c) 2013-2025, The PurpleI2P Project
|
||||||
*
|
*
|
||||||
* This file is part of Purple i2pd project and licensed under BSD3
|
* This file is part of Purple i2pd project and licensed under BSD3
|
||||||
*
|
*
|
||||||
@@ -20,6 +20,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
|
#include <random>
|
||||||
#include <boost/asio.hpp>
|
#include <boost/asio.hpp>
|
||||||
#include "TransportSession.h"
|
#include "TransportSession.h"
|
||||||
#include "SSU2.h"
|
#include "SSU2.h"
|
||||||
@@ -106,7 +107,8 @@ namespace transport
|
|||||||
};
|
};
|
||||||
|
|
||||||
const uint64_t SESSION_CREATION_TIMEOUT = 15; // in seconds
|
const uint64_t SESSION_CREATION_TIMEOUT = 15; // in seconds
|
||||||
const int PEER_TEST_INTERVAL = 71; // in minutes
|
const int PEER_TEST_INTERVAL = 68*60; // in seconds
|
||||||
|
const int PEER_TEST_INTERVAL_VARIANCE = 3*60; // in seconds
|
||||||
const int PEER_TEST_DELAY_INTERVAL = 20; // in milliseconds
|
const int PEER_TEST_DELAY_INTERVAL = 20; // in milliseconds
|
||||||
const int PEER_TEST_DELAY_INTERVAL_VARIANCE = 30; // in milliseconds
|
const int PEER_TEST_DELAY_INTERVAL_VARIANCE = 30; // in milliseconds
|
||||||
const int MAX_NUM_DELAYED_MESSAGES = 150;
|
const int MAX_NUM_DELAYED_MESSAGES = 150;
|
||||||
@@ -168,7 +170,7 @@ namespace transport
|
|||||||
std::shared_ptr<const i2p::data::RouterInfo> GetRandomPeer (bool isHighBandwidth) const;
|
std::shared_ptr<const i2p::data::RouterInfo> GetRandomPeer (bool isHighBandwidth) const;
|
||||||
|
|
||||||
/** get a trusted first hop for restricted routes */
|
/** get a trusted first hop for restricted routes */
|
||||||
std::shared_ptr<const i2p::data::RouterInfo> GetRestrictedPeer() const;
|
std::shared_ptr<const i2p::data::RouterInfo> GetRestrictedPeer();
|
||||||
/** do we want to use restricted routes? */
|
/** do we want to use restricted routes? */
|
||||||
bool RoutesRestricted() const;
|
bool RoutesRestricted() const;
|
||||||
/** restrict routes to use only these router families for first hops */
|
/** restrict routes to use only these router families for first hops */
|
||||||
@@ -191,7 +193,7 @@ namespace transport
|
|||||||
void HandleRequestComplete (std::shared_ptr<const i2p::data::RouterInfo> r, i2p::data::IdentHash ident);
|
void HandleRequestComplete (std::shared_ptr<const i2p::data::RouterInfo> r, i2p::data::IdentHash ident);
|
||||||
std::shared_ptr<TransportSession> PostMessages (const i2p::data::IdentHash& ident, std::list<std::shared_ptr<i2p::I2NPMessage> >& msgs);
|
std::shared_ptr<TransportSession> PostMessages (const i2p::data::IdentHash& ident, std::list<std::shared_ptr<i2p::I2NPMessage> >& msgs);
|
||||||
bool ConnectToPeer (const i2p::data::IdentHash& ident, std::shared_ptr<Peer> peer);
|
bool ConnectToPeer (const i2p::data::IdentHash& ident, std::shared_ptr<Peer> peer);
|
||||||
void SetPriority (std::shared_ptr<Peer> peer) const;
|
void SetPriority (std::shared_ptr<Peer> peer);
|
||||||
void HandlePeerCleanupTimer (const boost::system::error_code& ecode);
|
void HandlePeerCleanupTimer (const boost::system::error_code& ecode);
|
||||||
void HandlePeerTestTimer (const boost::system::error_code& ecode);
|
void HandlePeerTestTimer (const boost::system::error_code& ecode);
|
||||||
void HandleUpdateBandwidthTimer (const boost::system::error_code& ecode);
|
void HandleUpdateBandwidthTimer (const boost::system::error_code& ecode);
|
||||||
@@ -239,6 +241,7 @@ namespace transport
|
|||||||
mutable std::mutex m_TrustedRoutersMutex;
|
mutable std::mutex m_TrustedRoutersMutex;
|
||||||
|
|
||||||
i2p::I2NPMessagesHandler m_LoopbackHandler;
|
i2p::I2NPMessagesHandler m_LoopbackHandler;
|
||||||
|
std::mt19937 m_Rng;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2013-2024, The PurpleI2P Project
|
* Copyright (c) 2013-2025, The PurpleI2P Project
|
||||||
*
|
*
|
||||||
* This file is part of Purple i2pd project and licensed under BSD3
|
* This file is part of Purple i2pd project and licensed under BSD3
|
||||||
*
|
*
|
||||||
@@ -103,7 +103,7 @@ namespace tunnel
|
|||||||
if (m_Config->IsShort ())
|
if (m_Config->IsShort ())
|
||||||
{
|
{
|
||||||
auto ident = m_Config->GetFirstHop () ? m_Config->GetFirstHop ()->ident : nullptr;
|
auto ident = m_Config->GetFirstHop () ? m_Config->GetFirstHop ()->ident : nullptr;
|
||||||
if (ident && ident->GetIdentHash () != outboundTunnel->GetNextIdentHash ()) // don't encrypt if IBGW = OBEP
|
if (ident && ident->GetIdentHash () != outboundTunnel->GetEndpointIdentHash ()) // don't encrypt if IBGW = OBEP
|
||||||
{
|
{
|
||||||
auto msg1 = i2p::garlic::WrapECIESX25519MessageForRouter (msg, ident->GetEncryptionPublicKey ());
|
auto msg1 = i2p::garlic::WrapECIESX25519MessageForRouter (msg, ident->GetEncryptionPublicKey ());
|
||||||
if (msg1) msg = msg1;
|
if (msg1) msg = msg1;
|
||||||
@@ -179,9 +179,12 @@ namespace tunnel
|
|||||||
{
|
{
|
||||||
uint8_t ret = hop->GetRetCode (msg + 1);
|
uint8_t ret = hop->GetRetCode (msg + 1);
|
||||||
LogPrint (eLogDebug, "Tunnel: Build response ret code=", (int)ret);
|
LogPrint (eLogDebug, "Tunnel: Build response ret code=", (int)ret);
|
||||||
auto profile = i2p::data::netdb.FindRouterProfile (hop->ident->GetIdentHash ());
|
if (hop->ident)
|
||||||
if (profile)
|
i2p::data::UpdateRouterProfile (hop->ident->GetIdentHash (),
|
||||||
profile->TunnelBuildResponse (ret);
|
[ret](std::shared_ptr<i2p::data::RouterProfile> profile)
|
||||||
|
{
|
||||||
|
if (profile) profile->TunnelBuildResponse (ret);
|
||||||
|
});
|
||||||
if (ret)
|
if (ret)
|
||||||
// if any of participants declined the tunnel is not established
|
// if any of participants declined the tunnel is not established
|
||||||
established = false;
|
established = false;
|
||||||
@@ -278,6 +281,21 @@ namespace tunnel
|
|||||||
m_Endpoint.HandleDecryptedTunnelDataMsg (msg);
|
m_Endpoint.HandleDecryptedTunnelDataMsg (msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool InboundTunnel::Recreate ()
|
||||||
|
{
|
||||||
|
if (!IsRecreated ())
|
||||||
|
{
|
||||||
|
auto pool = GetTunnelPool ();
|
||||||
|
if (pool)
|
||||||
|
{
|
||||||
|
SetRecreated (true);
|
||||||
|
pool->RecreateInboundTunnel (std::static_pointer_cast<InboundTunnel>(shared_from_this ()));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
ZeroHopsInboundTunnel::ZeroHopsInboundTunnel ():
|
ZeroHopsInboundTunnel::ZeroHopsInboundTunnel ():
|
||||||
InboundTunnel (std::make_shared<ZeroHopsTunnelConfig> ()),
|
InboundTunnel (std::make_shared<ZeroHopsTunnelConfig> ()),
|
||||||
m_NumReceivedBytes (0)
|
m_NumReceivedBytes (0)
|
||||||
@@ -297,21 +315,27 @@ namespace tunnel
|
|||||||
void OutboundTunnel::SendTunnelDataMsgTo (const uint8_t * gwHash, uint32_t gwTunnel, std::shared_ptr<i2p::I2NPMessage> msg)
|
void OutboundTunnel::SendTunnelDataMsgTo (const uint8_t * gwHash, uint32_t gwTunnel, std::shared_ptr<i2p::I2NPMessage> msg)
|
||||||
{
|
{
|
||||||
TunnelMessageBlock block;
|
TunnelMessageBlock block;
|
||||||
|
block.tunnelID = 0; // Initialize tunnelID to a default value
|
||||||
|
|
||||||
if (gwHash)
|
if (gwHash)
|
||||||
{
|
{
|
||||||
block.hash = gwHash;
|
block.hash = gwHash;
|
||||||
if (gwTunnel)
|
if (gwTunnel)
|
||||||
{
|
{
|
||||||
block.deliveryType = eDeliveryTypeTunnel;
|
block.deliveryType = eDeliveryTypeTunnel;
|
||||||
block.tunnelID = gwTunnel;
|
block.tunnelID = gwTunnel; // Set tunnelID only if gwTunnel is non-zero
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
block.deliveryType = eDeliveryTypeRouter;
|
block.deliveryType = eDeliveryTypeRouter;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
block.deliveryType = eDeliveryTypeLocal;
|
block.deliveryType = eDeliveryTypeLocal;
|
||||||
block.data = msg;
|
}
|
||||||
|
|
||||||
|
block.data = msg;
|
||||||
SendTunnelDataMsgs({block});
|
SendTunnelDataMsgs({block});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -328,6 +352,21 @@ namespace tunnel
|
|||||||
LogPrint (eLogError, "Tunnel: Incoming message for outbound tunnel ", GetTunnelID ());
|
LogPrint (eLogError, "Tunnel: Incoming message for outbound tunnel ", GetTunnelID ());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool OutboundTunnel::Recreate ()
|
||||||
|
{
|
||||||
|
if (!IsRecreated ())
|
||||||
|
{
|
||||||
|
auto pool = GetTunnelPool ();
|
||||||
|
if (pool)
|
||||||
|
{
|
||||||
|
SetRecreated (true);
|
||||||
|
pool->RecreateOutboundTunnel (std::static_pointer_cast<OutboundTunnel>(shared_from_this ()));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
ZeroHopsOutboundTunnel::ZeroHopsOutboundTunnel ():
|
ZeroHopsOutboundTunnel::ZeroHopsOutboundTunnel ():
|
||||||
OutboundTunnel (std::make_shared<ZeroHopsTunnelConfig> ()),
|
OutboundTunnel (std::make_shared<ZeroHopsTunnelConfig> ()),
|
||||||
m_NumSentBytes (0)
|
m_NumSentBytes (0)
|
||||||
@@ -434,7 +473,7 @@ namespace tunnel
|
|||||||
std::shared_ptr<OutboundTunnel> Tunnels::GetNextOutboundTunnel ()
|
std::shared_ptr<OutboundTunnel> Tunnels::GetNextOutboundTunnel ()
|
||||||
{
|
{
|
||||||
if (m_OutboundTunnels.empty ()) return nullptr;
|
if (m_OutboundTunnels.empty ()) return nullptr;
|
||||||
uint32_t ind = rand () % m_OutboundTunnels.size (), i = 0;
|
uint32_t ind = m_Rng () % m_OutboundTunnels.size (), i = 0;
|
||||||
std::shared_ptr<OutboundTunnel> tunnel;
|
std::shared_ptr<OutboundTunnel> tunnel;
|
||||||
for (const auto& it: m_OutboundTunnels)
|
for (const auto& it: m_OutboundTunnels)
|
||||||
{
|
{
|
||||||
@@ -711,8 +750,17 @@ namespace tunnel
|
|||||||
void Tunnels::ManageTunnels (uint64_t ts)
|
void Tunnels::ManageTunnels (uint64_t ts)
|
||||||
{
|
{
|
||||||
ManagePendingTunnels (ts);
|
ManagePendingTunnels (ts);
|
||||||
ManageInboundTunnels (ts);
|
std::vector<std::shared_ptr<Tunnel> > tunnelsToRecreate;
|
||||||
ManageOutboundTunnels (ts);
|
ManageInboundTunnels (ts, tunnelsToRecreate);
|
||||||
|
ManageOutboundTunnels (ts, tunnelsToRecreate);
|
||||||
|
// rec-create in random order
|
||||||
|
if (!tunnelsToRecreate.empty ())
|
||||||
|
{
|
||||||
|
if (tunnelsToRecreate.size () > 1)
|
||||||
|
std::shuffle (tunnelsToRecreate.begin(), tunnelsToRecreate.end(), m_Rng);
|
||||||
|
for (auto& it: tunnelsToRecreate)
|
||||||
|
it->Recreate ();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Tunnels::ManagePendingTunnels (uint64_t ts)
|
void Tunnels::ManagePendingTunnels (uint64_t ts)
|
||||||
@@ -743,11 +791,11 @@ namespace tunnel
|
|||||||
while (hop)
|
while (hop)
|
||||||
{
|
{
|
||||||
if (hop->ident)
|
if (hop->ident)
|
||||||
|
i2p::data::UpdateRouterProfile (hop->ident->GetIdentHash (),
|
||||||
|
[](std::shared_ptr<i2p::data::RouterProfile> profile)
|
||||||
{
|
{
|
||||||
auto profile = i2p::data::netdb.FindRouterProfile (hop->ident->GetIdentHash ());
|
if (profile) profile->TunnelNonReplied ();
|
||||||
if (profile)
|
});
|
||||||
profile->TunnelNonReplied ();
|
|
||||||
}
|
|
||||||
hop = hop->next;
|
hop = hop->next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -775,7 +823,7 @@ namespace tunnel
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Tunnels::ManageOutboundTunnels (uint64_t ts)
|
void Tunnels::ManageOutboundTunnels (uint64_t ts, std::vector<std::shared_ptr<Tunnel> >& toRecreate)
|
||||||
{
|
{
|
||||||
for (auto it = m_OutboundTunnels.begin (); it != m_OutboundTunnels.end ();)
|
for (auto it = m_OutboundTunnels.begin (); it != m_OutboundTunnels.end ();)
|
||||||
{
|
{
|
||||||
@@ -799,10 +847,7 @@ namespace tunnel
|
|||||||
auto pool = tunnel->GetTunnelPool ();
|
auto pool = tunnel->GetTunnelPool ();
|
||||||
// let it die if the tunnel pool has been reconfigured and this is old
|
// let it die if the tunnel pool has been reconfigured and this is old
|
||||||
if (pool && tunnel->GetNumHops() == pool->GetNumOutboundHops())
|
if (pool && tunnel->GetNumHops() == pool->GetNumOutboundHops())
|
||||||
{
|
toRecreate.push_back (tunnel);
|
||||||
tunnel->SetRecreated (true);
|
|
||||||
pool->RecreateOutboundTunnel (tunnel);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (ts + TUNNEL_EXPIRATION_THRESHOLD > tunnel->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT)
|
if (ts + TUNNEL_EXPIRATION_THRESHOLD > tunnel->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT)
|
||||||
tunnel->SetState (eTunnelStateExpiring);
|
tunnel->SetState (eTunnelStateExpiring);
|
||||||
@@ -827,7 +872,7 @@ namespace tunnel
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Tunnels::ManageInboundTunnels (uint64_t ts)
|
void Tunnels::ManageInboundTunnels (uint64_t ts, std::vector<std::shared_ptr<Tunnel> >& toRecreate)
|
||||||
{
|
{
|
||||||
for (auto it = m_InboundTunnels.begin (); it != m_InboundTunnels.end ();)
|
for (auto it = m_InboundTunnels.begin (); it != m_InboundTunnels.end ();)
|
||||||
{
|
{
|
||||||
@@ -851,10 +896,7 @@ namespace tunnel
|
|||||||
auto pool = tunnel->GetTunnelPool ();
|
auto pool = tunnel->GetTunnelPool ();
|
||||||
// let it die if the tunnel pool was reconfigured and has different number of hops
|
// let it die if the tunnel pool was reconfigured and has different number of hops
|
||||||
if (pool && tunnel->GetNumHops() == pool->GetNumInboundHops())
|
if (pool && tunnel->GetNumHops() == pool->GetNumInboundHops())
|
||||||
{
|
toRecreate.push_back (tunnel);
|
||||||
tunnel->SetRecreated (true);
|
|
||||||
pool->RecreateInboundTunnel (tunnel);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ts + TUNNEL_EXPIRATION_THRESHOLD > tunnel->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT)
|
if (ts + TUNNEL_EXPIRATION_THRESHOLD > tunnel->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT)
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2013-2024, The PurpleI2P Project
|
* Copyright (c) 2013-2025, The PurpleI2P Project
|
||||||
*
|
*
|
||||||
* This file is part of Purple i2pd project and licensed under BSD3
|
* This file is part of Purple i2pd project and licensed under BSD3
|
||||||
*
|
*
|
||||||
@@ -99,6 +99,7 @@ namespace tunnel
|
|||||||
void SetRecreated (bool recreated) { m_IsRecreated = recreated; };
|
void SetRecreated (bool recreated) { m_IsRecreated = recreated; };
|
||||||
int GetNumHops () const { return m_Hops.size (); };
|
int GetNumHops () const { return m_Hops.size (); };
|
||||||
virtual bool IsInbound() const = 0;
|
virtual bool IsInbound() const = 0;
|
||||||
|
virtual bool Recreate () = 0;
|
||||||
|
|
||||||
std::shared_ptr<TunnelPool> GetTunnelPool () const { return m_Pool; };
|
std::shared_ptr<TunnelPool> GetTunnelPool () const { return m_Pool; };
|
||||||
void SetTunnelPool (std::shared_ptr<TunnelPool> pool) { m_Pool = pool; };
|
void SetTunnelPool (std::shared_ptr<TunnelPool> pool) { m_Pool = pool; };
|
||||||
@@ -150,6 +151,7 @@ namespace tunnel
|
|||||||
void HandleTunnelDataMsg (std::shared_ptr<i2p::I2NPMessage>&& tunnelMsg) override;
|
void HandleTunnelDataMsg (std::shared_ptr<i2p::I2NPMessage>&& tunnelMsg) override;
|
||||||
|
|
||||||
bool IsInbound() const override { return false; }
|
bool IsInbound() const override { return false; }
|
||||||
|
bool Recreate () override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
@@ -166,6 +168,7 @@ namespace tunnel
|
|||||||
void HandleTunnelDataMsg (std::shared_ptr<I2NPMessage>&& msg) override;
|
void HandleTunnelDataMsg (std::shared_ptr<I2NPMessage>&& msg) override;
|
||||||
virtual size_t GetNumReceivedBytes () const { return m_Endpoint.GetNumReceivedBytes (); };
|
virtual size_t GetNumReceivedBytes () const { return m_Endpoint.GetNumReceivedBytes (); };
|
||||||
bool IsInbound() const override { return true; }
|
bool IsInbound() const override { return true; }
|
||||||
|
bool Recreate () override;
|
||||||
|
|
||||||
// override TunnelBase
|
// override TunnelBase
|
||||||
void Cleanup () override { m_Endpoint.Cleanup (); };
|
void Cleanup () override { m_Endpoint.Cleanup (); };
|
||||||
@@ -246,8 +249,6 @@ namespace tunnel
|
|||||||
uint32_t GetMaxNumTransitTunnels () const { return m_MaxNumTransitTunnels; };
|
uint32_t GetMaxNumTransitTunnels () const { return m_MaxNumTransitTunnels; };
|
||||||
int GetCongestionLevel() const { return m_MaxNumTransitTunnels ? CONGESTION_LEVEL_FULL * m_TransitTunnels.GetNumTransitTunnels () / m_MaxNumTransitTunnels : CONGESTION_LEVEL_FULL; }
|
int GetCongestionLevel() const { return m_MaxNumTransitTunnels ? CONGESTION_LEVEL_FULL * m_TransitTunnels.GetNumTransitTunnels () / m_MaxNumTransitTunnels : CONGESTION_LEVEL_FULL; }
|
||||||
|
|
||||||
std::mt19937& GetRng () { return m_Rng; };
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
template<class TTunnel>
|
template<class TTunnel>
|
||||||
@@ -264,8 +265,8 @@ namespace tunnel
|
|||||||
|
|
||||||
void Run ();
|
void Run ();
|
||||||
void ManageTunnels (uint64_t ts);
|
void ManageTunnels (uint64_t ts);
|
||||||
void ManageOutboundTunnels (uint64_t ts);
|
void ManageOutboundTunnels (uint64_t ts, std::vector<std::shared_ptr<Tunnel> >& toRecreate);
|
||||||
void ManageInboundTunnels (uint64_t ts);
|
void ManageInboundTunnels (uint64_t ts, std::vector<std::shared_ptr<Tunnel> >& toRecreate);
|
||||||
void ManagePendingTunnels (uint64_t ts);
|
void ManagePendingTunnels (uint64_t ts);
|
||||||
template<class PendingTunnels>
|
template<class PendingTunnels>
|
||||||
void ManagePendingTunnels (PendingTunnels& pendingTunnels, uint64_t ts);
|
void ManagePendingTunnels (PendingTunnels& pendingTunnels, uint64_t ts);
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ namespace tunnel
|
|||||||
void HandleDecryptedTunnelDataMsg (std::shared_ptr<I2NPMessage> msg);
|
void HandleDecryptedTunnelDataMsg (std::shared_ptr<I2NPMessage> msg);
|
||||||
void FlushI2NPMsgs ();
|
void FlushI2NPMsgs ();
|
||||||
|
|
||||||
const i2p::data::IdentHash * GetCurrentHash () const; // return null if not avaiable
|
const i2p::data::IdentHash * GetCurrentHash () const; // return null if not available
|
||||||
const std::unique_ptr<TunnelTransportSender>& GetSender () const { return m_Sender; };
|
const std::unique_ptr<TunnelTransportSender>& GetSender () const { return m_Sender; };
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2013-2024, The PurpleI2P Project
|
* Copyright (c) 2013-2025, The PurpleI2P Project
|
||||||
*
|
*
|
||||||
* This file is part of Purple i2pd project and licensed under BSD3
|
* This file is part of Purple i2pd project and licensed under BSD3
|
||||||
*
|
*
|
||||||
@@ -351,10 +351,13 @@ namespace tunnel
|
|||||||
{
|
{
|
||||||
it.second.first->SetState (eTunnelStateFailed);
|
it.second.first->SetState (eTunnelStateFailed);
|
||||||
std::unique_lock<std::mutex> l(m_OutboundTunnelsMutex);
|
std::unique_lock<std::mutex> l(m_OutboundTunnelsMutex);
|
||||||
if (m_OutboundTunnels.size () > 1 || m_NumOutboundTunnels <= 1) // don't fail last tunnel
|
if (m_OutboundTunnels.size () > 1) // don't fail last tunnel
|
||||||
m_OutboundTunnels.erase (it.second.first);
|
m_OutboundTunnels.erase (it.second.first);
|
||||||
else
|
else
|
||||||
|
{
|
||||||
it.second.first->SetState (eTunnelStateTestFailed);
|
it.second.first->SetState (eTunnelStateTestFailed);
|
||||||
|
CreateOutboundTunnel (); // create new tunnel immediately because last one failed
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if (it.second.first->GetState () != eTunnelStateExpiring)
|
else if (it.second.first->GetState () != eTunnelStateExpiring)
|
||||||
it.second.first->SetState (eTunnelStateTestFailed);
|
it.second.first->SetState (eTunnelStateTestFailed);
|
||||||
@@ -368,13 +371,16 @@ namespace tunnel
|
|||||||
bool failed = false;
|
bool failed = false;
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
|
std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
|
||||||
if (m_InboundTunnels.size () > 1 || m_NumInboundTunnels <= 1) // don't fail last tunnel
|
if (m_InboundTunnels.size () > 1) // don't fail last tunnel
|
||||||
{
|
{
|
||||||
m_InboundTunnels.erase (it.second.second);
|
m_InboundTunnels.erase (it.second.second);
|
||||||
failed = true;
|
failed = true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
it.second.second->SetState (eTunnelStateTestFailed);
|
it.second.second->SetState (eTunnelStateTestFailed);
|
||||||
|
CreateInboundTunnel (); // create new tunnel immediately because last one failed
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (failed && m_LocalDestination)
|
if (failed && m_LocalDestination)
|
||||||
m_LocalDestination->SetLeaseSetUpdated (true);
|
m_LocalDestination->SetLeaseSetUpdated (true);
|
||||||
@@ -560,7 +566,7 @@ namespace tunnel
|
|||||||
i2p::data::netdb.GetRandomRouter (prevHop, reverse, endpoint, false);
|
i2p::data::netdb.GetRandomRouter (prevHop, reverse, endpoint, false);
|
||||||
if (hop)
|
if (hop)
|
||||||
{
|
{
|
||||||
if (!hop->GetProfile ()->IsBad ())
|
if (!hop->HasProfile () || !hop->GetProfile ()->IsBad ())
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else if (tryClient)
|
else if (tryClient)
|
||||||
@@ -588,7 +594,7 @@ namespace tunnel
|
|||||||
(inbound && i2p::transport::transports.GetNumPeers () > 25))
|
(inbound && i2p::transport::transports.GetNumPeers () > 25))
|
||||||
{
|
{
|
||||||
auto r = i2p::transport::transports.GetRandomPeer (m_IsHighBandwidth && !i2p::context.IsLimitedConnectivity ());
|
auto r = i2p::transport::transports.GetRandomPeer (m_IsHighBandwidth && !i2p::context.IsLimitedConnectivity ());
|
||||||
if (r && r->IsECIES () && !r->GetProfile ()->IsBad () &&
|
if (r && r->IsECIES () && (!r->HasProfile () || !r->GetProfile ()->IsBad ()) &&
|
||||||
(numHops > 1 || (r->IsV4 () && (!inbound || r->IsPublished (true))))) // first inbound must be published ipv4
|
(numHops > 1 || (r->IsV4 () && (!inbound || r->IsPublished (true))))) // first inbound must be published ipv4
|
||||||
{
|
{
|
||||||
prevHop = r;
|
prevHop = r;
|
||||||
|
|||||||
@@ -18,7 +18,7 @@
|
|||||||
#define MAKE_VERSION_NUMBER(a,b,c) ((a*100+b)*100+c)
|
#define MAKE_VERSION_NUMBER(a,b,c) ((a*100+b)*100+c)
|
||||||
|
|
||||||
#define I2PD_VERSION_MAJOR 2
|
#define I2PD_VERSION_MAJOR 2
|
||||||
#define I2PD_VERSION_MINOR 55
|
#define I2PD_VERSION_MINOR 56
|
||||||
#define I2PD_VERSION_MICRO 0
|
#define I2PD_VERSION_MICRO 0
|
||||||
#define I2PD_VERSION_PATCH 0
|
#define I2PD_VERSION_PATCH 0
|
||||||
#ifdef GITVER
|
#ifdef GITVER
|
||||||
@@ -33,7 +33,7 @@
|
|||||||
|
|
||||||
#define I2P_VERSION_MAJOR 0
|
#define I2P_VERSION_MAJOR 0
|
||||||
#define I2P_VERSION_MINOR 9
|
#define I2P_VERSION_MINOR 9
|
||||||
#define I2P_VERSION_MICRO 64
|
#define I2P_VERSION_MICRO 65
|
||||||
#define I2P_VERSION_PATCH 0
|
#define I2P_VERSION_PATCH 0
|
||||||
#define I2P_VERSION MAKE_VERSION(I2P_VERSION_MAJOR, I2P_VERSION_MINOR, I2P_VERSION_MICRO)
|
#define I2P_VERSION MAKE_VERSION(I2P_VERSION_MAJOR, I2P_VERSION_MINOR, I2P_VERSION_MICRO)
|
||||||
#define I2P_VERSION_NUMBER MAKE_VERSION_NUMBER(I2P_VERSION_MAJOR, I2P_VERSION_MINOR, I2P_VERSION_MICRO)
|
#define I2P_VERSION_NUMBER MAKE_VERSION_NUMBER(I2P_VERSION_MAJOR, I2P_VERSION_MINOR, I2P_VERSION_MICRO)
|
||||||
|
|||||||
@@ -9,7 +9,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <map>
|
#include <unordered_map>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <condition_variable>
|
#include <condition_variable>
|
||||||
@@ -17,6 +17,7 @@
|
|||||||
#include <boost/algorithm/string.hpp>
|
#include <boost/algorithm/string.hpp>
|
||||||
#include "Base.h"
|
#include "Base.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
#include "Timestamp.h"
|
||||||
#include "Identity.h"
|
#include "Identity.h"
|
||||||
#include "FS.h"
|
#include "FS.h"
|
||||||
#include "Log.h"
|
#include "Log.h"
|
||||||
@@ -49,9 +50,10 @@ namespace client
|
|||||||
if (m_IsPersist)
|
if (m_IsPersist)
|
||||||
i2p::config::GetOption("addressbook.hostsfile", m_HostsFile);
|
i2p::config::GetOption("addressbook.hostsfile", m_HostsFile);
|
||||||
}
|
}
|
||||||
std::shared_ptr<const i2p::data::IdentityEx> GetAddress (const i2p::data::IdentHash& ident) const override;
|
std::shared_ptr<const i2p::data::IdentityEx> GetAddress (const i2p::data::IdentHash& ident) override;
|
||||||
void AddAddress (std::shared_ptr<const i2p::data::IdentityEx> address) override;
|
void AddAddress (std::shared_ptr<const i2p::data::IdentityEx> address) override;
|
||||||
void RemoveAddress (const i2p::data::IdentHash& ident) override;
|
void RemoveAddress (const i2p::data::IdentHash& ident) override;
|
||||||
|
void CleanUpCache () override;
|
||||||
|
|
||||||
bool Init () override;
|
bool Init () override;
|
||||||
int Load (Addresses& addresses) override;
|
int Load (Addresses& addresses) override;
|
||||||
@@ -72,6 +74,8 @@ namespace client
|
|||||||
std::string etagsPath, indexPath, localPath;
|
std::string etagsPath, indexPath, localPath;
|
||||||
bool m_IsPersist;
|
bool m_IsPersist;
|
||||||
std::string m_HostsFile; // file to dump hosts.txt, empty if not used
|
std::string m_HostsFile; // file to dump hosts.txt, empty if not used
|
||||||
|
std::unordered_map<i2p::data::IdentHash, std::pair<std::vector<uint8_t>, uint64_t> > m_FullAddressCache; // ident hash -> (full ident buffer, last access timestamp)
|
||||||
|
std::mutex m_FullAddressCacheMutex;
|
||||||
};
|
};
|
||||||
|
|
||||||
bool AddressBookFilesystemStorage::Init()
|
bool AddressBookFilesystemStorage::Init()
|
||||||
@@ -92,8 +96,19 @@ namespace client
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<const i2p::data::IdentityEx> AddressBookFilesystemStorage::GetAddress (const i2p::data::IdentHash& ident) const
|
std::shared_ptr<const i2p::data::IdentityEx> AddressBookFilesystemStorage::GetAddress (const i2p::data::IdentHash& ident)
|
||||||
{
|
{
|
||||||
|
auto ts = i2p::util::GetMonotonicSeconds ();
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> l(m_FullAddressCacheMutex);
|
||||||
|
auto it = m_FullAddressCache.find (ident);
|
||||||
|
if (it != m_FullAddressCache.end ())
|
||||||
|
{
|
||||||
|
it->second.second = ts;
|
||||||
|
return std::make_shared<i2p::data::IdentityEx>(it->second.first.data (), it->second.first.size ());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!m_IsPersist)
|
if (!m_IsPersist)
|
||||||
{
|
{
|
||||||
LogPrint(eLogDebug, "Addressbook: Persistence is disabled");
|
LogPrint(eLogDebug, "Addressbook: Persistence is disabled");
|
||||||
@@ -101,43 +116,67 @@ namespace client
|
|||||||
}
|
}
|
||||||
std::string filename = storage.Path(ident.ToBase32());
|
std::string filename = storage.Path(ident.ToBase32());
|
||||||
std::ifstream f(filename, std::ifstream::binary);
|
std::ifstream f(filename, std::ifstream::binary);
|
||||||
if (!f.is_open ()) {
|
if (!f.is_open ())
|
||||||
|
{
|
||||||
LogPrint(eLogDebug, "Addressbook: Requested, but not found: ", filename);
|
LogPrint(eLogDebug, "Addressbook: Requested, but not found: ", filename);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
f.seekg (0,std::ios::end);
|
f.seekg (0,std::ios::end);
|
||||||
size_t len = f.tellg ();
|
size_t len = f.tellg ();
|
||||||
if (len < i2p::data::DEFAULT_IDENTITY_SIZE) {
|
if (len < i2p::data::DEFAULT_IDENTITY_SIZE)
|
||||||
|
{
|
||||||
LogPrint (eLogError, "Addressbook: File ", filename, " is too short: ", len);
|
LogPrint (eLogError, "Addressbook: File ", filename, " is too short: ", len);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
f.seekg(0, std::ios::beg);
|
f.seekg(0, std::ios::beg);
|
||||||
uint8_t * buf = new uint8_t[len];
|
std::vector<uint8_t> buf(len);
|
||||||
f.read((char *)buf, len);
|
f.read((char *)buf.data (), len);
|
||||||
auto address = std::make_shared<i2p::data::IdentityEx>(buf, len);
|
if (!f)
|
||||||
delete[] buf;
|
{
|
||||||
return address;
|
LogPrint (eLogError, "Addressbook: Couldn't read ", filename);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> l(m_FullAddressCacheMutex);
|
||||||
|
m_FullAddressCache.try_emplace (ident, buf, ts);
|
||||||
|
}
|
||||||
|
return std::make_shared<i2p::data::IdentityEx>(buf.data (), len);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AddressBookFilesystemStorage::AddAddress (std::shared_ptr<const i2p::data::IdentityEx> address)
|
void AddressBookFilesystemStorage::AddAddress (std::shared_ptr<const i2p::data::IdentityEx> address)
|
||||||
{
|
{
|
||||||
if (!m_IsPersist) return;
|
if (!address) return;
|
||||||
|
size_t len = address->GetFullLen ();
|
||||||
|
std::vector<uint8_t> buf;
|
||||||
|
if (!len) return; // invalid address
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> l(m_FullAddressCacheMutex);
|
||||||
|
auto [it, inserted] = m_FullAddressCache.try_emplace (address->GetIdentHash(), len, i2p::util::GetMonotonicSeconds ());
|
||||||
|
if (inserted)
|
||||||
|
address->ToBuffer (it->second.first.data (), len);
|
||||||
|
if (m_IsPersist)
|
||||||
|
buf = it->second.first;
|
||||||
|
}
|
||||||
|
if (m_IsPersist && !buf.empty ())
|
||||||
|
{
|
||||||
std::string path = storage.Path(address->GetIdentHash().ToBase32());
|
std::string path = storage.Path(address->GetIdentHash().ToBase32());
|
||||||
std::ofstream f (path, std::ofstream::binary | std::ofstream::out);
|
std::ofstream f (path, std::ofstream::binary | std::ofstream::out);
|
||||||
if (!f.is_open ()) {
|
if (!f.is_open ())
|
||||||
|
{
|
||||||
LogPrint (eLogError, "Addressbook: Can't open file ", path);
|
LogPrint (eLogError, "Addressbook: Can't open file ", path);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
size_t len = address->GetFullLen ();
|
f.write ((const char *)buf.data (), len);
|
||||||
uint8_t * buf = new uint8_t[len];
|
}
|
||||||
address->ToBuffer (buf, len);
|
|
||||||
f.write ((char *)buf, len);
|
|
||||||
delete[] buf;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void AddressBookFilesystemStorage::RemoveAddress (const i2p::data::IdentHash& ident)
|
void AddressBookFilesystemStorage::RemoveAddress (const i2p::data::IdentHash& ident)
|
||||||
{
|
{
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> l(m_FullAddressCacheMutex);
|
||||||
|
m_FullAddressCache.erase (ident);
|
||||||
|
}
|
||||||
if (!m_IsPersist) return;
|
if (!m_IsPersist) return;
|
||||||
storage.Remove( ident.ToBase32() );
|
storage.Remove( ident.ToBase32() );
|
||||||
}
|
}
|
||||||
@@ -281,6 +320,19 @@ namespace client
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void AddressBookFilesystemStorage::CleanUpCache ()
|
||||||
|
{
|
||||||
|
auto ts = i2p::util::GetMonotonicSeconds ();
|
||||||
|
std::lock_guard<std::mutex> l(m_FullAddressCacheMutex);
|
||||||
|
for (auto it = m_FullAddressCache.begin (); it != m_FullAddressCache.end ();)
|
||||||
|
{
|
||||||
|
if (ts > it->second.second + ADDRESS_CACHE_EXPIRATION_TIMEOUT)
|
||||||
|
it = m_FullAddressCache.erase (it);
|
||||||
|
else
|
||||||
|
it++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//---------------------------------------------------------------------
|
//---------------------------------------------------------------------
|
||||||
|
|
||||||
Address::Address (std::string_view b32):
|
Address::Address (std::string_view b32):
|
||||||
@@ -327,6 +379,7 @@ namespace client
|
|||||||
LoadHosts (); /* try storage, then hosts.txt, then download */
|
LoadHosts (); /* try storage, then hosts.txt, then download */
|
||||||
StartSubscriptions ();
|
StartSubscriptions ();
|
||||||
StartLookups ();
|
StartLookups ();
|
||||||
|
ScheduleCacheUpdate ();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -341,9 +394,14 @@ namespace client
|
|||||||
StopSubscriptions ();
|
StopSubscriptions ();
|
||||||
if (m_SubscriptionsUpdateTimer)
|
if (m_SubscriptionsUpdateTimer)
|
||||||
{
|
{
|
||||||
delete m_SubscriptionsUpdateTimer;
|
m_SubscriptionsUpdateTimer->cancel ();
|
||||||
m_SubscriptionsUpdateTimer = nullptr;
|
m_SubscriptionsUpdateTimer = nullptr;
|
||||||
}
|
}
|
||||||
|
if (m_AddressCacheUpdateTimer)
|
||||||
|
{
|
||||||
|
m_AddressCacheUpdateTimer->cancel ();
|
||||||
|
m_AddressCacheUpdateTimer = nullptr;
|
||||||
|
}
|
||||||
bool isDownloading = m_Downloading.valid ();
|
bool isDownloading = m_Downloading.valid ();
|
||||||
if (isDownloading)
|
if (isDownloading)
|
||||||
{
|
{
|
||||||
@@ -682,7 +740,7 @@ namespace client
|
|||||||
auto dest = i2p::client::context.GetSharedLocalDestination ();
|
auto dest = i2p::client::context.GetSharedLocalDestination ();
|
||||||
if (dest)
|
if (dest)
|
||||||
{
|
{
|
||||||
m_SubscriptionsUpdateTimer = new boost::asio::deadline_timer (dest->GetService ());
|
m_SubscriptionsUpdateTimer = std::make_unique<boost::asio::deadline_timer>(dest->GetService ());
|
||||||
m_SubscriptionsUpdateTimer->expires_from_now (boost::posix_time::minutes(INITIAL_SUBSCRIPTION_UPDATE_TIMEOUT));
|
m_SubscriptionsUpdateTimer->expires_from_now (boost::posix_time::minutes(INITIAL_SUBSCRIPTION_UPDATE_TIMEOUT));
|
||||||
m_SubscriptionsUpdateTimer->async_wait (std::bind (&AddressBook::HandleSubscriptionsUpdateTimer,
|
m_SubscriptionsUpdateTimer->async_wait (std::bind (&AddressBook::HandleSubscriptionsUpdateTimer,
|
||||||
this, std::placeholders::_1));
|
this, std::placeholders::_1));
|
||||||
@@ -833,6 +891,29 @@ namespace client
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void AddressBook::ScheduleCacheUpdate ()
|
||||||
|
{
|
||||||
|
if (!m_AddressCacheUpdateTimer)
|
||||||
|
{
|
||||||
|
auto dest = i2p::client::context.GetSharedLocalDestination ();
|
||||||
|
if(dest)
|
||||||
|
m_AddressCacheUpdateTimer = std::make_unique<boost::asio::deadline_timer>(dest->GetService ());
|
||||||
|
}
|
||||||
|
if (m_AddressCacheUpdateTimer)
|
||||||
|
{
|
||||||
|
m_AddressCacheUpdateTimer->expires_from_now (boost::posix_time::seconds(ADDRESS_CACHE_UPDATE_INTERVAL ));
|
||||||
|
m_AddressCacheUpdateTimer->async_wait (
|
||||||
|
[this](const boost::system::error_code& ecode)
|
||||||
|
{
|
||||||
|
if (ecode != boost::asio::error::operation_aborted)
|
||||||
|
{
|
||||||
|
if (m_Storage) m_Storage->CleanUpCache ();
|
||||||
|
ScheduleCacheUpdate ();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
AddressBookSubscription::AddressBookSubscription (AddressBook& book, std::string_view link):
|
AddressBookSubscription::AddressBookSubscription (AddressBook& book, std::string_view link):
|
||||||
m_Book (book), m_Link (link)
|
m_Book (book), m_Link (link)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -34,7 +34,9 @@ namespace client
|
|||||||
const int CONTINIOUS_SUBSCRIPTION_UPDATE_TIMEOUT = 720; // in minutes (12 hours)
|
const int CONTINIOUS_SUBSCRIPTION_UPDATE_TIMEOUT = 720; // in minutes (12 hours)
|
||||||
const int CONTINIOUS_SUBSCRIPTION_RETRY_TIMEOUT = 5; // in minutes
|
const int CONTINIOUS_SUBSCRIPTION_RETRY_TIMEOUT = 5; // in minutes
|
||||||
const int CONTINIOUS_SUBSCRIPTION_MAX_NUM_RETRIES = 10; // then update timeout
|
const int CONTINIOUS_SUBSCRIPTION_MAX_NUM_RETRIES = 10; // then update timeout
|
||||||
const int SUBSCRIPTION_REQUEST_TIMEOUT = 120; //in second
|
const int SUBSCRIPTION_REQUEST_TIMEOUT = 120; //in seconds
|
||||||
|
const int ADDRESS_CACHE_EXPIRATION_TIMEOUT = 710; // in seconds
|
||||||
|
const int ADDRESS_CACHE_UPDATE_INTERVAL = 76; // in seconds
|
||||||
|
|
||||||
const uint16_t ADDRESS_RESOLVER_DATAGRAM_PORT = 53;
|
const uint16_t ADDRESS_RESOLVER_DATAGRAM_PORT = 53;
|
||||||
const uint16_t ADDRESS_RESPONSE_DATAGRAM_PORT = 54;
|
const uint16_t ADDRESS_RESPONSE_DATAGRAM_PORT = 54;
|
||||||
@@ -62,9 +64,10 @@ namespace client
|
|||||||
typedef std::map<std::string, std::shared_ptr<Address>, std::less<> > Addresses;
|
typedef std::map<std::string, std::shared_ptr<Address>, std::less<> > Addresses;
|
||||||
|
|
||||||
virtual ~AddressBookStorage () {};
|
virtual ~AddressBookStorage () {};
|
||||||
virtual std::shared_ptr<const i2p::data::IdentityEx> GetAddress (const i2p::data::IdentHash& ident) const = 0;
|
virtual std::shared_ptr<const i2p::data::IdentityEx> GetAddress (const i2p::data::IdentHash& ident) = 0;
|
||||||
virtual void AddAddress (std::shared_ptr<const i2p::data::IdentityEx> address) = 0;
|
virtual void AddAddress (std::shared_ptr<const i2p::data::IdentityEx> address) = 0;
|
||||||
virtual void RemoveAddress (const i2p::data::IdentHash& ident) = 0;
|
virtual void RemoveAddress (const i2p::data::IdentHash& ident) = 0;
|
||||||
|
virtual void CleanUpCache () = 0;
|
||||||
|
|
||||||
virtual bool Init () = 0;
|
virtual bool Init () = 0;
|
||||||
virtual int Load (Addresses& addresses) = 0;
|
virtual int Load (Addresses& addresses) = 0;
|
||||||
@@ -120,6 +123,8 @@ namespace client
|
|||||||
void StopLookups ();
|
void StopLookups ();
|
||||||
void HandleLookupResponse (const i2p::data::IdentityEx& from, uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len);
|
void HandleLookupResponse (const i2p::data::IdentityEx& from, uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len);
|
||||||
|
|
||||||
|
void ScheduleCacheUpdate ();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
std::mutex m_AddressBookMutex;
|
std::mutex m_AddressBookMutex;
|
||||||
@@ -133,7 +138,7 @@ namespace client
|
|||||||
int m_NumRetries;
|
int m_NumRetries;
|
||||||
std::vector<std::shared_ptr<AddressBookSubscription> > m_Subscriptions;
|
std::vector<std::shared_ptr<AddressBookSubscription> > m_Subscriptions;
|
||||||
std::shared_ptr<AddressBookSubscription> m_DefaultSubscription; // in case if we don't know any addresses yet
|
std::shared_ptr<AddressBookSubscription> m_DefaultSubscription; // in case if we don't know any addresses yet
|
||||||
boost::asio::deadline_timer * m_SubscriptionsUpdateTimer;
|
std::unique_ptr<boost::asio::deadline_timer> m_SubscriptionsUpdateTimer, m_AddressCacheUpdateTimer;
|
||||||
bool m_IsEnabled;
|
bool m_IsEnabled;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2013-2024, The PurpleI2P Project
|
* Copyright (c) 2013-2025, The PurpleI2P Project
|
||||||
*
|
*
|
||||||
* This file is part of Purple i2pd project and licensed under BSD3
|
* This file is part of Purple i2pd project and licensed under BSD3
|
||||||
*
|
*
|
||||||
@@ -16,6 +16,24 @@ namespace i2p
|
|||||||
{
|
{
|
||||||
namespace client
|
namespace client
|
||||||
{
|
{
|
||||||
|
void BOBI2PTunnelIncomingConnection::Established ()
|
||||||
|
{
|
||||||
|
if (m_IsQuiet)
|
||||||
|
StreamReceive ();
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// send destination first like received from I2P
|
||||||
|
std::string dest = GetStream ()->GetRemoteIdentity ()->ToBase64 ();
|
||||||
|
dest += "\n";
|
||||||
|
if (dest.size() <= I2P_TUNNEL_CONNECTION_BUFFER_SIZE)
|
||||||
|
memcpy (GetStreamBuffer (), dest.c_str (), dest.size ());
|
||||||
|
else
|
||||||
|
memset (GetStreamBuffer (), 0, I2P_TUNNEL_CONNECTION_BUFFER_SIZE);
|
||||||
|
HandleStreamReceive (boost::system::error_code (), dest.size ());
|
||||||
|
}
|
||||||
|
Receive ();
|
||||||
|
}
|
||||||
|
|
||||||
BOBI2PInboundTunnel::BOBI2PInboundTunnel (const boost::asio::ip::tcp::endpoint& ep, std::shared_ptr<ClientDestination> localDestination):
|
BOBI2PInboundTunnel::BOBI2PInboundTunnel (const boost::asio::ip::tcp::endpoint& ep, std::shared_ptr<ClientDestination> localDestination):
|
||||||
BOBI2PTunnel (localDestination), m_Acceptor (localDestination->GetService (), ep)
|
BOBI2PTunnel (localDestination), m_Acceptor (localDestination->GetService (), ep)
|
||||||
{
|
{
|
||||||
@@ -156,7 +174,7 @@ namespace client
|
|||||||
{
|
{
|
||||||
if (stream)
|
if (stream)
|
||||||
{
|
{
|
||||||
auto conn = std::make_shared<I2PTunnelConnection> (this, stream, m_Endpoint, m_IsQuiet);
|
auto conn = std::make_shared<BOBI2PTunnelIncomingConnection> (this, stream, m_Endpoint, m_IsQuiet);
|
||||||
AddHandler (conn);
|
AddHandler (conn);
|
||||||
conn->Connect ();
|
conn->Connect ();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2013-2024, The PurpleI2P Project
|
* Copyright (c) 2013-2025, The PurpleI2P Project
|
||||||
*
|
*
|
||||||
* This file is part of Purple i2pd project and licensed under BSD3
|
* This file is part of Purple i2pd project and licensed under BSD3
|
||||||
*
|
*
|
||||||
@@ -71,6 +71,23 @@ namespace client
|
|||||||
const char BOB_HELP_STATUS[] = "status <NICKNAME> - Display status of a nicknamed tunnel.";
|
const char BOB_HELP_STATUS[] = "status <NICKNAME> - Display status of a nicknamed tunnel.";
|
||||||
const char BOB_HELP_HELP [] = "help <COMMAND> - Get help on a command.";
|
const char BOB_HELP_HELP [] = "help <COMMAND> - Get help on a command.";
|
||||||
|
|
||||||
|
class BOBI2PTunnelIncomingConnection: public I2PTunnelConnection
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
BOBI2PTunnelIncomingConnection (I2PService * owner, std::shared_ptr<i2p::stream::Stream> stream,
|
||||||
|
const boost::asio::ip::tcp::endpoint& target, bool quiet):
|
||||||
|
I2PTunnelConnection (owner, stream, target), m_IsQuiet (quiet) {};
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
|
void Established () override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
bool m_IsQuiet; // don't send destination
|
||||||
|
};
|
||||||
|
|
||||||
class BOBI2PTunnel: public I2PService
|
class BOBI2PTunnel: public I2PService
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|||||||
@@ -297,7 +297,7 @@ namespace client
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
LogPrint (eLogCritical, "Clients: Can't open file ", fullPath, " Creating new one with signature type ", sigType, " crypto type ", cryptoType);
|
LogPrint (eLogInfo, "Clients: Can't open file ", fullPath, " Creating new one with signature type ", sigType, " crypto type ", cryptoType);
|
||||||
keys = i2p::data::PrivateKeys::CreateRandomKeys (sigType, cryptoType, true);
|
keys = i2p::data::PrivateKeys::CreateRandomKeys (sigType, cryptoType, true);
|
||||||
std::ofstream f (fullPath, std::ofstream::binary | std::ofstream::out);
|
std::ofstream f (fullPath, std::ofstream::binary | std::ofstream::out);
|
||||||
size_t len = keys.GetFullLen ();
|
size_t len = keys.GetFullLen ();
|
||||||
@@ -871,7 +871,7 @@ namespace client
|
|||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
LogPrint (eLogWarning, "Clients: Unknown section type = ", type, " of ", name, " in ", tunConf);
|
LogPrint (eLogError, "Clients: Unknown section type = ", type, " of ", name, " in ", tunConf);
|
||||||
}
|
}
|
||||||
catch (std::exception& ex)
|
catch (std::exception& ex)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -29,13 +29,15 @@ namespace client
|
|||||||
const std::map<std::string, std::string>& params):
|
const std::map<std::string, std::string>& params):
|
||||||
LeaseSetDestination (service, isPublic, ¶ms),
|
LeaseSetDestination (service, isPublic, ¶ms),
|
||||||
m_Owner (owner), m_Identity (identity), m_EncryptionKeyType (m_Identity->GetCryptoKeyType ()),
|
m_Owner (owner), m_Identity (identity), m_EncryptionKeyType (m_Identity->GetCryptoKeyType ()),
|
||||||
m_IsCreatingLeaseSet (false), m_IsSameThread (isSameThread), m_LeaseSetCreationTimer (service)
|
m_IsCreatingLeaseSet (false), m_IsSameThread (isSameThread),
|
||||||
|
m_LeaseSetCreationTimer (service), m_ReadinessCheckTimer (service)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void I2CPDestination::Stop ()
|
void I2CPDestination::Stop ()
|
||||||
{
|
{
|
||||||
m_LeaseSetCreationTimer.cancel ();
|
m_LeaseSetCreationTimer.cancel ();
|
||||||
|
m_ReadinessCheckTimer.cancel ();
|
||||||
LeaseSetDestination::Stop ();
|
LeaseSetDestination::Stop ();
|
||||||
m_Owner = nullptr;
|
m_Owner = nullptr;
|
||||||
}
|
}
|
||||||
@@ -88,7 +90,7 @@ namespace client
|
|||||||
|
|
||||||
void I2CPDestination::CreateNewLeaseSet (const std::vector<std::shared_ptr<i2p::tunnel::InboundTunnel> >& tunnels)
|
void I2CPDestination::CreateNewLeaseSet (const std::vector<std::shared_ptr<i2p::tunnel::InboundTunnel> >& tunnels)
|
||||||
{
|
{
|
||||||
boost::asio::post (GetService (), std::bind (&I2CPDestination::PostCreateNewLeaseSet, this, tunnels));
|
boost::asio::post (GetService (), std::bind (&I2CPDestination::PostCreateNewLeaseSet, GetSharedFromThis (), tunnels));
|
||||||
}
|
}
|
||||||
|
|
||||||
void I2CPDestination::PostCreateNewLeaseSet (std::vector<std::shared_ptr<i2p::tunnel::InboundTunnel> > tunnels)
|
void I2CPDestination::PostCreateNewLeaseSet (std::vector<std::shared_ptr<i2p::tunnel::InboundTunnel> > tunnels)
|
||||||
@@ -98,6 +100,20 @@ namespace client
|
|||||||
LogPrint (eLogInfo, "I2CP: LeaseSet is being created");
|
LogPrint (eLogInfo, "I2CP: LeaseSet is being created");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
m_ReadinessCheckTimer.cancel ();
|
||||||
|
auto pool = GetTunnelPool ();
|
||||||
|
if (!pool || pool->GetOutboundTunnels ().empty ())
|
||||||
|
{
|
||||||
|
// try again later
|
||||||
|
m_ReadinessCheckTimer.expires_from_now (boost::posix_time::seconds(I2CP_DESTINATION_READINESS_CHECK_INTERVAL));
|
||||||
|
m_ReadinessCheckTimer.async_wait(
|
||||||
|
[s=GetSharedFromThis (), tunnels=std::move(tunnels)](const boost::system::error_code& ecode)
|
||||||
|
{
|
||||||
|
if (ecode != boost::asio::error::operation_aborted)
|
||||||
|
s->PostCreateNewLeaseSet (tunnels);
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
uint8_t priv[256] = {0};
|
uint8_t priv[256] = {0};
|
||||||
i2p::data::LocalLeaseSet ls (m_Identity, priv, tunnels); // we don't care about encryption key, we need leases only
|
i2p::data::LocalLeaseSet ls (m_Identity, priv, tunnels); // we don't care about encryption key, we need leases only
|
||||||
m_LeaseSetExpirationTime = ls.GetExpirationTime ();
|
m_LeaseSetExpirationTime = ls.GetExpirationTime ();
|
||||||
@@ -555,7 +571,7 @@ namespace client
|
|||||||
m_IsSending = false;
|
m_IsSending = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string_view I2CPSession::ExtractString (const uint8_t * buf, size_t len)
|
std::string_view I2CPSession::ExtractString (const uint8_t * buf, size_t len) const
|
||||||
{
|
{
|
||||||
uint8_t l = buf[0];
|
uint8_t l = buf[0];
|
||||||
if (l > len) l = len;
|
if (l > len) l = len;
|
||||||
@@ -572,7 +588,7 @@ namespace client
|
|||||||
return l + 1;
|
return l + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void I2CPSession::ExtractMapping (const uint8_t * buf, size_t len, std::map<std::string, std::string>& mapping)
|
void I2CPSession::ExtractMapping (const uint8_t * buf, size_t len, std::map<std::string, std::string>& mapping) const
|
||||||
// TODO: move to Base.cpp
|
// TODO: move to Base.cpp
|
||||||
{
|
{
|
||||||
size_t offset = 0;
|
size_t offset = 0;
|
||||||
@@ -764,6 +780,7 @@ namespace client
|
|||||||
void I2CPSession::AddRoutingSession (const i2p::data::IdentHash& signingKey, std::shared_ptr<i2p::garlic::GarlicRoutingSession> remoteSession)
|
void I2CPSession::AddRoutingSession (const i2p::data::IdentHash& signingKey, std::shared_ptr<i2p::garlic::GarlicRoutingSession> remoteSession)
|
||||||
{
|
{
|
||||||
if (!remoteSession) return;
|
if (!remoteSession) return;
|
||||||
|
remoteSession->SetAckRequestInterval (I2CP_SESSION_ACK_REQUEST_INTERVAL);
|
||||||
std::lock_guard<std::mutex> l(m_RoutingSessionsMutex);
|
std::lock_guard<std::mutex> l(m_RoutingSessionsMutex);
|
||||||
m_RoutingSessions[signingKey] = remoteSession;
|
m_RoutingSessions[signingKey] = remoteSession;
|
||||||
}
|
}
|
||||||
@@ -1110,12 +1127,12 @@ namespace client
|
|||||||
void I2CPServer::Stop ()
|
void I2CPServer::Stop ()
|
||||||
{
|
{
|
||||||
m_Acceptor.cancel ();
|
m_Acceptor.cancel ();
|
||||||
{
|
|
||||||
auto sessions = m_Sessions;
|
decltype(m_Sessions) sessions;
|
||||||
|
m_Sessions.swap (sessions);
|
||||||
for (auto& it: sessions)
|
for (auto& it: sessions)
|
||||||
it.second->Stop ();
|
it.second->Stop ();
|
||||||
}
|
|
||||||
m_Sessions.clear ();
|
|
||||||
StopIOService ();
|
StopIOService ();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -31,6 +31,8 @@ namespace client
|
|||||||
const size_t I2CP_MAX_MESSAGE_LENGTH = 65535;
|
const size_t I2CP_MAX_MESSAGE_LENGTH = 65535;
|
||||||
const size_t I2CP_MAX_SEND_QUEUE_SIZE = 1024*1024; // in bytes, 1M
|
const size_t I2CP_MAX_SEND_QUEUE_SIZE = 1024*1024; // in bytes, 1M
|
||||||
const int I2CP_LEASESET_CREATION_TIMEOUT = 10; // in seconds
|
const int I2CP_LEASESET_CREATION_TIMEOUT = 10; // in seconds
|
||||||
|
const int I2CP_DESTINATION_READINESS_CHECK_INTERVAL = 5; // in seconds
|
||||||
|
const int I2CP_SESSION_ACK_REQUEST_INTERVAL = 12100; // in milliseconds
|
||||||
|
|
||||||
const size_t I2CP_HEADER_LENGTH_OFFSET = 0;
|
const size_t I2CP_HEADER_LENGTH_OFFSET = 0;
|
||||||
const size_t I2CP_HEADER_TYPE_OFFSET = I2CP_HEADER_LENGTH_OFFSET + 4;
|
const size_t I2CP_HEADER_TYPE_OFFSET = I2CP_HEADER_LENGTH_OFFSET + 4;
|
||||||
@@ -130,7 +132,7 @@ namespace client
|
|||||||
uint8_t m_ECIESx25519PrivateKey[32];
|
uint8_t m_ECIESx25519PrivateKey[32];
|
||||||
uint64_t m_LeaseSetExpirationTime;
|
uint64_t m_LeaseSetExpirationTime;
|
||||||
bool m_IsCreatingLeaseSet, m_IsSameThread;
|
bool m_IsCreatingLeaseSet, m_IsSameThread;
|
||||||
boost::asio::deadline_timer m_LeaseSetCreationTimer;
|
boost::asio::deadline_timer m_LeaseSetCreationTimer, m_ReadinessCheckTimer;
|
||||||
i2p::util::MemoryPoolMt<I2NPMessageBuffer<I2NP_MAX_MESSAGE_SIZE> > m_I2NPMsgsPool;
|
i2p::util::MemoryPoolMt<I2NPMessageBuffer<I2NP_MAX_MESSAGE_SIZE> > m_I2NPMsgsPool;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -192,9 +194,9 @@ namespace client
|
|||||||
|
|
||||||
void HandleI2CPMessageSent (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
void HandleI2CPMessageSent (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
||||||
|
|
||||||
std::string_view ExtractString (const uint8_t * buf, size_t len);
|
std::string_view ExtractString (const uint8_t * buf, size_t len) const;
|
||||||
size_t PutString (uint8_t * buf, size_t len, std::string_view str);
|
size_t PutString (uint8_t * buf, size_t len, std::string_view str);
|
||||||
void ExtractMapping (const uint8_t * buf, size_t len, std::map<std::string, std::string>& mapping);
|
void ExtractMapping (const uint8_t * buf, size_t len, std::map<std::string, std::string>& mapping) const;
|
||||||
void SendSessionStatusMessage (I2CPSessionStatus status);
|
void SendSessionStatusMessage (I2CPSessionStatus status);
|
||||||
void SendHostReplyMessage (uint32_t requestID, std::shared_ptr<const i2p::data::IdentityEx> identity);
|
void SendHostReplyMessage (uint32_t requestID, std::shared_ptr<const i2p::data::IdentityEx> identity);
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2013-2024, The PurpleI2P Project
|
* Copyright (c) 2013-2025, The PurpleI2P Project
|
||||||
*
|
*
|
||||||
* This file is part of Purple i2pd project and licensed under BSD3
|
* This file is part of Purple i2pd project and licensed under BSD3
|
||||||
*
|
*
|
||||||
@@ -32,8 +32,7 @@ namespace client
|
|||||||
|
|
||||||
I2PTunnelConnection::I2PTunnelConnection (I2PService * owner, std::shared_ptr<boost::asio::ip::tcp::socket> socket,
|
I2PTunnelConnection::I2PTunnelConnection (I2PService * owner, std::shared_ptr<boost::asio::ip::tcp::socket> socket,
|
||||||
std::shared_ptr<const i2p::data::LeaseSet> leaseSet, uint16_t port):
|
std::shared_ptr<const i2p::data::LeaseSet> leaseSet, uint16_t port):
|
||||||
I2PServiceHandler(owner), m_Socket (socket), m_RemoteEndpoint (socket->remote_endpoint ()),
|
I2PServiceHandler(owner), m_Socket (socket), m_RemoteEndpoint (socket->remote_endpoint ())
|
||||||
m_IsQuiet (true)
|
|
||||||
{
|
{
|
||||||
m_Stream = GetOwner()->GetLocalDestination ()->CreateStream (leaseSet, port);
|
m_Stream = GetOwner()->GetLocalDestination ()->CreateStream (leaseSet, port);
|
||||||
}
|
}
|
||||||
@@ -41,14 +40,13 @@ namespace client
|
|||||||
I2PTunnelConnection::I2PTunnelConnection (I2PService * owner,
|
I2PTunnelConnection::I2PTunnelConnection (I2PService * owner,
|
||||||
std::shared_ptr<boost::asio::ip::tcp::socket> socket, std::shared_ptr<i2p::stream::Stream> stream):
|
std::shared_ptr<boost::asio::ip::tcp::socket> socket, std::shared_ptr<i2p::stream::Stream> stream):
|
||||||
I2PServiceHandler(owner), m_Socket (socket), m_Stream (stream),
|
I2PServiceHandler(owner), m_Socket (socket), m_Stream (stream),
|
||||||
m_RemoteEndpoint (socket->remote_endpoint ()), m_IsQuiet (true)
|
m_RemoteEndpoint (socket->remote_endpoint ())
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
I2PTunnelConnection::I2PTunnelConnection (I2PService * owner, std::shared_ptr<i2p::stream::Stream> stream,
|
I2PTunnelConnection::I2PTunnelConnection (I2PService * owner, std::shared_ptr<i2p::stream::Stream> stream,
|
||||||
const boost::asio::ip::tcp::endpoint& target, bool quiet,
|
const boost::asio::ip::tcp::endpoint& target,std::shared_ptr<boost::asio::ssl::context> sslCtx):
|
||||||
std::shared_ptr<boost::asio::ssl::context> sslCtx):
|
I2PServiceHandler(owner), m_Stream (stream), m_RemoteEndpoint (target)
|
||||||
I2PServiceHandler(owner), m_Stream (stream), m_RemoteEndpoint (target), m_IsQuiet (quiet)
|
|
||||||
{
|
{
|
||||||
m_Socket = std::make_shared<boost::asio::ip::tcp::socket> (owner->GetService ());
|
m_Socket = std::make_shared<boost::asio::ip::tcp::socket> (owner->GetService ());
|
||||||
if (sslCtx)
|
if (sslCtx)
|
||||||
@@ -292,18 +290,7 @@ namespace client
|
|||||||
|
|
||||||
void I2PTunnelConnection::Established ()
|
void I2PTunnelConnection::Established ()
|
||||||
{
|
{
|
||||||
if (m_IsQuiet)
|
|
||||||
StreamReceive ();
|
StreamReceive ();
|
||||||
else
|
|
||||||
{
|
|
||||||
// send destination first like received from I2P
|
|
||||||
std::string dest = m_Stream->GetRemoteIdentity ()->ToBase64 ();
|
|
||||||
dest += "\n";
|
|
||||||
if(sizeof(m_StreamBuffer) >= dest.size()) {
|
|
||||||
memcpy (m_StreamBuffer, dest.c_str (), dest.size ());
|
|
||||||
}
|
|
||||||
HandleStreamReceive (boost::system::error_code (), dest.size ());
|
|
||||||
}
|
|
||||||
Receive ();
|
Receive ();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -377,7 +364,7 @@ namespace client
|
|||||||
I2PServerTunnelConnectionHTTP::I2PServerTunnelConnectionHTTP (I2PService * owner, std::shared_ptr<i2p::stream::Stream> stream,
|
I2PServerTunnelConnectionHTTP::I2PServerTunnelConnectionHTTP (I2PService * owner, std::shared_ptr<i2p::stream::Stream> stream,
|
||||||
const boost::asio::ip::tcp::endpoint& target, const std::string& host, const std::string& XI2P,
|
const boost::asio::ip::tcp::endpoint& target, const std::string& host, const std::string& XI2P,
|
||||||
std::shared_ptr<boost::asio::ssl::context> sslCtx):
|
std::shared_ptr<boost::asio::ssl::context> sslCtx):
|
||||||
I2PTunnelConnection (owner, stream, target, true, sslCtx), m_Host (host), m_XI2P (XI2P),
|
I2PTunnelConnection (owner, stream, target, sslCtx), m_Host (host), m_XI2P (XI2P),
|
||||||
m_HeaderSent (false), m_ResponseHeaderSent (false)
|
m_HeaderSent (false), m_ResponseHeaderSent (false)
|
||||||
{
|
{
|
||||||
if (sslCtx)
|
if (sslCtx)
|
||||||
@@ -528,7 +515,7 @@ namespace client
|
|||||||
I2PTunnelConnectionIRC::I2PTunnelConnectionIRC (I2PService * owner, std::shared_ptr<i2p::stream::Stream> stream,
|
I2PTunnelConnectionIRC::I2PTunnelConnectionIRC (I2PService * owner, std::shared_ptr<i2p::stream::Stream> stream,
|
||||||
const boost::asio::ip::tcp::endpoint& target, const std::string& webircpass,
|
const boost::asio::ip::tcp::endpoint& target, const std::string& webircpass,
|
||||||
std::shared_ptr<boost::asio::ssl::context> sslCtx):
|
std::shared_ptr<boost::asio::ssl::context> sslCtx):
|
||||||
I2PTunnelConnection (owner, stream, target, true, sslCtx), m_From (stream->GetRemoteIdentity ()),
|
I2PTunnelConnection (owner, stream, target, sslCtx), m_From (stream->GetRemoteIdentity ()),
|
||||||
m_NeedsWebIrc (webircpass.length() ? true : false), m_WebircPass (webircpass)
|
m_NeedsWebIrc (webircpass.length() ? true : false), m_WebircPass (webircpass)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@@ -857,7 +844,7 @@ namespace client
|
|||||||
|
|
||||||
std::shared_ptr<I2PTunnelConnection> I2PServerTunnel::CreateI2PConnection (std::shared_ptr<i2p::stream::Stream> stream)
|
std::shared_ptr<I2PTunnelConnection> I2PServerTunnel::CreateI2PConnection (std::shared_ptr<i2p::stream::Stream> stream)
|
||||||
{
|
{
|
||||||
return std::make_shared<I2PTunnelConnection> (this, stream, GetEndpoint (), true, m_SSLCtx);
|
return std::make_shared<I2PTunnelConnection> (this, stream, GetEndpoint (), m_SSLCtx);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2013-2024, The PurpleI2P Project
|
* Copyright (c) 2013-2025, The PurpleI2P Project
|
||||||
*
|
*
|
||||||
* This file is part of Purple i2pd project and licensed under BSD3
|
* This file is part of Purple i2pd project and licensed under BSD3
|
||||||
*
|
*
|
||||||
@@ -27,7 +27,7 @@ namespace i2p
|
|||||||
{
|
{
|
||||||
namespace client
|
namespace client
|
||||||
{
|
{
|
||||||
const size_t I2P_TUNNEL_CONNECTION_BUFFER_SIZE = 65536;
|
const size_t I2P_TUNNEL_CONNECTION_BUFFER_SIZE = 16384;
|
||||||
const int I2P_TUNNEL_CONNECTION_MAX_IDLE = 3600; // in seconds
|
const int I2P_TUNNEL_CONNECTION_MAX_IDLE = 3600; // in seconds
|
||||||
const int I2P_TUNNEL_DESTINATION_REQUEST_TIMEOUT = 10; // in seconds
|
const int I2P_TUNNEL_DESTINATION_REQUEST_TIMEOUT = 10; // in seconds
|
||||||
// for HTTP tunnels
|
// for HTTP tunnels
|
||||||
@@ -45,7 +45,7 @@ namespace client
|
|||||||
I2PTunnelConnection (I2PService * owner, std::shared_ptr<boost::asio::ip::tcp::socket> socket,
|
I2PTunnelConnection (I2PService * owner, std::shared_ptr<boost::asio::ip::tcp::socket> socket,
|
||||||
std::shared_ptr<i2p::stream::Stream> stream); // to I2P using simplified API
|
std::shared_ptr<i2p::stream::Stream> stream); // to I2P using simplified API
|
||||||
I2PTunnelConnection (I2PService * owner, std::shared_ptr<i2p::stream::Stream> stream,
|
I2PTunnelConnection (I2PService * owner, std::shared_ptr<i2p::stream::Stream> stream,
|
||||||
const boost::asio::ip::tcp::endpoint& target, bool quiet = true,
|
const boost::asio::ip::tcp::endpoint& target,
|
||||||
std::shared_ptr<boost::asio::ssl::context> sslCtx = nullptr); // from I2P
|
std::shared_ptr<boost::asio::ssl::context> sslCtx = nullptr); // from I2P
|
||||||
~I2PTunnelConnection ();
|
~I2PTunnelConnection ();
|
||||||
void I2PConnect (const uint8_t * msg = nullptr, size_t len = 0);
|
void I2PConnect (const uint8_t * msg = nullptr, size_t len = 0);
|
||||||
@@ -54,24 +54,26 @@ namespace client
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
|
virtual void Established ();
|
||||||
void Terminate ();
|
void Terminate ();
|
||||||
|
|
||||||
void Receive ();
|
void Receive ();
|
||||||
void StreamReceive ();
|
void StreamReceive ();
|
||||||
|
void HandleStreamReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
||||||
virtual void Write (const uint8_t * buf, size_t len); // can be overloaded
|
virtual void Write (const uint8_t * buf, size_t len); // can be overloaded
|
||||||
virtual void WriteToStream (const uint8_t * buf, size_t len); // can be overloaded
|
virtual void WriteToStream (const uint8_t * buf, size_t len); // can be overloaded
|
||||||
|
|
||||||
std::shared_ptr<boost::asio::ip::tcp::socket> GetSocket () const { return m_Socket; };
|
std::shared_ptr<boost::asio::ip::tcp::socket> GetSocket () const { return m_Socket; };
|
||||||
|
std::shared_ptr<i2p::stream::Stream> GetStream () const { return m_Stream; };
|
||||||
std::shared_ptr<boost::asio::ssl::stream<boost::asio::ip::tcp::socket&> > GetSSL () const { return m_SSL; };
|
std::shared_ptr<boost::asio::ssl::stream<boost::asio::ip::tcp::socket&> > GetSSL () const { return m_SSL; };
|
||||||
|
uint8_t * GetStreamBuffer () { return m_StreamBuffer; };
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
void HandleConnect (const boost::system::error_code& ecode);
|
void HandleConnect (const boost::system::error_code& ecode);
|
||||||
void HandleHandshake (const boost::system::error_code& ecode);
|
void HandleHandshake (const boost::system::error_code& ecode);
|
||||||
void Established ();
|
|
||||||
void HandleReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
void HandleReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
||||||
void HandleWrite (const boost::system::error_code& ecode);
|
void HandleWrite (const boost::system::error_code& ecode);
|
||||||
void HandleStreamReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
@@ -80,7 +82,6 @@ namespace client
|
|||||||
std::shared_ptr<boost::asio::ssl::stream<boost::asio::ip::tcp::socket&> > m_SSL;
|
std::shared_ptr<boost::asio::ssl::stream<boost::asio::ip::tcp::socket&> > m_SSL;
|
||||||
std::shared_ptr<i2p::stream::Stream> m_Stream;
|
std::shared_ptr<i2p::stream::Stream> m_Stream;
|
||||||
boost::asio::ip::tcp::endpoint m_RemoteEndpoint;
|
boost::asio::ip::tcp::endpoint m_RemoteEndpoint;
|
||||||
bool m_IsQuiet; // don't send destination
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class I2PClientTunnelConnectionHTTP: public I2PTunnelConnection
|
class I2PClientTunnelConnectionHTTP: public I2PTunnelConnection
|
||||||
@@ -94,7 +95,7 @@ namespace client
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
void Write (const uint8_t * buf, size_t len);
|
void Write (const uint8_t * buf, size_t len) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
@@ -112,8 +113,8 @@ namespace client
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
void Write (const uint8_t * buf, size_t len);
|
void Write (const uint8_t * buf, size_t len) override;
|
||||||
void WriteToStream (const uint8_t * buf, size_t len);
|
void WriteToStream (const uint8_t * buf, size_t len) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
@@ -132,7 +133,7 @@ namespace client
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
void Write (const uint8_t * buf, size_t len);
|
void Write (const uint8_t * buf, size_t len) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2013-2024, The PurpleI2P Project
|
* Copyright (c) 2013-2025, The PurpleI2P Project
|
||||||
*
|
*
|
||||||
* This file is part of Purple i2pd project and licensed under BSD3
|
* This file is part of Purple i2pd project and licensed under BSD3
|
||||||
*
|
*
|
||||||
@@ -1350,12 +1350,14 @@ namespace client
|
|||||||
LogPrint (eLogError, "SAM: Runtime exception: ", ex.what ());
|
LogPrint (eLogError, "SAM: Runtime exception: ", ex.what ());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
decltype(m_Sessions) sessions;
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> l(m_SessionsMutex);
|
std::unique_lock<std::mutex> l(m_SessionsMutex);
|
||||||
for (auto& it: m_Sessions)
|
m_Sessions.swap (sessions);
|
||||||
it.second->Close ();
|
|
||||||
m_Sessions.clear ();
|
|
||||||
}
|
}
|
||||||
|
for (auto& it: sessions)
|
||||||
|
it.second->Close ();
|
||||||
|
|
||||||
StopIOService ();
|
StopIOService ();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user