Support fetching tracker list from URL

Trackers specified at the URL will be added to newly added public torrents.
This commit is contained in:
Thomas Piccirello 2024-11-12 22:00:36 -03:00
parent 33e3fb2f46
commit 2e1afc4f98
No known key found for this signature in database
7 changed files with 261 additions and 0 deletions

View file

@ -237,6 +237,14 @@ namespace BitTorrent
virtual Path finishedTorrentExportDirectory() const = 0;
virtual void setFinishedTorrentExportDirectory(const Path &path) = 0;
virtual bool isAddTrackersFromURLEnabled() const = 0;
virtual void setAddTrackersFromURLEnabled(bool enabled) = 0;
virtual QString additionalTrackersURL() const = 0;
virtual void setAdditionalTrackersURL(const QString &url) = 0;
virtual QString additionalTrackersFromURL() const = 0;
virtual void setAdditionalTrackersFromURL(const QString &trackers) = 0;
virtual void updateTrackersFromURL() = 0;
virtual int globalDownloadSpeedLimit() const = 0;
virtual void setGlobalDownloadSpeedLimit(int limit) = 0;
virtual int globalUploadSpeedLimit() const = 0;

View file

@ -77,6 +77,7 @@
#include "base/algorithm.h"
#include "base/global.h"
#include "base/logger.h"
#include "base/net/downloadmanager.h"
#include "base/net/proxyconfigurationmanager.h"
#include "base/preferences.h"
#include "base/profile.h"
@ -460,6 +461,9 @@ SessionImpl::SessionImpl(QObject *parent)
, m_blockPeersOnPrivilegedPorts(BITTORRENT_SESSION_KEY(u"BlockPeersOnPrivilegedPorts"_s), false)
, m_isAddTrackersEnabled(BITTORRENT_SESSION_KEY(u"AddTrackersEnabled"_s), false)
, m_additionalTrackers(BITTORRENT_SESSION_KEY(u"AdditionalTrackers"_s))
, m_isAddTrackersFromURLEnabled(BITTORRENT_SESSION_KEY(u"AddTrackersFromURLEnabled"_s), false)
, m_additionalTrackersURL(BITTORRENT_SESSION_KEY(u"AdditionalTrackersURL"_s))
, m_additionalTrackersFromURL(BITTORRENT_SESSION_KEY(u"AdditionalTrackersFromURL"_s))
, m_globalMaxRatio(BITTORRENT_SESSION_KEY(u"GlobalMaxRatio"_s), -1, [](qreal r) { return r < 0 ? -1. : r;})
, m_globalMaxSeedingMinutes(BITTORRENT_SESSION_KEY(u"GlobalMaxSeedingMinutes"_s), -1, lowerLimited(-1))
, m_globalMaxInactiveSeedingMinutes(BITTORRENT_SESSION_KEY(u"GlobalMaxInactiveSeedingMinutes"_s), -1, lowerLimited(-1))
@ -584,6 +588,7 @@ SessionImpl::SessionImpl(QObject *parent)
updateSeedingLimitTimer();
populateAdditionalTrackers();
populateAdditionalTrackersFromURL();
if (isExcludedFileNamesEnabled())
populateExcludedFileNamesRegExpList();
@ -614,6 +619,14 @@ SessionImpl::SessionImpl(QObject *parent)
enableTracker(isTrackerEnabled());
prepareStartup();
m_updateTrackersFromURLTimer = new QTimer(this);
m_updateTrackersFromURLTimer->setInterval(86400*1000);
connect(m_updateTrackersFromURLTimer, &QTimer::timeout, this, &SessionImpl::updateTrackersFromURL);
if (isAddTrackersFromURLEnabled()) {
m_updateTrackersFromURLTimer->start();
updateTrackersFromURL();
}
}
SessionImpl::~SessionImpl()
@ -2261,6 +2274,11 @@ void SessionImpl::populateAdditionalTrackers()
m_additionalTrackerEntries = parseTrackerEntries(additionalTrackers());
}
void SessionImpl::populateAdditionalTrackersFromURL()
{
m_additionalTrackerEntriesFromURL = parseTrackerEntries(additionalTrackersFromURL());
}
void SessionImpl::processTorrentShareLimits(TorrentImpl *torrent)
{
if (!torrent->isFinished() || torrent->isForced())
@ -2880,6 +2898,21 @@ bool SessionImpl::addTorrent_impl(const TorrentDescriptor &source, const AddTorr
}
}
if (isAddTrackersFromURLEnabled() && !(hasMetadata && p.ti->priv()))
{
const auto maxTierIter = std::max_element(p.tracker_tiers.cbegin(), p.tracker_tiers.cend());
const int baseTier = (maxTierIter != p.tracker_tiers.cend()) ? (*maxTierIter + 1) : 0;
p.trackers.reserve(p.trackers.size() + static_cast<std::size_t>(m_additionalTrackerEntriesFromURL.size()));
p.tracker_tiers.reserve(p.trackers.size() + static_cast<std::size_t>(m_additionalTrackerEntriesFromURL.size()));
p.tracker_tiers.resize(p.trackers.size(), 0);
for (const TrackerEntry &trackerEntry : asConst(m_additionalTrackerEntriesFromURL))
{
p.trackers.emplace_back(trackerEntry.url.toStdString());
p.tracker_tiers.emplace_back(Utils::Number::clampingAdd(trackerEntry.tier, baseTier));
}
}
p.upload_limit = addTorrentParams.uploadLimit;
p.download_limit = addTorrentParams.downloadLimit;
@ -3872,6 +3905,85 @@ void SessionImpl::setAdditionalTrackers(const QString &trackers)
populateAdditionalTrackers();
}
bool SessionImpl::isAddTrackersFromURLEnabled() const
{
return m_isAddTrackersFromURLEnabled;
}
void SessionImpl::setAddTrackersFromURLEnabled(bool enabled)
{
if (enabled != isAddTrackersFromURLEnabled())
{
m_isAddTrackersFromURLEnabled = enabled;
if (enabled)
{
m_updateTrackersFromURLTimer->start();
updateTrackersFromURL();
}
else
{
m_updateTrackersFromURLTimer->stop();
setAdditionalTrackersFromURL(u""_s);
}
}
}
QString SessionImpl::additionalTrackersURL() const
{
return m_additionalTrackersURL;
}
void SessionImpl::setAdditionalTrackersURL(const QString &url)
{
if (url != additionalTrackersURL())
{
m_additionalTrackersURL = url.trimmed();
updateTrackersFromURL();
}
}
QString SessionImpl::additionalTrackersFromURL() const
{
return m_additionalTrackersFromURL;
}
void SessionImpl::setAdditionalTrackersFromURL(const QString &trackers)
{
if (trackers != additionalTrackersFromURL())
{
m_additionalTrackersFromURL = trackers;
populateAdditionalTrackersFromURL();
}
}
void SessionImpl::updateTrackersFromURL()
{
const QString &url = additionalTrackersURL();
if (url.isEmpty())
{
setAdditionalTrackersFromURL(u""_s);
}
else
{
Preferences *const pref = Preferences::instance();
Net::DownloadManager::instance()->download(Net::DownloadRequest(url)
, pref->useProxyForGeneralPurposes(), this, &SessionImpl::handleTrackersFromURLDownloadFinished);
}
}
void SessionImpl::handleTrackersFromURLDownloadFinished(const Net::DownloadResult &result)
{
if (result.status == Net::DownloadStatus::Success)
{
setAdditionalTrackersFromURL(QString::fromUtf8(result.data.data()));
LogMsg(tr("Public tracker list updated"), Log::INFO);
}
else
{
LogMsg(tr("Failed to update public tracker list. Reason: \"%1\"").arg(result.errorString), Log::WARNING);
}
}
bool SessionImpl::isIPFilteringEnabled() const
{
return m_isIPFilteringEnabled;

View file

@ -46,6 +46,7 @@
#include <QSet>
#include <QThreadPool>
#include "base/net/downloadmanager.h"
#include "base/path.h"
#include "base/settingvalue.h"
#include "base/utils/thread.h"
@ -490,6 +491,14 @@ namespace BitTorrent
m_asyncWorker->start(std::forward<Func>(func));
}
bool isAddTrackersFromURLEnabled() const override;
void setAddTrackersFromURLEnabled(bool enabled) override;
QString additionalTrackersURL() const override;
void setAdditionalTrackersURL(const QString &url) override;
QString additionalTrackersFromURL() const override;
void setAdditionalTrackersFromURL(const QString &trackers) override;
void updateTrackersFromURL() override;
signals:
void addTorrentAlertsReceived(qsizetype count);
@ -502,6 +511,7 @@ namespace BitTorrent
void handleIPFilterError();
void fileSearchFinished(const TorrentID &id, const Path &savePath, const PathList &fileNames);
void torrentContentRemovingFinished(const QString &torrentName, const QString &errorMessage);
void handleTrackersFromURLDownloadFinished(const Net::DownloadResult &result);
private:
struct ResumeSessionContext;
@ -596,6 +606,8 @@ namespace BitTorrent
void saveTorrentsQueue();
void removeTorrentsQueue();
void populateAdditionalTrackersFromURL();
std::vector<lt::alert *> getPendingAlerts(lt::time_duration time = lt::time_duration::zero()) const;
void moveTorrentStorage(const MoveStorageJob &job) const;
@ -676,6 +688,9 @@ namespace BitTorrent
CachedSettingValue<bool> m_blockPeersOnPrivilegedPorts;
CachedSettingValue<bool> m_isAddTrackersEnabled;
CachedSettingValue<QString> m_additionalTrackers;
CachedSettingValue<bool> m_isAddTrackersFromURLEnabled;
CachedSettingValue<QString> m_additionalTrackersURL;
CachedSettingValue<QString> m_additionalTrackersFromURL;
CachedSettingValue<qreal> m_globalMaxRatio;
CachedSettingValue<int> m_globalMaxSeedingMinutes;
CachedSettingValue<int> m_globalMaxInactiveSeedingMinutes;
@ -749,6 +764,8 @@ namespace BitTorrent
bool m_IPFilteringConfigured = false;
mutable bool m_listenInterfaceConfigured = false;
QTimer *m_updateTrackersFromURLTimer;
bool m_isRestored = false;
bool m_isPaused = isStartPaused();
@ -759,6 +776,7 @@ namespace BitTorrent
int m_numResumeData = 0;
QList<TrackerEntry> m_additionalTrackerEntries;
QList<TrackerEntry> m_additionalTrackerEntriesFromURL;
QList<QRegularExpression> m_excludedFileNamesRegExpList;
// Statistics

