From 4947b0a44f1708ec6bf0a7a2b14a8fb61038a3d3 Mon Sep 17 00:00:00 2001 From: Thomas Piccirello Date: Mon, 20 Aug 2018 01:27:25 -0400 Subject: [PATCH] Add DHT, PeX, and LSD to WebUI Tracker list --- src/webui/api/torrentscontroller.cpp | 70 +++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/src/webui/api/torrentscontroller.cpp b/src/webui/api/torrentscontroller.cpp index f7f3d6994..696121dbf 100644 --- a/src/webui/api/torrentscontroller.cpp +++ b/src/webui/api/torrentscontroller.cpp @@ -40,6 +40,7 @@ #include #include "base/bittorrent/filepriority.h" +#include "base/bittorrent/peerinfo.h" #include "base/bittorrent/session.h" #include "base/bittorrent/torrenthandle.h" #include "base/bittorrent/torrentinfo.h" @@ -129,6 +130,72 @@ namespace } } } + + QVariantList getStickyTrackers(const BitTorrent::TorrentHandle *const torrent) + { + uint seedsDHT = 0, seedsPeX = 0, seedsLSD = 0, leechesDHT = 0, leechesPeX = 0, leechesLSD = 0; + for (const BitTorrent::PeerInfo &peer : torrent->peers()) { + if (peer.isConnecting()) continue; + + if (peer.isSeed()) { + if (peer.fromDHT()) + ++seedsDHT; + if (peer.fromPeX()) + ++seedsPeX; + if (peer.fromLSD()) + ++seedsLSD; + } + else { + if (peer.fromDHT()) + ++leechesDHT; + if (peer.fromPeX()) + ++leechesPeX; + if (peer.fromLSD()) + ++leechesLSD; + } + } + + const int working = static_cast(BitTorrent::TrackerEntry::Working); + const int disabled = 0; + + const QString privateMsg {QCoreApplication::translate("TrackerListWidget", "This torrent is private")}; + const bool isTorrentPrivate = torrent->isPrivate(); + + const QVariantMap dht { + {KEY_TRACKER_URL, "** [DHT] **"}, + {KEY_TRACKER_TIER, ""}, + {KEY_TRACKER_MSG, (isTorrentPrivate ? privateMsg : "")}, + {KEY_TRACKER_STATUS, ((BitTorrent::Session::instance()->isDHTEnabled() && !isTorrentPrivate) ? working : disabled)}, + {KEY_TRACKER_PEERS_COUNT, 0}, + {KEY_TRACKER_DOWNLOADED_COUNT, 0}, + {KEY_TRACKER_SEEDS_COUNT, seedsDHT}, + {KEY_TRACKER_LEECHES_COUNT, leechesDHT} + }; + + const QVariantMap pex { + {KEY_TRACKER_URL, "** [PeX] **"}, + {KEY_TRACKER_TIER, ""}, + {KEY_TRACKER_MSG, (isTorrentPrivate ? privateMsg : "")}, + {KEY_TRACKER_STATUS, ((BitTorrent::Session::instance()->isPeXEnabled() && !isTorrentPrivate) ? working : disabled)}, + {KEY_TRACKER_PEERS_COUNT, 0}, + {KEY_TRACKER_DOWNLOADED_COUNT, 0}, + {KEY_TRACKER_SEEDS_COUNT, seedsPeX}, + {KEY_TRACKER_LEECHES_COUNT, leechesPeX} + }; + + const QVariantMap lsd { + {KEY_TRACKER_URL, "** [LSD] **"}, + {KEY_TRACKER_TIER, ""}, + {KEY_TRACKER_MSG, (isTorrentPrivate ? privateMsg : "")}, + {KEY_TRACKER_STATUS, ((BitTorrent::Session::instance()->isLSDEnabled() && !isTorrentPrivate) ? working : disabled)}, + {KEY_TRACKER_PEERS_COUNT, 0}, + {KEY_TRACKER_DOWNLOADED_COUNT, 0}, + {KEY_TRACKER_SEEDS_COUNT, seedsLSD}, + {KEY_TRACKER_LEECHES_COUNT, leechesLSD} + }; + + return QVariantList {dht, pex, lsd}; + } } // Returns all the torrents in JSON format. @@ -310,11 +377,12 @@ void TorrentsController::trackersAction() checkParams({"hash"}); const QString hash {params()["hash"]}; - QVariantList trackerList; BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(hash); if (!torrent) throw APIError(APIErrorType::NotFound); + QVariantList trackerList = getStickyTrackers(torrent); + QHash trackersData = torrent->trackerInfos(); for (const BitTorrent::TrackerEntry &tracker : asConst(torrent->trackers())) { QVariantMap trackerDict;