diff --git a/libi2pd_client/ClientContext.cpp b/libi2pd_client/ClientContext.cpp index b638314e..55e4f388 100644 --- a/libi2pd_client/ClientContext.cpp +++ b/libi2pd_client/ClientContext.cpp @@ -1048,6 +1048,7 @@ namespace client i2p::data::SigningKeyType sigType = section.second.get (TORRENTS_TUNNEL_SIGNATURE_TYPE, i2p::data::SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519); if (sigType > i2p::data::SIGNING_KEY_TYPE_REDDSA_SHA512_ED25519) sigType = i2p::data::SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519; bool dht = section.second.get (TORRENTS_TUNNEL_DHT, true); + std::string dhtFileName = section.second.get (TORRENTS_TUNNEL_DHT_FILE, ""); bool web = section.second.get (TORRENTS_TUNNEL_WEB, true); // I2CP @@ -1080,7 +1081,7 @@ namespace client localDestination->SetPublic (true); } auto torrentsTunnel = std::make_shared (name, - localDestination, torrentsDir, trackers, dht, web); + localDestination, torrentsDir, trackers, dht, dhtFileName, web); auto [iit, inserted] = m_TorrentsTunnels.emplace (localDestination->GetIdentHash (), torrentsTunnel); if (inserted) { diff --git a/libi2pd_client/ClientContext.h b/libi2pd_client/ClientContext.h index 543e92cb..3289711e 100644 --- a/libi2pd_client/ClientContext.h +++ b/libi2pd_client/ClientContext.h @@ -85,6 +85,7 @@ namespace client const char TORRENTS_TUNNEL_RPC_USER[] = "rpcuser"; const char TORRENTS_TUNNEL_RPC_PASS[] = "rpcpass"; const char TORRENTS_TUNNEL_DHT[] = "dht"; + const char TORRENTS_TUNNEL_DHT_FILE[] = "dhtfile"; const char TORRENTS_TUNNEL_WEB[] = "web"; constexpr int STOP_DESTINATIONS_TIMEOUT = 8; // in seconds diff --git a/libi2pd_client/TorrentsDHT.cpp b/libi2pd_client/TorrentsDHT.cpp index ff25d3b8..9fc58410 100644 --- a/libi2pd_client/TorrentsDHT.cpp +++ b/libi2pd_client/TorrentsDHT.cpp @@ -316,11 +316,10 @@ namespace torrents return node; } - TorrentsDHT::TorrentsDHT (TorrentsTunnel& tunnel, uint16_t port): - m_Tunnel (tunnel), m_DHTUpdateCheckTimer (tunnel.GetService ()), - m_DHTExpirationCheckTimer (tunnel.GetService ()), - m_DHTSendPingCheckTimer (tunnel.GetService ()), - m_DHTQueryExpirationCheckTimer (tunnel.GetService ()), m_Port (port), + TorrentsDHT::TorrentsDHT (TorrentsTunnel& tunnel, uint16_t port, std::string_view dhtFileName): + m_Tunnel (tunnel), m_Port (port), m_DHTFileName (dhtFileName), + m_DHTUpdateCheckTimer (tunnel.GetService ()), m_DHTExpirationCheckTimer (tunnel.GetService ()), + m_DHTSendPingCheckTimer (tunnel.GetService ()), m_DHTQueryExpirationCheckTimer (tunnel.GetService ()), m_NextDHTExploratoryTime (i2p::util::GetMonotonicSeconds () + DHT_INITIAL_EXPLORATORY_INTERVAL) { auto dest = tunnel.GetLocalDestination (); @@ -338,16 +337,17 @@ namespace torrents void TorrentsDHT::Start () { - std::string filename ("nodest"); auto dest = m_Tunnel.GetLocalDestination (); if (dest) { auto dgramDest = dest->GetDatagramDestination (); if (dgramDest) dgramDest->SetReceiver (std::bind_front (&TorrentsDHT::HandleDatagram, this)); - filename = dest->GetIdentHash ().ToBase32 (); + if (m_DHTFileName.empty ()) + m_DHTFileName = dest->GetIdentHash ().ToBase32 () + ".dht"; } - Load (GetDHTFilePath (filename)); + if (m_DHTFileName.empty ()) m_DHTFileName = "nodest.dht"; + Load (GetDHTFilePath (m_DHTFileName)); ScheduleDHTUpdateCheck (); ScheduleDHTExpirationCheck (); ScheduleDHTSendPingCheck (); @@ -360,16 +360,7 @@ namespace torrents m_DHTExpirationCheckTimer.cancel (); m_DHTSendPingCheckTimer.cancel (); m_DHTQueryExpirationCheckTimer.cancel (); - std::string filename ("nodest"); - auto dest = m_Tunnel.GetLocalDestination (); - if (dest) - { - auto dgramDest = dest->GetDatagramDestination (); - if (dgramDest) - dgramDest->ResetReceiver (); - filename = dest->GetIdentHash ().ToBase32 (); - } - Save (GetDHTFilePath (filename)); + Save (GetDHTFilePath (m_DHTFileName)); } std::filesystem::path TorrentsDHT::GetDHTFilePath (std::string_view filename) const @@ -377,7 +368,7 @@ namespace torrents std::filesystem::path dhtFilePath (i2p::fs::GetDataDir()); dhtFilePath /= "torrents"; if (!std::filesystem::exists (dhtFilePath)) std::filesystem::create_directories (dhtFilePath); - dhtFilePath /= filename; dhtFilePath += ".dht"; + dhtFilePath /= filename; return dhtFilePath; } diff --git a/libi2pd_client/TorrentsDHT.h b/libi2pd_client/TorrentsDHT.h index 378744ce..8537856a 100644 --- a/libi2pd_client/TorrentsDHT.h +++ b/libi2pd_client/TorrentsDHT.h @@ -222,7 +222,7 @@ namespace torrents { public: - TorrentsDHT (TorrentsTunnel& tunnel, uint16_t port); + TorrentsDHT (TorrentsTunnel& tunnel, uint16_t port, std::string_view dhtFileName); void Start (); void Stop (); @@ -299,10 +299,11 @@ namespace torrents private: TorrentsTunnel& m_Tunnel; + uint16_t m_Port; + std::string m_DHTFileName; + NodeID m_NodeID; boost::asio::steady_timer m_DHTUpdateCheckTimer, m_DHTExpirationCheckTimer, m_DHTSendPingCheckTimer, m_DHTQueryExpirationCheckTimer; - uint16_t m_Port; - NodeID m_NodeID; std::unique_ptr m_RoutingTable; std::map m_HeardFrom; // nodeid -> time in monotonic seconds std::unordered_map m_FailedPeers; // peer -> failure time in monotonic seconds diff --git a/libi2pd_client/TorrentsTunnel.cpp b/libi2pd_client/TorrentsTunnel.cpp index 9f38f647..b057f5dd 100644 --- a/libi2pd_client/TorrentsTunnel.cpp +++ b/libi2pd_client/TorrentsTunnel.cpp @@ -25,7 +25,7 @@ namespace i2p namespace torrents { TorrentsTunnel::TorrentsTunnel (std::string_view name, std::shared_ptr localDestination, - std::string_view torrentsDir, std::string_view trackers, bool dht, bool web): + std::string_view torrentsDir, std::string_view trackers, bool dht, std::string_view dhtFileName, bool web): i2p::client::I2PService (localDestination), m_Name (name), m_PeerID ("-I2PD-"), m_TorrentsDir (torrentsDir), m_TrackerRequestsCheckTimer (GetService ()), m_KeepAliveCheckTimer (GetService ()), m_ReconnectCheckTimer (GetService ()), @@ -59,7 +59,7 @@ namespace torrents m_Trackers.emplace_back (TrackerInfo{ it.second, true, 0, 0, 0 }); } if (dht) - m_DHT = std::make_unique(*this, TORRENT_PORT + (localDestination ? localDestination->GetRng()() % (65535 - TORRENT_PORT - 1) : 1)); + m_DHT = std::make_unique(*this, TORRENT_PORT + (localDestination ? localDestination->GetRng()() % (65535 - TORRENT_PORT - 1) : 1), dhtFileName); } void TorrentsTunnel::Start () diff --git a/libi2pd_client/TorrentsTunnel.h b/libi2pd_client/TorrentsTunnel.h index aaa02120..eec1c574 100644 --- a/libi2pd_client/TorrentsTunnel.h +++ b/libi2pd_client/TorrentsTunnel.h @@ -91,7 +91,8 @@ namespace torrents public: TorrentsTunnel (std::string_view name, std::shared_ptr localDestination, - std::string_view torrentsDir, std::string_view trackers = "", bool dht = true, bool web = true); + std::string_view torrentsDir, std::string_view trackers = "", bool dht = true, std::string_view dhtFileName = "", + bool web = true); void Start () override; void Stop () override;