View file

@ -53,6 +53,7 @@
#include "base/bittorrent/sharelimitaction.h"
#include "base/exceptions.h"
#include "base/global.h"
#include "base/net/downloadmanager.h"
#include "base/net/portforwarder.h"
#include "base/net/proxyconfigurationmanager.h"
#include "base/path.h"
@ -1148,6 +1149,10 @@ void OptionsDialog::loadBittorrentTabOptions()
m_ui->checkEnableAddTrackers->setChecked(session->isAddTrackersEnabled());
m_ui->textTrackers->setPlainText(session->additionalTrackers());
m_ui->checkAddTrackersFromURL->setChecked(session->isAddTrackersFromURLEnabled());
m_ui->textTrackersURL->setText(session->additionalTrackersURL());
m_ui->textTrackersFromURL->setPlainText(session->additionalTrackersFromURL());
connect(m_ui->checkDHT, &QAbstractButton::toggled, this, &ThisType::enableApplyButton);
connect(m_ui->checkPeX, &QAbstractButton::toggled, this, &ThisType::enableApplyButton);
connect(m_ui->checkLSD, &QAbstractButton::toggled, this, &ThisType::enableApplyButton);
@ -1181,6 +1186,9 @@ void OptionsDialog::loadBittorrentTabOptions()
connect(m_ui->checkEnableAddTrackers, &QGroupBox::toggled, this, &ThisType::enableApplyButton);
connect(m_ui->textTrackers, &QPlainTextEdit::textChanged, this, &ThisType::enableApplyButton);
connect(m_ui->checkAddTrackersFromURL, &QGroupBox::toggled, this, &ThisType::enableApplyButton);
connect(m_ui->textTrackersURL, &QLineEdit::textChanged, this, &ThisType::enableApplyButton);
}
void OptionsDialog::saveBittorrentTabOptions() const
@ -1218,6 +1226,9 @@ void OptionsDialog::saveBittorrentTabOptions() const
session->setAddTrackersEnabled(m_ui->checkEnableAddTrackers->isChecked());
session->setAdditionalTrackers(m_ui->textTrackers->toPlainText());
session->setAddTrackersFromURLEnabled(m_ui->checkAddTrackersFromURL->isChecked());
session->setAdditionalTrackersURL(m_ui->textTrackersURL->text());
}
void OptionsDialog::loadRSSTabOptions()

