2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2015-2023 Vladimir Golovnev <glassez@yandex.ru>
4 * Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * In addition, as a special exception, the copyright holders give permission to
21 * link this program with the OpenSSL project's "OpenSSL" library (or with
22 * modified versions of it that use the same license as the "OpenSSL" library),
23 * and distribute the linked executables. You must obey the GNU General Public
24 * License in all respects for all of the code used other than "OpenSSL". If you
25 * modify file(s), you may extend this exception to your version of the file(s),
26 * but you are not obligated to do so. If you do not wish to do so, delete this
27 * exception statement from your version.
30 #include "sessionimpl.h"
45 #include <boost/asio/ip/tcp.hpp>
47 #include <libtorrent/add_torrent_params.hpp>
48 #include <libtorrent/address.hpp>
49 #include <libtorrent/alert_types.hpp>
50 #include <libtorrent/error_code.hpp>
51 #include <libtorrent/extensions/smart_ban.hpp>
52 #include <libtorrent/extensions/ut_metadata.hpp>
53 #include <libtorrent/extensions/ut_pex.hpp>
54 #include <libtorrent/ip_filter.hpp>
55 #include <libtorrent/magnet_uri.hpp>
56 #include <libtorrent/session.hpp>
57 #include <libtorrent/session_stats.hpp>
58 #include <libtorrent/session_status.hpp>
59 #include <libtorrent/torrent_info.hpp>
63 #include <QHostAddress>
65 #include <QJsonDocument>
66 #include <QJsonObject>
68 #include <QNetworkAddressEntry>
69 #include <QNetworkInterface>
70 #include <QRegularExpression>
73 #include <QThreadPool>
77 #include "base/algorithm.h"
78 #include "base/global.h"
79 #include "base/logger.h"
80 #include "base/net/proxyconfigurationmanager.h"
81 #include "base/preferences.h"
82 #include "base/profile.h"
83 #include "base/unicodestrings.h"
84 #include "base/utils/fs.h"
85 #include "base/utils/io.h"
86 #include "base/utils/net.h"
87 #include "base/utils/random.h"
88 #include "base/version.h"
89 #include "bandwidthscheduler.h"
90 #include "bencoderesumedatastorage.h"
91 #include "customstorage.h"
92 #include "dbresumedatastorage.h"
93 #include "downloadpriority.h"
94 #include "extensiondata.h"
95 #include "filesearcher.h"
96 #include "filterparserthread.h"
97 #include "loadtorrentparams.h"
98 #include "lttypecast.h"
99 #include "nativesessionextension.h"
100 #include "portforwarderimpl.h"
101 #include "resumedatastorage.h"
102 #include "torrentdescriptor.h"
103 #include "torrentimpl.h"
106 using namespace std::chrono_literals
;
107 using namespace BitTorrent
;
109 const Path CATEGORIES_FILE_NAME
{u
"categories.json"_s
};
110 const int MAX_PROCESSING_RESUMEDATA_COUNT
= 50;
111 const int STATISTICS_SAVE_INTERVAL
= std::chrono::milliseconds(15min
).count();
115 const char PEER_ID
[] = "qB";
116 const auto USER_AGENT
= QStringLiteral("qBittorrent/" QBT_VERSION_2
);
117 const QString DEFAULT_DHT_BOOTSTRAP_NODES
= u
"dht.libtorrent.org:25401, dht.transmissionbt.com:6881, router.bittorrent.com:6881, router.utorrent.com:6881, dht.aelitis.com:6881"_s
;
119 void torrentQueuePositionUp(const lt::torrent_handle
&handle
)
123 handle
.queue_position_up();
125 catch (const std::exception
&exc
)
127 qDebug() << Q_FUNC_INFO
<< " fails: " << exc
.what();
131 void torrentQueuePositionDown(const lt::torrent_handle
&handle
)
135 handle
.queue_position_down();
137 catch (const std::exception
&exc
)
139 qDebug() << Q_FUNC_INFO
<< " fails: " << exc
.what();
143 void torrentQueuePositionTop(const lt::torrent_handle
&handle
)
147 handle
.queue_position_top();
149 catch (const std::exception
&exc
)
151 qDebug() << Q_FUNC_INFO
<< " fails: " << exc
.what();
155 void torrentQueuePositionBottom(const lt::torrent_handle
&handle
)
159 handle
.queue_position_bottom();
161 catch (const std::exception
&exc
)
163 qDebug() << Q_FUNC_INFO
<< " fails: " << exc
.what();
167 QMap
<QString
, CategoryOptions
> expandCategories(const QMap
<QString
, CategoryOptions
> &categories
)
169 QMap
<QString
, CategoryOptions
> expanded
= categories
;
171 for (auto i
= categories
.cbegin(); i
!= categories
.cend(); ++i
)
173 const QString
&category
= i
.key();
174 for (const QString
&subcat
: asConst(Session::expandCategory(category
)))
176 if (!expanded
.contains(subcat
))
177 expanded
[subcat
] = {};
184 QString
toString(const lt::socket_type_t socketType
)
188 #ifdef QBT_USES_LIBTORRENT2
189 case lt::socket_type_t::http
:
191 case lt::socket_type_t::http_ssl
:
192 return u
"HTTP_SSL"_s
;
194 case lt::socket_type_t::i2p
:
196 case lt::socket_type_t::socks5
:
198 #ifdef QBT_USES_LIBTORRENT2
199 case lt::socket_type_t::socks5_ssl
:
200 return u
"SOCKS5_SSL"_s
;
202 case lt::socket_type_t::tcp
:
204 case lt::socket_type_t::tcp_ssl
:
206 #ifdef QBT_USES_LIBTORRENT2
207 case lt::socket_type_t::utp
:
210 case lt::socket_type_t::udp
:
213 case lt::socket_type_t::utp_ssl
:
219 QString
toString(const lt::address
&address
)
223 return QString::fromLatin1(address
.to_string().c_str());
225 catch (const std::exception
&)
227 // suppress conversion error
232 template <typename T
>
235 LowerLimited(T limit
, T ret
)
241 explicit LowerLimited(T limit
)
242 : LowerLimited(limit
, limit
)
246 T
operator()(T val
) const
248 return val
<= m_limit
? m_ret
: val
;
256 template <typename T
>
257 LowerLimited
<T
> lowerLimited(T limit
) { return LowerLimited
<T
>(limit
); }
259 template <typename T
>
260 LowerLimited
<T
> lowerLimited(T limit
, T ret
) { return LowerLimited
<T
>(limit
, ret
); }
262 template <typename T
>
263 auto clampValue(const T lower
, const T upper
)
265 return [lower
, upper
](const T value
) -> T
267 return std::clamp(value
, lower
, upper
);
272 QString
convertIfaceNameToGuid(const QString
&name
)
274 // Under Windows XP or on Qt version <= 5.5 'name' will be a GUID already.
275 const QUuid
uuid(name
);
277 return uuid
.toString().toUpper(); // Libtorrent expects the GUID in uppercase
279 const std::wstring nameWStr
= name
.toStdWString();
281 const LONG res
= ::ConvertInterfaceNameToLuidW(nameWStr
.c_str(), &luid
);
285 if (::ConvertInterfaceLuidToGuid(&luid
, &guid
) == 0)
286 return QUuid(guid
).toString().toUpper();
293 constexpr lt::move_flags_t
toNative(const MoveStorageMode mode
)
299 case MoveStorageMode::FailIfExist
:
300 return lt::move_flags_t::fail_if_exist
;
301 case MoveStorageMode::KeepExistingFiles
:
302 return lt::move_flags_t::dont_replace
;
303 case MoveStorageMode::Overwrite
:
304 return lt::move_flags_t::always_replace_files
;
309 struct BitTorrent::SessionImpl::ResumeSessionContext final
: public QObject
311 using QObject::QObject
;
313 ResumeDataStorage
*startupStorage
= nullptr;
314 ResumeDataStorageType currentStorageType
= ResumeDataStorageType::Legacy
;
315 QList
<LoadedResumeData
> loadedResumeData
;
316 int processingResumeDataCount
= 0;
317 int64_t totalResumeDataCount
= 0;
318 int64_t finishedResumeDataCount
= 0;
319 bool isLoadFinished
= false;
320 bool isLoadedResumeDataHandlingEnqueued
= false;
321 QSet
<QString
> recoveredCategories
;
322 #ifdef QBT_USES_LIBTORRENT2
323 QSet
<TorrentID
> indexedTorrents
;
324 QSet
<TorrentID
> skippedIDs
;
328 const int addTorrentParamsId
= qRegisterMetaType
<AddTorrentParams
>();
330 Session
*SessionImpl::m_instance
= nullptr;
332 void Session::initInstance()
334 if (!SessionImpl::m_instance
)
335 SessionImpl::m_instance
= new SessionImpl
;
338 void Session::freeInstance()
340 delete SessionImpl::m_instance
;
341 SessionImpl::m_instance
= nullptr;
344 Session
*Session::instance()
346 return SessionImpl::m_instance
;
349 bool Session::isValidCategoryName(const QString
&name
)
351 const QRegularExpression re
{uR
"(^([^\\\/]|[^\\\/]([^\\\/]|\/(?=[^\/]))*[^\\\/])$)"_s
};
352 return (name
.isEmpty() || (name
.indexOf(re
) == 0));
355 QString
Session::subcategoryName(const QString
&category
)
357 const int sepIndex
= category
.lastIndexOf(u
'/');
359 return category
.mid(sepIndex
+ 1);
364 QString
Session::parentCategoryName(const QString
&category
)
366 const int sepIndex
= category
.lastIndexOf(u
'/');
368 return category
.left(sepIndex
);
373 QStringList
Session::expandCategory(const QString
&category
)
377 while ((index
= category
.indexOf(u
'/', index
)) >= 0)
379 result
<< category
.left(index
);
387 #define BITTORRENT_KEY(name) u"BitTorrent/" name
388 #define BITTORRENT_SESSION_KEY(name) BITTORRENT_KEY(u"Session/") name
390 SessionImpl::SessionImpl(QObject
*parent
)
392 , m_DHTBootstrapNodes(BITTORRENT_SESSION_KEY(u
"DHTBootstrapNodes"_s
), DEFAULT_DHT_BOOTSTRAP_NODES
)
393 , m_isDHTEnabled(BITTORRENT_SESSION_KEY(u
"DHTEnabled"_s
), true)
394 , m_isLSDEnabled(BITTORRENT_SESSION_KEY(u
"LSDEnabled"_s
), true)
395 , m_isPeXEnabled(BITTORRENT_SESSION_KEY(u
"PeXEnabled"_s
), true)
396 , m_isIPFilteringEnabled(BITTORRENT_SESSION_KEY(u
"IPFilteringEnabled"_s
), false)
397 , m_isTrackerFilteringEnabled(BITTORRENT_SESSION_KEY(u
"TrackerFilteringEnabled"_s
), false)
398 , m_IPFilterFile(BITTORRENT_SESSION_KEY(u
"IPFilter"_s
))
399 , m_announceToAllTrackers(BITTORRENT_SESSION_KEY(u
"AnnounceToAllTrackers"_s
), false)
400 , m_announceToAllTiers(BITTORRENT_SESSION_KEY(u
"AnnounceToAllTiers"_s
), true)
401 , m_asyncIOThreads(BITTORRENT_SESSION_KEY(u
"AsyncIOThreadsCount"_s
), 10)
402 , m_hashingThreads(BITTORRENT_SESSION_KEY(u
"HashingThreadsCount"_s
), 1)
403 , m_filePoolSize(BITTORRENT_SESSION_KEY(u
"FilePoolSize"_s
), 100)
404 , m_checkingMemUsage(BITTORRENT_SESSION_KEY(u
"CheckingMemUsageSize"_s
), 32)
405 , m_diskCacheSize(BITTORRENT_SESSION_KEY(u
"DiskCacheSize"_s
), -1)
406 , m_diskCacheTTL(BITTORRENT_SESSION_KEY(u
"DiskCacheTTL"_s
), 60)
407 , m_diskQueueSize(BITTORRENT_SESSION_KEY(u
"DiskQueueSize"_s
), (1024 * 1024))
408 , m_diskIOType(BITTORRENT_SESSION_KEY(u
"DiskIOType"_s
), DiskIOType::Default
)
409 , m_diskIOReadMode(BITTORRENT_SESSION_KEY(u
"DiskIOReadMode"_s
), DiskIOReadMode::EnableOSCache
)
410 , m_diskIOWriteMode(BITTORRENT_SESSION_KEY(u
"DiskIOWriteMode"_s
), DiskIOWriteMode::EnableOSCache
)
412 , m_coalesceReadWriteEnabled(BITTORRENT_SESSION_KEY(u
"CoalesceReadWrite"_s
), true)
414 , m_coalesceReadWriteEnabled(BITTORRENT_SESSION_KEY(u
"CoalesceReadWrite"_s
), false)
416 , m_usePieceExtentAffinity(BITTORRENT_SESSION_KEY(u
"PieceExtentAffinity"_s
), false)
417 , m_isSuggestMode(BITTORRENT_SESSION_KEY(u
"SuggestMode"_s
), false)
418 , m_sendBufferWatermark(BITTORRENT_SESSION_KEY(u
"SendBufferWatermark"_s
), 500)
419 , m_sendBufferLowWatermark(BITTORRENT_SESSION_KEY(u
"SendBufferLowWatermark"_s
), 10)
420 , m_sendBufferWatermarkFactor(BITTORRENT_SESSION_KEY(u
"SendBufferWatermarkFactor"_s
), 50)
421 , m_connectionSpeed(BITTORRENT_SESSION_KEY(u
"ConnectionSpeed"_s
), 30)
422 , m_socketSendBufferSize(BITTORRENT_SESSION_KEY(u
"SocketSendBufferSize"_s
), 0)
423 , m_socketReceiveBufferSize(BITTORRENT_SESSION_KEY(u
"SocketReceiveBufferSize"_s
), 0)
424 , m_socketBacklogSize(BITTORRENT_SESSION_KEY(u
"SocketBacklogSize"_s
), 30)
425 , m_isAnonymousModeEnabled(BITTORRENT_SESSION_KEY(u
"AnonymousModeEnabled"_s
), false)
426 , m_isQueueingEnabled(BITTORRENT_SESSION_KEY(u
"QueueingSystemEnabled"_s
), false)
427 , m_maxActiveDownloads(BITTORRENT_SESSION_KEY(u
"MaxActiveDownloads"_s
), 3, lowerLimited(-1))
428 , m_maxActiveUploads(BITTORRENT_SESSION_KEY(u
"MaxActiveUploads"_s
), 3, lowerLimited(-1))
429 , m_maxActiveTorrents(BITTORRENT_SESSION_KEY(u
"MaxActiveTorrents"_s
), 5, lowerLimited(-1))
430 , m_ignoreSlowTorrentsForQueueing(BITTORRENT_SESSION_KEY(u
"IgnoreSlowTorrentsForQueueing"_s
), false)
431 , m_downloadRateForSlowTorrents(BITTORRENT_SESSION_KEY(u
"SlowTorrentsDownloadRate"_s
), 2)
432 , m_uploadRateForSlowTorrents(BITTORRENT_SESSION_KEY(u
"SlowTorrentsUploadRate"_s
), 2)
433 , m_slowTorrentsInactivityTimer(BITTORRENT_SESSION_KEY(u
"SlowTorrentsInactivityTimer"_s
), 60)
434 , m_outgoingPortsMin(BITTORRENT_SESSION_KEY(u
"OutgoingPortsMin"_s
), 0)
435 , m_outgoingPortsMax(BITTORRENT_SESSION_KEY(u
"OutgoingPortsMax"_s
), 0)
436 , m_UPnPLeaseDuration(BITTORRENT_SESSION_KEY(u
"UPnPLeaseDuration"_s
), 0)
437 , m_peerToS(BITTORRENT_SESSION_KEY(u
"PeerToS"_s
), 0x04)
438 , m_ignoreLimitsOnLAN(BITTORRENT_SESSION_KEY(u
"IgnoreLimitsOnLAN"_s
), false)
439 , m_includeOverheadInLimits(BITTORRENT_SESSION_KEY(u
"IncludeOverheadInLimits"_s
), false)
440 , m_announceIP(BITTORRENT_SESSION_KEY(u
"AnnounceIP"_s
))
441 , m_maxConcurrentHTTPAnnounces(BITTORRENT_SESSION_KEY(u
"MaxConcurrentHTTPAnnounces"_s
), 50)
442 , m_isReannounceWhenAddressChangedEnabled(BITTORRENT_SESSION_KEY(u
"ReannounceWhenAddressChanged"_s
), false)
443 , m_stopTrackerTimeout(BITTORRENT_SESSION_KEY(u
"StopTrackerTimeout"_s
), 2)
444 , m_maxConnections(BITTORRENT_SESSION_KEY(u
"MaxConnections"_s
), 500, lowerLimited(0, -1))
445 , m_maxUploads(BITTORRENT_SESSION_KEY(u
"MaxUploads"_s
), 20, lowerLimited(0, -1))
446 , m_maxConnectionsPerTorrent(BITTORRENT_SESSION_KEY(u
"MaxConnectionsPerTorrent"_s
), 100, lowerLimited(0, -1))
447 , m_maxUploadsPerTorrent(BITTORRENT_SESSION_KEY(u
"MaxUploadsPerTorrent"_s
), 4, lowerLimited(0, -1))
448 , m_btProtocol(BITTORRENT_SESSION_KEY(u
"BTProtocol"_s
), BTProtocol::Both
449 , clampValue(BTProtocol::Both
, BTProtocol::UTP
))
450 , m_isUTPRateLimited(BITTORRENT_SESSION_KEY(u
"uTPRateLimited"_s
), true)
451 , m_utpMixedMode(BITTORRENT_SESSION_KEY(u
"uTPMixedMode"_s
), MixedModeAlgorithm::TCP
452 , clampValue(MixedModeAlgorithm::TCP
, MixedModeAlgorithm::Proportional
))
453 , m_IDNSupportEnabled(BITTORRENT_SESSION_KEY(u
"IDNSupportEnabled"_s
), false)
454 , m_multiConnectionsPerIpEnabled(BITTORRENT_SESSION_KEY(u
"MultiConnectionsPerIp"_s
), false)
455 , m_validateHTTPSTrackerCertificate(BITTORRENT_SESSION_KEY(u
"ValidateHTTPSTrackerCertificate"_s
), true)
456 , m_SSRFMitigationEnabled(BITTORRENT_SESSION_KEY(u
"SSRFMitigation"_s
), true)
457 , m_blockPeersOnPrivilegedPorts(BITTORRENT_SESSION_KEY(u
"BlockPeersOnPrivilegedPorts"_s
), false)
458 , m_isAddTrackersEnabled(BITTORRENT_SESSION_KEY(u
"AddTrackersEnabled"_s
), false)
459 , m_additionalTrackers(BITTORRENT_SESSION_KEY(u
"AdditionalTrackers"_s
))
460 , m_globalMaxRatio(BITTORRENT_SESSION_KEY(u
"GlobalMaxRatio"_s
), -1, [](qreal r
) { return r
< 0 ? -1. : r
;})
461 , m_globalMaxSeedingMinutes(BITTORRENT_SESSION_KEY(u
"GlobalMaxSeedingMinutes"_s
), -1, lowerLimited(-1))
462 , m_globalMaxInactiveSeedingMinutes(BITTORRENT_SESSION_KEY(u
"GlobalMaxInactiveSeedingMinutes"_s
), -1, lowerLimited(-1))
463 , m_isAddTorrentToQueueTop(BITTORRENT_SESSION_KEY(u
"AddTorrentToTopOfQueue"_s
), false)
464 , m_isAddTorrentPaused(BITTORRENT_SESSION_KEY(u
"AddTorrentPaused"_s
), false)
465 , m_torrentStopCondition(BITTORRENT_SESSION_KEY(u
"TorrentStopCondition"_s
), Torrent::StopCondition::None
)
466 , m_torrentContentLayout(BITTORRENT_SESSION_KEY(u
"TorrentContentLayout"_s
), TorrentContentLayout::Original
)
467 , m_isAppendExtensionEnabled(BITTORRENT_SESSION_KEY(u
"AddExtensionToIncompleteFiles"_s
), false)
468 , m_isUnwantedFolderEnabled(BITTORRENT_SESSION_KEY(u
"UseUnwantedFolder"_s
), false)
469 , m_refreshInterval(BITTORRENT_SESSION_KEY(u
"RefreshInterval"_s
), 1500)
470 , m_isPreallocationEnabled(BITTORRENT_SESSION_KEY(u
"Preallocation"_s
), false)
471 , m_torrentExportDirectory(BITTORRENT_SESSION_KEY(u
"TorrentExportDirectory"_s
))
472 , m_finishedTorrentExportDirectory(BITTORRENT_SESSION_KEY(u
"FinishedTorrentExportDirectory"_s
))
473 , m_globalDownloadSpeedLimit(BITTORRENT_SESSION_KEY(u
"GlobalDLSpeedLimit"_s
), 0, lowerLimited(0))
474 , m_globalUploadSpeedLimit(BITTORRENT_SESSION_KEY(u
"GlobalUPSpeedLimit"_s
), 0, lowerLimited(0))
475 , m_altGlobalDownloadSpeedLimit(BITTORRENT_SESSION_KEY(u
"AlternativeGlobalDLSpeedLimit"_s
), 10, lowerLimited(0))
476 , m_altGlobalUploadSpeedLimit(BITTORRENT_SESSION_KEY(u
"AlternativeGlobalUPSpeedLimit"_s
), 10, lowerLimited(0))
477 , m_isAltGlobalSpeedLimitEnabled(BITTORRENT_SESSION_KEY(u
"UseAlternativeGlobalSpeedLimit"_s
), false)
478 , m_isBandwidthSchedulerEnabled(BITTORRENT_SESSION_KEY(u
"BandwidthSchedulerEnabled"_s
), false)
479 , m_isPerformanceWarningEnabled(BITTORRENT_SESSION_KEY(u
"PerformanceWarning"_s
), false)
480 , m_saveResumeDataInterval(BITTORRENT_SESSION_KEY(u
"SaveResumeDataInterval"_s
), 60)
481 , m_port(BITTORRENT_SESSION_KEY(u
"Port"_s
), -1)
482 , m_networkInterface(BITTORRENT_SESSION_KEY(u
"Interface"_s
))
483 , m_networkInterfaceName(BITTORRENT_SESSION_KEY(u
"InterfaceName"_s
))
484 , m_networkInterfaceAddress(BITTORRENT_SESSION_KEY(u
"InterfaceAddress"_s
))
485 , m_encryption(BITTORRENT_SESSION_KEY(u
"Encryption"_s
), 0)
486 , m_maxActiveCheckingTorrents(BITTORRENT_SESSION_KEY(u
"MaxActiveCheckingTorrents"_s
), 1)
487 , m_isProxyPeerConnectionsEnabled(BITTORRENT_SESSION_KEY(u
"ProxyPeerConnections"_s
), false)
488 , m_chokingAlgorithm(BITTORRENT_SESSION_KEY(u
"ChokingAlgorithm"_s
), ChokingAlgorithm::FixedSlots
489 , clampValue(ChokingAlgorithm::FixedSlots
, ChokingAlgorithm::RateBased
))
490 , m_seedChokingAlgorithm(BITTORRENT_SESSION_KEY(u
"SeedChokingAlgorithm"_s
), SeedChokingAlgorithm::FastestUpload
491 , clampValue(SeedChokingAlgorithm::RoundRobin
, SeedChokingAlgorithm::AntiLeech
))
492 , m_storedTags(BITTORRENT_SESSION_KEY(u
"Tags"_s
))
493 , m_maxRatioAction(BITTORRENT_SESSION_KEY(u
"MaxRatioAction"_s
), Pause
)
494 , m_savePath(BITTORRENT_SESSION_KEY(u
"DefaultSavePath"_s
), specialFolderLocation(SpecialFolder::Downloads
))
495 , m_downloadPath(BITTORRENT_SESSION_KEY(u
"TempPath"_s
), (savePath() / Path(u
"temp"_s
)))
496 , m_isDownloadPathEnabled(BITTORRENT_SESSION_KEY(u
"TempPathEnabled"_s
), false)
497 , m_isSubcategoriesEnabled(BITTORRENT_SESSION_KEY(u
"SubcategoriesEnabled"_s
), false)
498 , m_useCategoryPathsInManualMode(BITTORRENT_SESSION_KEY(u
"UseCategoryPathsInManualMode"_s
), false)
499 , m_isAutoTMMDisabledByDefault(BITTORRENT_SESSION_KEY(u
"DisableAutoTMMByDefault"_s
), true)
500 , m_isDisableAutoTMMWhenCategoryChanged(BITTORRENT_SESSION_KEY(u
"DisableAutoTMMTriggers/CategoryChanged"_s
), false)
501 , m_isDisableAutoTMMWhenDefaultSavePathChanged(BITTORRENT_SESSION_KEY(u
"DisableAutoTMMTriggers/DefaultSavePathChanged"_s
), true)
502 , m_isDisableAutoTMMWhenCategorySavePathChanged(BITTORRENT_SESSION_KEY(u
"DisableAutoTMMTriggers/CategorySavePathChanged"_s
), true)
503 , m_isTrackerEnabled(BITTORRENT_KEY(u
"TrackerEnabled"_s
), false)
504 , m_peerTurnover(BITTORRENT_SESSION_KEY(u
"PeerTurnover"_s
), 4)
505 , m_peerTurnoverCutoff(BITTORRENT_SESSION_KEY(u
"PeerTurnoverCutOff"_s
), 90)
506 , m_peerTurnoverInterval(BITTORRENT_SESSION_KEY(u
"PeerTurnoverInterval"_s
), 300)
507 , m_requestQueueSize(BITTORRENT_SESSION_KEY(u
"RequestQueueSize"_s
), 500)
508 , m_isExcludedFileNamesEnabled(BITTORRENT_KEY(u
"ExcludedFileNamesEnabled"_s
), false)
509 , m_excludedFileNames(BITTORRENT_SESSION_KEY(u
"ExcludedFileNames"_s
))
510 , m_bannedIPs(u
"State/BannedIPs"_s
, QStringList(), Algorithm::sorted
<QStringList
>)
511 , m_resumeDataStorageType(BITTORRENT_SESSION_KEY(u
"ResumeDataStorageType"_s
), ResumeDataStorageType::Legacy
)
512 , m_isMergeTrackersEnabled(BITTORRENT_KEY(u
"MergeTrackersEnabled"_s
), false)
513 , m_isI2PEnabled
{BITTORRENT_SESSION_KEY(u
"I2P/Enabled"_s
), false}
514 , m_I2PAddress
{BITTORRENT_SESSION_KEY(u
"I2P/Address"_s
), u
"127.0.0.1"_s
}
515 , m_I2PPort
{BITTORRENT_SESSION_KEY(u
"I2P/Port"_s
), 7656}
516 , m_I2PMixedMode
{BITTORRENT_SESSION_KEY(u
"I2P/MixedMode"_s
), false}
517 , m_I2PInboundQuantity
{BITTORRENT_SESSION_KEY(u
"I2P/InboundQuantity"_s
), 3}
518 , m_I2POutboundQuantity
{BITTORRENT_SESSION_KEY(u
"I2P/OutboundQuantity"_s
), 3}
519 , m_I2PInboundLength
{BITTORRENT_SESSION_KEY(u
"I2P/InboundLength"_s
), 3}
520 , m_I2POutboundLength
{BITTORRENT_SESSION_KEY(u
"I2P/OutboundLength"_s
), 3}
521 , m_seedingLimitTimer
{new QTimer(this)}
522 , m_resumeDataTimer
{new QTimer(this)}
523 , m_ioThread
{new QThread
}
524 , m_asyncWorker
{new QThreadPool(this)}
525 , m_recentErroredTorrentsTimer
{new QTimer(this)}
527 // It is required to perform async access to libtorrent sequentially
528 m_asyncWorker
->setMaxThreadCount(1);
531 m_port
= Utils::Random::rand(1024, 65535);
533 m_recentErroredTorrentsTimer
->setSingleShot(true);
534 m_recentErroredTorrentsTimer
->setInterval(1s
);
535 connect(m_recentErroredTorrentsTimer
, &QTimer::timeout
536 , this, [this]() { m_recentErroredTorrents
.clear(); });
538 m_seedingLimitTimer
->setInterval(10s
);
539 connect(m_seedingLimitTimer
, &QTimer::timeout
, this, &SessionImpl::processShareLimits
);
541 initializeNativeSession();
542 configureComponents();
544 if (isBandwidthSchedulerEnabled())
545 enableBandwidthScheduler();
548 if (isSubcategoriesEnabled())
550 // if subcategories support changed manually
551 m_categories
= expandCategories(m_categories
);
554 const QStringList storedTags
= m_storedTags
.get();
555 for (const QString
&tagStr
: storedTags
)
557 if (const Tag tag
{tagStr
}; tag
.isValid())
561 updateSeedingLimitTimer();
562 populateAdditionalTrackers();
563 if (isExcludedFileNamesEnabled())
564 populateExcludedFileNamesRegExpList();
566 connect(Net::ProxyConfigurationManager::instance()
567 , &Net::ProxyConfigurationManager::proxyConfigurationChanged
568 , this, &SessionImpl::configureDeferred
);
570 m_fileSearcher
= new FileSearcher
;
571 m_fileSearcher
->moveToThread(m_ioThread
.get());
572 connect(m_ioThread
.get(), &QThread::finished
, m_fileSearcher
, &QObject::deleteLater
);
573 connect(m_fileSearcher
, &FileSearcher::searchFinished
, this, &SessionImpl::fileSearchFinished
);
580 // initialize PortForwarder instance
581 new PortForwarderImpl(this);
583 // start embedded tracker
584 enableTracker(isTrackerEnabled());
589 SessionImpl::~SessionImpl()
591 m_nativeSession
->pause();
593 if (m_torrentsQueueChanged
)
595 m_nativeSession
->post_torrent_updates({});
596 m_torrentsQueueChanged
= false;
597 m_needSaveTorrentsQueue
= true;
600 // Do some bittorrent related saving
601 // After this, (ideally) no more important alerts will be generated/handled
606 // We must delete FilterParserThread
607 // before we delete lt::session
608 delete m_filterParser
;
610 // We must delete PortForwarderImpl before
611 // we delete lt::session
612 delete Net::PortForwarder::instance();
614 // We must stop "async worker" only after deletion
615 // of all the components that could potentially use it
616 m_asyncWorker
->clear();
617 m_asyncWorker
->waitForDone();
619 qDebug("Deleting libtorrent session...");
620 delete m_nativeSession
;
623 QString
SessionImpl::getDHTBootstrapNodes() const
625 const QString nodes
= m_DHTBootstrapNodes
;
626 return !nodes
.isEmpty() ? nodes
: DEFAULT_DHT_BOOTSTRAP_NODES
;
629 void SessionImpl::setDHTBootstrapNodes(const QString
&nodes
)
631 if (nodes
== m_DHTBootstrapNodes
)
634 m_DHTBootstrapNodes
= nodes
;
638 bool SessionImpl::isDHTEnabled() const
640 return m_isDHTEnabled
;
643 void SessionImpl::setDHTEnabled(bool enabled
)
645 if (enabled
!= m_isDHTEnabled
)
647 m_isDHTEnabled
= enabled
;
649 LogMsg(tr("Distributed Hash Table (DHT) support: %1").arg(enabled
? tr("ON") : tr("OFF")), Log::INFO
);
653 bool SessionImpl::isLSDEnabled() const
655 return m_isLSDEnabled
;
658 void SessionImpl::setLSDEnabled(const bool enabled
)
660 if (enabled
!= m_isLSDEnabled
)
662 m_isLSDEnabled
= enabled
;
664 LogMsg(tr("Local Peer Discovery support: %1").arg(enabled
? tr("ON") : tr("OFF"))
669 bool SessionImpl::isPeXEnabled() const
671 return m_isPeXEnabled
;
674 void SessionImpl::setPeXEnabled(const bool enabled
)
676 m_isPeXEnabled
= enabled
;
677 if (m_wasPexEnabled
!= enabled
)
678 LogMsg(tr("Restart is required to toggle Peer Exchange (PeX) support"), Log::WARNING
);
681 bool SessionImpl::isDownloadPathEnabled() const
683 return m_isDownloadPathEnabled
;
686 void SessionImpl::setDownloadPathEnabled(const bool enabled
)
688 if (enabled
!= isDownloadPathEnabled())
690 m_isDownloadPathEnabled
= enabled
;
691 for (TorrentImpl
*const torrent
: asConst(m_torrents
))
692 torrent
->handleCategoryOptionsChanged();
696 bool SessionImpl::isAppendExtensionEnabled() const
698 return m_isAppendExtensionEnabled
;
701 void SessionImpl::setAppendExtensionEnabled(const bool enabled
)
703 if (isAppendExtensionEnabled() != enabled
)
705 m_isAppendExtensionEnabled
= enabled
;
707 // append or remove .!qB extension for incomplete files
708 for (TorrentImpl
*const torrent
: asConst(m_torrents
))
709 torrent
->handleAppendExtensionToggled();
713 bool SessionImpl::isUnwantedFolderEnabled() const
715 return m_isUnwantedFolderEnabled
;
718 void SessionImpl::setUnwantedFolderEnabled(const bool enabled
)
720 if (isUnwantedFolderEnabled() != enabled
)
722 m_isUnwantedFolderEnabled
= enabled
;
724 // append or remove .!qB extension for incomplete files
725 for (TorrentImpl
*const torrent
: asConst(m_torrents
))
726 torrent
->handleUnwantedFolderToggled();
730 int SessionImpl::refreshInterval() const
732 return m_refreshInterval
;
735 void SessionImpl::setRefreshInterval(const int value
)
737 if (value
!= refreshInterval())
739 m_refreshInterval
= value
;
743 bool SessionImpl::isPreallocationEnabled() const
745 return m_isPreallocationEnabled
;
748 void SessionImpl::setPreallocationEnabled(const bool enabled
)
750 m_isPreallocationEnabled
= enabled
;
753 Path
SessionImpl::torrentExportDirectory() const
755 return m_torrentExportDirectory
;
758 void SessionImpl::setTorrentExportDirectory(const Path
&path
)
760 if (path
!= torrentExportDirectory())
761 m_torrentExportDirectory
= path
;
764 Path
SessionImpl::finishedTorrentExportDirectory() const
766 return m_finishedTorrentExportDirectory
;
769 void SessionImpl::setFinishedTorrentExportDirectory(const Path
&path
)
771 if (path
!= finishedTorrentExportDirectory())
772 m_finishedTorrentExportDirectory
= path
;
775 Path
SessionImpl::savePath() const
777 // TODO: Make sure it is always non-empty
781 Path
SessionImpl::downloadPath() const
783 // TODO: Make sure it is always non-empty
784 return m_downloadPath
;
787 QStringList
SessionImpl::categories() const
789 return m_categories
.keys();
792 CategoryOptions
SessionImpl::categoryOptions(const QString
&categoryName
) const
794 return m_categories
.value(categoryName
);
797 Path
SessionImpl::categorySavePath(const QString
&categoryName
) const
799 return categorySavePath(categoryName
, categoryOptions(categoryName
));
802 Path
SessionImpl::categorySavePath(const QString
&categoryName
, const CategoryOptions
&options
) const
804 Path basePath
= savePath();
805 if (categoryName
.isEmpty())
808 Path path
= options
.savePath
;
811 // use implicit save path
812 if (isSubcategoriesEnabled())
814 path
= Utils::Fs::toValidPath(subcategoryName(categoryName
));
815 basePath
= categorySavePath(parentCategoryName(categoryName
));
819 path
= Utils::Fs::toValidPath(categoryName
);
823 return (path
.isAbsolute() ? path
: (basePath
/ path
));
826 Path
SessionImpl::categoryDownloadPath(const QString
&categoryName
) const
828 return categoryDownloadPath(categoryName
, categoryOptions(categoryName
));
831 Path
SessionImpl::categoryDownloadPath(const QString
&categoryName
, const CategoryOptions
&options
) const
833 const DownloadPathOption downloadPathOption
= resolveCategoryDownloadPathOption(categoryName
, options
.downloadPath
);
834 if (!downloadPathOption
.enabled
)
837 if (categoryName
.isEmpty())
838 return downloadPath();
840 const bool useSubcategories
= isSubcategoriesEnabled();
841 const QString name
= useSubcategories
? subcategoryName(categoryName
) : categoryName
;
842 const Path path
= !downloadPathOption
.path
.isEmpty()
843 ? downloadPathOption
.path
844 : Utils::Fs::toValidPath(name
); // use implicit download path
846 if (path
.isAbsolute())
849 const QString parentName
= useSubcategories
? parentCategoryName(categoryName
) : QString();
850 CategoryOptions parentOptions
= categoryOptions(parentName
);
851 // Even if download path of parent category is disabled (directly or by inheritance)
852 // we need to construct the one as if it would be enabled.
853 if (!parentOptions
.downloadPath
|| !parentOptions
.downloadPath
->enabled
)
854 parentOptions
.downloadPath
= {true, {}};
855 const Path parentDownloadPath
= categoryDownloadPath(parentName
, parentOptions
);
856 const Path basePath
= parentDownloadPath
.isEmpty() ? downloadPath() : parentDownloadPath
;
857 return (basePath
/ path
);
860 DownloadPathOption
SessionImpl::resolveCategoryDownloadPathOption(const QString
&categoryName
, const std::optional
<DownloadPathOption
> &option
) const
862 if (categoryName
.isEmpty())
863 return {isDownloadPathEnabled(), Path()};
865 if (option
.has_value())
868 const QString parentName
= isSubcategoriesEnabled() ? parentCategoryName(categoryName
) : QString();
869 return resolveCategoryDownloadPathOption(parentName
, categoryOptions(parentName
).downloadPath
);
872 bool SessionImpl::addCategory(const QString
&name
, const CategoryOptions
&options
)
877 if (!isValidCategoryName(name
) || m_categories
.contains(name
))
880 if (isSubcategoriesEnabled())
882 for (const QString
&parent
: asConst(expandCategory(name
)))
884 if ((parent
!= name
) && !m_categories
.contains(parent
))
886 m_categories
[parent
] = {};
887 emit
categoryAdded(parent
);
892 m_categories
[name
] = options
;
894 emit
categoryAdded(name
);
899 bool SessionImpl::editCategory(const QString
&name
, const CategoryOptions
&options
)
901 const auto it
= m_categories
.find(name
);
902 if (it
== m_categories
.end())
905 CategoryOptions
¤tOptions
= it
.value();
906 if (options
== currentOptions
)
909 currentOptions
= options
;
911 if (isDisableAutoTMMWhenCategorySavePathChanged())
913 for (TorrentImpl
*const torrent
: asConst(m_torrents
))
915 if (torrent
->category() == name
)
916 torrent
->setAutoTMMEnabled(false);
921 for (TorrentImpl
*const torrent
: asConst(m_torrents
))
923 if (torrent
->category() == name
)
924 torrent
->handleCategoryOptionsChanged();
928 emit
categoryOptionsChanged(name
);
932 bool SessionImpl::removeCategory(const QString
&name
)
934 for (TorrentImpl
*const torrent
: asConst(m_torrents
))
936 if (torrent
->belongsToCategory(name
))
937 torrent
->setCategory(u
""_s
);
940 // remove stored category and its subcategories if exist
942 if (isSubcategoriesEnabled())
944 // remove subcategories
945 const QString test
= name
+ u
'/';
946 Algorithm::removeIf(m_categories
, [this, &test
, &result
](const QString
&category
, const CategoryOptions
&)
948 if (category
.startsWith(test
))
951 emit
categoryRemoved(category
);
958 result
= (m_categories
.remove(name
) > 0) || result
;
962 // update stored categories
964 emit
categoryRemoved(name
);
970 bool SessionImpl::isSubcategoriesEnabled() const
972 return m_isSubcategoriesEnabled
;
975 void SessionImpl::setSubcategoriesEnabled(const bool value
)
977 if (isSubcategoriesEnabled() == value
) return;
981 // expand categories to include all parent categories
982 m_categories
= expandCategories(m_categories
);
983 // update stored categories
992 m_isSubcategoriesEnabled
= value
;
993 emit
subcategoriesSupportChanged();
996 bool SessionImpl::useCategoryPathsInManualMode() const
998 return m_useCategoryPathsInManualMode
;
1001 void SessionImpl::setUseCategoryPathsInManualMode(const bool value
)
1003 m_useCategoryPathsInManualMode
= value
;
1006 Path
SessionImpl::suggestedSavePath(const QString
&categoryName
, std::optional
<bool> useAutoTMM
) const
1008 const bool useCategoryPaths
= useAutoTMM
.value_or(!isAutoTMMDisabledByDefault()) || useCategoryPathsInManualMode();
1009 const auto path
= (useCategoryPaths
? categorySavePath(categoryName
) : savePath());
1013 Path
SessionImpl::suggestedDownloadPath(const QString
&categoryName
, std::optional
<bool> useAutoTMM
) const
1015 const bool useCategoryPaths
= useAutoTMM
.value_or(!isAutoTMMDisabledByDefault()) || useCategoryPathsInManualMode();
1016 const auto categoryDownloadPath
= this->categoryDownloadPath(categoryName
);
1017 const auto path
= ((useCategoryPaths
&& !categoryDownloadPath
.isEmpty()) ? categoryDownloadPath
: downloadPath());
1021 TagSet
SessionImpl::tags() const
1026 bool SessionImpl::hasTag(const Tag
&tag
) const
1028 return m_tags
.contains(tag
);
1031 bool SessionImpl::addTag(const Tag
&tag
)
1033 if (!tag
.isValid() || hasTag(tag
))
1037 m_storedTags
= QStringList(m_tags
.cbegin(), m_tags
.cend());
1043 bool SessionImpl::removeTag(const Tag
&tag
)
1045 if (m_tags
.remove(tag
))
1047 for (TorrentImpl
*const torrent
: asConst(m_torrents
))
1048 torrent
->removeTag(tag
);
1050 m_storedTags
= QStringList(m_tags
.cbegin(), m_tags
.cend());
1052 emit
tagRemoved(tag
);
1058 bool SessionImpl::isAutoTMMDisabledByDefault() const
1060 return m_isAutoTMMDisabledByDefault
;
1063 void SessionImpl::setAutoTMMDisabledByDefault(const bool value
)
1065 m_isAutoTMMDisabledByDefault
= value
;
1068 bool SessionImpl::isDisableAutoTMMWhenCategoryChanged() const
1070 return m_isDisableAutoTMMWhenCategoryChanged
;
1073 void SessionImpl::setDisableAutoTMMWhenCategoryChanged(const bool value
)
1075 m_isDisableAutoTMMWhenCategoryChanged
= value
;
1078 bool SessionImpl::isDisableAutoTMMWhenDefaultSavePathChanged() const
1080 return m_isDisableAutoTMMWhenDefaultSavePathChanged
;
1083 void SessionImpl::setDisableAutoTMMWhenDefaultSavePathChanged(const bool value
)
1085 m_isDisableAutoTMMWhenDefaultSavePathChanged
= value
;
1088 bool SessionImpl::isDisableAutoTMMWhenCategorySavePathChanged() const
1090 return m_isDisableAutoTMMWhenCategorySavePathChanged
;
1093 void SessionImpl::setDisableAutoTMMWhenCategorySavePathChanged(const bool value
)
1095 m_isDisableAutoTMMWhenCategorySavePathChanged
= value
;
1098 bool SessionImpl::isAddTorrentToQueueTop() const
1100 return m_isAddTorrentToQueueTop
;
1103 void SessionImpl::setAddTorrentToQueueTop(bool value
)
1105 m_isAddTorrentToQueueTop
= value
;
1108 bool SessionImpl::isAddTorrentPaused() const
1110 return m_isAddTorrentPaused
;
1113 void SessionImpl::setAddTorrentPaused(const bool value
)
1115 m_isAddTorrentPaused
= value
;
1118 Torrent::StopCondition
SessionImpl::torrentStopCondition() const
1120 return m_torrentStopCondition
;
1123 void SessionImpl::setTorrentStopCondition(const Torrent::StopCondition stopCondition
)
1125 m_torrentStopCondition
= stopCondition
;
1128 bool SessionImpl::isTrackerEnabled() const
1130 return m_isTrackerEnabled
;
1133 void SessionImpl::setTrackerEnabled(const bool enabled
)
1135 if (m_isTrackerEnabled
!= enabled
)
1136 m_isTrackerEnabled
= enabled
;
1138 // call enableTracker() unconditionally, otherwise port change won't trigger
1140 enableTracker(enabled
);
1143 qreal
SessionImpl::globalMaxRatio() const
1145 return m_globalMaxRatio
;
1148 // Torrents with a ratio superior to the given value will
1149 // be automatically deleted
1150 void SessionImpl::setGlobalMaxRatio(qreal ratio
)
1155 if (ratio
!= globalMaxRatio())
1157 m_globalMaxRatio
= ratio
;
1158 updateSeedingLimitTimer();
1162 int SessionImpl::globalMaxSeedingMinutes() const
1164 return m_globalMaxSeedingMinutes
;
1167 void SessionImpl::setGlobalMaxSeedingMinutes(int minutes
)
1172 if (minutes
!= globalMaxSeedingMinutes())
1174 m_globalMaxSeedingMinutes
= minutes
;
1175 updateSeedingLimitTimer();
1179 int SessionImpl::globalMaxInactiveSeedingMinutes() const
1181 return m_globalMaxInactiveSeedingMinutes
;
1184 void SessionImpl::setGlobalMaxInactiveSeedingMinutes(int minutes
)
1186 minutes
= std::max(minutes
, -1);
1188 if (minutes
!= globalMaxInactiveSeedingMinutes())
1190 m_globalMaxInactiveSeedingMinutes
= minutes
;
1191 updateSeedingLimitTimer();
1195 void SessionImpl::applyBandwidthLimits()
1197 lt::settings_pack settingsPack
;
1198 settingsPack
.set_int(lt::settings_pack::download_rate_limit
, downloadSpeedLimit());
1199 settingsPack
.set_int(lt::settings_pack::upload_rate_limit
, uploadSpeedLimit());
1200 m_nativeSession
->apply_settings(std::move(settingsPack
));
1203 void SessionImpl::configure()
1205 m_nativeSession
->apply_settings(loadLTSettings());
1206 configureComponents();
1208 m_deferredConfigureScheduled
= false;
1211 void SessionImpl::configureComponents()
1213 // This function contains components/actions that:
1214 // 1. Need to be setup at start up
1215 // 2. When deferred configure is called
1217 configurePeerClasses();
1219 if (!m_IPFilteringConfigured
)
1221 if (isIPFilteringEnabled())
1225 m_IPFilteringConfigured
= true;
1229 void SessionImpl::prepareStartup()
1231 qDebug("Initializing torrents resume data storage...");
1233 const Path dbPath
= specialFolderLocation(SpecialFolder::Data
) / Path(u
"torrents.db"_s
);
1234 const bool dbStorageExists
= dbPath
.exists();
1236 auto *context
= new ResumeSessionContext(this);
1237 context
->currentStorageType
= resumeDataStorageType();
1239 if (context
->currentStorageType
== ResumeDataStorageType::SQLite
)
1241 m_resumeDataStorage
= new DBResumeDataStorage(dbPath
, this);
1243 if (!dbStorageExists
)
1245 const Path dataPath
= specialFolderLocation(SpecialFolder::Data
) / Path(u
"BT_backup"_s
);
1246 context
->startupStorage
= new BencodeResumeDataStorage(dataPath
, this);
1251 const Path dataPath
= specialFolderLocation(SpecialFolder::Data
) / Path(u
"BT_backup"_s
);
1252 m_resumeDataStorage
= new BencodeResumeDataStorage(dataPath
, this);
1254 if (dbStorageExists
)
1255 context
->startupStorage
= new DBResumeDataStorage(dbPath
, this);
1258 if (!context
->startupStorage
)
1259 context
->startupStorage
= m_resumeDataStorage
;
1261 connect(context
->startupStorage
, &ResumeDataStorage::loadStarted
, context
1262 , [this, context
](const QVector
<TorrentID
> &torrents
)
1264 context
->totalResumeDataCount
= torrents
.size();
1265 #ifdef QBT_USES_LIBTORRENT2
1266 context
->indexedTorrents
= QSet
<TorrentID
>(torrents
.cbegin(), torrents
.cend());
1269 handleLoadedResumeData(context
);
1272 connect(context
->startupStorage
, &ResumeDataStorage::loadFinished
, context
, [context
]()
1274 context
->isLoadFinished
= true;
1277 connect(this, &SessionImpl::torrentsLoaded
, context
, [this, context
](const QVector
<Torrent
*> &torrents
)
1279 context
->processingResumeDataCount
-= torrents
.count();
1280 context
->finishedResumeDataCount
+= torrents
.count();
1281 if (!context
->isLoadedResumeDataHandlingEnqueued
)
1283 QMetaObject::invokeMethod(this, [this, context
]() { handleLoadedResumeData(context
); }, Qt::QueuedConnection
);
1284 context
->isLoadedResumeDataHandlingEnqueued
= true;
1287 if (!m_refreshEnqueued
)
1289 m_nativeSession
->post_torrent_updates();
1290 m_refreshEnqueued
= true;
1293 emit
startupProgressUpdated((context
->finishedResumeDataCount
* 100.) / context
->totalResumeDataCount
);
1296 context
->startupStorage
->loadAll();
1299 void SessionImpl::handleLoadedResumeData(ResumeSessionContext
*context
)
1301 context
->isLoadedResumeDataHandlingEnqueued
= false;
1303 int count
= context
->processingResumeDataCount
;
1304 while (context
->processingResumeDataCount
< MAX_PROCESSING_RESUMEDATA_COUNT
)
1306 if (context
->loadedResumeData
.isEmpty())
1307 context
->loadedResumeData
= context
->startupStorage
->fetchLoadedResumeData();
1309 if (context
->loadedResumeData
.isEmpty())
1311 if (context
->processingResumeDataCount
== 0)
1313 if (context
->isLoadFinished
)
1315 endStartup(context
);
1317 else if (!context
->isLoadedResumeDataHandlingEnqueued
)
1319 QMetaObject::invokeMethod(this, [this, context
]() { handleLoadedResumeData(context
); }, Qt::QueuedConnection
);
1320 context
->isLoadedResumeDataHandlingEnqueued
= true;
1327 processNextResumeData(context
);
1331 context
->finishedResumeDataCount
+= (count
- context
->processingResumeDataCount
);
1334 void SessionImpl::processNextResumeData(ResumeSessionContext
*context
)
1336 const LoadedResumeData loadedResumeDataItem
= context
->loadedResumeData
.takeFirst();
1338 TorrentID torrentID
= loadedResumeDataItem
.torrentID
;
1339 #ifdef QBT_USES_LIBTORRENT2
1340 if (context
->skippedIDs
.contains(torrentID
))
1344 const nonstd::expected
<LoadTorrentParams
, QString
> &loadResumeDataResult
= loadedResumeDataItem
.result
;
1345 if (!loadResumeDataResult
)
1347 LogMsg(tr("Failed to resume torrent. Torrent: \"%1\". Reason: \"%2\"")
1348 .arg(torrentID
.toString(), loadResumeDataResult
.error()), Log::CRITICAL
);
1352 LoadTorrentParams resumeData
= *loadResumeDataResult
;
1353 bool needStore
= false;
1355 #ifdef QBT_USES_LIBTORRENT2
1356 const InfoHash infoHash
{(resumeData
.ltAddTorrentParams
.ti
1357 ? resumeData
.ltAddTorrentParams
.ti
->info_hashes()
1358 : resumeData
.ltAddTorrentParams
.info_hashes
)};
1359 const bool isHybrid
= infoHash
.isHybrid();
1360 const auto torrentIDv2
= TorrentID::fromInfoHash(infoHash
);
1361 const auto torrentIDv1
= TorrentID::fromSHA1Hash(infoHash
.v1());
1362 if (torrentID
== torrentIDv2
)
1364 if (isHybrid
&& context
->indexedTorrents
.contains(torrentIDv1
))
1366 // if we don't have metadata, try to find it in alternative "resume data"
1367 if (!resumeData
.ltAddTorrentParams
.ti
)
1369 const nonstd::expected
<LoadTorrentParams
, QString
> loadAltResumeDataResult
= context
->startupStorage
->load(torrentIDv1
);
1370 if (loadAltResumeDataResult
)
1371 resumeData
.ltAddTorrentParams
.ti
= loadAltResumeDataResult
->ltAddTorrentParams
.ti
;
1374 // remove alternative "resume data" and skip the attempt to load it
1375 m_resumeDataStorage
->remove(torrentIDv1
);
1376 context
->skippedIDs
.insert(torrentIDv1
);
1379 else if (torrentID
== torrentIDv1
)
1381 torrentID
= torrentIDv2
;
1383 m_resumeDataStorage
->remove(torrentIDv1
);
1385 if (context
->indexedTorrents
.contains(torrentID
))
1387 context
->skippedIDs
.insert(torrentID
);
1389 const nonstd::expected
<LoadTorrentParams
, QString
> loadPreferredResumeDataResult
= context
->startupStorage
->load(torrentID
);
1390 if (loadPreferredResumeDataResult
)
1392 std::shared_ptr
<lt::torrent_info
> ti
= resumeData
.ltAddTorrentParams
.ti
;
1393 resumeData
= *loadPreferredResumeDataResult
;
1394 if (!resumeData
.ltAddTorrentParams
.ti
)
1395 resumeData
.ltAddTorrentParams
.ti
= std::move(ti
);
1401 LogMsg(tr("Failed to resume torrent: inconsistent torrent ID is detected. Torrent: \"%1\"")
1402 .arg(torrentID
.toString()), Log::WARNING
);
1406 const lt::sha1_hash infoHash
= (resumeData
.ltAddTorrentParams
.ti
1407 ? resumeData
.ltAddTorrentParams
.ti
->info_hash()
1408 : resumeData
.ltAddTorrentParams
.info_hash
);
1409 if (torrentID
!= TorrentID::fromInfoHash(infoHash
))
1411 LogMsg(tr("Failed to resume torrent: inconsistent torrent ID is detected. Torrent: \"%1\"")
1412 .arg(torrentID
.toString()), Log::WARNING
);
1417 if (m_resumeDataStorage
!= context
->startupStorage
)
1420 // TODO: Remove the following upgrade code in v4.6
1421 // == BEGIN UPGRADE CODE ==
1424 if (m_needUpgradeDownloadPath
&& isDownloadPathEnabled() && !resumeData
.useAutoTMM
)
1426 resumeData
.downloadPath
= downloadPath();
1430 // == END UPGRADE CODE ==
1433 m_resumeDataStorage
->store(torrentID
, resumeData
);
1435 const QString category
= resumeData
.category
;
1436 bool isCategoryRecovered
= context
->recoveredCategories
.contains(category
);
1437 if (!category
.isEmpty() && (isCategoryRecovered
|| !m_categories
.contains(category
)))
1439 if (!isCategoryRecovered
)
1441 if (addCategory(category
))
1443 context
->recoveredCategories
.insert(category
);
1444 isCategoryRecovered
= true;
1445 LogMsg(tr("Detected inconsistent data: category is missing from the configuration file."
1446 " Category will be recovered but its settings will be reset to default."
1447 " Torrent: \"%1\". Category: \"%2\"").arg(torrentID
.toString(), category
), Log::WARNING
);
1451 resumeData
.category
.clear();
1452 LogMsg(tr("Detected inconsistent data: invalid category. Torrent: \"%1\". Category: \"%2\"")
1453 .arg(torrentID
.toString(), category
), Log::WARNING
);
1457 // We should check isCategoryRecovered again since the category
1458 // can be just recovered by the code above
1459 if (isCategoryRecovered
&& resumeData
.useAutoTMM
)
1461 const Path storageLocation
{resumeData
.ltAddTorrentParams
.save_path
};
1462 if ((storageLocation
!= categorySavePath(resumeData
.category
)) && (storageLocation
!= categoryDownloadPath(resumeData
.category
)))
1464 resumeData
.useAutoTMM
= false;
1465 resumeData
.savePath
= storageLocation
;
1466 resumeData
.downloadPath
= {};
1467 LogMsg(tr("Detected mismatch between the save paths of the recovered category and the current save path of the torrent."
1468 " Torrent is now switched to Manual mode."
1469 " Torrent: \"%1\". Category: \"%2\"").arg(torrentID
.toString(), category
), Log::WARNING
);
1474 std::erase_if(resumeData
.tags
, [this, &torrentID
](const Tag
&tag
)
1481 LogMsg(tr("Detected inconsistent data: tag is missing from the configuration file."
1482 " Tag will be recovered."
1483 " Torrent: \"%1\". Tag: \"%2\"").arg(torrentID
.toString(), tag
.toString()), Log::WARNING
);
1487 LogMsg(tr("Detected inconsistent data: invalid tag. Torrent: \"%1\". Tag: \"%2\"")
1488 .arg(torrentID
.toString(), tag
.toString()), Log::WARNING
);
1492 resumeData
.ltAddTorrentParams
.userdata
= LTClientData(new ExtensionData
);
1493 #ifndef QBT_USES_LIBTORRENT2
1494 resumeData
.ltAddTorrentParams
.storage
= customStorageConstructor
;
1497 qDebug() << "Starting up torrent" << torrentID
.toString() << "...";
1498 m_loadingTorrents
.insert(torrentID
, resumeData
);
1499 #ifdef QBT_USES_LIBTORRENT2
1500 if (infoHash
.isHybrid())
1502 // this allows to know the being added hybrid torrent by its v1 info hash
1503 // without having yet another mapping table
1504 m_hybridTorrentsByAltID
.insert(torrentIDv1
, nullptr);
1507 m_nativeSession
->async_add_torrent(resumeData
.ltAddTorrentParams
);
1508 ++context
->processingResumeDataCount
;
1511 void SessionImpl::endStartup(ResumeSessionContext
*context
)
1513 if (m_resumeDataStorage
!= context
->startupStorage
)
1515 if (isQueueingSystemEnabled())
1516 saveTorrentsQueue();
1518 const Path dbPath
= context
->startupStorage
->path();
1519 context
->startupStorage
->deleteLater();
1521 if (context
->currentStorageType
== ResumeDataStorageType::Legacy
)
1523 connect(context
->startupStorage
, &QObject::destroyed
, [dbPath
]
1525 Utils::Fs::removeFile(dbPath
);
1530 context
->deleteLater();
1531 connect(context
, &QObject::destroyed
, this, [this]
1533 m_nativeSession
->resume();
1534 if (m_refreshEnqueued
)
1535 m_refreshEnqueued
= false;
1539 m_statisticsLastUpdateTimer
.start();
1541 // Regular saving of fastresume data
1542 connect(m_resumeDataTimer
, &QTimer::timeout
, this, &SessionImpl::generateResumeData
);
1543 const int saveInterval
= saveResumeDataInterval();
1544 if (saveInterval
> 0)
1546 m_resumeDataTimer
->setInterval(std::chrono::minutes(saveInterval
));
1547 m_resumeDataTimer
->start();
1550 m_wakeupCheckTimer
= new QTimer(this);
1551 connect(m_wakeupCheckTimer
, &QTimer::timeout
, this, [this]
1553 const auto now
= QDateTime::currentDateTime();
1554 if (m_wakeupCheckTimestamp
.secsTo(now
) > 100)
1556 LogMsg(tr("System wake-up event detected. Re-announcing to all the trackers..."));
1557 reannounceToAllTrackers();
1560 m_wakeupCheckTimestamp
= QDateTime::currentDateTime();
1562 m_wakeupCheckTimestamp
= QDateTime::currentDateTime();
1563 m_wakeupCheckTimer
->start(30s
);
1565 m_isRestored
= true;
1566 emit
startupProgressUpdated(100);
1571 void SessionImpl::initializeNativeSession()
1573 lt::settings_pack pack
= loadLTSettings();
1575 const std::string peerId
= lt::generate_fingerprint(PEER_ID
, QBT_VERSION_MAJOR
, QBT_VERSION_MINOR
, QBT_VERSION_BUGFIX
, QBT_VERSION_BUILD
);
1576 pack
.set_str(lt::settings_pack::peer_fingerprint
, peerId
);
1578 pack
.set_bool(lt::settings_pack::listen_system_port_fallback
, false);
1579 pack
.set_str(lt::settings_pack::user_agent
, USER_AGENT
.toStdString());
1580 pack
.set_bool(lt::settings_pack::use_dht_as_fallback
, false);
1582 pack
.set_int(lt::settings_pack::auto_scrape_interval
, 1200); // 20 minutes
1583 pack
.set_int(lt::settings_pack::auto_scrape_min_interval
, 900); // 15 minutes
1584 // libtorrent 1.1 enables UPnP & NAT-PMP by default
1585 // turn them off before `lt::session` ctor to avoid split second effects
1586 pack
.set_bool(lt::settings_pack::enable_upnp
, false);
1587 pack
.set_bool(lt::settings_pack::enable_natpmp
, false);
1589 #ifdef QBT_USES_LIBTORRENT2
1590 // preserve the same behavior as in earlier libtorrent versions
1591 pack
.set_bool(lt::settings_pack::enable_set_file_valid_data
, true);
1594 lt::session_params sessionParams
{std::move(pack
), {}};
1595 #ifdef QBT_USES_LIBTORRENT2
1596 switch (diskIOType())
1598 case DiskIOType::Posix
:
1599 sessionParams
.disk_io_constructor
= customPosixDiskIOConstructor
;
1601 case DiskIOType::MMap
:
1602 sessionParams
.disk_io_constructor
= customMMapDiskIOConstructor
;
1605 sessionParams
.disk_io_constructor
= customDiskIOConstructor
;
1610 #if LIBTORRENT_VERSION_NUM < 20100
1611 m_nativeSession
= new lt::session(sessionParams
, lt::session::paused
);
1613 m_nativeSession
= new lt::session(sessionParams
);
1614 m_nativeSession
->pause();
1617 LogMsg(tr("Peer ID: \"%1\"").arg(QString::fromStdString(peerId
)), Log::INFO
);
1618 LogMsg(tr("HTTP User-Agent: \"%1\"").arg(USER_AGENT
), Log::INFO
);
1619 LogMsg(tr("Distributed Hash Table (DHT) support: %1").arg(isDHTEnabled() ? tr("ON") : tr("OFF")), Log::INFO
);
1620 LogMsg(tr("Local Peer Discovery support: %1").arg(isLSDEnabled() ? tr("ON") : tr("OFF")), Log::INFO
);
1621 LogMsg(tr("Peer Exchange (PeX) support: %1").arg(isPeXEnabled() ? tr("ON") : tr("OFF")), Log::INFO
);
1622 LogMsg(tr("Anonymous mode: %1").arg(isAnonymousModeEnabled() ? tr("ON") : tr("OFF")), Log::INFO
);
1623 LogMsg(tr("Encryption support: %1").arg((encryption() == 0) ? tr("ON") : ((encryption() == 1) ? tr("FORCED") : tr("OFF"))), Log::INFO
);
1625 m_nativeSession
->set_alert_notify([this]()
1627 QMetaObject::invokeMethod(this, &SessionImpl::readAlerts
, Qt::QueuedConnection
);
1631 m_nativeSession
->add_extension(<::create_smart_ban_plugin
);
1632 m_nativeSession
->add_extension(<::create_ut_metadata_plugin
);
1634 m_nativeSession
->add_extension(<::create_ut_pex_plugin
);
1636 auto nativeSessionExtension
= std::make_shared
<NativeSessionExtension
>();
1637 m_nativeSession
->add_extension(nativeSessionExtension
);
1638 m_nativeSessionExtension
= nativeSessionExtension
.get();
1641 void SessionImpl::processBannedIPs(lt::ip_filter
&filter
)
1643 // First, import current filter
1644 for (const QString
&ip
: asConst(m_bannedIPs
.get()))
1647 const lt::address addr
= lt::make_address(ip
.toLatin1().constData(), ec
);
1650 filter
.add_rule(addr
, addr
, lt::ip_filter::blocked
);
1654 void SessionImpl::initMetrics()
1656 const auto findMetricIndex
= [](const char *name
) -> int
1658 const int index
= lt::find_metric_idx(name
);
1659 Q_ASSERT(index
>= 0);
1667 .hasIncomingConnections
= findMetricIndex("net.has_incoming_connections"),
1668 .sentPayloadBytes
= findMetricIndex("net.sent_payload_bytes"),
1669 .recvPayloadBytes
= findMetricIndex("net.recv_payload_bytes"),
1670 .sentBytes
= findMetricIndex("net.sent_bytes"),
1671 .recvBytes
= findMetricIndex("net.recv_bytes"),
1672 .sentIPOverheadBytes
= findMetricIndex("net.sent_ip_overhead_bytes"),
1673 .recvIPOverheadBytes
= findMetricIndex("net.recv_ip_overhead_bytes"),
1674 .sentTrackerBytes
= findMetricIndex("net.sent_tracker_bytes"),
1675 .recvTrackerBytes
= findMetricIndex("net.recv_tracker_bytes"),
1676 .recvRedundantBytes
= findMetricIndex("net.recv_redundant_bytes"),
1677 .recvFailedBytes
= findMetricIndex("net.recv_failed_bytes")
1681 .numPeersConnected
= findMetricIndex("peer.num_peers_connected"),
1682 .numPeersUpDisk
= findMetricIndex("peer.num_peers_up_disk"),
1683 .numPeersDownDisk
= findMetricIndex("peer.num_peers_down_disk")
1687 .dhtBytesIn
= findMetricIndex("dht.dht_bytes_in"),
1688 .dhtBytesOut
= findMetricIndex("dht.dht_bytes_out"),
1689 .dhtNodes
= findMetricIndex("dht.dht_nodes")
1693 .diskBlocksInUse
= findMetricIndex("disk.disk_blocks_in_use"),
1694 .numBlocksRead
= findMetricIndex("disk.num_blocks_read"),
1695 #ifndef QBT_USES_LIBTORRENT2
1696 .numBlocksCacheHits
= findMetricIndex("disk.num_blocks_cache_hits"),
1698 .writeJobs
= findMetricIndex("disk.num_write_ops"),
1699 .readJobs
= findMetricIndex("disk.num_read_ops"),
1700 .hashJobs
= findMetricIndex("disk.num_blocks_hashed"),
1701 .queuedDiskJobs
= findMetricIndex("disk.queued_disk_jobs"),
1702 .diskJobTime
= findMetricIndex("disk.disk_job_time")
1707 lt::settings_pack
SessionImpl::loadLTSettings() const
1709 lt::settings_pack settingsPack
;
1711 const lt::alert_category_t alertMask
= lt::alert::error_notification
1712 | lt::alert::file_progress_notification
1713 | lt::alert::ip_block_notification
1714 | lt::alert::peer_notification
1715 | (isPerformanceWarningEnabled() ? lt::alert::performance_warning
: lt::alert_category_t())
1716 | lt::alert::port_mapping_notification
1717 | lt::alert::status_notification
1718 | lt::alert::storage_notification
1719 | lt::alert::tracker_notification
;
1720 settingsPack
.set_int(lt::settings_pack::alert_mask
, alertMask
);
1722 settingsPack
.set_int(lt::settings_pack::connection_speed
, connectionSpeed());
1724 // from libtorrent doc:
1725 // It will not take affect until the listen_interfaces settings is updated
1726 settingsPack
.set_int(lt::settings_pack::send_socket_buffer_size
, socketSendBufferSize());
1727 settingsPack
.set_int(lt::settings_pack::recv_socket_buffer_size
, socketReceiveBufferSize());
1728 settingsPack
.set_int(lt::settings_pack::listen_queue_size
, socketBacklogSize());
1730 applyNetworkInterfacesSettings(settingsPack
);
1732 settingsPack
.set_int(lt::settings_pack::download_rate_limit
, downloadSpeedLimit());
1733 settingsPack
.set_int(lt::settings_pack::upload_rate_limit
, uploadSpeedLimit());
1735 // The most secure, rc4 only so that all streams are encrypted
1736 settingsPack
.set_int(lt::settings_pack::allowed_enc_level
, lt::settings_pack::pe_rc4
);
1737 settingsPack
.set_bool(lt::settings_pack::prefer_rc4
, true);
1738 switch (encryption())
1741 settingsPack
.set_int(lt::settings_pack::out_enc_policy
, lt::settings_pack::pe_enabled
);
1742 settingsPack
.set_int(lt::settings_pack::in_enc_policy
, lt::settings_pack::pe_enabled
);
1745 settingsPack
.set_int(lt::settings_pack::out_enc_policy
, lt::settings_pack::pe_forced
);
1746 settingsPack
.set_int(lt::settings_pack::in_enc_policy
, lt::settings_pack::pe_forced
);
1748 default: // Disabled
1749 settingsPack
.set_int(lt::settings_pack::out_enc_policy
, lt::settings_pack::pe_disabled
);
1750 settingsPack
.set_int(lt::settings_pack::in_enc_policy
, lt::settings_pack::pe_disabled
);
1753 settingsPack
.set_int(lt::settings_pack::active_checking
, maxActiveCheckingTorrents());
1756 #if defined(QBT_USES_LIBTORRENT2) && TORRENT_USE_I2P
1759 settingsPack
.set_str(lt::settings_pack::i2p_hostname
, I2PAddress().toStdString());
1760 settingsPack
.set_int(lt::settings_pack::i2p_port
, I2PPort());
1761 settingsPack
.set_bool(lt::settings_pack::allow_i2p_mixed
, I2PMixedMode());
1765 settingsPack
.set_str(lt::settings_pack::i2p_hostname
, "");
1766 settingsPack
.set_int(lt::settings_pack::i2p_port
, 0);
1767 settingsPack
.set_bool(lt::settings_pack::allow_i2p_mixed
, false);
1770 // I2P session options
1771 settingsPack
.set_int(lt::settings_pack::i2p_inbound_quantity
, I2PInboundQuantity());
1772 settingsPack
.set_int(lt::settings_pack::i2p_outbound_quantity
, I2POutboundQuantity());
1773 settingsPack
.set_int(lt::settings_pack::i2p_inbound_length
, I2PInboundLength());
1774 settingsPack
.set_int(lt::settings_pack::i2p_outbound_length
, I2POutboundLength());
1778 settingsPack
.set_int(lt::settings_pack::proxy_type
, lt::settings_pack::none
);
1779 const auto *proxyManager
= Net::ProxyConfigurationManager::instance();
1780 const Net::ProxyConfiguration proxyConfig
= proxyManager
->proxyConfiguration();
1781 if ((proxyConfig
.type
!= Net::ProxyType::None
) && Preferences::instance()->useProxyForBT())
1783 switch (proxyConfig
.type
)
1785 case Net::ProxyType::SOCKS4
:
1786 settingsPack
.set_int(lt::settings_pack::proxy_type
, lt::settings_pack::socks4
);
1789 case Net::ProxyType::HTTP
:
1790 if (proxyConfig
.authEnabled
)
1791 settingsPack
.set_int(lt::settings_pack::proxy_type
, lt::settings_pack::http_pw
);
1793 settingsPack
.set_int(lt::settings_pack::proxy_type
, lt::settings_pack::http
);
1796 case Net::ProxyType::SOCKS5
:
1797 if (proxyConfig
.authEnabled
)
1798 settingsPack
.set_int(lt::settings_pack::proxy_type
, lt::settings_pack::socks5_pw
);
1800 settingsPack
.set_int(lt::settings_pack::proxy_type
, lt::settings_pack::socks5
);
1807 settingsPack
.set_str(lt::settings_pack::proxy_hostname
, proxyConfig
.ip
.toStdString());
1808 settingsPack
.set_int(lt::settings_pack::proxy_port
, proxyConfig
.port
);
1810 if (proxyConfig
.authEnabled
)
1812 settingsPack
.set_str(lt::settings_pack::proxy_username
, proxyConfig
.username
.toStdString());
1813 settingsPack
.set_str(lt::settings_pack::proxy_password
, proxyConfig
.password
.toStdString());
1816 settingsPack
.set_bool(lt::settings_pack::proxy_peer_connections
, isProxyPeerConnectionsEnabled());
1817 settingsPack
.set_bool(lt::settings_pack::proxy_hostnames
, proxyConfig
.hostnameLookupEnabled
);
1820 settingsPack
.set_bool(lt::settings_pack::announce_to_all_trackers
, announceToAllTrackers());
1821 settingsPack
.set_bool(lt::settings_pack::announce_to_all_tiers
, announceToAllTiers());
1823 settingsPack
.set_int(lt::settings_pack::peer_turnover
, peerTurnover());
1824 settingsPack
.set_int(lt::settings_pack::peer_turnover_cutoff
, peerTurnoverCutoff());
1825 settingsPack
.set_int(lt::settings_pack::peer_turnover_interval
, peerTurnoverInterval());
1827 settingsPack
.set_int(lt::settings_pack::max_out_request_queue
, requestQueueSize());
1829 #ifdef QBT_USES_LIBTORRENT2
1830 settingsPack
.set_int(lt::settings_pack::metadata_token_limit
, Preferences::instance()->getBdecodeTokenLimit());
1833 settingsPack
.set_int(lt::settings_pack::aio_threads
, asyncIOThreads());
1834 #ifdef QBT_USES_LIBTORRENT2
1835 settingsPack
.set_int(lt::settings_pack::hashing_threads
, hashingThreads());
1837 settingsPack
.set_int(lt::settings_pack::file_pool_size
, filePoolSize());
1839 const int checkingMemUsageSize
= checkingMemUsage() * 64;
1840 settingsPack
.set_int(lt::settings_pack::checking_mem_usage
, checkingMemUsageSize
);
1842 #ifndef QBT_USES_LIBTORRENT2
1843 const int cacheSize
= (diskCacheSize() > -1) ? (diskCacheSize() * 64) : -1;
1844 settingsPack
.set_int(lt::settings_pack::cache_size
, cacheSize
);
1845 settingsPack
.set_int(lt::settings_pack::cache_expiry
, diskCacheTTL());
1848 settingsPack
.set_int(lt::settings_pack::max_queued_disk_bytes
, diskQueueSize());
1850 switch (diskIOReadMode())
1852 case DiskIOReadMode::DisableOSCache
:
1853 settingsPack
.set_int(lt::settings_pack::disk_io_read_mode
, lt::settings_pack::disable_os_cache
);
1855 case DiskIOReadMode::EnableOSCache
:
1857 settingsPack
.set_int(lt::settings_pack::disk_io_read_mode
, lt::settings_pack::enable_os_cache
);
1861 switch (diskIOWriteMode())
1863 case DiskIOWriteMode::DisableOSCache
:
1864 settingsPack
.set_int(lt::settings_pack::disk_io_write_mode
, lt::settings_pack::disable_os_cache
);
1866 case DiskIOWriteMode::EnableOSCache
:
1868 settingsPack
.set_int(lt::settings_pack::disk_io_write_mode
, lt::settings_pack::enable_os_cache
);
1870 #ifdef QBT_USES_LIBTORRENT2
1871 case DiskIOWriteMode::WriteThrough
:
1872 settingsPack
.set_int(lt::settings_pack::disk_io_write_mode
, lt::settings_pack::write_through
);
1877 #ifndef QBT_USES_LIBTORRENT2
1878 settingsPack
.set_bool(lt::settings_pack::coalesce_reads
, isCoalesceReadWriteEnabled());
1879 settingsPack
.set_bool(lt::settings_pack::coalesce_writes
, isCoalesceReadWriteEnabled());
1882 settingsPack
.set_bool(lt::settings_pack::piece_extent_affinity
, usePieceExtentAffinity());
1884 settingsPack
.set_int(lt::settings_pack::suggest_mode
, isSuggestModeEnabled()
1885 ? lt::settings_pack::suggest_read_cache
: lt::settings_pack::no_piece_suggestions
);
1887 settingsPack
.set_int(lt::settings_pack::send_buffer_watermark
, sendBufferWatermark() * 1024);
1888 settingsPack
.set_int(lt::settings_pack::send_buffer_low_watermark
, sendBufferLowWatermark() * 1024);
1889 settingsPack
.set_int(lt::settings_pack::send_buffer_watermark_factor
, sendBufferWatermarkFactor());
1891 settingsPack
.set_bool(lt::settings_pack::anonymous_mode
, isAnonymousModeEnabled());
1894 if (isQueueingSystemEnabled())
1896 settingsPack
.set_int(lt::settings_pack::active_downloads
, maxActiveDownloads());
1897 settingsPack
.set_int(lt::settings_pack::active_limit
, maxActiveTorrents());
1898 settingsPack
.set_int(lt::settings_pack::active_seeds
, maxActiveUploads());
1899 settingsPack
.set_bool(lt::settings_pack::dont_count_slow_torrents
, ignoreSlowTorrentsForQueueing());
1900 settingsPack
.set_int(lt::settings_pack::inactive_down_rate
, downloadRateForSlowTorrents() * 1024); // KiB to Bytes
1901 settingsPack
.set_int(lt::settings_pack::inactive_up_rate
, uploadRateForSlowTorrents() * 1024); // KiB to Bytes
1902 settingsPack
.set_int(lt::settings_pack::auto_manage_startup
, slowTorrentsInactivityTimer());
1906 settingsPack
.set_int(lt::settings_pack::active_downloads
, -1);
1907 settingsPack
.set_int(lt::settings_pack::active_seeds
, -1);
1908 settingsPack
.set_int(lt::settings_pack::active_limit
, -1);
1910 settingsPack
.set_int(lt::settings_pack::active_tracker_limit
, -1);
1911 settingsPack
.set_int(lt::settings_pack::active_dht_limit
, -1);
1912 settingsPack
.set_int(lt::settings_pack::active_lsd_limit
, -1);
1913 settingsPack
.set_int(lt::settings_pack::alert_queue_size
, std::numeric_limits
<int>::max() / 2);
1916 settingsPack
.set_int(lt::settings_pack::outgoing_port
, outgoingPortsMin());
1917 settingsPack
.set_int(lt::settings_pack::num_outgoing_ports
, (outgoingPortsMax() - outgoingPortsMin()));
1918 // UPnP lease duration
1919 settingsPack
.set_int(lt::settings_pack::upnp_lease_duration
, UPnPLeaseDuration());
1921 settingsPack
.set_int(lt::settings_pack::peer_tos
, peerToS());
1922 // Include overhead in transfer limits
1923 settingsPack
.set_bool(lt::settings_pack::rate_limit_ip_overhead
, includeOverheadInLimits());
1924 // IP address to announce to trackers
1925 settingsPack
.set_str(lt::settings_pack::announce_ip
, announceIP().toStdString());
1926 // Max concurrent HTTP announces
1927 settingsPack
.set_int(lt::settings_pack::max_concurrent_http_announces
, maxConcurrentHTTPAnnounces());
1928 // Stop tracker timeout
1929 settingsPack
.set_int(lt::settings_pack::stop_tracker_timeout
, stopTrackerTimeout());
1930 // * Max connections limit
1931 settingsPack
.set_int(lt::settings_pack::connections_limit
, maxConnections());
1932 // * Global max upload slots
1933 settingsPack
.set_int(lt::settings_pack::unchoke_slots_limit
, maxUploads());
1935 switch (btProtocol())
1937 case BTProtocol::Both
:
1939 settingsPack
.set_bool(lt::settings_pack::enable_incoming_tcp
, true);
1940 settingsPack
.set_bool(lt::settings_pack::enable_outgoing_tcp
, true);
1941 settingsPack
.set_bool(lt::settings_pack::enable_incoming_utp
, true);
1942 settingsPack
.set_bool(lt::settings_pack::enable_outgoing_utp
, true);
1945 case BTProtocol::TCP
:
1946 settingsPack
.set_bool(lt::settings_pack::enable_incoming_tcp
, true);
1947 settingsPack
.set_bool(lt::settings_pack::enable_outgoing_tcp
, true);
1948 settingsPack
.set_bool(lt::settings_pack::enable_incoming_utp
, false);
1949 settingsPack
.set_bool(lt::settings_pack::enable_outgoing_utp
, false);
1952 case BTProtocol::UTP
:
1953 settingsPack
.set_bool(lt::settings_pack::enable_incoming_tcp
, false);
1954 settingsPack
.set_bool(lt::settings_pack::enable_outgoing_tcp
, false);
1955 settingsPack
.set_bool(lt::settings_pack::enable_incoming_utp
, true);
1956 settingsPack
.set_bool(lt::settings_pack::enable_outgoing_utp
, true);
1960 switch (utpMixedMode())
1962 case MixedModeAlgorithm::TCP
:
1964 settingsPack
.set_int(lt::settings_pack::mixed_mode_algorithm
, lt::settings_pack::prefer_tcp
);
1966 case MixedModeAlgorithm::Proportional
:
1967 settingsPack
.set_int(lt::settings_pack::mixed_mode_algorithm
, lt::settings_pack::peer_proportional
);
1971 settingsPack
.set_bool(lt::settings_pack::allow_idna
, isIDNSupportEnabled());
1973 settingsPack
.set_bool(lt::settings_pack::allow_multiple_connections_per_ip
, multiConnectionsPerIpEnabled());
1975 settingsPack
.set_bool(lt::settings_pack::validate_https_trackers
, validateHTTPSTrackerCertificate());
1977 settingsPack
.set_bool(lt::settings_pack::ssrf_mitigation
, isSSRFMitigationEnabled());
1979 settingsPack
.set_bool(lt::settings_pack::no_connect_privileged_ports
, blockPeersOnPrivilegedPorts());
1981 settingsPack
.set_bool(lt::settings_pack::apply_ip_filter_to_trackers
, isTrackerFilteringEnabled());
1983 settingsPack
.set_str(lt::settings_pack::dht_bootstrap_nodes
, getDHTBootstrapNodes().toStdString());
1984 settingsPack
.set_bool(lt::settings_pack::enable_dht
, isDHTEnabled());
1985 settingsPack
.set_bool(lt::settings_pack::enable_lsd
, isLSDEnabled());
1987 switch (chokingAlgorithm())
1989 case ChokingAlgorithm::FixedSlots
:
1991 settingsPack
.set_int(lt::settings_pack::choking_algorithm
, lt::settings_pack::fixed_slots_choker
);
1993 case ChokingAlgorithm::RateBased
:
1994 settingsPack
.set_int(lt::settings_pack::choking_algorithm
, lt::settings_pack::rate_based_choker
);
1998 switch (seedChokingAlgorithm())
2000 case SeedChokingAlgorithm::RoundRobin
:
2001 settingsPack
.set_int(lt::settings_pack::seed_choking_algorithm
, lt::settings_pack::round_robin
);
2003 case SeedChokingAlgorithm::FastestUpload
:
2005 settingsPack
.set_int(lt::settings_pack::seed_choking_algorithm
, lt::settings_pack::fastest_upload
);
2007 case SeedChokingAlgorithm::AntiLeech
:
2008 settingsPack
.set_int(lt::settings_pack::seed_choking_algorithm
, lt::settings_pack::anti_leech
);
2012 return settingsPack
;
2015 void SessionImpl::applyNetworkInterfacesSettings(lt::settings_pack
&settingsPack
) const
2017 if (m_listenInterfaceConfigured
)
2020 if (port() > 0) // user has specified port number
2021 settingsPack
.set_int(lt::settings_pack::max_retry_port_bind
, 0);
2023 QStringList endpoints
;
2024 QStringList outgoingInterfaces
;
2025 const QString portString
= u
':' + QString::number(port());
2027 for (const QString
&ip
: asConst(getListeningIPs()))
2029 const QHostAddress addr
{ip
};
2032 const bool isIPv6
= (addr
.protocol() == QAbstractSocket::IPv6Protocol
);
2033 const QString ip
= isIPv6
2034 ? Utils::Net::canonicalIPv6Addr(addr
).toString()
2037 endpoints
<< ((isIPv6
? (u
'[' + ip
+ u
']') : ip
) + portString
);
2039 if ((ip
!= u
"0.0.0.0") && (ip
!= u
"::"))
2040 outgoingInterfaces
<< ip
;
2044 // ip holds an interface name
2046 // On Vista+ versions and after Qt 5.5 QNetworkInterface::name() returns
2047 // the interface's LUID and not the GUID.
2048 // Libtorrent expects GUIDs for the 'listen_interfaces' setting.
2049 const QString guid
= convertIfaceNameToGuid(ip
);
2050 if (!guid
.isEmpty())
2052 endpoints
<< (guid
+ portString
);
2053 outgoingInterfaces
<< guid
;
2057 LogMsg(tr("Could not find GUID of network interface. Interface: \"%1\"").arg(ip
), Log::WARNING
);
2058 // Since we can't get the GUID, we'll pass the interface name instead.
2059 // Otherwise an empty string will be passed to outgoing_interface which will cause IP leak.
2060 endpoints
<< (ip
+ portString
);
2061 outgoingInterfaces
<< ip
;
2064 endpoints
<< (ip
+ portString
);
2065 outgoingInterfaces
<< ip
;
2070 const QString finalEndpoints
= endpoints
.join(u
',');
2071 settingsPack
.set_str(lt::settings_pack::listen_interfaces
, finalEndpoints
.toStdString());
2072 LogMsg(tr("Trying to listen on the following list of IP addresses: \"%1\"").arg(finalEndpoints
));
2074 settingsPack
.set_str(lt::settings_pack::outgoing_interfaces
, outgoingInterfaces
.join(u
',').toStdString());
2075 m_listenInterfaceConfigured
= true;
2078 void SessionImpl::configurePeerClasses()
2081 // lt::make_address("255.255.255.255") crashes on some people's systems
2082 // so instead we use address_v4::broadcast()
2083 // Proactively do the same for 0.0.0.0 and address_v4::any()
2084 f
.add_rule(lt::address_v4::any()
2085 , lt::address_v4::broadcast()
2086 , 1 << LT::toUnderlyingType(lt::session::global_peer_class_id
));
2088 // IPv6 may not be available on OS and the parsing
2089 // would result in an exception -> abnormal program termination
2090 // Affects Windows XP
2093 f
.add_rule(lt::address_v6::any()
2094 , lt::make_address("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")
2095 , 1 << LT::toUnderlyingType(lt::session::global_peer_class_id
));
2097 catch (const std::exception
&) {}
2099 if (ignoreLimitsOnLAN())
2102 f
.add_rule(lt::make_address("10.0.0.0")
2103 , lt::make_address("10.255.255.255")
2104 , 1 << LT::toUnderlyingType(lt::session::local_peer_class_id
));
2105 f
.add_rule(lt::make_address("172.16.0.0")
2106 , lt::make_address("172.31.255.255")
2107 , 1 << LT::toUnderlyingType(lt::session::local_peer_class_id
));
2108 f
.add_rule(lt::make_address("192.168.0.0")
2109 , lt::make_address("192.168.255.255")
2110 , 1 << LT::toUnderlyingType(lt::session::local_peer_class_id
));
2112 f
.add_rule(lt::make_address("169.254.0.0")
2113 , lt::make_address("169.254.255.255")
2114 , 1 << LT::toUnderlyingType(lt::session::local_peer_class_id
));
2116 f
.add_rule(lt::make_address("127.0.0.0")
2117 , lt::make_address("127.255.255.255")
2118 , 1 << LT::toUnderlyingType(lt::session::local_peer_class_id
));
2120 // IPv6 may not be available on OS and the parsing
2121 // would result in an exception -> abnormal program termination
2122 // Affects Windows XP
2126 f
.add_rule(lt::make_address("fe80::")
2127 , lt::make_address("febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff")
2128 , 1 << LT::toUnderlyingType(lt::session::local_peer_class_id
));
2129 // unique local addresses
2130 f
.add_rule(lt::make_address("fc00::")
2131 , lt::make_address("fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")
2132 , 1 << LT::toUnderlyingType(lt::session::local_peer_class_id
));
2134 f
.add_rule(lt::address_v6::loopback()
2135 , lt::address_v6::loopback()
2136 , 1 << LT::toUnderlyingType(lt::session::local_peer_class_id
));
2138 catch (const std::exception
&) {}
2140 m_nativeSession
->set_peer_class_filter(f
);
2142 lt::peer_class_type_filter peerClassTypeFilter
;
2143 peerClassTypeFilter
.add(lt::peer_class_type_filter::tcp_socket
, lt::session::tcp_peer_class_id
);
2144 peerClassTypeFilter
.add(lt::peer_class_type_filter::ssl_tcp_socket
, lt::session::tcp_peer_class_id
);
2145 peerClassTypeFilter
.add(lt::peer_class_type_filter::i2p_socket
, lt::session::tcp_peer_class_id
);
2146 if (!isUTPRateLimited())
2148 peerClassTypeFilter
.disallow(lt::peer_class_type_filter::utp_socket
2149 , lt::session::global_peer_class_id
);
2150 peerClassTypeFilter
.disallow(lt::peer_class_type_filter::ssl_utp_socket
2151 , lt::session::global_peer_class_id
);
2153 m_nativeSession
->set_peer_class_type_filter(peerClassTypeFilter
);
2156 void SessionImpl::enableTracker(const bool enable
)
2158 const QString profile
= u
"embeddedTracker"_s
;
2159 auto *portForwarder
= Net::PortForwarder::instance();
2164 m_tracker
= new Tracker(this);
2168 const auto *pref
= Preferences::instance();
2169 if (pref
->isTrackerPortForwardingEnabled())
2170 portForwarder
->setPorts(profile
, {static_cast<quint16
>(pref
->getTrackerPort())});
2172 portForwarder
->removePorts(profile
);
2178 portForwarder
->removePorts(profile
);
2182 void SessionImpl::enableBandwidthScheduler()
2186 m_bwScheduler
= new BandwidthScheduler(this);
2187 connect(m_bwScheduler
.data(), &BandwidthScheduler::bandwidthLimitRequested
2188 , this, &SessionImpl::setAltGlobalSpeedLimitEnabled
);
2190 m_bwScheduler
->start();
2193 void SessionImpl::populateAdditionalTrackers()
2195 m_additionalTrackerList
.clear();
2197 const QString trackers
= additionalTrackers();
2198 for (QStringView tracker
: asConst(QStringView(trackers
).split(u
'\n')))
2200 tracker
= tracker
.trimmed();
2201 if (!tracker
.isEmpty())
2202 m_additionalTrackerList
.append({tracker
.toString()});
2206 void SessionImpl::processShareLimits()
2208 qDebug("Processing share limits...");
2210 const auto resolveLimitValue
= []<typename T
>(const T limit
, const T useGlobalLimit
, const T globalLimit
) -> T
2212 return (limit
== useGlobalLimit
) ? globalLimit
: limit
;
2215 // We shouldn't iterate over `m_torrents` in the loop below
2216 // since `deleteTorrent()` modifies it indirectly
2217 const QHash
<TorrentID
, TorrentImpl
*> torrents
{m_torrents
};
2218 for (const auto &[torrentID
, torrent
] : torrents
.asKeyValueRange())
2220 if (!torrent
->isFinished() || torrent
->isForced())
2223 if (const qreal ratioLimit
= resolveLimitValue(torrent
->ratioLimit(), Torrent::USE_GLOBAL_RATIO
, globalMaxRatio());
2226 const qreal ratio
= torrent
->realRatio();
2227 qDebug("Ratio: %f (limit: %f)", ratio
, ratioLimit
);
2229 if ((ratio
<= Torrent::MAX_RATIO
) && (ratio
>= ratioLimit
))
2231 const QString description
= tr("Torrent reached the share ratio limit.");
2232 const QString torrentName
= tr("Torrent: \"%1\".").arg(torrent
->name());
2234 if (m_maxRatioAction
== Remove
)
2236 LogMsg(u
"%1 %2 %3"_s
.arg(description
, tr("Removed torrent."), torrentName
));
2237 deleteTorrent(torrentID
);
2239 else if (m_maxRatioAction
== DeleteFiles
)
2241 LogMsg(u
"%1 %2 %3"_s
.arg(description
, tr("Removed torrent and deleted its content."), torrentName
));
2242 deleteTorrent(torrentID
, DeleteTorrentAndFiles
);
2244 else if ((m_maxRatioAction
== Pause
) && !torrent
->isPaused())
2247 LogMsg(u
"%1 %2 %3"_s
.arg(description
, tr("Torrent paused."), torrentName
));
2249 else if ((m_maxRatioAction
== EnableSuperSeeding
) && !torrent
->isPaused() && !torrent
->superSeeding())
2251 torrent
->setSuperSeeding(true);
2252 LogMsg(u
"%1 %2 %3"_s
.arg(description
, tr("Super seeding enabled."), torrentName
));
2259 if (const int seedingTimeLimit
= resolveLimitValue(torrent
->seedingTimeLimit(), Torrent::USE_GLOBAL_SEEDING_TIME
, globalMaxSeedingMinutes());
2260 seedingTimeLimit
>= 0)
2262 const qlonglong seedingTimeInMinutes
= torrent
->finishedTime() / 60;
2264 if ((seedingTimeInMinutes
<= Torrent::MAX_SEEDING_TIME
) && (seedingTimeInMinutes
>= seedingTimeLimit
))
2266 const QString description
= tr("Torrent reached the seeding time limit.");
2267 const QString torrentName
= tr("Torrent: \"%1\".").arg(torrent
->name());
2269 if (m_maxRatioAction
== Remove
)
2271 LogMsg(u
"%1 %2 %3"_s
.arg(description
, tr("Removed torrent."), torrentName
));
2272 deleteTorrent(torrentID
);
2274 else if (m_maxRatioAction
== DeleteFiles
)
2276 LogMsg(u
"%1 %2 %3"_s
.arg(description
, tr("Removed torrent and deleted its content."), torrentName
));
2277 deleteTorrent(torrentID
, DeleteTorrentAndFiles
);
2279 else if ((m_maxRatioAction
== Pause
) && !torrent
->isPaused())
2282 LogMsg(u
"%1 %2 %3"_s
.arg(description
, tr("Torrent paused."), torrentName
));
2284 else if ((m_maxRatioAction
== EnableSuperSeeding
) && !torrent
->isPaused() && !torrent
->superSeeding())
2286 torrent
->setSuperSeeding(true);
2287 LogMsg(u
"%1 %2 %3"_s
.arg(description
, tr("Super seeding enabled."), torrentName
));
2294 if (const int inactiveSeedingTimeLimit
= resolveLimitValue(torrent
->inactiveSeedingTimeLimit(), Torrent::USE_GLOBAL_INACTIVE_SEEDING_TIME
, globalMaxInactiveSeedingMinutes());
2295 inactiveSeedingTimeLimit
>= 0)
2297 const qlonglong inactiveSeedingTimeInMinutes
= torrent
->timeSinceActivity() / 60;
2299 if ((inactiveSeedingTimeInMinutes
<= Torrent::MAX_INACTIVE_SEEDING_TIME
) && (inactiveSeedingTimeInMinutes
>= inactiveSeedingTimeLimit
))
2301 const QString description
= tr("Torrent reached the inactive seeding time limit.");
2302 const QString torrentName
= tr("Torrent: \"%1\".").arg(torrent
->name());
2304 if (m_maxRatioAction
== Remove
)
2306 LogMsg(u
"%1 %2 %3"_s
.arg(description
, tr("Removed torrent."), torrentName
));
2307 deleteTorrent(torrentID
);
2309 else if (m_maxRatioAction
== DeleteFiles
)
2311 LogMsg(u
"%1 %2 %3"_s
.arg(description
, tr("Removed torrent and deleted its content."), torrentName
));
2312 deleteTorrent(torrentID
, DeleteTorrentAndFiles
);
2314 else if ((m_maxRatioAction
== Pause
) && !torrent
->isPaused())
2317 LogMsg(u
"%1 %2 %3"_s
.arg(description
, tr("Torrent paused."), torrentName
));
2319 else if ((m_maxRatioAction
== EnableSuperSeeding
) && !torrent
->isPaused() && !torrent
->superSeeding())
2321 torrent
->setSuperSeeding(true);
2322 LogMsg(u
"%1 %2 %3"_s
.arg(description
, tr("Super seeding enabled."), torrentName
));
2329 void SessionImpl::fileSearchFinished(const TorrentID
&id
, const Path
&savePath
, const PathList
&fileNames
)
2331 TorrentImpl
*torrent
= m_torrents
.value(id
);
2334 torrent
->fileSearchFinished(savePath
, fileNames
);
2338 const auto loadingTorrentsIter
= m_loadingTorrents
.find(id
);
2339 if (loadingTorrentsIter
!= m_loadingTorrents
.end())
2341 LoadTorrentParams
¶ms
= loadingTorrentsIter
.value();
2342 lt::add_torrent_params
&p
= params
.ltAddTorrentParams
;
2344 p
.save_path
= savePath
.toString().toStdString();
2345 const TorrentInfo torrentInfo
{*p
.ti
};
2346 const auto nativeIndexes
= torrentInfo
.nativeIndexes();
2347 for (int i
= 0; i
< fileNames
.size(); ++i
)
2348 p
.renamed_files
[nativeIndexes
[i
]] = fileNames
[i
].toString().toStdString();
2350 m_nativeSession
->async_add_torrent(p
);
2354 Torrent
*SessionImpl::getTorrent(const TorrentID
&id
) const
2356 return m_torrents
.value(id
);
2359 Torrent
*SessionImpl::findTorrent(const InfoHash
&infoHash
) const
2361 const auto id
= TorrentID::fromInfoHash(infoHash
);
2362 if (Torrent
*torrent
= m_torrents
.value(id
); torrent
)
2365 if (!infoHash
.isHybrid())
2366 return m_hybridTorrentsByAltID
.value(id
);
2368 // alternative ID can be useful to find existing torrent
2369 // in case if hybrid torrent was added by v1 info hash
2370 const auto altID
= TorrentID::fromSHA1Hash(infoHash
.v1());
2371 return m_torrents
.value(altID
);
2374 void SessionImpl::banIP(const QString
&ip
)
2376 if (m_bannedIPs
.get().contains(ip
))
2380 const lt::address addr
= lt::make_address(ip
.toLatin1().constData(), ec
);
2385 invokeAsync([session
= m_nativeSession
, addr
]
2387 lt::ip_filter filter
= session
->get_ip_filter();
2388 filter
.add_rule(addr
, addr
, lt::ip_filter::blocked
);
2389 session
->set_ip_filter(std::move(filter
));
2392 QStringList bannedIPs
= m_bannedIPs
;
2393 bannedIPs
.append(ip
);
2395 m_bannedIPs
= bannedIPs
;
2398 // Delete a torrent from the session, given its hash
2399 // and from the disk, if the corresponding deleteOption is chosen
2400 bool SessionImpl::deleteTorrent(const TorrentID
&id
, const DeleteOption deleteOption
)
2402 TorrentImpl
*const torrent
= m_torrents
.take(id
);
2403 if (!torrent
) return false;
2405 qDebug("Deleting torrent with ID: %s", qUtf8Printable(torrent
->id().toString()));
2406 emit
torrentAboutToBeRemoved(torrent
);
2408 if (const InfoHash infoHash
= torrent
->infoHash(); infoHash
.isHybrid())
2409 m_hybridTorrentsByAltID
.remove(TorrentID::fromSHA1Hash(infoHash
.v1()));
2411 // Remove it from session
2412 if (deleteOption
== DeleteTorrent
)
2414 m_removingTorrents
[torrent
->id()] = {torrent
->name(), {}, deleteOption
};
2416 const lt::torrent_handle nativeHandle
{torrent
->nativeHandle()};
2417 const auto iter
= std::find_if(m_moveStorageQueue
.begin(), m_moveStorageQueue
.end()
2418 , [&nativeHandle
](const MoveStorageJob
&job
)
2420 return job
.torrentHandle
== nativeHandle
;
2422 if (iter
!= m_moveStorageQueue
.end())
2424 // We shouldn't actually remove torrent until existing "move storage jobs" are done
2425 torrentQueuePositionBottom(nativeHandle
);
2426 nativeHandle
.unset_flags(lt::torrent_flags::auto_managed
);
2427 nativeHandle
.pause();
2431 m_nativeSession
->remove_torrent(nativeHandle
, lt::session::delete_partfile
);
2436 m_removingTorrents
[torrent
->id()] = {torrent
->name(), torrent
->rootPath(), deleteOption
};
2438 if (m_moveStorageQueue
.size() > 1)
2440 // Delete "move storage job" for the deleted torrent
2441 // (note: we shouldn't delete active job)
2442 const auto iter
= std::find_if((m_moveStorageQueue
.begin() + 1), m_moveStorageQueue
.end()
2443 , [torrent
](const MoveStorageJob
&job
)
2445 return job
.torrentHandle
== torrent
->nativeHandle();
2447 if (iter
!= m_moveStorageQueue
.end())
2448 m_moveStorageQueue
.erase(iter
);
2451 m_nativeSession
->remove_torrent(torrent
->nativeHandle(), lt::session::delete_files
);
2454 // Remove it from torrent resume directory
2455 m_resumeDataStorage
->remove(torrent
->id());
2461 bool SessionImpl::cancelDownloadMetadata(const TorrentID
&id
)
2463 const auto downloadedMetadataIter
= m_downloadedMetadata
.find(id
);
2464 if (downloadedMetadataIter
== m_downloadedMetadata
.end())
2467 const lt::torrent_handle nativeHandle
= downloadedMetadataIter
.value();
2468 m_downloadedMetadata
.erase(downloadedMetadataIter
);
2470 if (!nativeHandle
.is_valid())
2473 #ifdef QBT_USES_LIBTORRENT2
2474 const InfoHash infoHash
{nativeHandle
.info_hashes()};
2475 if (infoHash
.isHybrid())
2477 // if magnet link was hybrid initially then it is indexed also by v1 info hash
2478 // so we need to remove both entries
2479 const auto altID
= TorrentID::fromSHA1Hash(infoHash
.v1());
2480 m_downloadedMetadata
.remove((altID
== downloadedMetadataIter
.key()) ? id
: altID
);
2484 m_nativeSession
->remove_torrent(nativeHandle
, lt::session::delete_files
);
2488 void SessionImpl::increaseTorrentsQueuePos(const QVector
<TorrentID
> &ids
)
2490 using ElementType
= std::pair
<int, const TorrentImpl
*>;
2491 std::priority_queue
<ElementType
2492 , std::vector
<ElementType
>
2493 , std::greater
<ElementType
>> torrentQueue
;
2495 // Sort torrents by queue position
2496 for (const TorrentID
&id
: ids
)
2498 const TorrentImpl
*torrent
= m_torrents
.value(id
);
2499 if (!torrent
) continue;
2500 if (const int position
= torrent
->queuePosition(); position
>= 0)
2501 torrentQueue
.emplace(position
, torrent
);
2504 // Increase torrents queue position (starting with the one in the highest queue position)
2505 while (!torrentQueue
.empty())
2507 const TorrentImpl
*torrent
= torrentQueue
.top().second
;
2508 torrentQueuePositionUp(torrent
->nativeHandle());
2512 m_torrentsQueueChanged
= true;
2515 void SessionImpl::decreaseTorrentsQueuePos(const QVector
<TorrentID
> &ids
)
2517 using ElementType
= std::pair
<int, const TorrentImpl
*>;
2518 std::priority_queue
<ElementType
> torrentQueue
;
2520 // Sort torrents by queue position
2521 for (const TorrentID
&id
: ids
)
2523 const TorrentImpl
*torrent
= m_torrents
.value(id
);
2524 if (!torrent
) continue;
2525 if (const int position
= torrent
->queuePosition(); position
>= 0)
2526 torrentQueue
.emplace(position
, torrent
);
2529 // Decrease torrents queue position (starting with the one in the lowest queue position)
2530 while (!torrentQueue
.empty())
2532 const TorrentImpl
*torrent
= torrentQueue
.top().second
;
2533 torrentQueuePositionDown(torrent
->nativeHandle());
2537 for (const lt::torrent_handle
&torrentHandle
: asConst(m_downloadedMetadata
))
2538 torrentQueuePositionBottom(torrentHandle
);
2540 m_torrentsQueueChanged
= true;
2543 void SessionImpl::topTorrentsQueuePos(const QVector
<TorrentID
> &ids
)
2545 using ElementType
= std::pair
<int, const TorrentImpl
*>;
2546 std::priority_queue
<ElementType
> torrentQueue
;
2548 // Sort torrents by queue position
2549 for (const TorrentID
&id
: ids
)
2551 const TorrentImpl
*torrent
= m_torrents
.value(id
);
2552 if (!torrent
) continue;
2553 if (const int position
= torrent
->queuePosition(); position
>= 0)
2554 torrentQueue
.emplace(position
, torrent
);
2557 // Top torrents queue position (starting with the one in the lowest queue position)
2558 while (!torrentQueue
.empty())
2560 const TorrentImpl
*torrent
= torrentQueue
.top().second
;
2561 torrentQueuePositionTop(torrent
->nativeHandle());
2565 m_torrentsQueueChanged
= true;
2568 void SessionImpl::bottomTorrentsQueuePos(const QVector
<TorrentID
> &ids
)
2570 using ElementType
= std::pair
<int, const TorrentImpl
*>;
2571 std::priority_queue
<ElementType
2572 , std::vector
<ElementType
>
2573 , std::greater
<ElementType
>> torrentQueue
;
2575 // Sort torrents by queue position
2576 for (const TorrentID
&id
: ids
)
2578 const TorrentImpl
*torrent
= m_torrents
.value(id
);
2579 if (!torrent
) continue;
2580 if (const int position
= torrent
->queuePosition(); position
>= 0)
2581 torrentQueue
.emplace(position
, torrent
);
2584 // Bottom torrents queue position (starting with the one in the highest queue position)
2585 while (!torrentQueue
.empty())
2587 const TorrentImpl
*torrent
= torrentQueue
.top().second
;
2588 torrentQueuePositionBottom(torrent
->nativeHandle());
2592 for (const lt::torrent_handle
&torrentHandle
: asConst(m_downloadedMetadata
))
2593 torrentQueuePositionBottom(torrentHandle
);
2595 m_torrentsQueueChanged
= true;
2598 void SessionImpl::handleTorrentNeedSaveResumeData(const TorrentImpl
*torrent
)
2600 if (m_needSaveResumeDataTorrents
.empty())
2602 QMetaObject::invokeMethod(this, [this]()
2604 for (const TorrentID
&torrentID
: asConst(m_needSaveResumeDataTorrents
))
2606 TorrentImpl
*torrent
= m_torrents
.value(torrentID
);
2608 torrent
->saveResumeData();
2610 m_needSaveResumeDataTorrents
.clear();
2611 }, Qt::QueuedConnection
);
2614 m_needSaveResumeDataTorrents
.insert(torrent
->id());
2617 void SessionImpl::handleTorrentSaveResumeDataRequested(const TorrentImpl
*torrent
)
2619 qDebug("Saving resume data is requested for torrent '%s'...", qUtf8Printable(torrent
->name()));
2623 void SessionImpl::handleTorrentSaveResumeDataFailed([[maybe_unused
]] const TorrentImpl
*torrent
)
2628 QVector
<Torrent
*> SessionImpl::torrents() const
2630 QVector
<Torrent
*> result
;
2631 result
.reserve(m_torrents
.size());
2632 for (TorrentImpl
*torrent
: asConst(m_torrents
))
2638 qsizetype
SessionImpl::torrentsCount() const
2640 return m_torrents
.size();
2643 bool SessionImpl::addTorrent(const TorrentDescriptor
&torrentDescr
, const AddTorrentParams
¶ms
)
2648 return addTorrent_impl(torrentDescr
, params
);
2651 LoadTorrentParams
SessionImpl::initLoadTorrentParams(const AddTorrentParams
&addTorrentParams
)
2653 LoadTorrentParams loadTorrentParams
;
2655 loadTorrentParams
.name
= addTorrentParams
.name
;
2656 loadTorrentParams
.firstLastPiecePriority
= addTorrentParams
.firstLastPiecePriority
;
2657 loadTorrentParams
.hasFinishedStatus
= addTorrentParams
.skipChecking
; // do not react on 'torrent_finished_alert' when skipping
2658 loadTorrentParams
.contentLayout
= addTorrentParams
.contentLayout
.value_or(torrentContentLayout());
2659 loadTorrentParams
.operatingMode
= (addTorrentParams
.addForced
? TorrentOperatingMode::Forced
: TorrentOperatingMode::AutoManaged
);
2660 loadTorrentParams
.stopped
= addTorrentParams
.addPaused
.value_or(isAddTorrentPaused());
2661 loadTorrentParams
.stopCondition
= addTorrentParams
.stopCondition
.value_or(torrentStopCondition());
2662 loadTorrentParams
.addToQueueTop
= addTorrentParams
.addToQueueTop
.value_or(isAddTorrentToQueueTop());
2663 loadTorrentParams
.ratioLimit
= addTorrentParams
.ratioLimit
;
2664 loadTorrentParams
.seedingTimeLimit
= addTorrentParams
.seedingTimeLimit
;
2666 const QString category
= addTorrentParams
.category
;
2667 if (!category
.isEmpty() && !m_categories
.contains(category
) && !addCategory(category
))
2668 loadTorrentParams
.category
= u
""_s
;
2670 loadTorrentParams
.category
= category
;
2672 const auto defaultSavePath
= suggestedSavePath(loadTorrentParams
.category
, addTorrentParams
.useAutoTMM
);
2673 const auto defaultDownloadPath
= suggestedDownloadPath(loadTorrentParams
.category
, addTorrentParams
.useAutoTMM
);
2675 loadTorrentParams
.useAutoTMM
= addTorrentParams
.useAutoTMM
.value_or(
2676 addTorrentParams
.savePath
.isEmpty() && addTorrentParams
.downloadPath
.isEmpty() && !isAutoTMMDisabledByDefault());
2678 if (!loadTorrentParams
.useAutoTMM
)
2680 if (addTorrentParams
.savePath
.isAbsolute())
2681 loadTorrentParams
.savePath
= addTorrentParams
.savePath
;
2683 loadTorrentParams
.savePath
= defaultSavePath
/ addTorrentParams
.savePath
;
2685 // if useDownloadPath isn't specified but downloadPath is explicitly set we prefer to use it
2686 const bool useDownloadPath
= addTorrentParams
.useDownloadPath
.value_or(!addTorrentParams
.downloadPath
.isEmpty() || isDownloadPathEnabled());
2687 if (useDownloadPath
)
2689 // Overridden "Download path" settings
2691 if (addTorrentParams
.downloadPath
.isAbsolute())
2693 loadTorrentParams
.downloadPath
= addTorrentParams
.downloadPath
;
2697 const Path basePath
= (!defaultDownloadPath
.isEmpty() ? defaultDownloadPath
: downloadPath());
2698 loadTorrentParams
.downloadPath
= basePath
/ addTorrentParams
.downloadPath
;
2703 for (const Tag
&tag
: addTorrentParams
.tags
)
2705 if (hasTag(tag
) || addTag(tag
))
2706 loadTorrentParams
.tags
.insert(tag
);
2709 return loadTorrentParams
;
2712 // Add a torrent to the BitTorrent session
2713 bool SessionImpl::addTorrent_impl(const TorrentDescriptor
&source
, const AddTorrentParams
&addTorrentParams
)
2715 Q_ASSERT(isRestored());
2717 const bool hasMetadata
= (source
.info().has_value());
2718 const auto infoHash
= source
.infoHash();
2719 const auto id
= TorrentID::fromInfoHash(infoHash
);
2721 // alternative ID can be useful to find existing torrent in case if hybrid torrent was added by v1 info hash
2722 const auto altID
= (infoHash
.isHybrid() ? TorrentID::fromSHA1Hash(infoHash
.v1()) : TorrentID());
2724 // We should not add the torrent if it is already
2725 // processed or is pending to add to session
2726 if (m_loadingTorrents
.contains(id
) || (infoHash
.isHybrid() && m_loadingTorrents
.contains(altID
)))
2729 if (findTorrent(infoHash
))
2732 // It looks illogical that we don't just use an existing handle,
2733 // but as previous experience has shown, it actually creates unnecessary
2734 // problems and unwanted behavior due to the fact that it was originally
2735 // added with parameters other than those provided by the user.
2736 cancelDownloadMetadata(id
);
2737 if (infoHash
.isHybrid())
2738 cancelDownloadMetadata(altID
);
2740 LoadTorrentParams loadTorrentParams
= initLoadTorrentParams(addTorrentParams
);
2741 lt::add_torrent_params
&p
= loadTorrentParams
.ltAddTorrentParams
;
2742 p
= source
.ltAddTorrentParams();
2744 bool isFindingIncompleteFiles
= false;
2746 const bool useAutoTMM
= loadTorrentParams
.useAutoTMM
;
2747 const Path actualSavePath
= useAutoTMM
? categorySavePath(loadTorrentParams
.category
) : loadTorrentParams
.savePath
;
2751 const TorrentInfo
&torrentInfo
= *source
.info();
2753 Q_ASSERT(addTorrentParams
.filePaths
.isEmpty() || (addTorrentParams
.filePaths
.size() == torrentInfo
.filesCount()));
2755 PathList filePaths
= addTorrentParams
.filePaths
;
2756 if (filePaths
.isEmpty())
2758 filePaths
= torrentInfo
.filePaths();
2759 if (loadTorrentParams
.contentLayout
!= TorrentContentLayout::Original
)
2761 const Path originalRootFolder
= Path::findRootFolder(filePaths
);
2762 const auto originalContentLayout
= (originalRootFolder
.isEmpty()
2763 ? TorrentContentLayout::NoSubfolder
: TorrentContentLayout::Subfolder
);
2764 if (loadTorrentParams
.contentLayout
!= originalContentLayout
)
2766 if (loadTorrentParams
.contentLayout
== TorrentContentLayout::NoSubfolder
)
2767 Path::stripRootFolder(filePaths
);
2769 Path::addRootFolder(filePaths
, filePaths
.at(0).removedExtension());
2774 // if torrent name wasn't explicitly set we handle the case of
2775 // initial renaming of torrent content and rename torrent accordingly
2776 if (loadTorrentParams
.name
.isEmpty())
2778 QString contentName
= Path::findRootFolder(filePaths
).toString();
2779 if (contentName
.isEmpty() && (filePaths
.size() == 1))
2780 contentName
= filePaths
.at(0).filename();
2782 if (!contentName
.isEmpty() && (contentName
!= torrentInfo
.name()))
2783 loadTorrentParams
.name
= contentName
;
2786 if (!loadTorrentParams
.hasFinishedStatus
)
2788 const Path actualDownloadPath
= useAutoTMM
2789 ? categoryDownloadPath(loadTorrentParams
.category
) : loadTorrentParams
.downloadPath
;
2790 findIncompleteFiles(torrentInfo
, actualSavePath
, actualDownloadPath
, filePaths
);
2791 isFindingIncompleteFiles
= true;
2794 const auto nativeIndexes
= torrentInfo
.nativeIndexes();
2795 if (!isFindingIncompleteFiles
)
2797 for (int index
= 0; index
< filePaths
.size(); ++index
)
2798 p
.renamed_files
[nativeIndexes
[index
]] = filePaths
.at(index
).toString().toStdString();
2801 Q_ASSERT(p
.file_priorities
.empty());
2802 Q_ASSERT(addTorrentParams
.filePriorities
.isEmpty() || (addTorrentParams
.filePriorities
.size() == nativeIndexes
.size()));
2804 const int internalFilesCount
= torrentInfo
.nativeInfo()->files().num_files(); // including .pad files
2805 // Use qBittorrent default priority rather than libtorrent's (4)
2806 p
.file_priorities
= std::vector(internalFilesCount
, LT::toNative(DownloadPriority::Normal
));
2808 if (addTorrentParams
.filePriorities
.isEmpty())
2810 if (isExcludedFileNamesEnabled())
2812 // Check file name blacklist when priorities are not explicitly set
2813 for (int i
= 0; i
< filePaths
.size(); ++i
)
2815 if (isFilenameExcluded(filePaths
.at(i
).filename()))
2816 p
.file_priorities
[LT::toUnderlyingType(nativeIndexes
[i
])] = lt::dont_download
;
2822 for (int i
= 0; i
< addTorrentParams
.filePriorities
.size(); ++i
)
2823 p
.file_priorities
[LT::toUnderlyingType(nativeIndexes
[i
])] = LT::toNative(addTorrentParams
.filePriorities
[i
]);
2830 if (loadTorrentParams
.name
.isEmpty() && !p
.name
.empty())
2831 loadTorrentParams
.name
= QString::fromStdString(p
.name
);
2834 p
.save_path
= actualSavePath
.toString().toStdString();
2836 if (isAddTrackersEnabled() && !(hasMetadata
&& p
.ti
->priv()))
2838 p
.trackers
.reserve(p
.trackers
.size() + static_cast<std::size_t>(m_additionalTrackerList
.size()));
2839 p
.tracker_tiers
.reserve(p
.trackers
.size() + static_cast<std::size_t>(m_additionalTrackerList
.size()));
2840 p
.tracker_tiers
.resize(p
.trackers
.size(), 0);
2841 for (const TrackerEntry
&trackerEntry
: asConst(m_additionalTrackerList
))
2843 p
.trackers
.push_back(trackerEntry
.url
.toStdString());
2844 p
.tracker_tiers
.push_back(trackerEntry
.tier
);
2848 p
.upload_limit
= addTorrentParams
.uploadLimit
;
2849 p
.download_limit
= addTorrentParams
.downloadLimit
;
2851 // Preallocation mode
2852 p
.storage_mode
= isPreallocationEnabled() ? lt::storage_mode_allocate
: lt::storage_mode_sparse
;
2854 if (addTorrentParams
.sequential
)
2855 p
.flags
|= lt::torrent_flags::sequential_download
;
2857 p
.flags
&= ~lt::torrent_flags::sequential_download
;
2860 // Skip checking and directly start seeding
2861 if (addTorrentParams
.skipChecking
)
2862 p
.flags
|= lt::torrent_flags::seed_mode
;
2864 p
.flags
&= ~lt::torrent_flags::seed_mode
;
2866 if (loadTorrentParams
.stopped
|| (loadTorrentParams
.operatingMode
== TorrentOperatingMode::AutoManaged
))
2867 p
.flags
|= lt::torrent_flags::paused
;
2869 p
.flags
&= ~lt::torrent_flags::paused
;
2870 if (loadTorrentParams
.stopped
|| (loadTorrentParams
.operatingMode
== TorrentOperatingMode::Forced
))
2871 p
.flags
&= ~lt::torrent_flags::auto_managed
;
2873 p
.flags
|= lt::torrent_flags::auto_managed
;
2875 p
.flags
|= lt::torrent_flags::duplicate_is_error
;
2877 p
.added_time
= std::time(nullptr);
2880 p
.max_connections
= maxConnectionsPerTorrent();
2881 p
.max_uploads
= maxUploadsPerTorrent();
2883 p
.userdata
= LTClientData(new ExtensionData
);
2884 #ifndef QBT_USES_LIBTORRENT2
2885 p
.storage
= customStorageConstructor
;
2888 m_loadingTorrents
.insert(id
, loadTorrentParams
);
2889 if (infoHash
.isHybrid())
2890 m_hybridTorrentsByAltID
.insert(altID
, nullptr);
2891 if (!isFindingIncompleteFiles
)
2892 m_nativeSession
->async_add_torrent(p
);
2897 void SessionImpl::findIncompleteFiles(const TorrentInfo
&torrentInfo
, const Path
&savePath
2898 , const Path
&downloadPath
, const PathList
&filePaths
) const
2900 Q_ASSERT(filePaths
.isEmpty() || (filePaths
.size() == torrentInfo
.filesCount()));
2902 const auto searchId
= TorrentID::fromInfoHash(torrentInfo
.infoHash());
2903 const PathList originalFileNames
= (filePaths
.isEmpty() ? torrentInfo
.filePaths() : filePaths
);
2904 QMetaObject::invokeMethod(m_fileSearcher
, [=, this]
2906 m_fileSearcher
->search(searchId
, originalFileNames
, savePath
, downloadPath
, isAppendExtensionEnabled());
2910 void SessionImpl::enablePortMapping()
2914 if (m_isPortMappingEnabled
)
2917 lt::settings_pack settingsPack
;
2918 settingsPack
.set_bool(lt::settings_pack::enable_upnp
, true);
2919 settingsPack
.set_bool(lt::settings_pack::enable_natpmp
, true);
2920 m_nativeSession
->apply_settings(std::move(settingsPack
));
2922 m_isPortMappingEnabled
= true;
2924 LogMsg(tr("UPnP/NAT-PMP support: ON"), Log::INFO
);
2928 void SessionImpl::disablePortMapping()
2932 if (!m_isPortMappingEnabled
)
2935 lt::settings_pack settingsPack
;
2936 settingsPack
.set_bool(lt::settings_pack::enable_upnp
, false);
2937 settingsPack
.set_bool(lt::settings_pack::enable_natpmp
, false);
2938 m_nativeSession
->apply_settings(std::move(settingsPack
));
2940 m_mappedPorts
.clear();
2941 m_isPortMappingEnabled
= false;
2943 LogMsg(tr("UPnP/NAT-PMP support: OFF"), Log::INFO
);
2947 void SessionImpl::addMappedPorts(const QSet
<quint16
> &ports
)
2949 invokeAsync([this, ports
]
2951 if (!m_isPortMappingEnabled
)
2954 for (const quint16 port
: ports
)
2956 if (!m_mappedPorts
.contains(port
))
2957 m_mappedPorts
.insert(port
, m_nativeSession
->add_port_mapping(lt::session::tcp
, port
, port
));
2962 void SessionImpl::removeMappedPorts(const QSet
<quint16
> &ports
)
2964 invokeAsync([this, ports
]
2966 if (!m_isPortMappingEnabled
)
2969 Algorithm::removeIf(m_mappedPorts
, [this, ports
](const quint16 port
, const std::vector
<lt::port_mapping_t
> &handles
)
2971 if (!ports
.contains(port
))
2974 for (const lt::port_mapping_t
&handle
: handles
)
2975 m_nativeSession
->delete_port_mapping(handle
);
2982 void SessionImpl::invokeAsync(std::function
<void ()> func
)
2984 m_asyncWorker
->start(std::move(func
));
2987 // Add a torrent to libtorrent session in hidden mode
2988 // and force it to download its metadata
2989 bool SessionImpl::downloadMetadata(const TorrentDescriptor
&torrentDescr
)
2991 Q_ASSERT(!torrentDescr
.info().has_value());
2992 if (torrentDescr
.info().has_value()) [[unlikely
]]
2995 const InfoHash infoHash
= torrentDescr
.infoHash();
2997 // We should not add torrent if it's already
2998 // processed or adding to session
2999 if (isKnownTorrent(infoHash
))
3002 lt::add_torrent_params p
= torrentDescr
.ltAddTorrentParams();
3004 if (isAddTrackersEnabled())
3006 // Use "additional trackers" when metadata retrieving (this can help when the DHT nodes are few)
3007 p
.trackers
.reserve(p
.trackers
.size() + static_cast<std::size_t>(m_additionalTrackerList
.size()));
3008 p
.tracker_tiers
.reserve(p
.trackers
.size() + static_cast<std::size_t>(m_additionalTrackerList
.size()));
3009 p
.tracker_tiers
.resize(p
.trackers
.size(), 0);
3010 for (const TrackerEntry
&trackerEntry
: asConst(m_additionalTrackerList
))
3012 p
.trackers
.push_back(trackerEntry
.url
.toStdString());
3013 p
.tracker_tiers
.push_back(trackerEntry
.tier
);
3018 // Preallocation mode
3019 if (isPreallocationEnabled())
3020 p
.storage_mode
= lt::storage_mode_allocate
;
3022 p
.storage_mode
= lt::storage_mode_sparse
;
3025 p
.max_connections
= maxConnectionsPerTorrent();
3026 p
.max_uploads
= maxUploadsPerTorrent();
3028 const auto id
= TorrentID::fromInfoHash(infoHash
);
3029 const Path savePath
= Utils::Fs::tempPath() / Path(id
.toString());
3030 p
.save_path
= savePath
.toString().toStdString();
3033 p
.flags
&= ~lt::torrent_flags::paused
;
3034 p
.flags
&= ~lt::torrent_flags::auto_managed
;
3036 // Solution to avoid accidental file writes
3037 p
.flags
|= lt::torrent_flags::upload_mode
;
3039 #ifndef QBT_USES_LIBTORRENT2
3040 p
.storage
= customStorageConstructor
;
3043 // Adding torrent to libtorrent session
3044 m_nativeSession
->async_add_torrent(p
);
3045 m_downloadedMetadata
.insert(id
, {});
3050 void SessionImpl::exportTorrentFile(const Torrent
*torrent
, const Path
&folderPath
)
3052 if (!folderPath
.exists() && !Utils::Fs::mkpath(folderPath
))
3055 const QString validName
= Utils::Fs::toValidFileName(torrent
->name());
3056 QString torrentExportFilename
= u
"%1.torrent"_s
.arg(validName
);
3057 Path newTorrentPath
= folderPath
/ Path(torrentExportFilename
);
3059 while (newTorrentPath
.exists())
3061 // Append number to torrent name to make it unique
3062 torrentExportFilename
= u
"%1 %2.torrent"_s
.arg(validName
).arg(++counter
);
3063 newTorrentPath
= folderPath
/ Path(torrentExportFilename
);
3066 const nonstd::expected
<void, QString
> result
= torrent
->exportToFile(newTorrentPath
);
3069 LogMsg(tr("Failed to export torrent. Torrent: \"%1\". Destination: \"%2\". Reason: \"%3\"")
3070 .arg(torrent
->name(), newTorrentPath
.toString(), result
.error()), Log::WARNING
);
3074 void SessionImpl::generateResumeData()
3076 for (TorrentImpl
*const torrent
: asConst(m_torrents
))
3078 if (!torrent
->isValid()) continue;
3080 if (torrent
->needSaveResumeData())
3082 torrent
->saveResumeData();
3083 m_needSaveResumeDataTorrents
.remove(torrent
->id());
3089 void SessionImpl::saveResumeData()
3091 for (const TorrentImpl
*torrent
: asConst(m_torrents
))
3093 // When the session is terminated due to unrecoverable error
3094 // some of the torrent handles can be corrupted
3097 torrent
->nativeHandle().save_resume_data(lt::torrent_handle::only_if_modified
);
3100 catch (const std::exception
&) {}
3103 // clear queued storage move jobs except the current ongoing one
3104 if (m_moveStorageQueue
.size() > 1)
3105 m_moveStorageQueue
.resize(1);
3107 QElapsedTimer timer
;
3110 while ((m_numResumeData
> 0) || !m_moveStorageQueue
.isEmpty() || m_needSaveTorrentsQueue
)
3112 const lt::seconds waitTime
{5};
3113 const lt::seconds expireTime
{30};
3115 // only terminate when no storage is moving
3116 if (timer
.hasExpired(lt::total_milliseconds(expireTime
)) && m_moveStorageQueue
.isEmpty())
3118 LogMsg(tr("Aborted saving resume data. Number of outstanding torrents: %1").arg(QString::number(m_numResumeData
))
3123 const std::vector
<lt::alert
*> alerts
= getPendingAlerts(waitTime
);
3125 bool hasWantedAlert
= false;
3126 for (const lt::alert
*a
: alerts
)
3128 if (const int alertType
= a
->type();
3129 (alertType
== lt::save_resume_data_alert::alert_type
) || (alertType
== lt::save_resume_data_failed_alert::alert_type
)
3130 || (alertType
== lt::storage_moved_alert::alert_type
) || (alertType
== lt::storage_moved_failed_alert::alert_type
)
3131 || (alertType
== lt::state_update_alert::alert_type
))
3133 hasWantedAlert
= true;
3144 void SessionImpl::saveTorrentsQueue()
3146 QVector
<TorrentID
> queue
;
3147 for (const TorrentImpl
*torrent
: asConst(m_torrents
))
3149 if (const int queuePos
= torrent
->queuePosition(); queuePos
>= 0)
3151 if (queuePos
>= queue
.size())
3152 queue
.resize(queuePos
+ 1);
3153 queue
[queuePos
] = torrent
->id();
3157 m_resumeDataStorage
->storeQueue(queue
);
3158 m_needSaveTorrentsQueue
= false;
3161 void SessionImpl::removeTorrentsQueue()
3163 m_resumeDataStorage
->storeQueue({});
3164 m_torrentsQueueChanged
= false;
3165 m_needSaveTorrentsQueue
= false;
3168 void SessionImpl::setSavePath(const Path
&path
)
3170 const auto newPath
= (path
.isAbsolute() ? path
: (specialFolderLocation(SpecialFolder::Downloads
) / path
));
3171 if (newPath
== m_savePath
)
3174 if (isDisableAutoTMMWhenDefaultSavePathChanged())
3176 QSet
<QString
> affectedCatogories
{{}}; // includes default (unnamed) category
3177 for (auto it
= m_categories
.cbegin(); it
!= m_categories
.cend(); ++it
)
3179 const QString
&categoryName
= it
.key();
3180 const CategoryOptions
&categoryOptions
= it
.value();
3181 if (categoryOptions
.savePath
.isRelative())
3182 affectedCatogories
.insert(categoryName
);
3185 for (TorrentImpl
*const torrent
: asConst(m_torrents
))
3187 if (affectedCatogories
.contains(torrent
->category()))
3188 torrent
->setAutoTMMEnabled(false);
3192 m_savePath
= newPath
;
3193 for (TorrentImpl
*const torrent
: asConst(m_torrents
))
3194 torrent
->handleCategoryOptionsChanged();
3197 void SessionImpl::setDownloadPath(const Path
&path
)
3199 const Path newPath
= (path
.isAbsolute() ? path
: (savePath() / Path(u
"temp"_s
) / path
));
3200 if (newPath
== m_downloadPath
)
3203 if (isDisableAutoTMMWhenDefaultSavePathChanged())
3205 QSet
<QString
> affectedCatogories
{{}}; // includes default (unnamed) category
3206 for (auto it
= m_categories
.cbegin(); it
!= m_categories
.cend(); ++it
)
3208 const QString
&categoryName
= it
.key();
3209 const CategoryOptions
&categoryOptions
= it
.value();
3210 const DownloadPathOption downloadPathOption
=
3211 categoryOptions
.downloadPath
.value_or(DownloadPathOption
{isDownloadPathEnabled(), downloadPath()});
3212 if (downloadPathOption
.enabled
&& downloadPathOption
.path
.isRelative())
3213 affectedCatogories
.insert(categoryName
);
3216 for (TorrentImpl
*const torrent
: asConst(m_torrents
))
3218 if (affectedCatogories
.contains(torrent
->category()))
3219 torrent
->setAutoTMMEnabled(false);
3223 m_downloadPath
= newPath
;
3224 for (TorrentImpl
*const torrent
: asConst(m_torrents
))
3225 torrent
->handleCategoryOptionsChanged();
3228 QStringList
SessionImpl::getListeningIPs() const
3232 const QString ifaceName
= networkInterface();
3233 const QString ifaceAddr
= networkInterfaceAddress();
3234 const QHostAddress
configuredAddr(ifaceAddr
);
3235 const bool allIPv4
= (ifaceAddr
== u
"0.0.0.0"); // Means All IPv4 addresses
3236 const bool allIPv6
= (ifaceAddr
== u
"::"); // Means All IPv6 addresses
3238 if (!ifaceAddr
.isEmpty() && !allIPv4
&& !allIPv6
&& configuredAddr
.isNull())
3240 LogMsg(tr("The configured network address is invalid. Address: \"%1\"").arg(ifaceAddr
), Log::CRITICAL
);
3241 // Pass the invalid user configured interface name/address to libtorrent
3242 // in hopes that it will come online later.
3243 // This will not cause IP leak but allow user to reconnect the interface
3244 // and re-establish connection without restarting the client.
3245 IPs
.append(ifaceAddr
);
3249 if (ifaceName
.isEmpty())
3251 if (ifaceAddr
.isEmpty())
3252 return {u
"0.0.0.0"_s
, u
"::"_s
}; // Indicates all interfaces + all addresses (aka default)
3255 return {u
"0.0.0.0"_s
};
3261 const auto checkAndAddIP
= [allIPv4
, allIPv6
, &IPs
](const QHostAddress
&addr
, const QHostAddress
&match
)
3263 if ((allIPv4
&& (addr
.protocol() != QAbstractSocket::IPv4Protocol
))
3264 || (allIPv6
&& (addr
.protocol() != QAbstractSocket::IPv6Protocol
)))
3267 if ((match
== addr
) || allIPv4
|| allIPv6
)
3268 IPs
.append(addr
.toString());
3271 if (ifaceName
.isEmpty())
3273 const QList
<QHostAddress
> addresses
= QNetworkInterface::allAddresses();
3274 for (const auto &addr
: addresses
)
3275 checkAndAddIP(addr
, configuredAddr
);
3277 // At this point ifaceAddr was non-empty
3278 // If IPs.isEmpty() it means the configured Address was not found
3281 LogMsg(tr("Failed to find the configured network address to listen on. Address: \"%1\"")
3282 .arg(ifaceAddr
), Log::CRITICAL
);
3283 IPs
.append(ifaceAddr
);
3289 // Attempt to listen on provided interface
3290 const QNetworkInterface networkIFace
= QNetworkInterface::interfaceFromName(ifaceName
);
3291 if (!networkIFace
.isValid())
3293 qDebug("Invalid network interface: %s", qUtf8Printable(ifaceName
));
3294 LogMsg(tr("The configured network interface is invalid. Interface: \"%1\"").arg(ifaceName
), Log::CRITICAL
);
3295 IPs
.append(ifaceName
);
3299 if (ifaceAddr
.isEmpty())
3301 IPs
.append(ifaceName
);
3302 return IPs
; // On Windows calling code converts it to GUID
3305 const QList
<QNetworkAddressEntry
> addresses
= networkIFace
.addressEntries();
3306 qDebug() << "This network interface has " << addresses
.size() << " IP addresses";
3307 for (const QNetworkAddressEntry
&entry
: addresses
)
3308 checkAndAddIP(entry
.ip(), configuredAddr
);
3310 // Make sure there is at least one IP
3311 // At this point there was an explicit interface and an explicit address set
3312 // and the address should have been found
3315 LogMsg(tr("Failed to find the configured network address to listen on. Address: \"%1\"")
3316 .arg(ifaceAddr
), Log::CRITICAL
);
3317 IPs
.append(ifaceAddr
);
3323 // Set the ports range in which is chosen the port
3324 // the BitTorrent session will listen to
3325 void SessionImpl::configureListeningInterface()
3327 m_listenInterfaceConfigured
= false;
3328 configureDeferred();
3331 int SessionImpl::globalDownloadSpeedLimit() const
3333 // Unfortunately the value was saved as KiB instead of B.
3334 // But it is better to pass it around internally(+ webui) as Bytes.
3335 return m_globalDownloadSpeedLimit
* 1024;
3338 void SessionImpl::setGlobalDownloadSpeedLimit(const int limit
)
3340 // Unfortunately the value was saved as KiB instead of B.
3341 // But it is better to pass it around internally(+ webui) as Bytes.
3342 if (limit
== globalDownloadSpeedLimit())
3346 m_globalDownloadSpeedLimit
= 0;
3347 else if (limit
<= 1024)
3348 m_globalDownloadSpeedLimit
= 1;
3350 m_globalDownloadSpeedLimit
= (limit
/ 1024);
3352 if (!isAltGlobalSpeedLimitEnabled())
3353 configureDeferred();
3356 int SessionImpl::globalUploadSpeedLimit() const
3358 // Unfortunately the value was saved as KiB instead of B.
3359 // But it is better to pass it around internally(+ webui) as Bytes.
3360 return m_globalUploadSpeedLimit
* 1024;
3363 void SessionImpl::setGlobalUploadSpeedLimit(const int limit
)
3365 // Unfortunately the value was saved as KiB instead of B.
3366 // But it is better to pass it around internally(+ webui) as Bytes.
3367 if (limit
== globalUploadSpeedLimit())
3371 m_globalUploadSpeedLimit
= 0;
3372 else if (limit
<= 1024)
3373 m_globalUploadSpeedLimit
= 1;
3375 m_globalUploadSpeedLimit
= (limit
/ 1024);
3377 if (!isAltGlobalSpeedLimitEnabled())
3378 configureDeferred();
3381 int SessionImpl::altGlobalDownloadSpeedLimit() const
3383 // Unfortunately the value was saved as KiB instead of B.
3384 // But it is better to pass it around internally(+ webui) as Bytes.
3385 return m_altGlobalDownloadSpeedLimit
* 1024;
3388 void SessionImpl::setAltGlobalDownloadSpeedLimit(const int limit
)
3390 // Unfortunately the value was saved as KiB instead of B.
3391 // But it is better to pass it around internally(+ webui) as Bytes.
3392 if (limit
== altGlobalDownloadSpeedLimit())
3396 m_altGlobalDownloadSpeedLimit
= 0;
3397 else if (limit
<= 1024)
3398 m_altGlobalDownloadSpeedLimit
= 1;
3400 m_altGlobalDownloadSpeedLimit
= (limit
/ 1024);
3402 if (isAltGlobalSpeedLimitEnabled())
3403 configureDeferred();
3406 int SessionImpl::altGlobalUploadSpeedLimit() const
3408 // Unfortunately the value was saved as KiB instead of B.
3409 // But it is better to pass it around internally(+ webui) as Bytes.
3410 return m_altGlobalUploadSpeedLimit
* 1024;
3413 void SessionImpl::setAltGlobalUploadSpeedLimit(const int limit
)
3415 // Unfortunately the value was saved as KiB instead of B.
3416 // But it is better to pass it around internally(+ webui) as Bytes.
3417 if (limit
== altGlobalUploadSpeedLimit())
3421 m_altGlobalUploadSpeedLimit
= 0;
3422 else if (limit
<= 1024)
3423 m_altGlobalUploadSpeedLimit
= 1;
3425 m_altGlobalUploadSpeedLimit
= (limit
/ 1024);
3427 if (isAltGlobalSpeedLimitEnabled())
3428 configureDeferred();
3431 int SessionImpl::downloadSpeedLimit() const
3433 return isAltGlobalSpeedLimitEnabled()
3434 ? altGlobalDownloadSpeedLimit()
3435 : globalDownloadSpeedLimit();
3438 void SessionImpl::setDownloadSpeedLimit(const int limit
)
3440 if (isAltGlobalSpeedLimitEnabled())
3441 setAltGlobalDownloadSpeedLimit(limit
);
3443 setGlobalDownloadSpeedLimit(limit
);
3446 int SessionImpl::uploadSpeedLimit() const
3448 return isAltGlobalSpeedLimitEnabled()
3449 ? altGlobalUploadSpeedLimit()
3450 : globalUploadSpeedLimit();
3453 void SessionImpl::setUploadSpeedLimit(const int limit
)
3455 if (isAltGlobalSpeedLimitEnabled())
3456 setAltGlobalUploadSpeedLimit(limit
);
3458 setGlobalUploadSpeedLimit(limit
);
3461 bool SessionImpl::isAltGlobalSpeedLimitEnabled() const
3463 return m_isAltGlobalSpeedLimitEnabled
;
3466 void SessionImpl::setAltGlobalSpeedLimitEnabled(const bool enabled
)
3468 if (enabled
== isAltGlobalSpeedLimitEnabled()) return;
3470 // Save new state to remember it on startup
3471 m_isAltGlobalSpeedLimitEnabled
= enabled
;
3472 applyBandwidthLimits();
3474 emit
speedLimitModeChanged(m_isAltGlobalSpeedLimitEnabled
);
3477 bool SessionImpl::isBandwidthSchedulerEnabled() const
3479 return m_isBandwidthSchedulerEnabled
;
3482 void SessionImpl::setBandwidthSchedulerEnabled(const bool enabled
)
3484 if (enabled
!= isBandwidthSchedulerEnabled())
3486 m_isBandwidthSchedulerEnabled
= enabled
;
3488 enableBandwidthScheduler();
3490 delete m_bwScheduler
;
3494 bool SessionImpl::isPerformanceWarningEnabled() const
3496 return m_isPerformanceWarningEnabled
;
3499 void SessionImpl::setPerformanceWarningEnabled(const bool enable
)
3501 if (enable
== m_isPerformanceWarningEnabled
)
3504 m_isPerformanceWarningEnabled
= enable
;
3505 configureDeferred();
3508 int SessionImpl::saveResumeDataInterval() const
3510 return m_saveResumeDataInterval
;
3513 void SessionImpl::setSaveResumeDataInterval(const int value
)
3515 if (value
== m_saveResumeDataInterval
)
3518 m_saveResumeDataInterval
= value
;
3522 m_resumeDataTimer
->setInterval(std::chrono::minutes(value
));
3523 m_resumeDataTimer
->start();
3527 m_resumeDataTimer
->stop();
3531 int SessionImpl::port() const
3536 void SessionImpl::setPort(const int port
)
3541 configureListeningInterface();
3543 if (isReannounceWhenAddressChangedEnabled())
3544 reannounceToAllTrackers();
3548 QString
SessionImpl::networkInterface() const
3550 return m_networkInterface
;
3553 void SessionImpl::setNetworkInterface(const QString
&iface
)
3555 if (iface
!= networkInterface())
3557 m_networkInterface
= iface
;
3558 configureListeningInterface();
3562 QString
SessionImpl::networkInterfaceName() const
3564 return m_networkInterfaceName
;
3567 void SessionImpl::setNetworkInterfaceName(const QString
&name
)
3569 m_networkInterfaceName
= name
;
3572 QString
SessionImpl::networkInterfaceAddress() const
3574 return m_networkInterfaceAddress
;
3577 void SessionImpl::setNetworkInterfaceAddress(const QString
&address
)
3579 if (address
!= networkInterfaceAddress())
3581 m_networkInterfaceAddress
= address
;
3582 configureListeningInterface();
3586 int SessionImpl::encryption() const
3588 return m_encryption
;
3591 void SessionImpl::setEncryption(const int state
)
3593 if (state
!= encryption())
3595 m_encryption
= state
;
3596 configureDeferred();
3597 LogMsg(tr("Encryption support: %1").arg(
3598 state
== 0 ? tr("ON") : ((state
== 1) ? tr("FORCED") : tr("OFF")))
3603 int SessionImpl::maxActiveCheckingTorrents() const
3605 return m_maxActiveCheckingTorrents
;
3608 void SessionImpl::setMaxActiveCheckingTorrents(const int val
)
3610 if (val
== m_maxActiveCheckingTorrents
)
3613 m_maxActiveCheckingTorrents
= val
;
3614 configureDeferred();
3617 bool SessionImpl::isI2PEnabled() const
3619 return m_isI2PEnabled
;
3622 void SessionImpl::setI2PEnabled(const bool enabled
)
3624 if (m_isI2PEnabled
!= enabled
)
3626 m_isI2PEnabled
= enabled
;
3627 configureDeferred();
3631 QString
SessionImpl::I2PAddress() const
3633 return m_I2PAddress
;
3636 void SessionImpl::setI2PAddress(const QString
&address
)
3638 if (m_I2PAddress
!= address
)
3640 m_I2PAddress
= address
;
3641 configureDeferred();
3645 int SessionImpl::I2PPort() const
3650 void SessionImpl::setI2PPort(int port
)
3652 if (m_I2PPort
!= port
)
3655 configureDeferred();
3659 bool SessionImpl::I2PMixedMode() const
3661 return m_I2PMixedMode
;
3664 void SessionImpl::setI2PMixedMode(const bool enabled
)
3666 if (m_I2PMixedMode
!= enabled
)
3668 m_I2PMixedMode
= enabled
;
3669 configureDeferred();
3673 int SessionImpl::I2PInboundQuantity() const
3675 return m_I2PInboundQuantity
;
3678 void SessionImpl::setI2PInboundQuantity(const int value
)
3680 if (value
== m_I2PInboundQuantity
)
3683 m_I2PInboundQuantity
= value
;
3684 configureDeferred();
3687 int SessionImpl::I2POutboundQuantity() const
3689 return m_I2POutboundQuantity
;
3692 void SessionImpl::setI2POutboundQuantity(const int value
)
3694 if (value
== m_I2POutboundQuantity
)
3697 m_I2POutboundQuantity
= value
;
3698 configureDeferred();
3701 int SessionImpl::I2PInboundLength() const
3703 return m_I2PInboundLength
;
3706 void SessionImpl::setI2PInboundLength(const int value
)
3708 if (value
== m_I2PInboundLength
)
3711 m_I2PInboundLength
= value
;
3712 configureDeferred();
3715 int SessionImpl::I2POutboundLength() const
3717 return m_I2POutboundLength
;
3720 void SessionImpl::setI2POutboundLength(const int value
)
3722 if (value
== m_I2POutboundLength
)
3725 m_I2POutboundLength
= value
;
3726 configureDeferred();
3729 bool SessionImpl::isProxyPeerConnectionsEnabled() const
3731 return m_isProxyPeerConnectionsEnabled
;
3734 void SessionImpl::setProxyPeerConnectionsEnabled(const bool enabled
)
3736 if (enabled
!= isProxyPeerConnectionsEnabled())
3738 m_isProxyPeerConnectionsEnabled
= enabled
;
3739 configureDeferred();
3743 ChokingAlgorithm
SessionImpl::chokingAlgorithm() const
3745 return m_chokingAlgorithm
;
3748 void SessionImpl::setChokingAlgorithm(const ChokingAlgorithm mode
)
3750 if (mode
== m_chokingAlgorithm
) return;
3752 m_chokingAlgorithm
= mode
;
3753 configureDeferred();
3756 SeedChokingAlgorithm
SessionImpl::seedChokingAlgorithm() const
3758 return m_seedChokingAlgorithm
;
3761 void SessionImpl::setSeedChokingAlgorithm(const SeedChokingAlgorithm mode
)
3763 if (mode
== m_seedChokingAlgorithm
) return;
3765 m_seedChokingAlgorithm
= mode
;
3766 configureDeferred();
3769 bool SessionImpl::isAddTrackersEnabled() const
3771 return m_isAddTrackersEnabled
;
3774 void SessionImpl::setAddTrackersEnabled(const bool enabled
)
3776 m_isAddTrackersEnabled
= enabled
;
3779 QString
SessionImpl::additionalTrackers() const
3781 return m_additionalTrackers
;
3784 void SessionImpl::setAdditionalTrackers(const QString
&trackers
)
3786 if (trackers
!= additionalTrackers())
3788 m_additionalTrackers
= trackers
;
3789 populateAdditionalTrackers();
3793 bool SessionImpl::isIPFilteringEnabled() const
3795 return m_isIPFilteringEnabled
;
3798 void SessionImpl::setIPFilteringEnabled(const bool enabled
)
3800 if (enabled
!= m_isIPFilteringEnabled
)
3802 m_isIPFilteringEnabled
= enabled
;
3803 m_IPFilteringConfigured
= false;
3804 configureDeferred();
3808 Path
SessionImpl::IPFilterFile() const
3810 return m_IPFilterFile
;
3813 void SessionImpl::setIPFilterFile(const Path
&path
)
3815 if (path
!= IPFilterFile())
3817 m_IPFilterFile
= path
;
3818 m_IPFilteringConfigured
= false;
3819 configureDeferred();
3823 bool SessionImpl::isExcludedFileNamesEnabled() const
3825 return m_isExcludedFileNamesEnabled
;
3828 void SessionImpl::setExcludedFileNamesEnabled(const bool enabled
)
3830 if (m_isExcludedFileNamesEnabled
== enabled
)
3833 m_isExcludedFileNamesEnabled
= enabled
;
3836 populateExcludedFileNamesRegExpList();
3838 m_excludedFileNamesRegExpList
.clear();
3841 QStringList
SessionImpl::excludedFileNames() const
3843 return m_excludedFileNames
;
3846 void SessionImpl::setExcludedFileNames(const QStringList
&excludedFileNames
)
3848 if (excludedFileNames
!= m_excludedFileNames
)
3850 m_excludedFileNames
= excludedFileNames
;
3851 populateExcludedFileNamesRegExpList();
3855 void SessionImpl::populateExcludedFileNamesRegExpList()
3857 const QStringList excludedNames
= excludedFileNames();
3859 m_excludedFileNamesRegExpList
.clear();
3860 m_excludedFileNamesRegExpList
.reserve(excludedNames
.size());
3862 for (const QString
&str
: excludedNames
)
3864 const QString pattern
= QRegularExpression::anchoredPattern(QRegularExpression::wildcardToRegularExpression(str
));
3865 const QRegularExpression re
{pattern
, QRegularExpression::CaseInsensitiveOption
};
3866 m_excludedFileNamesRegExpList
.append(re
);
3870 bool SessionImpl::isFilenameExcluded(const QString
&fileName
) const
3872 if (!isExcludedFileNamesEnabled())
3875 return std::any_of(m_excludedFileNamesRegExpList
.begin(), m_excludedFileNamesRegExpList
.end(), [&fileName
](const QRegularExpression
&re
)
3877 return re
.match(fileName
).hasMatch();
3881 void SessionImpl::setBannedIPs(const QStringList
&newList
)
3883 if (newList
== m_bannedIPs
)
3884 return; // do nothing
3885 // here filter out incorrect IP
3886 QStringList filteredList
;
3887 for (const QString
&ip
: newList
)
3889 if (Utils::Net::isValidIP(ip
))
3891 // the same IPv6 addresses could be written in different forms;
3892 // QHostAddress::toString() result format follows RFC5952;
3893 // thus we avoid duplicate entries pointing to the same address
3894 filteredList
<< QHostAddress(ip
).toString();
3898 LogMsg(tr("Rejected invalid IP address while applying the list of banned IP addresses. IP: \"%1\"")
3903 // now we have to sort IPs and make them unique
3904 filteredList
.sort();
3905 filteredList
.removeDuplicates();
3906 // Again ensure that the new list is different from the stored one.
3907 if (filteredList
== m_bannedIPs
)
3908 return; // do nothing
3909 // store to session settings
3910 // also here we have to recreate filter list including 3rd party ban file
3911 // and install it again into m_session
3912 m_bannedIPs
= filteredList
;
3913 m_IPFilteringConfigured
= false;
3914 configureDeferred();
3917 ResumeDataStorageType
SessionImpl::resumeDataStorageType() const
3919 return m_resumeDataStorageType
;
3922 void SessionImpl::setResumeDataStorageType(const ResumeDataStorageType type
)
3924 m_resumeDataStorageType
= type
;
3927 bool SessionImpl::isMergeTrackersEnabled() const
3929 return m_isMergeTrackersEnabled
;
3932 void SessionImpl::setMergeTrackersEnabled(const bool enabled
)
3934 m_isMergeTrackersEnabled
= enabled
;
3937 QStringList
SessionImpl::bannedIPs() const
3942 bool SessionImpl::isRestored() const
3944 return m_isRestored
;
3947 int SessionImpl::maxConnectionsPerTorrent() const
3949 return m_maxConnectionsPerTorrent
;
3952 void SessionImpl::setMaxConnectionsPerTorrent(int max
)
3954 max
= (max
> 0) ? max
: -1;
3955 if (max
!= maxConnectionsPerTorrent())
3957 m_maxConnectionsPerTorrent
= max
;
3959 for (const TorrentImpl
*torrent
: asConst(m_torrents
))
3963 torrent
->nativeHandle().set_max_connections(max
);
3965 catch (const std::exception
&) {}
3970 int SessionImpl::maxUploadsPerTorrent() const
3972 return m_maxUploadsPerTorrent
;
3975 void SessionImpl::setMaxUploadsPerTorrent(int max
)
3977 max
= (max
> 0) ? max
: -1;
3978 if (max
!= maxUploadsPerTorrent())
3980 m_maxUploadsPerTorrent
= max
;
3982 for (const TorrentImpl
*torrent
: asConst(m_torrents
))
3986 torrent
->nativeHandle().set_max_uploads(max
);
3988 catch (const std::exception
&) {}
3993 bool SessionImpl::announceToAllTrackers() const
3995 return m_announceToAllTrackers
;
3998 void SessionImpl::setAnnounceToAllTrackers(const bool val
)
4000 if (val
!= m_announceToAllTrackers
)
4002 m_announceToAllTrackers
= val
;
4003 configureDeferred();
4007 bool SessionImpl::announceToAllTiers() const
4009 return m_announceToAllTiers
;
4012 void SessionImpl::setAnnounceToAllTiers(const bool val
)
4014 if (val
!= m_announceToAllTiers
)
4016 m_announceToAllTiers
= val
;
4017 configureDeferred();
4021 int SessionImpl::peerTurnover() const
4023 return m_peerTurnover
;
4026 void SessionImpl::setPeerTurnover(const int val
)
4028 if (val
== m_peerTurnover
)
4031 m_peerTurnover
= val
;
4032 configureDeferred();
4035 int SessionImpl::peerTurnoverCutoff() const
4037 return m_peerTurnoverCutoff
;
4040 void SessionImpl::setPeerTurnoverCutoff(const int val
)
4042 if (val
== m_peerTurnoverCutoff
)
4045 m_peerTurnoverCutoff
= val
;
4046 configureDeferred();
4049 int SessionImpl::peerTurnoverInterval() const
4051 return m_peerTurnoverInterval
;
4054 void SessionImpl::setPeerTurnoverInterval(const int val
)
4056 if (val
== m_peerTurnoverInterval
)
4059 m_peerTurnoverInterval
= val
;
4060 configureDeferred();
4063 DiskIOType
SessionImpl::diskIOType() const
4065 return m_diskIOType
;
4068 void SessionImpl::setDiskIOType(const DiskIOType type
)
4070 if (type
!= m_diskIOType
)
4072 m_diskIOType
= type
;
4076 int SessionImpl::requestQueueSize() const
4078 return m_requestQueueSize
;
4081 void SessionImpl::setRequestQueueSize(const int val
)
4083 if (val
== m_requestQueueSize
)
4086 m_requestQueueSize
= val
;
4087 configureDeferred();
4090 int SessionImpl::asyncIOThreads() const
4092 return std::clamp(m_asyncIOThreads
.get(), 1, 1024);
4095 void SessionImpl::setAsyncIOThreads(const int num
)
4097 if (num
== m_asyncIOThreads
)
4100 m_asyncIOThreads
= num
;
4101 configureDeferred();
4104 int SessionImpl::hashingThreads() const
4106 return std::clamp(m_hashingThreads
.get(), 1, 1024);
4109 void SessionImpl::setHashingThreads(const int num
)
4111 if (num
== m_hashingThreads
)
4114 m_hashingThreads
= num
;
4115 configureDeferred();
4118 int SessionImpl::filePoolSize() const
4120 return m_filePoolSize
;
4123 void SessionImpl::setFilePoolSize(const int size
)
4125 if (size
== m_filePoolSize
)
4128 m_filePoolSize
= size
;
4129 configureDeferred();
4132 int SessionImpl::checkingMemUsage() const
4134 return std::max(1, m_checkingMemUsage
.get());
4137 void SessionImpl::setCheckingMemUsage(int size
)
4139 size
= std::max(size
, 1);
4141 if (size
== m_checkingMemUsage
)
4144 m_checkingMemUsage
= size
;
4145 configureDeferred();
4148 int SessionImpl::diskCacheSize() const
4150 #ifdef QBT_APP_64BIT
4151 return std::min(m_diskCacheSize
.get(), 33554431); // 32768GiB
4153 // When build as 32bit binary, set the maximum at less than 2GB to prevent crashes
4154 // allocate 1536MiB and leave 512MiB to the rest of program data in RAM
4155 return std::min(m_diskCacheSize
.get(), 1536);
4159 void SessionImpl::setDiskCacheSize(int size
)
4161 #ifdef QBT_APP_64BIT
4162 size
= std::min(size
, 33554431); // 32768GiB
4164 // allocate 1536MiB and leave 512MiB to the rest of program data in RAM
4165 size
= std::min(size
, 1536);
4167 if (size
!= m_diskCacheSize
)
4169 m_diskCacheSize
= size
;
4170 configureDeferred();
4174 int SessionImpl::diskCacheTTL() const
4176 return m_diskCacheTTL
;
4179 void SessionImpl::setDiskCacheTTL(const int ttl
)
4181 if (ttl
!= m_diskCacheTTL
)
4183 m_diskCacheTTL
= ttl
;
4184 configureDeferred();
4188 qint64
SessionImpl::diskQueueSize() const
4190 return m_diskQueueSize
;
4193 void SessionImpl::setDiskQueueSize(const qint64 size
)
4195 if (size
== m_diskQueueSize
)
4198 m_diskQueueSize
= size
;
4199 configureDeferred();
4202 DiskIOReadMode
SessionImpl::diskIOReadMode() const
4204 return m_diskIOReadMode
;
4207 void SessionImpl::setDiskIOReadMode(const DiskIOReadMode mode
)
4209 if (mode
== m_diskIOReadMode
)
4212 m_diskIOReadMode
= mode
;
4213 configureDeferred();
4216 DiskIOWriteMode
SessionImpl::diskIOWriteMode() const
4218 return m_diskIOWriteMode
;
4221 void SessionImpl::setDiskIOWriteMode(const DiskIOWriteMode mode
)
4223 if (mode
== m_diskIOWriteMode
)
4226 m_diskIOWriteMode
= mode
;
4227 configureDeferred();
4230 bool SessionImpl::isCoalesceReadWriteEnabled() const
4232 return m_coalesceReadWriteEnabled
;
4235 void SessionImpl::setCoalesceReadWriteEnabled(const bool enabled
)
4237 if (enabled
== m_coalesceReadWriteEnabled
) return;
4239 m_coalesceReadWriteEnabled
= enabled
;
4240 configureDeferred();
4243 bool SessionImpl::isSuggestModeEnabled() const
4245 return m_isSuggestMode
;
4248 bool SessionImpl::usePieceExtentAffinity() const
4250 return m_usePieceExtentAffinity
;
4253 void SessionImpl::setPieceExtentAffinity(const bool enabled
)
4255 if (enabled
== m_usePieceExtentAffinity
) return;
4257 m_usePieceExtentAffinity
= enabled
;
4258 configureDeferred();
4261 void SessionImpl::setSuggestMode(const bool mode
)
4263 if (mode
== m_isSuggestMode
) return;
4265 m_isSuggestMode
= mode
;
4266 configureDeferred();
4269 int SessionImpl::sendBufferWatermark() const
4271 return m_sendBufferWatermark
;
4274 void SessionImpl::setSendBufferWatermark(const int value
)
4276 if (value
== m_sendBufferWatermark
) return;
4278 m_sendBufferWatermark
= value
;
4279 configureDeferred();
4282 int SessionImpl::sendBufferLowWatermark() const
4284 return m_sendBufferLowWatermark
;
4287 void SessionImpl::setSendBufferLowWatermark(const int value
)
4289 if (value
== m_sendBufferLowWatermark
) return;
4291 m_sendBufferLowWatermark
= value
;
4292 configureDeferred();
4295 int SessionImpl::sendBufferWatermarkFactor() const
4297 return m_sendBufferWatermarkFactor
;
4300 void SessionImpl::setSendBufferWatermarkFactor(const int value
)
4302 if (value
== m_sendBufferWatermarkFactor
) return;
4304 m_sendBufferWatermarkFactor
= value
;
4305 configureDeferred();
4308 int SessionImpl::connectionSpeed() const
4310 return m_connectionSpeed
;
4313 void SessionImpl::setConnectionSpeed(const int value
)
4315 if (value
== m_connectionSpeed
) return;
4317 m_connectionSpeed
= value
;
4318 configureDeferred();
4321 int SessionImpl::socketSendBufferSize() const
4323 return m_socketSendBufferSize
;
4326 void SessionImpl::setSocketSendBufferSize(const int value
)
4328 if (value
== m_socketSendBufferSize
)
4331 m_socketSendBufferSize
= value
;
4332 configureDeferred();
4335 int SessionImpl::socketReceiveBufferSize() const
4337 return m_socketReceiveBufferSize
;
4340 void SessionImpl::setSocketReceiveBufferSize(const int value
)
4342 if (value
== m_socketReceiveBufferSize
)
4345 m_socketReceiveBufferSize
= value
;
4346 configureDeferred();
4349 int SessionImpl::socketBacklogSize() const
4351 return m_socketBacklogSize
;
4354 void SessionImpl::setSocketBacklogSize(const int value
)
4356 if (value
== m_socketBacklogSize
) return;
4358 m_socketBacklogSize
= value
;
4359 configureDeferred();
4362 bool SessionImpl::isAnonymousModeEnabled() const
4364 return m_isAnonymousModeEnabled
;
4367 void SessionImpl::setAnonymousModeEnabled(const bool enabled
)
4369 if (enabled
!= m_isAnonymousModeEnabled
)
4371 m_isAnonymousModeEnabled
= enabled
;
4372 configureDeferred();
4373 LogMsg(tr("Anonymous mode: %1").arg(isAnonymousModeEnabled() ? tr("ON") : tr("OFF"))
4378 bool SessionImpl::isQueueingSystemEnabled() const
4380 return m_isQueueingEnabled
;
4383 void SessionImpl::setQueueingSystemEnabled(const bool enabled
)
4385 if (enabled
!= m_isQueueingEnabled
)
4387 m_isQueueingEnabled
= enabled
;
4388 configureDeferred();
4391 m_torrentsQueueChanged
= true;
4393 removeTorrentsQueue();
4397 int SessionImpl::maxActiveDownloads() const
4399 return m_maxActiveDownloads
;
4402 void SessionImpl::setMaxActiveDownloads(int max
)
4404 max
= std::max(max
, -1);
4405 if (max
!= m_maxActiveDownloads
)
4407 m_maxActiveDownloads
= max
;
4408 configureDeferred();
4412 int SessionImpl::maxActiveUploads() const
4414 return m_maxActiveUploads
;
4417 void SessionImpl::setMaxActiveUploads(int max
)
4419 max
= std::max(max
, -1);
4420 if (max
!= m_maxActiveUploads
)
4422 m_maxActiveUploads
= max
;
4423 configureDeferred();
4427 int SessionImpl::maxActiveTorrents() const
4429 return m_maxActiveTorrents
;
4432 void SessionImpl::setMaxActiveTorrents(int max
)
4434 max
= std::max(max
, -1);
4435 if (max
!= m_maxActiveTorrents
)
4437 m_maxActiveTorrents
= max
;
4438 configureDeferred();
4442 bool SessionImpl::ignoreSlowTorrentsForQueueing() const
4444 return m_ignoreSlowTorrentsForQueueing
;
4447 void SessionImpl::setIgnoreSlowTorrentsForQueueing(const bool ignore
)
4449 if (ignore
!= m_ignoreSlowTorrentsForQueueing
)
4451 m_ignoreSlowTorrentsForQueueing
= ignore
;
4452 configureDeferred();
4456 int SessionImpl::downloadRateForSlowTorrents() const
4458 return m_downloadRateForSlowTorrents
;
4461 void SessionImpl::setDownloadRateForSlowTorrents(const int rateInKibiBytes
)
4463 if (rateInKibiBytes
== m_downloadRateForSlowTorrents
)
4466 m_downloadRateForSlowTorrents
= rateInKibiBytes
;
4467 configureDeferred();
4470 int SessionImpl::uploadRateForSlowTorrents() const
4472 return m_uploadRateForSlowTorrents
;
4475 void SessionImpl::setUploadRateForSlowTorrents(const int rateInKibiBytes
)
4477 if (rateInKibiBytes
== m_uploadRateForSlowTorrents
)
4480 m_uploadRateForSlowTorrents
= rateInKibiBytes
;
4481 configureDeferred();
4484 int SessionImpl::slowTorrentsInactivityTimer() const
4486 return m_slowTorrentsInactivityTimer
;
4489 void SessionImpl::setSlowTorrentsInactivityTimer(const int timeInSeconds
)
4491 if (timeInSeconds
== m_slowTorrentsInactivityTimer
)
4494 m_slowTorrentsInactivityTimer
= timeInSeconds
;
4495 configureDeferred();
4498 int SessionImpl::outgoingPortsMin() const
4500 return m_outgoingPortsMin
;
4503 void SessionImpl::setOutgoingPortsMin(const int min
)
4505 if (min
!= m_outgoingPortsMin
)
4507 m_outgoingPortsMin
= min
;
4508 configureDeferred();
4512 int SessionImpl::outgoingPortsMax() const
4514 return m_outgoingPortsMax
;
4517 void SessionImpl::setOutgoingPortsMax(const int max
)
4519 if (max
!= m_outgoingPortsMax
)
4521 m_outgoingPortsMax
= max
;
4522 configureDeferred();
4526 int SessionImpl::UPnPLeaseDuration() const
4528 return m_UPnPLeaseDuration
;
4531 void SessionImpl::setUPnPLeaseDuration(const int duration
)
4533 if (duration
!= m_UPnPLeaseDuration
)
4535 m_UPnPLeaseDuration
= duration
;
4536 configureDeferred();
4540 int SessionImpl::peerToS() const
4545 void SessionImpl::setPeerToS(const int value
)
4547 if (value
== m_peerToS
)
4551 configureDeferred();
4554 bool SessionImpl::ignoreLimitsOnLAN() const
4556 return m_ignoreLimitsOnLAN
;
4559 void SessionImpl::setIgnoreLimitsOnLAN(const bool ignore
)
4561 if (ignore
!= m_ignoreLimitsOnLAN
)
4563 m_ignoreLimitsOnLAN
= ignore
;
4564 configureDeferred();
4568 bool SessionImpl::includeOverheadInLimits() const
4570 return m_includeOverheadInLimits
;
4573 void SessionImpl::setIncludeOverheadInLimits(const bool include
)
4575 if (include
!= m_includeOverheadInLimits
)
4577 m_includeOverheadInLimits
= include
;
4578 configureDeferred();
4582 QString
SessionImpl::announceIP() const
4584 return m_announceIP
;
4587 void SessionImpl::setAnnounceIP(const QString
&ip
)
4589 if (ip
!= m_announceIP
)
4592 configureDeferred();
4596 int SessionImpl::maxConcurrentHTTPAnnounces() const
4598 return m_maxConcurrentHTTPAnnounces
;
4601 void SessionImpl::setMaxConcurrentHTTPAnnounces(const int value
)
4603 if (value
== m_maxConcurrentHTTPAnnounces
)
4606 m_maxConcurrentHTTPAnnounces
= value
;
4607 configureDeferred();
4610 bool SessionImpl::isReannounceWhenAddressChangedEnabled() const
4612 return m_isReannounceWhenAddressChangedEnabled
;
4615 void SessionImpl::setReannounceWhenAddressChangedEnabled(const bool enabled
)
4617 if (enabled
== m_isReannounceWhenAddressChangedEnabled
)
4620 m_isReannounceWhenAddressChangedEnabled
= enabled
;
4623 void SessionImpl::reannounceToAllTrackers() const
4625 for (const TorrentImpl
*torrent
: asConst(m_torrents
))
4629 torrent
->nativeHandle().force_reannounce(0, -1, lt::torrent_handle::ignore_min_interval
);
4631 catch (const std::exception
&) {}
4635 int SessionImpl::stopTrackerTimeout() const
4637 return m_stopTrackerTimeout
;
4640 void SessionImpl::setStopTrackerTimeout(const int value
)
4642 if (value
== m_stopTrackerTimeout
)
4645 m_stopTrackerTimeout
= value
;
4646 configureDeferred();
4649 int SessionImpl::maxConnections() const
4651 return m_maxConnections
;
4654 void SessionImpl::setMaxConnections(int max
)
4656 max
= (max
> 0) ? max
: -1;
4657 if (max
!= m_maxConnections
)
4659 m_maxConnections
= max
;
4660 configureDeferred();
4664 int SessionImpl::maxUploads() const
4666 return m_maxUploads
;
4669 void SessionImpl::setMaxUploads(int max
)
4671 max
= (max
> 0) ? max
: -1;
4672 if (max
!= m_maxUploads
)
4675 configureDeferred();
4679 BTProtocol
SessionImpl::btProtocol() const
4681 return m_btProtocol
;
4684 void SessionImpl::setBTProtocol(const BTProtocol protocol
)
4686 if ((protocol
< BTProtocol::Both
) || (BTProtocol::UTP
< protocol
))
4689 if (protocol
== m_btProtocol
) return;
4691 m_btProtocol
= protocol
;
4692 configureDeferred();
4695 bool SessionImpl::isUTPRateLimited() const
4697 return m_isUTPRateLimited
;
4700 void SessionImpl::setUTPRateLimited(const bool limited
)
4702 if (limited
!= m_isUTPRateLimited
)
4704 m_isUTPRateLimited
= limited
;
4705 configureDeferred();
4709 MixedModeAlgorithm
SessionImpl::utpMixedMode() const
4711 return m_utpMixedMode
;
4714 void SessionImpl::setUtpMixedMode(const MixedModeAlgorithm mode
)
4716 if (mode
== m_utpMixedMode
) return;
4718 m_utpMixedMode
= mode
;
4719 configureDeferred();
4722 bool SessionImpl::isIDNSupportEnabled() const
4724 return m_IDNSupportEnabled
;
4727 void SessionImpl::setIDNSupportEnabled(const bool enabled
)
4729 if (enabled
== m_IDNSupportEnabled
) return;
4731 m_IDNSupportEnabled
= enabled
;
4732 configureDeferred();
4735 bool SessionImpl::multiConnectionsPerIpEnabled() const
4737 return m_multiConnectionsPerIpEnabled
;
4740 void SessionImpl::setMultiConnectionsPerIpEnabled(const bool enabled
)
4742 if (enabled
== m_multiConnectionsPerIpEnabled
) return;
4744 m_multiConnectionsPerIpEnabled
= enabled
;
4745 configureDeferred();
4748 bool SessionImpl::validateHTTPSTrackerCertificate() const
4750 return m_validateHTTPSTrackerCertificate
;
4753 void SessionImpl::setValidateHTTPSTrackerCertificate(const bool enabled
)
4755 if (enabled
== m_validateHTTPSTrackerCertificate
) return;
4757 m_validateHTTPSTrackerCertificate
= enabled
;
4758 configureDeferred();
4761 bool SessionImpl::isSSRFMitigationEnabled() const
4763 return m_SSRFMitigationEnabled
;
4766 void SessionImpl::setSSRFMitigationEnabled(const bool enabled
)
4768 if (enabled
== m_SSRFMitigationEnabled
) return;
4770 m_SSRFMitigationEnabled
= enabled
;
4771 configureDeferred();
4774 bool SessionImpl::blockPeersOnPrivilegedPorts() const
4776 return m_blockPeersOnPrivilegedPorts
;
4779 void SessionImpl::setBlockPeersOnPrivilegedPorts(const bool enabled
)
4781 if (enabled
== m_blockPeersOnPrivilegedPorts
) return;
4783 m_blockPeersOnPrivilegedPorts
= enabled
;
4784 configureDeferred();
4787 bool SessionImpl::isTrackerFilteringEnabled() const
4789 return m_isTrackerFilteringEnabled
;
4792 void SessionImpl::setTrackerFilteringEnabled(const bool enabled
)
4794 if (enabled
!= m_isTrackerFilteringEnabled
)
4796 m_isTrackerFilteringEnabled
= enabled
;
4797 configureDeferred();
4801 bool SessionImpl::isListening() const
4803 return m_nativeSessionExtension
->isSessionListening();
4806 MaxRatioAction
SessionImpl::maxRatioAction() const
4808 return static_cast<MaxRatioAction
>(m_maxRatioAction
.get());
4811 void SessionImpl::setMaxRatioAction(const MaxRatioAction act
)
4813 m_maxRatioAction
= static_cast<int>(act
);
4816 bool SessionImpl::isKnownTorrent(const InfoHash
&infoHash
) const
4818 const bool isHybrid
= infoHash
.isHybrid();
4819 const auto id
= TorrentID::fromInfoHash(infoHash
);
4820 // alternative ID can be useful to find existing torrent
4821 // in case if hybrid torrent was added by v1 info hash
4822 const auto altID
= (isHybrid
? TorrentID::fromSHA1Hash(infoHash
.v1()) : TorrentID());
4824 if (m_loadingTorrents
.contains(id
) || (isHybrid
&& m_loadingTorrents
.contains(altID
)))
4826 if (m_downloadedMetadata
.contains(id
) || (isHybrid
&& m_downloadedMetadata
.contains(altID
)))
4828 return findTorrent(infoHash
);
4831 void SessionImpl::updateSeedingLimitTimer()
4833 if ((globalMaxRatio() == Torrent::NO_RATIO_LIMIT
) && !hasPerTorrentRatioLimit()
4834 && (globalMaxSeedingMinutes() == Torrent::NO_SEEDING_TIME_LIMIT
) && !hasPerTorrentSeedingTimeLimit()
4835 && (globalMaxInactiveSeedingMinutes() == Torrent::NO_INACTIVE_SEEDING_TIME_LIMIT
) && !hasPerTorrentInactiveSeedingTimeLimit())
4837 if (m_seedingLimitTimer
->isActive())
4838 m_seedingLimitTimer
->stop();
4840 else if (!m_seedingLimitTimer
->isActive())
4842 m_seedingLimitTimer
->start();
4846 void SessionImpl::handleTorrentShareLimitChanged(TorrentImpl
*const)
4848 updateSeedingLimitTimer();
4851 void SessionImpl::handleTorrentNameChanged(TorrentImpl
*const)
4855 void SessionImpl::handleTorrentSavePathChanged(TorrentImpl
*const torrent
)
4857 emit
torrentSavePathChanged(torrent
);
4860 void SessionImpl::handleTorrentCategoryChanged(TorrentImpl
*const torrent
, const QString
&oldCategory
)
4862 emit
torrentCategoryChanged(torrent
, oldCategory
);
4865 void SessionImpl::handleTorrentTagAdded(TorrentImpl
*const torrent
, const Tag
&tag
)
4867 emit
torrentTagAdded(torrent
, tag
);
4870 void SessionImpl::handleTorrentTagRemoved(TorrentImpl
*const torrent
, const Tag
&tag
)
4872 emit
torrentTagRemoved(torrent
, tag
);
4875 void SessionImpl::handleTorrentSavingModeChanged(TorrentImpl
*const torrent
)
4877 emit
torrentSavingModeChanged(torrent
);
4880 void SessionImpl::handleTorrentTrackersAdded(TorrentImpl
*const torrent
, const QVector
<TrackerEntry
> &newTrackers
)
4882 for (const TrackerEntry
&newTracker
: newTrackers
)
4883 LogMsg(tr("Added tracker to torrent. Torrent: \"%1\". Tracker: \"%2\"").arg(torrent
->name(), newTracker
.url
));
4884 emit
trackersAdded(torrent
, newTrackers
);
4887 void SessionImpl::handleTorrentTrackersRemoved(TorrentImpl
*const torrent
, const QStringList
&deletedTrackers
)
4889 for (const QString
&deletedTracker
: deletedTrackers
)
4890 LogMsg(tr("Removed tracker from torrent. Torrent: \"%1\". Tracker: \"%2\"").arg(torrent
->name(), deletedTracker
));
4891 emit
trackersRemoved(torrent
, deletedTrackers
);
4894 void SessionImpl::handleTorrentTrackersChanged(TorrentImpl
*const torrent
)
4896 emit
trackersChanged(torrent
);
4899 void SessionImpl::handleTorrentUrlSeedsAdded(TorrentImpl
*const torrent
, const QVector
<QUrl
> &newUrlSeeds
)
4901 for (const QUrl
&newUrlSeed
: newUrlSeeds
)
4902 LogMsg(tr("Added URL seed to torrent. Torrent: \"%1\". URL: \"%2\"").arg(torrent
->name(), newUrlSeed
.toString()));
4905 void SessionImpl::handleTorrentUrlSeedsRemoved(TorrentImpl
*const torrent
, const QVector
<QUrl
> &urlSeeds
)
4907 for (const QUrl
&urlSeed
: urlSeeds
)
4908 LogMsg(tr("Removed URL seed from torrent. Torrent: \"%1\". URL: \"%2\"").arg(torrent
->name(), urlSeed
.toString()));
4911 void SessionImpl::handleTorrentMetadataReceived(TorrentImpl
*const torrent
)
4913 if (!torrentExportDirectory().isEmpty())
4914 exportTorrentFile(torrent
, torrentExportDirectory());
4916 emit
torrentMetadataReceived(torrent
);
4919 void SessionImpl::handleTorrentPaused(TorrentImpl
*const torrent
)
4921 torrent
->resetTrackerEntries();
4923 const auto &trackerEntries
= torrent
->trackers();
4924 QHash
<QString
, TrackerEntry
> updatedTrackerEntries
;
4925 updatedTrackerEntries
.reserve(trackerEntries
.size());
4926 for (const auto &trackerEntry
: trackerEntries
)
4927 updatedTrackerEntries
.emplace(trackerEntry
.url
, trackerEntry
);
4928 emit
trackerEntriesUpdated(torrent
, updatedTrackerEntries
);
4930 LogMsg(tr("Torrent paused. Torrent: \"%1\"").arg(torrent
->name()));
4931 emit
torrentPaused(torrent
);
4934 void SessionImpl::handleTorrentResumed(TorrentImpl
*const torrent
)
4936 LogMsg(tr("Torrent resumed. Torrent: \"%1\"").arg(torrent
->name()));
4937 emit
torrentResumed(torrent
);
4940 void SessionImpl::handleTorrentChecked(TorrentImpl
*const torrent
)
4942 emit
torrentFinishedChecking(torrent
);
4945 void SessionImpl::handleTorrentFinished(TorrentImpl
*const torrent
)
4947 LogMsg(tr("Torrent download finished. Torrent: \"%1\"").arg(torrent
->name()));
4948 emit
torrentFinished(torrent
);
4950 if (const Path exportPath
= finishedTorrentExportDirectory(); !exportPath
.isEmpty())
4951 exportTorrentFile(torrent
, exportPath
);
4953 const bool hasUnfinishedTorrents
= std::any_of(m_torrents
.cbegin(), m_torrents
.cend(), [](const TorrentImpl
*torrent
)
4955 return !(torrent
->isFinished() || torrent
->isPaused() || torrent
->isErrored());
4957 if (!hasUnfinishedTorrents
)
4958 emit
allTorrentsFinished();
4961 void SessionImpl::handleTorrentResumeDataReady(TorrentImpl
*const torrent
, const LoadTorrentParams
&data
)
4965 m_resumeDataStorage
->store(torrent
->id(), data
);
4966 const auto iter
= m_changedTorrentIDs
.find(torrent
->id());
4967 if (iter
!= m_changedTorrentIDs
.end())
4969 m_resumeDataStorage
->remove(iter
.value());
4970 m_changedTorrentIDs
.erase(iter
);
4974 void SessionImpl::handleTorrentInfoHashChanged(TorrentImpl
*torrent
, const InfoHash
&prevInfoHash
)
4976 Q_ASSERT(torrent
->infoHash().isHybrid());
4978 m_hybridTorrentsByAltID
.insert(TorrentID::fromSHA1Hash(torrent
->infoHash().v1()), torrent
);
4980 const auto prevID
= TorrentID::fromInfoHash(prevInfoHash
);
4981 const TorrentID currentID
= torrent
->id();
4982 if (currentID
!= prevID
)
4984 m_torrents
[torrent
->id()] = m_torrents
.take(prevID
);
4985 m_changedTorrentIDs
[torrent
->id()] = prevID
;
4989 void SessionImpl::handleTorrentStorageMovingStateChanged(TorrentImpl
*torrent
)
4991 emit
torrentsUpdated({torrent
});
4994 bool SessionImpl::addMoveTorrentStorageJob(TorrentImpl
*torrent
, const Path
&newPath
, const MoveStorageMode mode
, const MoveStorageContext context
)
4998 const lt::torrent_handle torrentHandle
= torrent
->nativeHandle();
4999 const Path currentLocation
= torrent
->actualStorageLocation();
5000 const bool torrentHasActiveJob
= !m_moveStorageQueue
.isEmpty() && (m_moveStorageQueue
.first().torrentHandle
== torrentHandle
);
5002 if (m_moveStorageQueue
.size() > 1)
5004 auto iter
= std::find_if((m_moveStorageQueue
.begin() + 1), m_moveStorageQueue
.end()
5005 , [&torrentHandle
](const MoveStorageJob
&job
)
5007 return job
.torrentHandle
== torrentHandle
;
5010 if (iter
!= m_moveStorageQueue
.end())
5012 // remove existing inactive job
5013 torrent
->handleMoveStorageJobFinished(currentLocation
, iter
->context
, torrentHasActiveJob
);
5014 LogMsg(tr("Torrent move canceled. Torrent: \"%1\". Source: \"%2\". Destination: \"%3\"").arg(torrent
->name(), currentLocation
.toString(), iter
->path
.toString()));
5015 m_moveStorageQueue
.erase(iter
);
5019 if (torrentHasActiveJob
)
5021 // if there is active job for this torrent prevent creating meaningless
5022 // job that will move torrent to the same location as current one
5023 if (m_moveStorageQueue
.first().path
== newPath
)
5025 LogMsg(tr("Failed to enqueue torrent move. Torrent: \"%1\". Source: \"%2\". Destination: \"%3\". Reason: torrent is currently moving to the destination")
5026 .arg(torrent
->name(), currentLocation
.toString(), newPath
.toString()));
5032 if (currentLocation
== newPath
)
5034 LogMsg(tr("Failed to enqueue torrent move. Torrent: \"%1\". Source: \"%2\" Destination: \"%3\". Reason: both paths point to the same location")
5035 .arg(torrent
->name(), currentLocation
.toString(), newPath
.toString()));
5040 const MoveStorageJob moveStorageJob
{torrentHandle
, newPath
, mode
, context
};
5041 m_moveStorageQueue
<< moveStorageJob
;
5042 LogMsg(tr("Enqueued torrent move. Torrent: \"%1\". Source: \"%2\". Destination: \"%3\"").arg(torrent
->name(), currentLocation
.toString(), newPath
.toString()));
5044 if (m_moveStorageQueue
.size() == 1)
5045 moveTorrentStorage(moveStorageJob
);
5050 void SessionImpl::moveTorrentStorage(const MoveStorageJob
&job
) const
5052 #ifdef QBT_USES_LIBTORRENT2
5053 const auto id
= TorrentID::fromInfoHash(job
.torrentHandle
.info_hashes());
5055 const auto id
= TorrentID::fromInfoHash(job
.torrentHandle
.info_hash());
5057 const TorrentImpl
*torrent
= m_torrents
.value(id
);
5058 const QString torrentName
= (torrent
? torrent
->name() : id
.toString());
5059 LogMsg(tr("Start moving torrent. Torrent: \"%1\". Destination: \"%2\"").arg(torrentName
, job
.path
.toString()));
5061 job
.torrentHandle
.move_storage(job
.path
.toString().toStdString(), toNative(job
.mode
));
5064 void SessionImpl::handleMoveTorrentStorageJobFinished(const Path
&newPath
)
5066 const MoveStorageJob finishedJob
= m_moveStorageQueue
.takeFirst();
5067 if (!m_moveStorageQueue
.isEmpty())
5068 moveTorrentStorage(m_moveStorageQueue
.first());
5070 const auto iter
= std::find_if(m_moveStorageQueue
.cbegin(), m_moveStorageQueue
.cend()
5071 , [&finishedJob
](const MoveStorageJob
&job
)
5073 return job
.torrentHandle
== finishedJob
.torrentHandle
;
5076 const bool torrentHasOutstandingJob
= (iter
!= m_moveStorageQueue
.cend());
5078 TorrentImpl
*torrent
= m_torrents
.value(finishedJob
.torrentHandle
.info_hash());
5081 torrent
->handleMoveStorageJobFinished(newPath
, finishedJob
.context
, torrentHasOutstandingJob
);
5083 else if (!torrentHasOutstandingJob
)
5085 // Last job is completed for torrent that being removing, so actually remove it
5086 const lt::torrent_handle nativeHandle
{finishedJob
.torrentHandle
};
5087 const RemovingTorrentData
&removingTorrentData
= m_removingTorrents
[nativeHandle
.info_hash()];
5088 if (removingTorrentData
.deleteOption
== DeleteTorrent
)
5089 m_nativeSession
->remove_torrent(nativeHandle
, lt::session::delete_partfile
);
5093 void SessionImpl::storeCategories() const
5095 QJsonObject jsonObj
;
5096 for (auto it
= m_categories
.cbegin(); it
!= m_categories
.cend(); ++it
)
5098 const QString
&categoryName
= it
.key();
5099 const CategoryOptions
&categoryOptions
= it
.value();
5100 jsonObj
[categoryName
] = categoryOptions
.toJSON();
5103 const Path path
= specialFolderLocation(SpecialFolder::Config
) / CATEGORIES_FILE_NAME
;
5104 const QByteArray data
= QJsonDocument(jsonObj
).toJson();
5105 const nonstd::expected
<void, QString
> result
= Utils::IO::saveToFile(path
, data
);
5108 LogMsg(tr("Failed to save Categories configuration. File: \"%1\". Error: \"%2\"")
5109 .arg(path
.toString(), result
.error()), Log::WARNING
);
5113 void SessionImpl::upgradeCategories()
5115 const auto legacyCategories
= SettingValue
<QVariantMap
>(u
"BitTorrent/Session/Categories"_s
).get();
5116 for (auto it
= legacyCategories
.cbegin(); it
!= legacyCategories
.cend(); ++it
)
5118 const QString
&categoryName
= it
.key();
5119 CategoryOptions categoryOptions
;
5120 categoryOptions
.savePath
= Path(it
.value().toString());
5121 m_categories
[categoryName
] = categoryOptions
;
5127 void SessionImpl::loadCategories()
5129 m_categories
.clear();
5131 const Path path
= specialFolderLocation(SpecialFolder::Config
) / CATEGORIES_FILE_NAME
;
5134 // TODO: Remove the following upgrade code in v4.5
5135 // == BEGIN UPGRADE CODE ==
5136 upgradeCategories();
5137 m_needUpgradeDownloadPath
= true;
5138 // == END UPGRADE CODE ==
5143 const int fileMaxSize
= 1024 * 1024;
5144 const auto readResult
= Utils::IO::readFile(path
, fileMaxSize
);
5147 LogMsg(tr("Failed to load Categories. %1").arg(readResult
.error().message
), Log::WARNING
);
5151 QJsonParseError jsonError
;
5152 const QJsonDocument jsonDoc
= QJsonDocument::fromJson(readResult
.value(), &jsonError
);
5153 if (jsonError
.error
!= QJsonParseError::NoError
)
5155 LogMsg(tr("Failed to parse Categories configuration. File: \"%1\". Error: \"%2\"")
5156 .arg(path
.toString(), jsonError
.errorString()), Log::WARNING
);
5160 if (!jsonDoc
.isObject())
5162 LogMsg(tr("Failed to load Categories configuration. File: \"%1\". Error: \"Invalid data format\"")
5163 .arg(path
.toString()), Log::WARNING
);
5167 const QJsonObject jsonObj
= jsonDoc
.object();
5168 for (auto it
= jsonObj
.constBegin(); it
!= jsonObj
.constEnd(); ++it
)
5170 const QString
&categoryName
= it
.key();
5171 const auto categoryOptions
= CategoryOptions::fromJSON(it
.value().toObject());
5172 m_categories
[categoryName
] = categoryOptions
;
5176 bool SessionImpl::hasPerTorrentRatioLimit() const
5178 return std::any_of(m_torrents
.cbegin(), m_torrents
.cend(), [](const TorrentImpl
*torrent
)
5180 return (torrent
->ratioLimit() >= 0);
5184 bool SessionImpl::hasPerTorrentSeedingTimeLimit() const
5186 return std::any_of(m_torrents
.cbegin(), m_torrents
.cend(), [](const TorrentImpl
*torrent
)
5188 return (torrent
->seedingTimeLimit() >= 0);
5192 bool SessionImpl::hasPerTorrentInactiveSeedingTimeLimit() const
5194 return std::any_of(m_torrents
.cbegin(), m_torrents
.cend(), [](const TorrentImpl
*torrent
)
5196 return (torrent
->inactiveSeedingTimeLimit() >= 0);
5200 void SessionImpl::configureDeferred()
5202 if (m_deferredConfigureScheduled
)
5205 m_deferredConfigureScheduled
= true;
5206 QMetaObject::invokeMethod(this, qOverload
<>(&SessionImpl::configure
), Qt::QueuedConnection
);
5209 // Enable IP Filtering
5210 // this method creates ban list from scratch combining user ban list and 3rd party ban list file
5211 void SessionImpl::enableIPFilter()
5213 qDebug("Enabling IPFilter");
5214 // 1. Parse the IP filter
5215 // 2. In the slot add the manually banned IPs to the provided lt::ip_filter
5216 // 3. Set the ip_filter in one go so there isn't a time window where there isn't an ip_filter
5217 // set between clearing the old one and setting the new one.
5218 if (!m_filterParser
)
5220 m_filterParser
= new FilterParserThread(this);
5221 connect(m_filterParser
.data(), &FilterParserThread::IPFilterParsed
, this, &SessionImpl::handleIPFilterParsed
);
5222 connect(m_filterParser
.data(), &FilterParserThread::IPFilterError
, this, &SessionImpl::handleIPFilterError
);
5224 m_filterParser
->processFilterFile(IPFilterFile());
5227 // Disable IP Filtering
5228 void SessionImpl::disableIPFilter()
5230 qDebug("Disabling IPFilter");
5233 disconnect(m_filterParser
.data(), nullptr, this, nullptr);
5234 delete m_filterParser
;
5237 // Add the banned IPs after the IPFilter disabling
5238 // which creates an empty filter and overrides all previously
5240 lt::ip_filter filter
;
5241 processBannedIPs(filter
);
5242 m_nativeSession
->set_ip_filter(filter
);
5245 const SessionStatus
&SessionImpl::status() const
5250 const CacheStatus
&SessionImpl::cacheStatus() const
5252 return m_cacheStatus
;
5255 void SessionImpl::enqueueRefresh()
5257 Q_ASSERT(!m_refreshEnqueued
);
5259 QTimer::singleShot(refreshInterval(), Qt::CoarseTimer
, this, [this]
5261 m_nativeSession
->post_torrent_updates();
5262 m_nativeSession
->post_session_stats();
5264 if (m_torrentsQueueChanged
)
5266 m_torrentsQueueChanged
= false;
5267 m_needSaveTorrentsQueue
= true;
5271 m_refreshEnqueued
= true;
5274 void SessionImpl::handleIPFilterParsed(const int ruleCount
)
5278 lt::ip_filter filter
= m_filterParser
->IPfilter();
5279 processBannedIPs(filter
);
5280 m_nativeSession
->set_ip_filter(filter
);
5282 LogMsg(tr("Successfully parsed the IP filter file. Number of rules applied: %1").arg(ruleCount
));
5283 emit
IPFilterParsed(false, ruleCount
);
5286 void SessionImpl::handleIPFilterError()
5288 lt::ip_filter filter
;
5289 processBannedIPs(filter
);
5290 m_nativeSession
->set_ip_filter(filter
);
5292 LogMsg(tr("Failed to parse the IP filter file"), Log::WARNING
);
5293 emit
IPFilterParsed(true, 0);
5296 std::vector
<lt::alert
*> SessionImpl::getPendingAlerts(const lt::time_duration time
) const
5298 if (time
> lt::time_duration::zero())
5299 m_nativeSession
->wait_for_alert(time
);
5301 std::vector
<lt::alert
*> alerts
;
5302 m_nativeSession
->pop_alerts(&alerts
);
5306 TorrentContentLayout
SessionImpl::torrentContentLayout() const
5308 return m_torrentContentLayout
;
5311 void SessionImpl::setTorrentContentLayout(const TorrentContentLayout value
)
5313 m_torrentContentLayout
= value
;
5316 // Read alerts sent by the BitTorrent session
5317 void SessionImpl::readAlerts()
5319 const std::vector
<lt::alert
*> alerts
= getPendingAlerts();
5320 handleAddTorrentAlerts(alerts
);
5321 for (const lt::alert
*a
: alerts
)
5324 processTrackerStatuses();
5327 void SessionImpl::handleAddTorrentAlerts(const std::vector
<lt::alert
*> &alerts
)
5329 QVector
<Torrent
*> loadedTorrents
;
5331 loadedTorrents
.reserve(MAX_PROCESSING_RESUMEDATA_COUNT
);
5333 for (const lt::alert
*a
: alerts
)
5335 if (a
->type() != lt::add_torrent_alert::alert_type
)
5338 const auto *alert
= static_cast<const lt::add_torrent_alert
*>(a
);
5341 const QString msg
= QString::fromStdString(alert
->message());
5342 LogMsg(tr("Failed to load torrent. Reason: \"%1\"").arg(msg
), Log::WARNING
);
5343 emit
loadTorrentFailed(msg
);
5345 const lt::add_torrent_params
¶ms
= alert
->params
;
5346 const bool hasMetadata
= (params
.ti
&& params
.ti
->is_valid());
5347 #ifdef QBT_USES_LIBTORRENT2
5348 const InfoHash infoHash
{(hasMetadata
? params
.ti
->info_hashes() : params
.info_hashes
)};
5349 if (infoHash
.isHybrid())
5350 m_hybridTorrentsByAltID
.remove(TorrentID::fromSHA1Hash(infoHash
.v1()));
5352 const InfoHash infoHash
{(hasMetadata
? params
.ti
->info_hash() : params
.info_hash
)};
5354 if (const auto loadingTorrentsIter
= m_loadingTorrents
.find(TorrentID::fromInfoHash(infoHash
))
5355 ; loadingTorrentsIter
!= m_loadingTorrents
.end())
5357 emit
addTorrentFailed(infoHash
, msg
);
5358 m_loadingTorrents
.erase(loadingTorrentsIter
);
5360 else if (const auto downloadedMetadataIter
= m_downloadedMetadata
.find(TorrentID::fromInfoHash(infoHash
))
5361 ; downloadedMetadataIter
!= m_downloadedMetadata
.end())
5363 m_downloadedMetadata
.erase(downloadedMetadataIter
);
5364 if (infoHash
.isHybrid())
5366 // index hybrid magnet links by both v1 and v2 info hashes
5367 const auto altID
= TorrentID::fromSHA1Hash(infoHash
.v1());
5368 m_downloadedMetadata
.remove(altID
);
5376 #ifdef QBT_USES_LIBTORRENT2
5377 const InfoHash infoHash
{alert
->handle
.info_hashes()};
5379 const InfoHash infoHash
{alert
->handle
.info_hash()};
5381 const auto torrentID
= TorrentID::fromInfoHash(infoHash
);
5383 if (const auto loadingTorrentsIter
= m_loadingTorrents
.find(torrentID
)
5384 ; loadingTorrentsIter
!= m_loadingTorrents
.end())
5386 const LoadTorrentParams params
= loadingTorrentsIter
.value();
5387 m_loadingTorrents
.erase(loadingTorrentsIter
);
5389 Torrent
*torrent
= createTorrent(alert
->handle
, params
);
5390 loadedTorrents
.append(torrent
);
5392 else if (const auto downloadedMetadataIter
= m_downloadedMetadata
.find(torrentID
)
5393 ; downloadedMetadataIter
!= m_downloadedMetadata
.end())
5395 downloadedMetadataIter
.value() = alert
->handle
;
5396 if (infoHash
.isHybrid())
5398 // index hybrid magnet links by both v1 and v2 info hashes
5399 const auto altID
= TorrentID::fromSHA1Hash(infoHash
.v1());
5400 m_downloadedMetadata
[altID
] = alert
->handle
;
5405 if (!loadedTorrents
.isEmpty())
5408 m_torrentsQueueChanged
= true;
5409 emit
torrentsLoaded(loadedTorrents
);
5413 void SessionImpl::handleAlert(const lt::alert
*a
)
5419 #ifdef QBT_USES_LIBTORRENT2
5420 case lt::file_prio_alert::alert_type
:
5422 case lt::file_renamed_alert::alert_type
:
5423 case lt::file_rename_failed_alert::alert_type
:
5424 case lt::file_completed_alert::alert_type
:
5425 case lt::torrent_finished_alert::alert_type
:
5426 case lt::save_resume_data_alert::alert_type
:
5427 case lt::save_resume_data_failed_alert::alert_type
:
5428 case lt::torrent_paused_alert::alert_type
:
5429 case lt::torrent_resumed_alert::alert_type
:
5430 case lt::fastresume_rejected_alert::alert_type
:
5431 case lt::torrent_checked_alert::alert_type
:
5432 case lt::metadata_received_alert::alert_type
:
5433 case lt::performance_alert::alert_type
:
5434 dispatchTorrentAlert(static_cast<const lt::torrent_alert
*>(a
));
5436 case lt::state_update_alert::alert_type
:
5437 handleStateUpdateAlert(static_cast<const lt::state_update_alert
*>(a
));
5439 case lt::session_error_alert::alert_type
:
5440 handleSessionErrorAlert(static_cast<const lt::session_error_alert
*>(a
));
5442 case lt::session_stats_alert::alert_type
:
5443 handleSessionStatsAlert(static_cast<const lt::session_stats_alert
*>(a
));
5445 case lt::tracker_announce_alert::alert_type
:
5446 case lt::tracker_error_alert::alert_type
:
5447 case lt::tracker_reply_alert::alert_type
:
5448 case lt::tracker_warning_alert::alert_type
:
5449 handleTrackerAlert(static_cast<const lt::tracker_alert
*>(a
));
5451 case lt::file_error_alert::alert_type
:
5452 handleFileErrorAlert(static_cast<const lt::file_error_alert
*>(a
));
5454 case lt::add_torrent_alert::alert_type
:
5455 // handled separately
5457 case lt::torrent_removed_alert::alert_type
:
5458 handleTorrentRemovedAlert(static_cast<const lt::torrent_removed_alert
*>(a
));
5460 case lt::torrent_deleted_alert::alert_type
:
5461 handleTorrentDeletedAlert(static_cast<const lt::torrent_deleted_alert
*>(a
));
5463 case lt::torrent_delete_failed_alert::alert_type
:
5464 handleTorrentDeleteFailedAlert(static_cast<const lt::torrent_delete_failed_alert
*>(a
));
5466 case lt::portmap_error_alert::alert_type
:
5467 handlePortmapWarningAlert(static_cast<const lt::portmap_error_alert
*>(a
));
5469 case lt::portmap_alert::alert_type
:
5470 handlePortmapAlert(static_cast<const lt::portmap_alert
*>(a
));
5472 case lt::peer_blocked_alert::alert_type
:
5473 handlePeerBlockedAlert(static_cast<const lt::peer_blocked_alert
*>(a
));
5475 case lt::peer_ban_alert::alert_type
:
5476 handlePeerBanAlert(static_cast<const lt::peer_ban_alert
*>(a
));
5478 case lt::url_seed_alert::alert_type
:
5479 handleUrlSeedAlert(static_cast<const lt::url_seed_alert
*>(a
));
5481 case lt::listen_succeeded_alert::alert_type
:
5482 handleListenSucceededAlert(static_cast<const lt::listen_succeeded_alert
*>(a
));
5484 case lt::listen_failed_alert::alert_type
:
5485 handleListenFailedAlert(static_cast<const lt::listen_failed_alert
*>(a
));
5487 case lt::external_ip_alert::alert_type
:
5488 handleExternalIPAlert(static_cast<const lt::external_ip_alert
*>(a
));
5490 case lt::alerts_dropped_alert::alert_type
:
5491 handleAlertsDroppedAlert(static_cast<const lt::alerts_dropped_alert
*>(a
));
5493 case lt::storage_moved_alert::alert_type
:
5494 handleStorageMovedAlert(static_cast<const lt::storage_moved_alert
*>(a
));
5496 case lt::storage_moved_failed_alert::alert_type
:
5497 handleStorageMovedFailedAlert(static_cast<const lt::storage_moved_failed_alert
*>(a
));
5499 case lt::socks5_alert::alert_type
:
5500 handleSocks5Alert(static_cast<const lt::socks5_alert
*>(a
));
5502 case lt::i2p_alert::alert_type
:
5503 handleI2PAlert(static_cast<const lt::i2p_alert
*>(a
));
5505 #ifdef QBT_USES_LIBTORRENT2
5506 case lt::torrent_conflict_alert::alert_type
:
5507 handleTorrentConflictAlert(static_cast<const lt::torrent_conflict_alert
*>(a
));
5512 catch (const std::exception
&exc
)
5514 qWarning() << "Caught exception in " << Q_FUNC_INFO
<< ": " << QString::fromStdString(exc
.what());
5518 void SessionImpl::dispatchTorrentAlert(const lt::torrent_alert
*a
)
5520 const TorrentID torrentID
{a
->handle
.info_hash()};
5521 TorrentImpl
*torrent
= m_torrents
.value(torrentID
);
5522 #ifdef QBT_USES_LIBTORRENT2
5523 if (!torrent
&& (a
->type() == lt::metadata_received_alert::alert_type
))
5525 const InfoHash infoHash
{a
->handle
.info_hashes()};
5526 if (infoHash
.isHybrid())
5527 torrent
= m_torrents
.value(TorrentID::fromSHA1Hash(infoHash
.v1()));
5533 torrent
->handleAlert(a
);
5539 case lt::metadata_received_alert::alert_type
:
5540 handleMetadataReceivedAlert(static_cast<const lt::metadata_received_alert
*>(a
));
5545 TorrentImpl
*SessionImpl::createTorrent(const lt::torrent_handle
&nativeHandle
, const LoadTorrentParams
¶ms
)
5547 auto *const torrent
= new TorrentImpl(this, m_nativeSession
, nativeHandle
, params
);
5548 m_torrents
.insert(torrent
->id(), torrent
);
5549 if (const InfoHash infoHash
= torrent
->infoHash(); infoHash
.isHybrid())
5550 m_hybridTorrentsByAltID
.insert(TorrentID::fromSHA1Hash(infoHash
.v1()), torrent
);
5554 if (params
.addToQueueTop
)
5555 nativeHandle
.queue_position_top();
5557 torrent
->saveResumeData(lt::torrent_handle::save_info_dict
);
5559 // The following is useless for newly added magnet
5560 if (torrent
->hasMetadata())
5562 if (!torrentExportDirectory().isEmpty())
5563 exportTorrentFile(torrent
, torrentExportDirectory());
5567 if (((torrent
->ratioLimit() >= 0) || (torrent
->seedingTimeLimit() >= 0))
5568 && !m_seedingLimitTimer
->isActive())
5570 m_seedingLimitTimer
->start();
5575 LogMsg(tr("Restored torrent. Torrent: \"%1\"").arg(torrent
->name()));
5579 LogMsg(tr("Added new torrent. Torrent: \"%1\"").arg(torrent
->name()));
5580 emit
torrentAdded(torrent
);
5583 // Torrent could have error just after adding to libtorrent
5584 if (torrent
->hasError())
5585 LogMsg(tr("Torrent errored. Torrent: \"%1\". Error: \"%2\"").arg(torrent
->name(), torrent
->error()), Log::WARNING
);
5590 void SessionImpl::handleTorrentRemovedAlert(const lt::torrent_removed_alert
*p
)
5592 #ifdef QBT_USES_LIBTORRENT2
5593 const auto id
= TorrentID::fromInfoHash(p
->info_hashes
);
5595 const auto id
= TorrentID::fromInfoHash(p
->info_hash
);
5598 const auto removingTorrentDataIter
= m_removingTorrents
.find(id
);
5599 if (removingTorrentDataIter
!= m_removingTorrents
.end())
5601 if (removingTorrentDataIter
->deleteOption
== DeleteTorrent
)
5603 LogMsg(tr("Removed torrent. Torrent: \"%1\"").arg(removingTorrentDataIter
->name
));
5604 m_removingTorrents
.erase(removingTorrentDataIter
);
5609 void SessionImpl::handleTorrentDeletedAlert(const lt::torrent_deleted_alert
*p
)
5611 #ifdef QBT_USES_LIBTORRENT2
5612 const auto id
= TorrentID::fromInfoHash(p
->info_hashes
);
5614 const auto id
= TorrentID::fromInfoHash(p
->info_hash
);
5617 const auto removingTorrentDataIter
= m_removingTorrents
.find(id
);
5618 if (removingTorrentDataIter
== m_removingTorrents
.end())
5621 // torrent_deleted_alert can also be posted due to deletion of partfile. Ignore it in such a case.
5622 if (removingTorrentDataIter
->deleteOption
== DeleteTorrent
)
5625 Utils::Fs::smartRemoveEmptyFolderTree(removingTorrentDataIter
->pathToRemove
);
5626 LogMsg(tr("Removed torrent and deleted its content. Torrent: \"%1\"").arg(removingTorrentDataIter
->name
));
5627 m_removingTorrents
.erase(removingTorrentDataIter
);
5630 void SessionImpl::handleTorrentDeleteFailedAlert(const lt::torrent_delete_failed_alert
*p
)
5632 #ifdef QBT_USES_LIBTORRENT2
5633 const auto id
= TorrentID::fromInfoHash(p
->info_hashes
);
5635 const auto id
= TorrentID::fromInfoHash(p
->info_hash
);
5638 const auto removingTorrentDataIter
= m_removingTorrents
.find(id
);
5639 if (removingTorrentDataIter
== m_removingTorrents
.end())
5644 // libtorrent won't delete the directory if it contains files not listed in the torrent,
5645 // so we remove the directory ourselves
5646 Utils::Fs::smartRemoveEmptyFolderTree(removingTorrentDataIter
->pathToRemove
);
5648 LogMsg(tr("Removed torrent but failed to delete its content and/or partfile. Torrent: \"%1\". Error: \"%2\"")
5649 .arg(removingTorrentDataIter
->name
, QString::fromLocal8Bit(p
->error
.message().c_str()))
5652 else // torrent without metadata, hence no files on disk
5654 LogMsg(tr("Removed torrent. Torrent: \"%1\"").arg(removingTorrentDataIter
->name
));
5657 m_removingTorrents
.erase(removingTorrentDataIter
);
5660 void SessionImpl::handleMetadataReceivedAlert(const lt::metadata_received_alert
*p
)
5662 const TorrentID torrentID
{p
->handle
.info_hash()};
5665 if (const auto iter
= m_downloadedMetadata
.find(torrentID
); iter
!= m_downloadedMetadata
.end())
5668 m_downloadedMetadata
.erase(iter
);
5670 #ifdef QBT_USES_LIBTORRENT2
5671 const InfoHash infoHash
{p
->handle
.info_hashes()};
5672 if (infoHash
.isHybrid())
5674 const auto altID
= TorrentID::fromSHA1Hash(infoHash
.v1());
5675 if (const auto iter
= m_downloadedMetadata
.find(altID
); iter
!= m_downloadedMetadata
.end())
5678 m_downloadedMetadata
.erase(iter
);
5684 const TorrentInfo metadata
{*p
->handle
.torrent_file()};
5685 m_nativeSession
->remove_torrent(p
->handle
, lt::session::delete_files
);
5687 emit
metadataDownloaded(metadata
);
5691 void SessionImpl::handleFileErrorAlert(const lt::file_error_alert
*p
)
5693 TorrentImpl
*const torrent
= m_torrents
.value(p
->handle
.info_hash());
5697 torrent
->handleAlert(p
);
5699 const TorrentID id
= torrent
->id();
5700 if (!m_recentErroredTorrents
.contains(id
))
5702 m_recentErroredTorrents
.insert(id
);
5704 const QString msg
= QString::fromStdString(p
->message());
5705 LogMsg(tr("File error alert. Torrent: \"%1\". File: \"%2\". Reason: \"%3\"")
5706 .arg(torrent
->name(), QString::fromUtf8(p
->filename()), msg
)
5708 emit
fullDiskError(torrent
, msg
);
5711 m_recentErroredTorrentsTimer
->start();
5714 void SessionImpl::handlePortmapWarningAlert(const lt::portmap_error_alert
*p
)
5716 LogMsg(tr("UPnP/NAT-PMP port mapping failed. Message: \"%1\"").arg(QString::fromStdString(p
->message())), Log::WARNING
);
5719 void SessionImpl::handlePortmapAlert(const lt::portmap_alert
*p
)
5721 qDebug("UPnP Success, msg: %s", p
->message().c_str());
5722 LogMsg(tr("UPnP/NAT-PMP port mapping succeeded. Message: \"%1\"").arg(QString::fromStdString(p
->message())), Log::INFO
);
5725 void SessionImpl::handlePeerBlockedAlert(const lt::peer_blocked_alert
*p
)
5730 case lt::peer_blocked_alert::ip_filter
:
5731 reason
= tr("IP filter", "this peer was blocked. Reason: IP filter.");
5733 case lt::peer_blocked_alert::port_filter
:
5734 reason
= tr("filtered port (%1)", "this peer was blocked. Reason: filtered port (8899).").arg(QString::number(p
->endpoint
.port()));
5736 case lt::peer_blocked_alert::i2p_mixed
:
5737 reason
= tr("%1 mixed mode restrictions", "this peer was blocked. Reason: I2P mixed mode restrictions.").arg(u
"I2P"_s
); // don't translate I2P
5739 case lt::peer_blocked_alert::privileged_ports
:
5740 reason
= tr("privileged port (%1)", "this peer was blocked. Reason: privileged port (80).").arg(QString::number(p
->endpoint
.port()));
5742 case lt::peer_blocked_alert::utp_disabled
:
5743 reason
= tr("%1 is disabled", "this peer was blocked. Reason: uTP is disabled.").arg(C_UTP
); // don't translate μTP
5745 case lt::peer_blocked_alert::tcp_disabled
:
5746 reason
= tr("%1 is disabled", "this peer was blocked. Reason: TCP is disabled.").arg(u
"TCP"_s
); // don't translate TCP
5750 const QString ip
{toString(p
->endpoint
.address())};
5752 Logger::instance()->addPeer(ip
, true, reason
);
5755 void SessionImpl::handlePeerBanAlert(const lt::peer_ban_alert
*p
)
5757 const QString ip
{toString(p
->endpoint
.address())};
5759 Logger::instance()->addPeer(ip
, false);
5762 void SessionImpl::handleUrlSeedAlert(const lt::url_seed_alert
*p
)
5764 const TorrentImpl
*torrent
= m_torrents
.value(p
->handle
.info_hash());
5770 LogMsg(tr("URL seed DNS lookup failed. Torrent: \"%1\". URL: \"%2\". Error: \"%3\"")
5771 .arg(torrent
->name(), QString::fromUtf8(p
->server_url()), QString::fromStdString(p
->message()))
5776 LogMsg(tr("Received error message from URL seed. Torrent: \"%1\". URL: \"%2\". Message: \"%3\"")
5777 .arg(torrent
->name(), QString::fromUtf8(p
->server_url()), QString::fromUtf8(p
->error_message()))
5782 void SessionImpl::handleListenSucceededAlert(const lt::listen_succeeded_alert
*p
)
5784 const QString proto
{toString(p
->socket_type
)};
5785 LogMsg(tr("Successfully listening on IP. IP: \"%1\". Port: \"%2/%3\"")
5786 .arg(toString(p
->address
), proto
, QString::number(p
->port
)), Log::INFO
);
5789 void SessionImpl::handleListenFailedAlert(const lt::listen_failed_alert
*p
)
5791 const QString proto
{toString(p
->socket_type
)};
5792 LogMsg(tr("Failed to listen on IP. IP: \"%1\". Port: \"%2/%3\". Reason: \"%4\"")
5793 .arg(toString(p
->address
), proto
, QString::number(p
->port
)
5794 , QString::fromLocal8Bit(p
->error
.message().c_str())), Log::CRITICAL
);
5797 void SessionImpl::handleExternalIPAlert(const lt::external_ip_alert
*p
)
5799 const QString externalIP
{toString(p
->external_address
)};
5800 LogMsg(tr("Detected external IP. IP: \"%1\"")
5801 .arg(externalIP
), Log::INFO
);
5803 if (m_lastExternalIP
!= externalIP
)
5805 if (isReannounceWhenAddressChangedEnabled() && !m_lastExternalIP
.isEmpty())
5806 reannounceToAllTrackers();
5807 m_lastExternalIP
= externalIP
;
5811 void SessionImpl::handleSessionErrorAlert(const lt::session_error_alert
*p
) const
5813 LogMsg(tr("BitTorrent session encountered a serious error. Reason: \"%1\"")
5814 .arg(QString::fromStdString(p
->message())), Log::CRITICAL
);
5817 void SessionImpl::handleSessionStatsAlert(const lt::session_stats_alert
*p
)
5819 if (m_refreshEnqueued
)
5820 m_refreshEnqueued
= false;
5824 const int64_t interval
= lt::total_microseconds(p
->timestamp() - m_statsLastTimestamp
);
5828 m_statsLastTimestamp
= p
->timestamp();
5830 const auto stats
= p
->counters();
5832 m_status
.hasIncomingConnections
= static_cast<bool>(stats
[m_metricIndices
.net
.hasIncomingConnections
]);
5834 const int64_t ipOverheadDownload
= stats
[m_metricIndices
.net
.recvIPOverheadBytes
];
5835 const int64_t ipOverheadUpload
= stats
[m_metricIndices
.net
.sentIPOverheadBytes
];
5836 const int64_t totalDownload
= stats
[m_metricIndices
.net
.recvBytes
] + ipOverheadDownload
;
5837 const int64_t totalUpload
= stats
[m_metricIndices
.net
.sentBytes
] + ipOverheadUpload
;
5838 const int64_t totalPayloadDownload
= stats
[m_metricIndices
.net
.recvPayloadBytes
];
5839 const int64_t totalPayloadUpload
= stats
[m_metricIndices
.net
.sentPayloadBytes
];
5840 const int64_t trackerDownload
= stats
[m_metricIndices
.net
.recvTrackerBytes
];
5841 const int64_t trackerUpload
= stats
[m_metricIndices
.net
.sentTrackerBytes
];
5842 const int64_t dhtDownload
= stats
[m_metricIndices
.dht
.dhtBytesIn
];
5843 const int64_t dhtUpload
= stats
[m_metricIndices
.dht
.dhtBytesOut
];
5845 const auto calcRate
= [interval
](const qint64 previous
, const qint64 current
) -> qint64
5847 Q_ASSERT(current
>= previous
);
5848 Q_ASSERT(interval
>= 0);
5849 return (((current
- previous
) * lt::microseconds(1s
).count()) / interval
);
5852 m_status
.payloadDownloadRate
= calcRate(m_status
.totalPayloadDownload
, totalPayloadDownload
);
5853 m_status
.payloadUploadRate
= calcRate(m_status
.totalPayloadUpload
, totalPayloadUpload
);
5854 m_status
.downloadRate
= calcRate(m_status
.totalDownload
, totalDownload
);
5855 m_status
.uploadRate
= calcRate(m_status
.totalUpload
, totalUpload
);
5856 m_status
.ipOverheadDownloadRate
= calcRate(m_status
.ipOverheadDownload
, ipOverheadDownload
);
5857 m_status
.ipOverheadUploadRate
= calcRate(m_status
.ipOverheadUpload
, ipOverheadUpload
);
5858 m_status
.dhtDownloadRate
= calcRate(m_status
.dhtDownload
, dhtDownload
);
5859 m_status
.dhtUploadRate
= calcRate(m_status
.dhtUpload
, dhtUpload
);
5860 m_status
.trackerDownloadRate
= calcRate(m_status
.trackerDownload
, trackerDownload
);
5861 m_status
.trackerUploadRate
= calcRate(m_status
.trackerUpload
, trackerUpload
);
5863 m_status
.totalPayloadDownload
= totalPayloadDownload
;
5864 m_status
.totalPayloadUpload
= totalPayloadUpload
;
5865 m_status
.ipOverheadDownload
= ipOverheadDownload
;
5866 m_status
.ipOverheadUpload
= ipOverheadUpload
;
5867 m_status
.trackerDownload
= trackerDownload
;
5868 m_status
.trackerUpload
= trackerUpload
;
5869 m_status
.dhtDownload
= dhtDownload
;
5870 m_status
.dhtUpload
= dhtUpload
;
5871 m_status
.totalWasted
= stats
[m_metricIndices
.net
.recvRedundantBytes
]
5872 + stats
[m_metricIndices
.net
.recvFailedBytes
];
5873 m_status
.dhtNodes
= stats
[m_metricIndices
.dht
.dhtNodes
];
5874 m_status
.diskReadQueue
= stats
[m_metricIndices
.peer
.numPeersUpDisk
];
5875 m_status
.diskWriteQueue
= stats
[m_metricIndices
.peer
.numPeersDownDisk
];
5876 m_status
.peersCount
= stats
[m_metricIndices
.peer
.numPeersConnected
];
5878 if (totalDownload
> m_status
.totalDownload
)
5880 m_status
.totalDownload
= totalDownload
;
5881 m_isStatisticsDirty
= true;
5884 if (totalUpload
> m_status
.totalUpload
)
5886 m_status
.totalUpload
= totalUpload
;
5887 m_isStatisticsDirty
= true;
5890 m_status
.allTimeDownload
= m_previouslyDownloaded
+ m_status
.totalDownload
;
5891 m_status
.allTimeUpload
= m_previouslyUploaded
+ m_status
.totalUpload
;
5893 if (m_statisticsLastUpdateTimer
.hasExpired(STATISTICS_SAVE_INTERVAL
))
5896 m_cacheStatus
.totalUsedBuffers
= stats
[m_metricIndices
.disk
.diskBlocksInUse
];
5897 m_cacheStatus
.jobQueueLength
= stats
[m_metricIndices
.disk
.queuedDiskJobs
];
5899 #ifndef QBT_USES_LIBTORRENT2
5900 const int64_t numBlocksRead
= stats
[m_metricIndices
.disk
.numBlocksRead
];
5901 const int64_t numBlocksCacheHits
= stats
[m_metricIndices
.disk
.numBlocksCacheHits
];
5902 m_cacheStatus
.readRatio
= static_cast<qreal
>(numBlocksCacheHits
) / std::max
<int64_t>((numBlocksCacheHits
+ numBlocksRead
), 1);
5905 const int64_t totalJobs
= stats
[m_metricIndices
.disk
.writeJobs
] + stats
[m_metricIndices
.disk
.readJobs
]
5906 + stats
[m_metricIndices
.disk
.hashJobs
];
5907 m_cacheStatus
.averageJobTime
= (totalJobs
> 0)
5908 ? (stats
[m_metricIndices
.disk
.diskJobTime
] / totalJobs
) : 0;
5910 emit
statsUpdated();
5913 void SessionImpl::handleAlertsDroppedAlert(const lt::alerts_dropped_alert
*p
) const
5915 LogMsg(tr("Error: Internal alert queue is full and alerts are dropped, you might see degraded performance. Dropped alert type: \"%1\". Message: \"%2\"")
5916 .arg(QString::fromStdString(p
->dropped_alerts
.to_string()), QString::fromStdString(p
->message())), Log::CRITICAL
);
5919 void SessionImpl::handleStorageMovedAlert(const lt::storage_moved_alert
*p
)
5921 Q_ASSERT(!m_moveStorageQueue
.isEmpty());
5923 const MoveStorageJob
¤tJob
= m_moveStorageQueue
.first();
5924 Q_ASSERT(currentJob
.torrentHandle
== p
->handle
);
5926 const Path newPath
{QString::fromUtf8(p
->storage_path())};
5927 Q_ASSERT(newPath
== currentJob
.path
);
5929 #ifdef QBT_USES_LIBTORRENT2
5930 const auto id
= TorrentID::fromInfoHash(currentJob
.torrentHandle
.info_hashes());
5932 const auto id
= TorrentID::fromInfoHash(currentJob
.torrentHandle
.info_hash());
5935 TorrentImpl
*torrent
= m_torrents
.value(id
);
5936 const QString torrentName
= (torrent
? torrent
->name() : id
.toString());
5937 LogMsg(tr("Moved torrent successfully. Torrent: \"%1\". Destination: \"%2\"").arg(torrentName
, newPath
.toString()));
5939 handleMoveTorrentStorageJobFinished(newPath
);
5942 void SessionImpl::handleStorageMovedFailedAlert(const lt::storage_moved_failed_alert
*p
)
5944 Q_ASSERT(!m_moveStorageQueue
.isEmpty());
5946 const MoveStorageJob
¤tJob
= m_moveStorageQueue
.first();
5947 Q_ASSERT(currentJob
.torrentHandle
== p
->handle
);
5949 #ifdef QBT_USES_LIBTORRENT2
5950 const auto id
= TorrentID::fromInfoHash(currentJob
.torrentHandle
.info_hashes());
5952 const auto id
= TorrentID::fromInfoHash(currentJob
.torrentHandle
.info_hash());
5955 TorrentImpl
*torrent
= m_torrents
.value(id
);
5956 const QString torrentName
= (torrent
? torrent
->name() : id
.toString());
5957 const Path currentLocation
= (torrent
? torrent
->actualStorageLocation()
5958 : Path(p
->handle
.status(lt::torrent_handle::query_save_path
).save_path
));
5959 const QString errorMessage
= QString::fromStdString(p
->message());
5960 LogMsg(tr("Failed to move torrent. Torrent: \"%1\". Source: \"%2\". Destination: \"%3\". Reason: \"%4\"")
5961 .arg(torrentName
, currentLocation
.toString(), currentJob
.path
.toString(), errorMessage
), Log::WARNING
);
5963 handleMoveTorrentStorageJobFinished(currentLocation
);
5966 void SessionImpl::handleStateUpdateAlert(const lt::state_update_alert
*p
)
5968 QVector
<Torrent
*> updatedTorrents
;
5969 updatedTorrents
.reserve(static_cast<decltype(updatedTorrents
)::size_type
>(p
->status
.size()));
5971 for (const lt::torrent_status
&status
: p
->status
)
5973 #ifdef QBT_USES_LIBTORRENT2
5974 const auto id
= TorrentID::fromInfoHash(status
.info_hashes
);
5976 const auto id
= TorrentID::fromInfoHash(status
.info_hash
);
5978 TorrentImpl
*const torrent
= m_torrents
.value(id
);
5982 torrent
->handleStateUpdate(status
);
5983 updatedTorrents
.push_back(torrent
);
5986 if (!updatedTorrents
.isEmpty())
5987 emit
torrentsUpdated(updatedTorrents
);
5989 if (m_needSaveTorrentsQueue
)
5990 saveTorrentsQueue();
5992 if (m_refreshEnqueued
)
5993 m_refreshEnqueued
= false;
5998 void SessionImpl::handleSocks5Alert(const lt::socks5_alert
*p
) const
6002 const auto addr
= p
->ip
.address();
6003 const QString endpoint
= (addr
.is_v6() ? u
"[%1]:%2"_s
: u
"%1:%2"_s
)
6004 .arg(QString::fromStdString(addr
.to_string()), QString::number(p
->ip
.port()));
6005 LogMsg(tr("SOCKS5 proxy error. Address: %1. Message: \"%2\".")
6006 .arg(endpoint
, QString::fromLocal8Bit(p
->error
.message().c_str()))
6011 void SessionImpl::handleI2PAlert(const lt::i2p_alert
*p
) const
6015 LogMsg(tr("I2P error. Message: \"%1\".")
6016 .arg(QString::fromStdString(p
->message())), Log::WARNING
);
6020 void SessionImpl::handleTrackerAlert(const lt::tracker_alert
*a
)
6022 TorrentImpl
*torrent
= m_torrents
.value(a
->handle
.info_hash());
6026 QMap
<int, int> &updateInfo
= m_updatedTrackerEntries
[torrent
->nativeHandle()][std::string(a
->tracker_url())][a
->local_endpoint
];
6028 if (a
->type() == lt::tracker_reply_alert::alert_type
)
6030 const int numPeers
= static_cast<const lt::tracker_reply_alert
*>(a
)->num_peers
;
6031 #ifdef QBT_USES_LIBTORRENT2
6032 const int protocolVersionNum
= (static_cast<const lt::tracker_reply_alert
*>(a
)->version
== lt::protocol_version::V1
) ? 1 : 2;
6034 const int protocolVersionNum
= 1;
6036 updateInfo
.insert(protocolVersionNum
, numPeers
);
6040 #ifdef QBT_USES_LIBTORRENT2
6041 void SessionImpl::handleTorrentConflictAlert(const lt::torrent_conflict_alert
*a
)
6043 const auto torrentIDv1
= TorrentID::fromSHA1Hash(a
->metadata
->info_hashes().v1
);
6044 const auto torrentIDv2
= TorrentID::fromSHA256Hash(a
->metadata
->info_hashes().v2
);
6045 TorrentImpl
*torrent1
= m_torrents
.value(torrentIDv1
);
6046 TorrentImpl
*torrent2
= m_torrents
.value(torrentIDv2
);
6050 deleteTorrent(torrentIDv1
);
6052 cancelDownloadMetadata(torrentIDv1
);
6054 invokeAsync([torrentHandle
= torrent2
->nativeHandle(), metadata
= a
->metadata
]
6058 torrentHandle
.set_metadata(metadata
->info_section());
6060 catch (const std::exception
&) {}
6066 cancelDownloadMetadata(torrentIDv2
);
6068 invokeAsync([torrentHandle
= torrent1
->nativeHandle(), metadata
= a
->metadata
]
6072 torrentHandle
.set_metadata(metadata
->info_section());
6074 catch (const std::exception
&) {}
6079 cancelDownloadMetadata(torrentIDv1
);
6080 cancelDownloadMetadata(torrentIDv2
);
6083 if (!torrent1
|| !torrent2
)
6084 emit
metadataDownloaded(TorrentInfo(*a
->metadata
));
6088 void SessionImpl::processTrackerStatuses()
6090 if (m_updatedTrackerEntries
.isEmpty())
6093 for (auto it
= m_updatedTrackerEntries
.cbegin(); it
!= m_updatedTrackerEntries
.cend(); ++it
)
6095 updateTrackerEntries(it
.key(), it
.value());
6098 m_updatedTrackerEntries
.clear();
6101 void SessionImpl::saveStatistics() const
6103 if (!m_isStatisticsDirty
)
6106 const QVariantHash stats
{
6107 {u
"AlltimeDL"_s
, m_status
.allTimeDownload
},
6108 {u
"AlltimeUL"_s
, m_status
.allTimeUpload
}};
6109 std::unique_ptr
<QSettings
> settings
= Profile::instance()->applicationSettings(u
"qBittorrent-data"_s
);
6110 settings
->setValue(u
"Stats/AllStats"_s
, stats
);
6112 m_statisticsLastUpdateTimer
.start();
6113 m_isStatisticsDirty
= false;
6116 void SessionImpl::loadStatistics()
6118 const std::unique_ptr
<QSettings
> settings
= Profile::instance()->applicationSettings(u
"qBittorrent-data"_s
);
6119 const QVariantHash value
= settings
->value(u
"Stats/AllStats"_s
).toHash();
6121 m_previouslyDownloaded
= value
[u
"AlltimeDL"_s
].toLongLong();
6122 m_previouslyUploaded
= value
[u
"AlltimeUL"_s
].toLongLong();
6125 void SessionImpl::updateTrackerEntries(lt::torrent_handle torrentHandle
, QHash
<std::string
, QHash
<lt::tcp::endpoint
, QMap
<int, int>>> updatedTrackers
)
6127 invokeAsync([this, torrentHandle
= std::move(torrentHandle
), updatedTrackers
= std::move(updatedTrackers
)]() mutable
6131 std::vector
<lt::announce_entry
> nativeTrackers
= torrentHandle
.trackers();
6132 invoke([this, torrentHandle
, nativeTrackers
= std::move(nativeTrackers
)
6133 , updatedTrackers
= std::move(updatedTrackers
)]
6135 TorrentImpl
*torrent
= m_torrents
.value(torrentHandle
.info_hash());
6136 if (!torrent
|| torrent
->isPaused())
6139 QHash
<QString
, TrackerEntry
> updatedTrackerEntries
;
6140 updatedTrackerEntries
.reserve(updatedTrackers
.size());
6141 for (const lt::announce_entry
&announceEntry
: nativeTrackers
)
6143 const auto updatedTrackersIter
= updatedTrackers
.find(announceEntry
.url
);
6144 if (updatedTrackersIter
== updatedTrackers
.end())
6147 const auto &updateInfo
= updatedTrackersIter
.value();
6148 TrackerEntry trackerEntry
= torrent
->updateTrackerEntry(announceEntry
, updateInfo
);
6149 const QString url
= trackerEntry
.url
;
6150 updatedTrackerEntries
.emplace(url
, std::move(trackerEntry
));
6153 emit
trackerEntriesUpdated(torrent
, updatedTrackerEntries
);
6156 catch (const std::exception
&)