From 0fa28f233fe271c36c7b5e4bd9fa7df166361ea0 Mon Sep 17 00:00:00 2001 From: Thomas Piccirello Date: Sun, 13 Jan 2019 21:40:15 -0800 Subject: [PATCH] Add ability to add and ban a peer from the Web UI --- src/webui/api/torrentscontroller.cpp | 37 +++++++++++ src/webui/api/torrentscontroller.h | 1 + src/webui/api/transfercontroller.cpp | 17 +++++ src/webui/api/transfercontroller.h | 1 + src/webui/www/private/addpeers.html | 69 +++++++++++++++++++++ src/webui/www/private/index.html | 5 ++ src/webui/www/private/scripts/prop-peers.js | 67 +++++++++++++++++++- src/webui/www/webui.qrc | 1 + 8 files changed, 197 insertions(+), 1 deletion(-) create mode 100644 src/webui/www/private/addpeers.html diff --git a/src/webui/api/torrentscontroller.cpp b/src/webui/api/torrentscontroller.cpp index 0a965bf28..40d05b4bb 100644 --- a/src/webui/api/torrentscontroller.cpp +++ b/src/webui/api/torrentscontroller.cpp @@ -40,6 +40,7 @@ #include #include "base/bittorrent/downloadpriority.h" +#include "base/bittorrent/peeraddress.h" #include "base/bittorrent/peerinfo.h" #include "base/bittorrent/session.h" #include "base/bittorrent/torrenthandle.h" @@ -682,6 +683,42 @@ void TorrentsController::removeTrackersAction() torrent->forceReannounce(); } +void TorrentsController::addPeersAction() +{ + checkParams({"hashes", "peers"}); + + const QStringList hashes = params()["hashes"].split('|'); + const QStringList peers = params()["peers"].split('|'); + + QVector peerList; + peerList.reserve(peers.size()); + for (const QString &peer : peers) { + const BitTorrent::PeerAddress addr = BitTorrent::PeerAddress::parse(peer.trimmed()); + if (!addr.ip.isNull()) + peerList.append(addr); + } + + if (peerList.isEmpty()) + throw APIError(APIErrorType::BadParams, "No valid peers were specified"); + + QJsonObject results; + + applyToTorrents(hashes, [peers, peerList, &results](BitTorrent::TorrentHandle *const torrent) + { + const int peersAdded = std::count_if(peerList.cbegin(), peerList.cend(), [torrent](const BitTorrent::PeerAddress &peer) + { + return torrent->connectPeer(peer); + }); + + results[torrent->hash()] = QJsonObject { + {"added", peersAdded}, + {"failed", (peers.size() - peersAdded)} + }; + }); + + setResult(results); +} + void TorrentsController::pauseAction() { checkParams({"hashes"}); diff --git a/src/webui/api/torrentscontroller.h b/src/webui/api/torrentscontroller.h index 61bf4e14f..36e75328b 100644 --- a/src/webui/api/torrentscontroller.h +++ b/src/webui/api/torrentscontroller.h @@ -66,6 +66,7 @@ private slots: void addTrackersAction(); void editTrackerAction(); void removeTrackersAction(); + void addPeersAction(); void filePrioAction(); void uploadLimitAction(); void downloadLimitAction(); diff --git a/src/webui/api/transfercontroller.cpp b/src/webui/api/transfercontroller.cpp index d623ad79b..ea95f144a 100644 --- a/src/webui/api/transfercontroller.cpp +++ b/src/webui/api/transfercontroller.cpp @@ -29,8 +29,13 @@ #include "transfercontroller.h" #include +#include +#include "base/bittorrent/peeraddress.h" +#include "base/bittorrent/peerinfo.h" #include "base/bittorrent/session.h" +#include "base/global.h" +#include "apierror.h" const char KEY_TRANSFER_DLSPEED[] = "dl_info_speed"; const char KEY_TRANSFER_DLDATA[] = "dl_info_data"; @@ -111,3 +116,15 @@ void TransferController::speedLimitsModeAction() { setResult(QString::number(BitTorrent::Session::instance()->isAltGlobalSpeedLimitEnabled())); } + +void TransferController::banPeersAction() +{ + checkParams({"peers"}); + + const QStringList peers = params()["peers"].split('|'); + for (const QString &peer : peers) { + const BitTorrent::PeerAddress addr = BitTorrent::PeerAddress::parse(peer.trimmed()); + if (!addr.ip.isNull()) + BitTorrent::Session::instance()->banIP(addr.ip.toString()); + } +} diff --git a/src/webui/api/transfercontroller.h b/src/webui/api/transfercontroller.h index c6ffe713e..5b3ce4fb0 100644 --- a/src/webui/api/transfercontroller.h +++ b/src/webui/api/transfercontroller.h @@ -46,4 +46,5 @@ private slots: void downloadLimitAction(); void setUploadLimitAction(); void setDownloadLimitAction(); + void banPeersAction(); }; diff --git a/src/webui/www/private/addpeers.html b/src/webui/www/private/addpeers.html new file mode 100644 index 000000000..e90ac099c --- /dev/null +++ b/src/webui/www/private/addpeers.html @@ -0,0 +1,69 @@ + + + + + QBT_TR(Add Peers)QBT_TR[CONTEXT=PeersAdditionDialog] + + + + + + + +
+

