Merge pull request #19291 from Chocobo1/limits

Expose 'bdecode limits' settings
This commit is contained in:
Chocobo1
2023-07-11 11:24:09 +08:00
committed by GitHub
13 changed files with 152 additions and 24 deletions

View File

@@ -44,12 +44,12 @@
#include "base/exceptions.h"
#include "base/global.h"
#include "base/logger.h"
#include "base/preferences.h"
#include "base/profile.h"
#include "base/tagset.h"
#include "base/utils/fs.h"
#include "base/utils/io.h"
#include "base/utils/string.h"
#include "common.h"
#include "infohash.h"
#include "loadtorrentparams.h"
@@ -134,12 +134,13 @@ BitTorrent::LoadResumeDataResult BitTorrent::BencodeResumeDataStorage::load(cons
const QString idString = id.toString();
const Path fastresumePath = path() / Path(idString + u".fastresume");
const Path torrentFilePath = path() / Path(idString + u".torrent");
const qint64 torrentSizeLimit = Preferences::instance()->getTorrentFileSizeLimit();
const auto resumeDataReadResult = Utils::IO::readFile(fastresumePath, MAX_TORRENT_SIZE);
const auto resumeDataReadResult = Utils::IO::readFile(fastresumePath, torrentSizeLimit);
if (!resumeDataReadResult)
return nonstd::make_unexpected(resumeDataReadResult.error().message);
const auto metadataReadResult = Utils::IO::readFile(torrentFilePath, MAX_TORRENT_SIZE);
const auto metadataReadResult = Utils::IO::readFile(torrentFilePath, torrentSizeLimit);
if (!metadataReadResult)
{
if (metadataReadResult.error().status != Utils::IO::ReadError::NotExist)
@@ -201,9 +202,11 @@ void BitTorrent::BencodeResumeDataStorage::loadQueue(const Path &queueFilename)
BitTorrent::LoadResumeDataResult BitTorrent::BencodeResumeDataStorage::loadTorrentResumeData(const QByteArray &data, const QByteArray &metadata) const
{
const auto *pref = Preferences::instance();
lt::error_code ec;
const lt::bdecode_node resumeDataRoot = lt::bdecode(data, ec
, nullptr, BENCODE_DEPTH_LIMIT, BENCODE_TOKEN_LIMIT);
, nullptr, pref->getBdecodeDepthLimit(), pref->getBdecodeTokenLimit());
if (ec)
return nonstd::make_unexpected(tr("Cannot parse resume data: %1").arg(QString::fromStdString(ec.message())));
@@ -270,8 +273,9 @@ BitTorrent::LoadResumeDataResult BitTorrent::BencodeResumeDataStorage::loadTorre
if (!metadata.isEmpty())
{
const auto *pref = Preferences::instance();
const lt::bdecode_node torentInfoRoot = lt::bdecode(metadata, ec
, nullptr, BENCODE_DEPTH_LIMIT, BENCODE_TOKEN_LIMIT);
, nullptr, pref->getBdecodeDepthLimit(), pref->getBdecodeTokenLimit());
if (ec)
return nonstd::make_unexpected(tr("Cannot parse torrent info: %1").arg(QString::fromStdString(ec.message())));

View File

@@ -33,7 +33,3 @@
#include "base/global.h"
inline const QString QB_EXT = u".!qB"_s;
inline const int MAX_TORRENT_SIZE = 100 * 1024 * 1024; // 100 MiB
inline const int BENCODE_DEPTH_LIMIT = 100;
inline const int BENCODE_TOKEN_LIMIT = 10'000'000;

View File

@@ -55,10 +55,10 @@
#include "base/global.h"
#include "base/logger.h"
#include "base/path.h"
#include "base/preferences.h"
#include "base/profile.h"
#include "base/utils/fs.h"
#include "base/utils/string.h"
#include "common.h"
#include "infohash.h"
#include "loadtorrentparams.h"
@@ -246,10 +246,13 @@ namespace
}
const QByteArray bencodedResumeData = query.value(DB_COLUMN_RESUMEDATA.name).toByteArray();
const auto *pref = Preferences::instance();
const int bdecodeDepthLimit = pref->getBdecodeDepthLimit();
const int bdecodeTokenLimit = pref->getBdecodeTokenLimit();
lt::error_code ec;
const lt::bdecode_node resumeDataRoot = lt::bdecode(bencodedResumeData, ec
, nullptr, BENCODE_DEPTH_LIMIT, BENCODE_TOKEN_LIMIT);
, nullptr, bdecodeDepthLimit, bdecodeTokenLimit);
lt::add_torrent_params &p = resumeData.ltAddTorrentParams;
@@ -259,7 +262,7 @@ namespace
; !bencodedMetadata.isEmpty())
{
const lt::bdecode_node torentInfoRoot = lt::bdecode(bencodedMetadata, ec
, nullptr, BENCODE_DEPTH_LIMIT, BENCODE_TOKEN_LIMIT);
, nullptr, bdecodeDepthLimit, bdecodeTokenLimit);
p.ti = std::make_shared<lt::torrent_info>(torentInfoRoot, ec);
}

View File