View file

@ -3131,6 +3131,81 @@ Disable encryption: Only connect to peers without protocol encryption</string>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="checkAddTrackersFromURL">
<property name="title">
<string>Automatically append trackers from URL to new downloads:</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
<property name="checked">
<bool>false</bool>
</property>
<layout class="QGridLayout" name="gridLayout_999">
<property name="leftMargin">
<number>9</number>
</property>
<property name="topMargin">
<number>9</number>
</property>
<property name="rightMargin">
<number>9</number>
</property>
<property name="bottomMargin">
<number>9</number>
</property>
<item row="0" column="0">
<widget class="QLabel" name="labelCustomizeTrackersListUrl">
<property name="text">
<string>URL:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="textTrackersURL">
<property name="enabled">
<bool>true</bool>
</property>
<property name="text">
<string>https://example.com</string>
</property>
<property name="readOnly">
<bool>false</bool>
</property>
<property name="cursorMoveStyle">
<enum>Qt::LogicalMoveStyle</enum>
</property>
</widget>
</item>
<item row="1" column="0" colspan="2">
<widget class="QGroupBox" name="trackersFromURLBox">
<property name="title">
<string>Fetched trackers</string>
</property>
<layout class="QGridLayout" name="gridLayout_112">
<item row="1" column="0" colspan="2">
<widget class="QPlainTextEdit" name="textTrackersFromURL">
<property name="enabled">
<bool>true</bool>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>153</height>
</size>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<spacer name="verticalSpacer_8">
<property name="orientation">

