mirror of
https://github.com/qbittorrent/qBittorrent.git
synced 2026-01-02 05:38:06 -06:00
Implement sync/torrent_peers request
This commit is contained in:
@@ -36,7 +36,9 @@
|
||||
#include "base/bittorrent/sessionstatus.h"
|
||||
#include "base/bittorrent/torrenthandle.h"
|
||||
#include "base/bittorrent/trackerentry.h"
|
||||
#include "base/bittorrent/peerinfo.h"
|
||||
#include "base/torrentfilter.h"
|
||||
#include "base/net/geoipmanager.h"
|
||||
#include "jsonutils.h"
|
||||
|
||||
#include <QDebug>
|
||||
@@ -105,6 +107,22 @@ static const char KEY_TORRENT_SUPER_SEEDING[] = "super_seeding";
|
||||
static const char KEY_TORRENT_FORCE_START[] = "force_start";
|
||||
static const char KEY_TORRENT_SAVE_PATH[] = "save_path";
|
||||
|
||||
// Peer keys
|
||||
static const char KEY_PEER_IP[] = "ip";
|
||||
static const char KEY_PEER_PORT[] = "port";
|
||||
static const char KEY_PEER_COUNTRY_CODE[] = "country_code";
|
||||
static const char KEY_PEER_COUNTRY[] = "country";
|
||||
static const char KEY_PEER_CLIENT[] = "client";
|
||||
static const char KEY_PEER_PROGRESS[] = "progress";
|
||||
static const char KEY_PEER_DOWN_SPEED[] = "dl_speed";
|
||||
static const char KEY_PEER_UP_SPEED[] = "up_speed";
|
||||
static const char KEY_PEER_TOT_DOWN[] = "downloaded";
|
||||
static const char KEY_PEER_TOT_UP[] = "uploaded";
|
||||
static const char KEY_PEER_CONNECTION_TYPE[] = "connection";
|
||||
static const char KEY_PEER_FLAGS[] = "flags";
|
||||
static const char KEY_PEER_FLAGS_DESCRIPTION[] = "flags_desc";
|
||||
static const char KEY_PEER_RELEVANCE[] = "relevance";
|
||||
|
||||
// Tracker keys
|
||||
static const char KEY_TRACKER_URL[] = "url";
|
||||
static const char KEY_TRACKER_STATUS[] = "status";
|
||||
@@ -171,6 +189,9 @@ static const char KEY_SYNC_MAINDATA_QUEUEING[] = "queueing";
|
||||
static const char KEY_SYNC_MAINDATA_USE_ALT_SPEED_LIMITS[] = "use_alt_speed_limits";
|
||||
static const char KEY_SYNC_MAINDATA_REFRESH_INTERVAL[] = "refresh_interval";
|
||||
|
||||
// Sync torrent peers keys
|
||||
static const char KEY_SYNC_TORRENT_PEERS_SHOW_FLAGS[] = "show_flags";
|
||||
|
||||
static const char KEY_FULL_UPDATE[] = "full_update";
|
||||
static const char KEY_RESPONSE_ID[] = "rid";
|
||||
static const char KEY_SUFFIX_REMOVED[] = "_removed";
|
||||
@@ -354,6 +375,54 @@ QByteArray btjson::getSyncMainData(int acceptedResponseId, QVariantMap &lastData
|
||||
return json::toJson(generateSyncData(acceptedResponseId, data, lastAcceptedData, lastData));
|
||||
}
|
||||
|
||||
QByteArray btjson::getSyncTorrentPeersData(int acceptedResponseId, QString hash, QVariantMap &lastData, QVariantMap &lastAcceptedData)
|
||||
{
|
||||
BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(hash);
|
||||
if (!torrent) {
|
||||
qWarning() << Q_FUNC_INFO << "Invalid torrent " << qPrintable(hash);
|
||||
return QByteArray();
|
||||
}
|
||||
|
||||
QVariantMap data;
|
||||
QVariantHash peers;
|
||||
QList<BitTorrent::PeerInfo> peersList = torrent->peers();
|
||||
#ifndef DISABLE_COUNTRIES_RESOLUTION
|
||||
bool resolvePeerCountries = Preferences::instance()->resolvePeerCountries();
|
||||
#else
|
||||
bool resolvePeerCountries = false;
|
||||
#endif
|
||||
|
||||
data[KEY_SYNC_TORRENT_PEERS_SHOW_FLAGS] = resolvePeerCountries;
|
||||
|
||||
foreach (const BitTorrent::PeerInfo &pi, peersList) {
|
||||
if (pi.address().ip.isNull()) continue;
|
||||
QVariantMap peer;
|
||||
#ifndef DISABLE_COUNTRIES_RESOLUTION
|
||||
if (resolvePeerCountries) {
|
||||
peer[KEY_PEER_COUNTRY_CODE] = pi.country().toLower();
|
||||
peer[KEY_PEER_COUNTRY] = Net::GeoIPManager::CountryName(pi.country());
|
||||
}
|
||||
#endif
|
||||
peer[KEY_PEER_IP] = pi.address().ip.toString();
|
||||
peer[KEY_PEER_PORT] = pi.address().port;
|
||||
peer[KEY_PEER_CLIENT] = pi.client();
|
||||
peer[KEY_PEER_PROGRESS] = pi.progress();
|
||||
peer[KEY_PEER_DOWN_SPEED] = pi.payloadDownSpeed();
|
||||
peer[KEY_PEER_UP_SPEED] = pi.payloadUpSpeed();
|
||||
peer[KEY_PEER_TOT_DOWN] = pi.totalDownload();
|
||||
peer[KEY_PEER_TOT_UP] = pi.totalUpload();
|
||||
peer[KEY_PEER_CONNECTION_TYPE] = pi.connectionType();
|
||||
peer[KEY_PEER_FLAGS] = pi.flags();
|
||||
peer[KEY_PEER_FLAGS_DESCRIPTION] = pi.flagsDescription();
|
||||
peer[KEY_PEER_RELEVANCE] = pi.relevance();
|
||||
peers[pi.address().ip.toString() + ":" + QString::number(pi.address().port)] = peer;
|
||||
}
|
||||
|
||||
data["peers"] = peers;
|
||||
|
||||
return json::toJson(generateSyncData(acceptedResponseId, data, lastAcceptedData, lastData));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the trackers for a torrent in JSON format.
|
||||
*
|
||||
@@ -610,7 +679,7 @@ QByteArray btjson::getTorrentsRatesLimits(QStringList &hashes, bool downloadLimi
|
||||
map[hash] = limit;
|
||||
}
|
||||
|
||||
return json::toJson(map);
|
||||
return json::toJson(map);
|
||||
}
|
||||
|
||||
QVariantMap toMap(BitTorrent::TorrentHandle *const torrent)
|
||||
|
||||
@@ -46,6 +46,7 @@ public:
|
||||
static QByteArray getTorrents(QString filter = "all", QString label = QString(),
|
||||
QString sortedColumn = "name", bool reverse = false, int limit = 0, int offset = 0);
|
||||
static QByteArray getSyncMainData(int acceptedResponseId, QVariantMap &lastData, QVariantMap &lastAcceptedData);
|
||||
static QByteArray getSyncTorrentPeersData(int acceptedResponseId, QString hash, QVariantMap &lastData, QVariantMap &lastAcceptedData);
|
||||
static QByteArray getTrackersForTorrent(const QString& hash);
|
||||
static QByteArray getWebSeedsForTorrent(const QString& hash);
|
||||
static QByteArray getPropertiesForTorrent(const QString& hash);
|
||||
|
||||
@@ -82,6 +82,7 @@ QMap<QString, QMap<QString, WebApplication::Action> > WebApplication::initialize
|
||||
ADD_ACTION(query, propertiesWebSeeds);
|
||||
ADD_ACTION(query, propertiesFiles);
|
||||
ADD_ACTION(sync, maindata);
|
||||
ADD_ACTION(sync, torrent_peers);
|
||||
ADD_ACTION(command, shutdown);
|
||||
ADD_ACTION(command, download);
|
||||
ADD_ACTION(command, upload);
|
||||
@@ -277,6 +278,19 @@ void WebApplication::action_sync_maindata()
|
||||
session()->syncMainDataLastAcceptedResponse), Http::CONTENT_TYPE_JSON);
|
||||
}
|
||||
|
||||
// GET param:
|
||||
// - hash (string): torrent hash
|
||||
// - rid (int): last response id
|
||||
void WebApplication::action_sync_torrent_peers()
|
||||
{
|
||||
CHECK_URI(0);
|
||||
print(btjson::getSyncTorrentPeersData(request().gets["rid"].toInt(),
|
||||
request().gets["hash"],
|
||||
session()->syncTorrentPeersLastResponse,
|
||||
session()->syncTorrentPeersLastAcceptedResponse), Http::CONTENT_TYPE_JSON);
|
||||
}
|
||||
|
||||
|
||||
void WebApplication::action_version_api()
|
||||
{
|
||||
CHECK_URI(0);
|
||||
|
||||
@@ -55,6 +55,7 @@ private:
|
||||
void action_query_propertiesWebSeeds();
|
||||
void action_query_propertiesFiles();
|
||||
void action_sync_maindata();
|
||||
void action_sync_torrent_peers();
|
||||
void action_command_shutdown();
|
||||
void action_command_download();
|
||||
void action_command_upload();
|
||||
|
||||
@@ -35,6 +35,8 @@ struct WebSessionData
|
||||
{
|
||||
QVariantMap syncMainDataLastResponse;
|
||||
QVariantMap syncMainDataLastAcceptedResponse;
|
||||
QVariantMap syncTorrentPeersLastResponse;
|
||||
QVariantMap syncTorrentPeersLastAcceptedResponse;
|
||||
};
|
||||
|
||||
#endif // WEBSESSIONDATA
|
||||
|
||||
Reference in New Issue
Block a user