@@ -96,7 +96,6 @@
#include "base/version.h"
#include "bandwidthscheduler.h"
#include "bencoderesumedatastorage.h"
#include "common.h"
#include "customstorage.h"
#include "dbresumedatastorage.h"
#include "downloadpriority.h"
@@ -1742,6 +1741,10 @@ lt::settings_pack SessionImpl::loadLTSettings() const
settingsPack.set_int(lt::settings_pack::max_out_request_queue, requestQueueSize());
#ifdef QBT_USES_LIBTORRENT2
settingsPack.set_int(lt::settings_pack::metadata_token_limit, Preferences::instance()->getBdecodeTokenLimit());
#endif
settingsPack.set_int(lt::settings_pack::aio_threads, asyncIOThreads());
#ifdef QBT_USES_LIBTORRENT2
settingsPack.set_int(lt::settings_pack::hashing_threads, hashingThreads());
@@ -2555,9 +2558,10 @@ bool SessionImpl::addTorrent(const QString &source, const AddTorrentParams &para
if (Net::DownloadManager::hasSupportedScheme(source))
{
LogMsg(tr("Downloading torrent, please wait... Source: \"%1\"").arg(source));
const auto *pref = Preferences::instance();
// Launch downloader
Net::DownloadManager::instance()->download(Net::DownloadRequest(source).limit(MAX_TORRENT_SIZE)
, Preferences::instance()->useProxyForGeneralPurposes(), this, &SessionImpl::handleDownloadFinished);
Net::DownloadManager::instance()->download(Net::DownloadRequest(source).limit(pref->getTorrentFileSizeLimit())
, pref->useProxyForGeneralPurposes(), this, &SessionImpl::handleDownloadFinished);
m_downloadedTorrents[source] = params;
return true;
}

View File

@@ -42,10 +42,10 @@
#include "base/global.h"
#include "base/path.h"
#include "base/preferences.h"
#include "base/utils/fs.h"
#include "base/utils/io.h"
#include "base/utils/misc.h"
#include "common.h"
#include "infohash.h"
#include "trackerentry.h"
@@ -86,9 +86,11 @@ nonstd::expected<TorrentInfo, QString> TorrentInfo::load(const QByteArray &data)
{
// 2-step construction to overcome default limits of `depth_limit` & `token_limit` which are
// used in `torrent_info()` constructor
const auto *pref = Preferences::instance();
lt::error_code ec;
const lt::bdecode_node node = lt::bdecode(data, ec
, nullptr, BENCODE_DEPTH_LIMIT, BENCODE_TOKEN_LIMIT);
, nullptr, pref->getBdecodeDepthLimit(), pref->getBdecodeTokenLimit());
if (ec)
return nonstd::make_unexpected(QString::fromStdString(ec.message()));
@@ -104,7 +106,8 @@ nonstd::expected<TorrentInfo, QString> TorrentInfo::loadFromFile(const Path &pat
QByteArray data;
try
{
const auto readResult = Utils::IO::readFile(path, MAX_TORRENT_SIZE);
const qint64 torrentSizeLimit = Preferences::instance()->getTorrentFileSizeLimit();
const auto readResult = Utils::IO::readFile(path, torrentSizeLimit);
if (!readResult)
return nonstd::make_unexpected(readResult.error().message);
data = readResult.value();

View File

@@ -304,6 +304,45 @@ void Preferences::setIconsInMenusEnabled(const bool enable)
}
#endif // Q_OS_MACOS
qint64 Preferences::getTorrentFileSizeLimit() const
{
return value(u"BitTorrent/TorrentFileSizeLimit"_s, (100 * 1024 * 1024));
}
void Preferences::setTorrentFileSizeLimit(const qint64 value)
{
if (value == getTorrentFileSizeLimit())
return;
setValue(u"BitTorrent/TorrentFileSizeLimit"_s, value);
}
int Preferences::getBdecodeDepthLimit() const
{
return value(u"BitTorrent/BdecodeDepthLimit"_s, 100);
}
void Preferences::setBdecodeDepthLimit(const int value)
{
if (value == getBdecodeDepthLimit())
return;
setValue(u"BitTorrent/BdecodeDepthLimit"_s, value);
}
int Preferences::getBdecodeTokenLimit() const
{
return value(u"BitTorrent/BdecodeTokenLimit"_s, 10'000'000);
}
void Preferences::setBdecodeTokenLimit(const int value)
{
if (value == getBdecodeTokenLimit())
return;
setValue(u"BitTorrent/BdecodeTokenLimit"_s, value);
}
bool Preferences::isToolbarDisplayed() const
{
return value(u"Preferences/General/ToolbarDisplayed"_s, true);

View File

@@ -335,6 +335,12 @@ public:
bool iconsInMenusEnabled() const;
void setIconsInMenusEnabled(bool enable);
#endif // Q_OS_MACOS
qint64 getTorrentFileSizeLimit() const;
void setTorrentFileSizeLimit(qint64 value);
int getBdecodeDepthLimit() const;
void setBdecodeDepthLimit(int value);
int getBdecodeTokenLimit() const;
void setBdecodeTokenLimit(int value);
// Stuff that don't appear in the Options GUI but are saved
// in the same file.