Display External IP Address in status bar
[qBittorrent.git] / src / webui / api / transfercontroller.cpp
blob66ecb27470a5cac53bb7554c6648c6b3b421ed22
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2018 Vladimir Golovnev <glassez@yandex.ru>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
29 #include "transfercontroller.h"
31 #include <QJsonObject>
32 #include <QList>
34 #include "base/bittorrent/peeraddress.h"
35 #include "base/bittorrent/peerinfo.h"
36 #include "base/bittorrent/session.h"
37 #include "base/bittorrent/sessionstatus.h"
38 #include "base/global.h"
39 #include "base/utils/string.h"
40 #include "apierror.h"
42 const QString KEY_TRANSFER_DLSPEED = u"dl_info_speed"_s;
43 const QString KEY_TRANSFER_DLDATA = u"dl_info_data"_s;
44 const QString KEY_TRANSFER_DLRATELIMIT = u"dl_rate_limit"_s;
45 const QString KEY_TRANSFER_UPSPEED = u"up_info_speed"_s;
46 const QString KEY_TRANSFER_UPDATA = u"up_info_data"_s;
47 const QString KEY_TRANSFER_UPRATELIMIT = u"up_rate_limit"_s;
48 const QString KEY_TRANSFER_LAST_EXTERNAL_ADDRESS_V4 = u"last_external_address_v4"_s;
49 const QString KEY_TRANSFER_LAST_EXTERNAL_ADDRESS_V6 = u"last_external_address_v6"_s;
50 const QString KEY_TRANSFER_DHT_NODES = u"dht_nodes"_s;
51 const QString KEY_TRANSFER_CONNECTION_STATUS = u"connection_status"_s;
53 // Returns the global transfer information in JSON format.
54 // The return value is a JSON-formatted dictionary.
55 // The dictionary keys are:
56 // - "dl_info_speed": Global download rate
57 // - "dl_info_data": Data downloaded this session
58 // - "up_info_speed": Global upload rate
59 // - "up_info_data": Data uploaded this session
60 // - "dl_rate_limit": Download rate limit
61 // - "up_rate_limit": Upload rate limit
62 // - "last_external_address_v4": external IPv4 address
63 // - "last_external_address_v6": external IPv6 address
64 // - "dht_nodes": DHT nodes connected to
65 // - "connection_status": Connection status
66 void TransferController::infoAction()
68 const auto *btSession = BitTorrent::Session::instance();
69 const BitTorrent::SessionStatus &sessionStatus = btSession->status();
71 QJsonObject dict;
73 dict[KEY_TRANSFER_DLSPEED] = static_cast<qint64>(sessionStatus.payloadDownloadRate);
74 dict[KEY_TRANSFER_DLDATA] = static_cast<qint64>(sessionStatus.totalPayloadDownload);
75 dict[KEY_TRANSFER_UPSPEED] = static_cast<qint64>(sessionStatus.payloadUploadRate);
76 dict[KEY_TRANSFER_UPDATA] = static_cast<qint64>(sessionStatus.totalPayloadUpload);
77 dict[KEY_TRANSFER_DLRATELIMIT] = btSession->downloadSpeedLimit();
78 dict[KEY_TRANSFER_UPRATELIMIT] = btSession->uploadSpeedLimit();
79 dict[KEY_TRANSFER_LAST_EXTERNAL_ADDRESS_V4] = btSession->lastExternalIPv4Address();
80 dict[KEY_TRANSFER_LAST_EXTERNAL_ADDRESS_V6] = btSession->lastExternalIPv6Address();
81 dict[KEY_TRANSFER_DHT_NODES] = static_cast<qint64>(sessionStatus.dhtNodes);
82 if (!btSession->isListening())
83 dict[KEY_TRANSFER_CONNECTION_STATUS] = u"disconnected"_s;
84 else
85 dict[KEY_TRANSFER_CONNECTION_STATUS] = sessionStatus.hasIncomingConnections ? u"connected"_s : u"firewalled"_s;
87 setResult(dict);
90 void TransferController::uploadLimitAction()
92 setResult(QString::number(BitTorrent::Session::instance()->uploadSpeedLimit()));
95 void TransferController::downloadLimitAction()
97 setResult(QString::number(BitTorrent::Session::instance()->downloadSpeedLimit()));
100 void TransferController::setUploadLimitAction()
102 requireParams({u"limit"_s});
103 qlonglong limit = params()[u"limit"_s].toLongLong();
104 if (limit == 0) limit = -1;
106 BitTorrent::Session::instance()->setUploadSpeedLimit(limit);
109 void TransferController::setDownloadLimitAction()
111 requireParams({u"limit"_s});
112 qlonglong limit = params()[u"limit"_s].toLongLong();
113 if (limit == 0) limit = -1;
115 BitTorrent::Session::instance()->setDownloadSpeedLimit(limit);
118 void TransferController::toggleSpeedLimitsModeAction()
120 BitTorrent::Session *const session = BitTorrent::Session::instance();
121 session->setAltGlobalSpeedLimitEnabled(!session->isAltGlobalSpeedLimitEnabled());
124 void TransferController::speedLimitsModeAction()
126 setResult(QString::number(BitTorrent::Session::instance()->isAltGlobalSpeedLimitEnabled()));
129 void TransferController::setSpeedLimitsModeAction()
131 requireParams({u"mode"_s});
133 const std::optional<int> mode = Utils::String::parseInt(params().value(u"mode"_s));
134 if (!mode)
135 throw APIError(APIErrorType::BadParams, tr("'mode': invalid argument"));
137 // Any non-zero values are considered as alternative mode
138 BitTorrent::Session::instance()->setAltGlobalSpeedLimitEnabled(mode != 0);
141 void TransferController::banPeersAction()
143 requireParams({u"peers"_s});
145 const QStringList peers = params()[u"peers"_s].split(u'|');
146 for (const QString &peer : peers)
148 const BitTorrent::PeerAddress addr = BitTorrent::PeerAddress::parse(peer.trimmed());
149 if (!addr.ip.isNull())
150 BitTorrent::Session::instance()->banIP(addr.ip.toString());