turn on/turn off DHT through RPC

This commit is contained in:
orignal committed 2026-11-15 18:46:37 -05:00
1 parent a467aaae08
commit 07e01ca79c
3 files changed
+48 -4

No files matched your search

+18 -1
View File
@@ -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";
+27 -2
View File
@@ -29,7 +29,7 @@ namespace torrents
{
TorrentsTunnel::TorrentsTunnel (std::string_view name, std::shared_ptr<i2p::client::ClientDestination> 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<TorrentsDHT>(*this, TORRENT_PORT + (localDestination ? localDestination->GetRng()() % (65535 - TORRENT_PORT - 1) : 1), dhtFileName);
m_DHT = std::make_unique<TorrentsDHT>(*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<TorrentsDHT>(*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> torrent)
{
if (!torrent) return;
+3 -1
View File
@@ -126,6 +126,8 @@ namespace torrents
std::pair<uint16_t, uint16_t> 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<TorrentsDHT>& 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<TrackerInfo> m_Trackers;
std::map<Torrent::InfoHash, std::shared_ptr<Torrent> > m_Torrents;