diff --git a/libi2pd_client/TorrentsRPC.cpp b/libi2pd_client/TorrentsRPC.cpp index f15c197d..22c7b39c 100644 --- a/libi2pd_client/TorrentsRPC.cpp +++ b/libi2pd_client/TorrentsRPC.cpp @@ -67,6 +67,7 @@ namespace torrents std::string HandleTorrentStop (boost::json::object&& jsonRequest); std::string HandleTorrentStart (boost::json::object&& jsonRequest); std::string HandleSessionGet (boost::json::object&& jsonRequest); // for transmission-rpc library + std::string HandleSessionSet (boost::json::object&& jsonRequest); std::string HandleSessionStats (boost::json::object&& jsonRequest); std::string HandleTorrentSet (boost::json::object&& jsonRequest); std::string HandleTorrentReannounce (boost::json::object&& jsonRequest); @@ -127,6 +128,7 @@ namespace torrents { "torrent-stop", &JSONRPCHandler::HandleTorrentStop }, { "torrent-start", &JSONRPCHandler::HandleTorrentStart }, { "session-get", &JSONRPCHandler::HandleSessionGet }, + { "session-set", &JSONRPCHandler::HandleSessionSet }, { "session-stats", &JSONRPCHandler::HandleSessionStats }, { "torrent-set", &JSONRPCHandler::HandleTorrentSet }, { "torrent-reannounce", &JSONRPCHandler::HandleTorrentReannounce }, @@ -226,6 +228,21 @@ namespace torrents return SuccessResponse (GetTag (jsonRequest), std::move (response)); } + std::string JSONRPCHandler::HandleSessionSet (boost::json::object&& jsonRequest) + { + boost::json::object response; // always empty + auto arguments = jsonRequest.at ("arguments").as_object (); + if (arguments.contains ("dht-enabled")) + { + auto dhtEnabled = arguments.at ("dht-enabled").as_bool (); + if (dhtEnabled) + m_Tunnel->StartDHT (); + else + m_Tunnel->StopDHT (); + } + return SuccessResponse (GetTag (jsonRequest), std::move (response)); + } + std::string JSONRPCHandler::HandleTorrentAdd (boost::json::object&& jsonRequest) { auto arguments = jsonRequest.at ("arguments").as_object (); @@ -1053,7 +1070,7 @@ namespace torrents } std::string contentType (""); - auto filePath = std::filesystem::path (i2p::fs::GetDataDir ()) / "torrents" / "web"; + auto filePath = i2p::fs::GetDataDir () / "torrents" / "web"; if (fileSubUrl.empty ()) { filePath /= "index.html"; diff --git a/libi2pd_client/TorrentsTunnel.cpp b/libi2pd_client/TorrentsTunnel.cpp index 4c640d53..ec8c424a 100644 --- a/libi2pd_client/TorrentsTunnel.cpp +++ b/libi2pd_client/TorrentsTunnel.cpp @@ -29,7 +29,7 @@ namespace torrents { TorrentsTunnel::TorrentsTunnel (std::string_view name, std::shared_ptr localDestination, 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-"), + i2p::client::I2PService (localDestination), m_Name (name), m_DHTFileName (dhtFileName), m_PeerID ("-I2PD-"), m_TorrentsDir (torrentsDir), m_TrackerRequestsCheckTimer (GetService ()), m_KeepAliveCheckTimer (GetService ()), m_ReconnectCheckTimer (GetService ()), m_TorrentsStatusUpdateTimer (GetService ()), m_Web (web) @@ -64,7 +64,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), dhtFileName); + m_DHT = std::make_unique(*this, TORRENT_PORT + (localDestination ? localDestination->GetRng()() % (65535 - TORRENT_PORT - 1) : 1), m_DHTFileName); } void TorrentsTunnel::Start () @@ -1365,6 +1365,31 @@ namespace torrents }); } + void TorrentsTunnel::StartDHT () + { + boost::asio::post (GetService (), [this]() + { + if (!m_DHT) + { + m_DHT = std::make_unique(*this, TORRENT_PORT + (GetLocalDestination () ? + GetLocalDestination ()->GetRng()() % (65535 - TORRENT_PORT - 1) : 1), m_DHTFileName); + m_DHT->Start (); + } + }); + } + + void TorrentsTunnel::StopDHT () + { + boost::asio::post (GetService (), [this]() + { + if (m_DHT) + { + m_DHT->Stop (); + m_DHT = nullptr; + } + }); + } + void TorrentsTunnel::StartVerifingTorrent (std::shared_ptr torrent) { if (!torrent) return; diff --git a/libi2pd_client/TorrentsTunnel.h b/libi2pd_client/TorrentsTunnel.h index 44826223..642fa1d1 100644 --- a/libi2pd_client/TorrentsTunnel.h +++ b/libi2pd_client/TorrentsTunnel.h @@ -126,6 +126,8 @@ namespace torrents std::pair GetDHTPorts () const { return { m_DHT ? m_DHT->GetPort () : 0, m_DHT ? m_DHT->GetRPort () : 0 }; } bool SupportsDHT () const { return (bool)m_DHT; } const std::unique_ptr& GetDHT () const { return m_DHT; } + void StartDHT (); + void StopDHT (); bool SupportsWeb () const { return m_Web; } const char* GetName() const override { return m_Name.c_str (); } @@ -182,7 +184,7 @@ namespace torrents private: - std::string m_Name, m_PeerID; // 20 characters + std::string m_Name, m_DHTFileName, m_PeerID; // 20 characters std::filesystem::path m_TorrentsDir; std::vector m_Trackers; std::map > m_Torrents;