Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions WebAPI_Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# WebAPI Changelog

## 2.15.1
* [#23708](https://github.com/qbittorrent/qBittorrent/pull/23708)
* `sync/torrentPeers` endpoint now includes peer `host_name` when peer host name resolution is enabled

## 2.14.1
* [#23212](https://github.com/qbittorrent/qBittorrent/pull/23212)
* Add `app/rotateAPIKey` endpoint for generating, and rotating, the WebAPI API key
Expand Down
2 changes: 2 additions & 0 deletions src/webui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ add_library(qbt_webui STATIC
api/transfercontroller.h
api/serialize/serialize_torrent.h
clientdatastorage.h
peerhostnameresolver.h
webapplication.h
webui.h

Expand All @@ -34,6 +35,7 @@ add_library(qbt_webui STATIC
api/transfercontroller.cpp
api/serialize/serialize_torrent.cpp
clientdatastorage.cpp
peerhostnameresolver.cpp
webapplication.cpp
webui.cpp
)
Expand Down
5 changes: 5 additions & 0 deletions src/webui/api/appcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,8 @@ void AppController::preferencesAction()
data[u"app_instance_name"_s] = app()->instanceName();
// Refresh interval
data[u"refresh_interval"_s] = session->refreshInterval();
// Resolve peer host names
data[u"resolve_peer_host_names"_s] = pref->resolvePeerHostNames();
// Resolve peer countries
data[u"resolve_peer_countries"_s] = pref->resolvePeerCountries();
// Reannounce to all trackers when ip/port changed
Expand Down Expand Up @@ -1029,6 +1031,9 @@ void AppController::setPreferencesAction()
// Refresh interval
if (hasKey(u"refresh_interval"_s))
session->setRefreshInterval(it.value().toInt());
// Resolve peer host names
if (hasKey(u"resolve_peer_host_names"_s))
pref->resolvePeerHostNames(it.value().toBool());
// Resolve peer countries
if (hasKey(u"resolve_peer_countries"_s))
pref->resolvePeerCountries(it.value().toBool());
Expand Down
12 changes: 10 additions & 2 deletions src/webui/api/synccontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#include "base/utils/string.h"
#include "apierror.h"
#include "serialize/serialize_torrent.h"
#include "webui/peerhostnameresolver.h"

namespace
{
Expand All @@ -70,6 +71,7 @@ namespace
const QString KEY_PEER_FILES = u"files"_s;
const QString KEY_PEER_FLAGS = u"flags"_s;
const QString KEY_PEER_FLAGS_DESCRIPTION = u"flags_desc"_s;
const QString KEY_PEER_HOST_NAME = u"host_name"_s;
const QString KEY_PEER_IP = u"ip"_s;
const QString KEY_PEER_I2P_DEST = u"i2p_dest"_s;
const QString KEY_PEER_PORT = u"port"_s;
Expand Down Expand Up @@ -441,8 +443,9 @@ namespace
}
}

SyncController::SyncController(IApplication *app, QObject *parent)
SyncController::SyncController(PeerHostNameResolver *peerHostNameResolver, IApplication *app, QObject *parent)
: APIController(app, parent)
, m_peerHostNameResolver {peerHostNameResolver}
{
}

Expand Down Expand Up @@ -839,7 +842,9 @@ void SyncController::torrentPeersAction()

const QList<BitTorrent::PeerInfo> peersList = torrent->fetchPeerInfo().takeResult();

bool resolvePeerCountries = Preferences::instance()->resolvePeerCountries();
const auto *pref = Preferences::instance();
const bool resolvePeerHostNames = pref->resolvePeerHostNames();
const bool resolvePeerCountries = pref->resolvePeerCountries();

data[KEY_SYNC_TORRENT_PEERS_SHOW_FLAGS] = resolvePeerCountries;

Expand Down Expand Up @@ -873,6 +878,9 @@ void SyncController::torrentPeersAction()
peer.insert(KEY_PEER_FILES, filesForPiece.join(u'\n'));
}

if (resolvePeerHostNames && !useI2PSocket)
peer[KEY_PEER_HOST_NAME] = m_peerHostNameResolver->lookupHostName(pi.address().ip);

if (resolvePeerCountries && !useI2PSocket)
{
peer[KEY_PEER_COUNTRY_CODE] = pi.country().toLower();
Expand Down
7 changes: 4 additions & 3 deletions src/webui/api/synccontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
#include "base/tag.h"
#include "apicontroller.h"

class PeerHostNameResolver;

namespace BitTorrent
{
class Torrent;
Expand All @@ -47,9 +49,7 @@ class SyncController : public APIController
Q_DISABLE_COPY_MOVE(SyncController)

public:
using APIController::APIController;

explicit SyncController(IApplication *app, QObject *parent = nullptr);
explicit SyncController(PeerHostNameResolver *peerHostNameResolver, IApplication *app, QObject *parent = nullptr);

public slots:
void updateFreeDiskSpace(qint64 freeDiskSpace);
Expand Down Expand Up @@ -83,6 +83,7 @@ private slots:
void onTorrentTrackerEntryStatusesUpdated(const BitTorrent::Torrent *torrent
, const QHash<QString, BitTorrent::TrackerEntryStatus> &updatedTrackers);

PeerHostNameResolver *m_peerHostNameResolver = nullptr;
qint64 m_freeDiskSpace = 0;

QVariantMap m_lastPeersResponse;
Expand Down
71 changes: 71 additions & 0 deletions src/webui/peerhostnameresolver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2026 Thomas Piccirello <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/

#include "peerhostnameresolver.h"

#include "base/net/reverseresolution.h"
#include "base/preferences.h"

PeerHostNameResolver::PeerHostNameResolver(QObject *parent)
: QObject(parent)
{
connect(Preferences::instance(), &Preferences::changed, this, &PeerHostNameResolver::updateState);
updateState();
}

QString PeerHostNameResolver::lookupHostName(const QHostAddress &ip)
{
const QString hostName = m_resolvedHosts.value(ip);
if (m_resolver && hostName.isEmpty())
m_resolver->resolve(ip);

return hostName;
}

void PeerHostNameResolver::onHostNameResolved(const QHostAddress &ip, const QString &hostname)
{
m_resolvedHosts.insert(ip, hostname);
}

void PeerHostNameResolver::updateState()
{
if (Preferences::instance()->resolvePeerHostNames())
{
if (!m_resolver)
{
m_resolver = new Net::ReverseResolution(this);
connect(m_resolver, &Net::ReverseResolution::ipResolved
, this, &PeerHostNameResolver::onHostNameResolved);
}
}
else
{
delete m_resolver;
m_resolver = nullptr;
}
}
57 changes: 57 additions & 0 deletions src/webui/peerhostnameresolver.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2026 Thomas Piccirello <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/

#pragma once

#include <QHash>
#include <QHostAddress>
#include <QObject>

namespace Net
{
class ReverseResolution;
}

class PeerHostNameResolver final : public QObject
{
Q_OBJECT
Q_DISABLE_COPY_MOVE(PeerHostNameResolver)

public:
explicit PeerHostNameResolver(QObject *parent = nullptr);

QString lookupHostName(const QHostAddress &ip);

private slots:
void onHostNameResolved(const QHostAddress &ip, const QString &hostname);
void updateState();

private:
Net::ReverseResolution *m_resolver = nullptr;
QHash<QHostAddress, QString> m_resolvedHosts;
};
4 changes: 3 additions & 1 deletion src/webui/webapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
#include "api/torrentscontroller.h"
#include "api/transfercontroller.h"
#include "clientdatastorage.h"
#include "peerhostnameresolver.h"

const int MAX_ALLOWED_FILESIZE = 10 * 1024 * 1024;
const QString SESSION_COOKIE_NAME_PREFIX = u"QBT_SID_"_s;
Expand Down Expand Up @@ -161,6 +162,7 @@ WebApplication::WebApplication(IApplication *app, QObject *parent)
, m_authController {new AuthController(this, app, this)}
, m_torrentCreationManager {new BitTorrent::TorrentCreationManager(app, this)}
, m_clientDataStorage {new ClientDataStorage(this)}
, m_peerHostNameResolver {new PeerHostNameResolver(this)}
{
declarePublicAPI(u"auth/login"_s);

Expand Down Expand Up @@ -838,7 +840,7 @@ void WebApplication::sessionStartImpl(const QString &sessionId, const bool useCo
m_currentSession->registerAPIController(u"transfer"_s, new TransferController(app(), m_currentSession));

const auto *btSession = BitTorrent::Session::instance();
auto *syncController = new SyncController(app(), m_currentSession);
auto *syncController = new SyncController(m_peerHostNameResolver, app(), m_currentSession);
syncController->updateFreeDiskSpace(btSession->freeDiskSpace());
connect(btSession, &BitTorrent::Session::freeDiskSpaceChecked, syncController, &SyncController::updateFreeDiskSpace);
m_currentSession->registerAPIController(u"sync"_s, syncController);
Expand Down
5 changes: 4 additions & 1 deletion src/webui/webapplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@

using namespace std::chrono_literals;

inline const Utils::Version<3, 2> API_VERSION {2, 15, 0};
inline const Utils::Version<3, 2> API_VERSION {2, 15, 1};

class APIController;
class AuthController;
Expand All @@ -69,6 +69,8 @@ namespace BitTorrent
class TorrentCreationManager;
}

class PeerHostNameResolver;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be moved to line 65. To group with them.


class WebSession final : public ApplicationComponent<QObject>, public ISession
{
public:
Expand Down Expand Up @@ -283,6 +285,7 @@ class WebApplication final : public ApplicationComponent<QObject>

BitTorrent::TorrentCreationManager *m_torrentCreationManager = nullptr;
ClientDataStorage *m_clientDataStorage = nullptr;
PeerHostNameResolver *m_peerHostNameResolver = nullptr;

struct FailedLogin
{
Expand Down
9 changes: 9 additions & 0 deletions src/webui/www/private/scripts/dynamicTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -1835,6 +1835,7 @@ window.qBittorrent.DynamicTable ??= (() => {
this.newColumn("files", "", "QBT_TR(Files)QBT_TR[CONTEXT=PeerListWidget]", 100, true);

this.columns["country"].dataProperties.push("country_code");
this.columns["ip"].dataProperties.push("host_name");
this.columns["flags"].dataProperties.push("flags_desc");
this.initColumnsFunctions();
}
Expand All @@ -1858,6 +1859,14 @@ window.qBittorrent.DynamicTable ??= (() => {
};

// ip
this.columns["ip"].updateTd = function(td, row) {
const ip = this.getRowValue(row, 0);
const hostname = this.getRowValue(row, 1);
// Display hostname if resolved, otherwise display IP
const displayValue = hostname || ip;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems hostname is always a string then I would suggest the following to avoid 'implicit conversion to bool':

Suggested change
const displayValue = hostname || ip;
const displayValue = (hostname.length === 0) || ip;

td.textContent = displayValue;
td.title = displayValue;
};
this.columns["ip"].compareRows = function(row1, row2) {
const ip1 = this.getRowValue(row1);
const ip2 = this.getRowValue(row2);
Expand Down
10 changes: 10 additions & 0 deletions src/webui/www/private/views/preferences.html
Original file line number Diff line number Diff line change
Expand Up @@ -1276,6 +1276,14 @@
<input type="text" id="refreshInterval" style="width: 15em;" title="QBT_TR(It controls the internal state update interval which in turn will affect UI updates)QBT_TR[CONTEXT=OptionsDialog]">&nbsp;&nbsp;QBT_TR(ms)QBT_TR[CONTEXT=OptionsDialog]
</td>
</tr>
<tr>
<td>
<label for="resolvePeerHostNames">QBT_TR(Resolve peer host names:)QBT_TR[CONTEXT=OptionsDialog]</label>
</td>
<td>
<input type="checkbox" id="resolvePeerHostNames">
</td>
</tr>
<tr>
<td>
<label for="resolvePeerCountries">QBT_TR(Resolve peer countries:)QBT_TR[CONTEXT=OptionsDialog]</label>
Expand Down Expand Up @@ -2641,6 +2649,7 @@
document.getElementById("recheckTorrentsOnCompletion").checked = pref.recheck_completed_torrents;
document.getElementById("appInstanceName").value = pref.app_instance_name;
document.getElementById("refreshInterval").value = pref.refresh_interval;
document.getElementById("resolvePeerHostNames").checked = pref.resolve_peer_host_names;
document.getElementById("resolvePeerCountries").checked = pref.resolve_peer_countries;
document.getElementById("reannounceWhenAddressChanged").checked = pref.reannounce_when_address_changed;
document.getElementById("enableEmbeddedTracker").checked = pref.enable_embedded_tracker;
Expand Down Expand Up @@ -3125,6 +3134,7 @@
settings["recheck_completed_torrents"] = document.getElementById("recheckTorrentsOnCompletion").checked;
settings["app_instance_name"] = document.getElementById("appInstanceName").value;
settings["refresh_interval"] = Number(document.getElementById("refreshInterval").value);
settings["resolve_peer_host_names"] = document.getElementById("resolvePeerHostNames").checked;
settings["resolve_peer_countries"] = document.getElementById("resolvePeerCountries").checked;
settings["reannounce_when_address_changed"] = document.getElementById("reannounceWhenAddressChanged").checked;
settings["enable_embedded_tracker"] = document.getElementById("enableEmbeddedTracker").checked;
Expand Down
Loading