View file

@ -306,6 +306,9 @@ void AppController::preferencesAction()
// Add trackers
data[u"add_trackers_enabled"_s] = session->isAddTrackersEnabled();
data[u"add_trackers"_s] = session->additionalTrackers();
data[u"add_trackers_from_url_enabled"_s] = session->isAddTrackersFromURLEnabled();
data[u"add_trackers_url"_s] = session->additionalTrackersURL();
data[u"add_trackers_url_list"_s] = session->additionalTrackersFromURL();
// WebUI
// HTTP Server
@ -860,6 +863,10 @@ void AppController::setPreferencesAction()
session->setAddTrackersEnabled(it.value().toBool());
if (hasKey(u"add_trackers"_s))
session->setAdditionalTrackers(it.value().toString());
if (hasKey(u"add_trackers_from_url_enabled"_s))
session->setAddTrackersFromURLEnabled(it.value().toBool());
if (hasKey(u"add_trackers_url"_s))
session->setAdditionalTrackersURL(it.value().toString());
// WebUI
// HTTP Server

View file

@ -820,6 +820,23 @@
</legend>
<textarea id="add_trackers_textarea" rows="5" cols="70" aria-labelledby="addTrackersLabel"></textarea>
</fieldset>
<fieldset class="settings">
<legend>
<input type="checkbox" id="add_trackers_from_url_checkbox" onclick="qBittorrent.Preferences.updateAddTrackersFromURLEnabled();">
<label for="add_trackers_from_url_checkbox">QBT_TR(Automatically append trackers from URL to new downloads:)QBT_TR[CONTEXT=OptionsDialog]</label>
</legend>
<div class="formRow">
<label for="add_trackers_url">QBT_TR(URL:)QBT_TR[CONTEXT=OptionsDialog]</label>
<input type="text" id="add_trackers_url" style="width: 40em;">
</div>
<div class="formRow">
<fieldset class="settings">
<legend id="fetchedTrackersFromURLLabel">QBT_TR(Fetched trackers)QBT_TR[CONTEXT=OptionsDialog]</legend>
<textarea id="add_trackers_url_list_textarea" aria-labelledby="fetchedTrackersFromURLLabel" rows="5" cols="70" readonly></textarea>
</fieldset>
</div>
</fieldset>
</div>
<div id="RSSTab" class="PrefTab invisible">
@ -1731,6 +1748,7 @@ Use ';' to split multiple entries. Can use wildcard '*'.)QBT_TR[CONTEXT=OptionsD
updateSlowTorrentsSettings: updateSlowTorrentsSettings,
updateMaxRatioTimeEnabled: updateMaxRatioTimeEnabled,
updateAddTrackersEnabled: updateAddTrackersEnabled,
updateAddTrackersFromURLEnabled: updateAddTrackersFromURLEnabled,
updateHttpsSettings: updateHttpsSettings,
updateBypasssAuthSettings: updateBypasssAuthSettings,
updateAlternativeWebUISettings: updateAlternativeWebUISettings,
@ -2009,6 +2027,11 @@ Use ';' to split multiple entries. Can use wildcard '*'.)QBT_TR[CONTEXT=OptionsD
$("add_trackers_textarea").disabled = !isAddTrackersEnabled;
};
const updateAddTrackersFromURLEnabled = () => {
const isAddTrackersFromURLEnabled = $("add_trackers_from_url_checkbox").checked;
$("add_trackers_url").disabled = !isAddTrackersFromURLEnabled;
};
// WebUI tab
const updateHttpsSettings = () => {
const isUseHttpsEnabled = $("use_https_checkbox").checked;
@ -2431,7 +2454,11 @@ Use ';' to split multiple entries. Can use wildcard '*'.)QBT_TR[CONTEXT=OptionsD
// Add trackers
$("add_trackers_checkbox").checked = pref.add_trackers_enabled;
$("add_trackers_textarea").value = pref.add_trackers;
$("add_trackers_from_url_checkbox").checked = pref.add_trackers_from_url_enabled;
$("add_trackers_url_list_textarea").value = pref.add_trackers_url_list;
$("add_trackers_url").value = pref.add_trackers_url;
updateAddTrackersEnabled();
updateAddTrackersFromURLEnabled();
// RSS Tab
$("enable_fetching_rss_feeds_checkbox").checked = pref.rss_processing_enabled;
@ -2864,6 +2891,9 @@ Use ';' to split multiple entries. Can use wildcard '*'.)QBT_TR[CONTEXT=OptionsD
// Add trackers
settings["add_trackers_enabled"] = $("add_trackers_checkbox").checked;
settings["add_trackers"] = $("add_trackers_textarea").value;
settings["add_trackers_from_url_enabled"] = $("add_trackers_from_url_checkbox").checked;
settings["add_trackers_url_list"] = $("add_trackers_url_list_textarea").value;
settings["add_trackers_url"] = $("add_trackers_url").value;
// RSS Tab
settings["rss_processing_enabled"] = $("enable_fetching_rss_feeds_checkbox").checked;