QBT_TR(List of peers to add (one IP per line):)QBT_TR[CONTEXT=PeersAdditionDialog]

+ +
+ + +
+
+ + diff --git a/src/webui/www/private/index.html b/src/webui/www/private/index.html index 89fc2c68e..cd6334e7c 100644 --- a/src/webui/www/private/index.html +++ b/src/webui/www/private/index.html @@ -188,6 +188,11 @@
  • QBT_TR(Remove tracker)QBT_TR[CONTEXT=TrackerListWidget] QBT_TR(Remove tracker)QBT_TR[CONTEXT=TrackerListWidget]
  • QBT_TR(Copy tracker URL)QBT_TR[CONTEXT=TrackerListWidget] QBT_TR(Copy tracker URL)QBT_TR[CONTEXT=TrackerListWidget]
  • +
    • QBT_TR(Priority)QBT_TR[CONTEXT=PropertiesWidget] diff --git a/src/webui/www/private/scripts/prop-peers.js b/src/webui/www/private/scripts/prop-peers.js index 164077492..08f10f5f4 100644 --- a/src/webui/www/private/scripts/prop-peers.js +++ b/src/webui/www/private/scripts/prop-peers.js @@ -103,4 +103,69 @@ updateTorrentPeersData = function() { loadTorrentPeersData(); }; -torrentPeersTable.setup('torrentPeersTableDiv', 'torrentPeersTableFixedHeaderDiv', null); +const torrentPeersContextMenu = new ContextMenu({ + targets: '#torrentPeersTableDiv', + menu: 'torrentPeersMenu', + actions: { + addPeer: function(element, ref) { + const hash = torrentsTable.getCurrentTorrentHash(); + if (!hash) + return; + + new MochaUI.Window({ + id: 'addPeersPage', + title: "QBT_TR(Add Peers)QBT_TR[CONTEXT=PeersAdditionDialog]", + loadMethod: 'iframe', + contentURL: 'addpeers.html?hash=' + hash, + scrollbars: false, + resizable: false, + maximizable: false, + paddingVertical: 0, + paddingHorizontal: 0, + width: 350, + height: 240 + }); + }, + banPeer: function(element, ref) { + const selectedPeers = torrentPeersTable.selectedRowsIds(); + if (selectedPeers.length === 0) + return; + + if (confirm('QBT_TR(Are you sure you want to permanently ban the selected peers?)QBT_TR[CONTEXT=PeerListWidget]')) { + new Request({ + url: 'api/v2/torrents/banPeers', + noCache: true, + method: 'post', + data: { + hash: torrentsTable.getCurrentTorrentHash(), + peers: selectedPeers.join('|') + } + }).send(); + } + } + }, + offsets: { + x: -15, + y: 2 + }, + onShow: function() { + const selectedPeers = torrentPeersTable.selectedRowsIds(); + + if (selectedPeers.length >= 1) { + this.showItem('copyPeer'); + this.showItem('banPeer'); + } + else { + this.hideItem('copyPeer'); + this.hideItem('banPeer'); + } + } +}); + +new ClipboardJS('#CopyPeerInfo', { + text: function(trigger) { + return torrentPeersTable.selectedRowsIds().join("\n"); + } +}); + +torrentPeersTable.setup('torrentPeersTableDiv', 'torrentPeersTableFixedHeaderDiv', torrentPeersContextMenu); diff --git a/src/webui/www/webui.qrc b/src/webui/www/webui.qrc index 0e8270e93..ad67c0b3e 100644 --- a/src/webui/www/webui.qrc +++ b/src/webui/www/webui.qrc @@ -2,6 +2,7 @@ private/about.html private/aboutToolbar.html + private/addpeers.html private/addtrackers.html private/confirmdeletion.html private/css/Core.css