2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2015-2024 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.
36 #include <libtorrent/fwd.hpp>
37 #include <libtorrent/portmap.hpp>
38 #include <libtorrent/torrent_handle.hpp>
40 #include <QtContainerFwd>
42 #include <QElapsedTimer>
48 #include "base/path.h"
49 #include "base/settingvalue.h"
50 #include "base/types.h"
51 #include "base/utils/thread.h"
52 #include "addtorrentparams.h"
53 #include "cachestatus.h"
54 #include "categoryoptions.h"
56 #include "sessionstatus.h"
57 #include "torrentinfo.h"
58 #include "trackerentry.h"
60 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
61 class QNetworkConfiguration
;
62 class QNetworkConfigurationManager
;
70 class BandwidthScheduler
;
72 class FilterParserThread
;
73 class NativeSessionExtension
;
77 struct DownloadResult
;
84 class ResumeDataStorage
;
88 struct LoadTorrentParams
;
90 enum class MoveStorageMode
;
91 enum class MoveStorageContext
;
93 struct SessionMetricIndices
97 int hasIncomingConnections
= -1;
98 int sentPayloadBytes
= -1;
99 int recvPayloadBytes
= -1;
102 int sentIPOverheadBytes
= -1;
103 int recvIPOverheadBytes
= -1;
104 int sentTrackerBytes
= -1;
105 int recvTrackerBytes
= -1;
106 int recvRedundantBytes
= -1;
107 int recvFailedBytes
= -1;
112 int numPeersConnected
= -1;
113 int numPeersUpDisk
= -1;
114 int numPeersDownDisk
= -1;
120 int dhtBytesOut
= -1;
126 int diskBlocksInUse
= -1;
127 int numBlocksRead
= -1;
128 #ifndef QBT_USES_LIBTORRENT2
129 int numBlocksCacheHits
= -1;
134 int queuedDiskJobs
= -1;
135 int diskJobTime
= -1;
139 class SessionImpl final
: public Session
142 Q_DISABLE_COPY_MOVE(SessionImpl
)
145 Path
savePath() const override
;
146 void setSavePath(const Path
&path
) override
;
147 Path
downloadPath() const override
;
148 void setDownloadPath(const Path
&path
) override
;
149 bool isDownloadPathEnabled() const override
;
150 void setDownloadPathEnabled(bool enabled
) override
;
152 QStringList
categories() const override
;
153 CategoryOptions
categoryOptions(const QString
&categoryName
) const override
;
154 Path
categorySavePath(const QString
&categoryName
) const override
;
155 Path
categoryDownloadPath(const QString
&categoryName
) const override
;
156 bool addCategory(const QString
&name
, const CategoryOptions
&options
= {}) override
;
157 bool editCategory(const QString
&name
, const CategoryOptions
&options
) override
;
158 bool removeCategory(const QString
&name
) override
;
159 bool isSubcategoriesEnabled() const override
;
160 void setSubcategoriesEnabled(bool value
) override
;
161 bool useCategoryPathsInManualMode() const override
;
162 void setUseCategoryPathsInManualMode(bool value
) override
;
164 Path
suggestedSavePath(const QString
&categoryName
, std::optional
<bool> useAutoTMM
) const override
;
165 Path
suggestedDownloadPath(const QString
&categoryName
, std::optional
<bool> useAutoTMM
) const override
;
167 QSet
<QString
> tags() const override
;
168 bool hasTag(const QString
&tag
) const override
;
169 bool addTag(const QString
&tag
) override
;
170 bool removeTag(const QString
&tag
) override
;
172 bool isAutoTMMDisabledByDefault() const override
;
173 void setAutoTMMDisabledByDefault(bool value
) override
;
174 bool isDisableAutoTMMWhenCategoryChanged() const override
;
175 void setDisableAutoTMMWhenCategoryChanged(bool value
) override
;
176 bool isDisableAutoTMMWhenDefaultSavePathChanged() const override
;
177 void setDisableAutoTMMWhenDefaultSavePathChanged(bool value
) override
;
178 bool isDisableAutoTMMWhenCategorySavePathChanged() const override
;
179 void setDisableAutoTMMWhenCategorySavePathChanged(bool value
) override
;
181 qreal
globalMaxRatio() const override
;
182 void setGlobalMaxRatio(qreal ratio
) override
;
183 int globalMaxSeedingMinutes() const override
;
184 void setGlobalMaxSeedingMinutes(int minutes
) override
;
185 int globalMaxInactiveSeedingMinutes() const override
;
186 void setGlobalMaxInactiveSeedingMinutes(int minutes
) override
;
187 bool isDHTEnabled() const override
;
188 void setDHTEnabled(bool enabled
) override
;
189 bool isLSDEnabled() const override
;
190 void setLSDEnabled(bool enabled
) override
;
191 bool isPeXEnabled() const override
;
192 void setPeXEnabled(bool enabled
) override
;
193 bool isAddTorrentToQueueTop() const override
;
194 void setAddTorrentToQueueTop(bool value
) override
;
195 bool isAddTorrentPaused() const override
;
196 void setAddTorrentPaused(bool value
) override
;
197 Torrent::StopCondition
torrentStopCondition() const override
;
198 void setTorrentStopCondition(Torrent::StopCondition stopCondition
) override
;
199 TorrentContentLayout
torrentContentLayout() const override
;
200 void setTorrentContentLayout(TorrentContentLayout value
) override
;
201 bool isTrackerEnabled() const override
;
202 void setTrackerEnabled(bool enabled
) override
;
203 bool isAppendExtensionEnabled() const override
;
204 void setAppendExtensionEnabled(bool enabled
) override
;
205 int refreshInterval() const override
;
206 void setRefreshInterval(int value
) override
;
207 bool isPreallocationEnabled() const override
;
208 void setPreallocationEnabled(bool enabled
) override
;
209 Path
torrentExportDirectory() const override
;
210 void setTorrentExportDirectory(const Path
&path
) override
;
211 Path
finishedTorrentExportDirectory() const override
;
212 void setFinishedTorrentExportDirectory(const Path
&path
) override
;
214 int globalDownloadSpeedLimit() const override
;
215 void setGlobalDownloadSpeedLimit(int limit
) override
;
216 int globalUploadSpeedLimit() const override
;
217 void setGlobalUploadSpeedLimit(int limit
) override
;
218 int altGlobalDownloadSpeedLimit() const override
;
219 void setAltGlobalDownloadSpeedLimit(int limit
) override
;
220 int altGlobalUploadSpeedLimit() const override
;
221 void setAltGlobalUploadSpeedLimit(int limit
) override
;
222 int downloadSpeedLimit() const override
;
223 void setDownloadSpeedLimit(int limit
) override
;
224 int uploadSpeedLimit() const override
;
225 void setUploadSpeedLimit(int limit
) override
;
226 bool isAltGlobalSpeedLimitEnabled() const override
;
227 void setAltGlobalSpeedLimitEnabled(bool enabled
) override
;
228 bool isBandwidthSchedulerEnabled() const override
;
229 void setBandwidthSchedulerEnabled(bool enabled
) override
;
231 bool isPerformanceWarningEnabled() const override
;
232 void setPerformanceWarningEnabled(bool enable
) override
;
233 int saveResumeDataInterval() const override
;
234 void setSaveResumeDataInterval(int value
) override
;
235 int port() const override
;
236 void setPort(int port
) override
;
237 QString
networkInterface() const override
;
238 void setNetworkInterface(const QString
&iface
) override
;
239 QString
networkInterfaceName() const override
;
240 void setNetworkInterfaceName(const QString
&name
) override
;
241 QString
networkInterfaceAddress() const override
;
242 void setNetworkInterfaceAddress(const QString
&address
) override
;
243 int encryption() const override
;
244 void setEncryption(int state
) override
;
245 int maxActiveCheckingTorrents() const override
;
246 void setMaxActiveCheckingTorrents(int val
) override
;
247 bool isI2PEnabled() const override
;
248 void setI2PEnabled(bool enabled
) override
;
249 QString
I2PAddress() const override
;
250 void setI2PAddress(const QString
&address
) override
;
251 int I2PPort() const override
;
252 void setI2PPort(int port
) override
;
253 bool I2PMixedMode() const override
;
254 void setI2PMixedMode(bool enabled
) override
;
255 int I2PInboundQuantity() const override
;
256 void setI2PInboundQuantity(int value
) override
;
257 int I2POutboundQuantity() const override
;
258 void setI2POutboundQuantity(int value
) override
;
259 int I2PInboundLength() const override
;
260 void setI2PInboundLength(int value
) override
;
261 int I2POutboundLength() const override
;
262 void setI2POutboundLength(int value
) override
;
263 bool isProxyPeerConnectionsEnabled() const override
;
264 void setProxyPeerConnectionsEnabled(bool enabled
) override
;
265 ChokingAlgorithm
chokingAlgorithm() const override
;
266 void setChokingAlgorithm(ChokingAlgorithm mode
) override
;
267 SeedChokingAlgorithm
seedChokingAlgorithm() const override
;
268 void setSeedChokingAlgorithm(SeedChokingAlgorithm mode
) override
;
269 bool isAddTrackersEnabled() const override
;
270 void setAddTrackersEnabled(bool enabled
) override
;
271 QString
additionalTrackers() const override
;
272 void setAdditionalTrackers(const QString
&trackers
) override
;
273 bool isIPFilteringEnabled() const override
;
274 void setIPFilteringEnabled(bool enabled
) override
;
275 Path
IPFilterFile() const override
;
276 void setIPFilterFile(const Path
&path
) override
;
277 bool announceToAllTrackers() const override
;
278 void setAnnounceToAllTrackers(bool val
) override
;
279 bool announceToAllTiers() const override
;
280 void setAnnounceToAllTiers(bool val
) override
;
281 int peerTurnover() const override
;
282 void setPeerTurnover(int val
) override
;
283 int peerTurnoverCutoff() const override
;
284 void setPeerTurnoverCutoff(int val
) override
;
285 int peerTurnoverInterval() const override
;
286 void setPeerTurnoverInterval(int val
) override
;
287 int requestQueueSize() const override
;
288 void setRequestQueueSize(int val
) override
;
289 int asyncIOThreads() const override
;
290 void setAsyncIOThreads(int num
) override
;
291 int hashingThreads() const override
;
292 void setHashingThreads(int num
) override
;
293 int filePoolSize() const override
;
294 void setFilePoolSize(int size
) override
;
295 int checkingMemUsage() const override
;
296 void setCheckingMemUsage(int size
) override
;
297 int diskCacheSize() const override
;
298 void setDiskCacheSize(int size
) override
;
299 int diskCacheTTL() const override
;
300 void setDiskCacheTTL(int ttl
) override
;
301 qint64
diskQueueSize() const override
;
302 void setDiskQueueSize(qint64 size
) override
;
303 DiskIOType
diskIOType() const override
;
304 void setDiskIOType(DiskIOType type
) override
;
305 DiskIOReadMode
diskIOReadMode() const override
;
306 void setDiskIOReadMode(DiskIOReadMode mode
) override
;
307 DiskIOWriteMode
diskIOWriteMode() const override
;
308 void setDiskIOWriteMode(DiskIOWriteMode mode
) override
;
309 bool isCoalesceReadWriteEnabled() const override
;
310 void setCoalesceReadWriteEnabled(bool enabled
) override
;
311 bool usePieceExtentAffinity() const override
;
312 void setPieceExtentAffinity(bool enabled
) override
;
313 bool isSuggestModeEnabled() const override
;
314 void setSuggestMode(bool mode
) override
;
315 int sendBufferWatermark() const override
;
316 void setSendBufferWatermark(int value
) override
;
317 int sendBufferLowWatermark() const override
;
318 void setSendBufferLowWatermark(int value
) override
;
319 int sendBufferWatermarkFactor() const override
;
320 void setSendBufferWatermarkFactor(int value
) override
;
321 int connectionSpeed() const override
;
322 void setConnectionSpeed(int value
) override
;
323 int socketSendBufferSize() const override
;
324 void setSocketSendBufferSize(int value
) override
;
325 int socketReceiveBufferSize() const override
;
326 void setSocketReceiveBufferSize(int value
) override
;
327 int socketBacklogSize() const override
;
328 void setSocketBacklogSize(int value
) override
;
329 bool isAnonymousModeEnabled() const override
;
330 void setAnonymousModeEnabled(bool enabled
) override
;
331 bool isQueueingSystemEnabled() const override
;
332 void setQueueingSystemEnabled(bool enabled
) override
;
333 bool ignoreSlowTorrentsForQueueing() const override
;
334 void setIgnoreSlowTorrentsForQueueing(bool ignore
) override
;
335 int downloadRateForSlowTorrents() const override
;
336 void setDownloadRateForSlowTorrents(int rateInKibiBytes
) override
;
337 int uploadRateForSlowTorrents() const override
;
338 void setUploadRateForSlowTorrents(int rateInKibiBytes
) override
;
339 int slowTorrentsInactivityTimer() const override
;
340 void setSlowTorrentsInactivityTimer(int timeInSeconds
) override
;
341 int outgoingPortsMin() const override
;
342 void setOutgoingPortsMin(int min
) override
;
343 int outgoingPortsMax() const override
;
344 void setOutgoingPortsMax(int max
) override
;
345 int UPnPLeaseDuration() const override
;
346 void setUPnPLeaseDuration(int duration
) override
;
347 int peerToS() const override
;
348 void setPeerToS(int value
) override
;
349 bool ignoreLimitsOnLAN() const override
;
350 void setIgnoreLimitsOnLAN(bool ignore
) override
;
351 bool includeOverheadInLimits() const override
;
352 void setIncludeOverheadInLimits(bool include
) override
;
353 QString
announceIP() const override
;
354 void setAnnounceIP(const QString
&ip
) override
;
355 int maxConcurrentHTTPAnnounces() const override
;
356 void setMaxConcurrentHTTPAnnounces(int value
) override
;
357 bool isReannounceWhenAddressChangedEnabled() const override
;
358 void setReannounceWhenAddressChangedEnabled(bool enabled
) override
;
359 void reannounceToAllTrackers() const override
;
360 int stopTrackerTimeout() const override
;
361 void setStopTrackerTimeout(int value
) override
;
362 int maxConnections() const override
;
363 void setMaxConnections(int max
) override
;
364 int maxConnectionsPerTorrent() const override
;
365 void setMaxConnectionsPerTorrent(int max
) override
;
366 int maxUploads() const override
;
367 void setMaxUploads(int max
) override
;
368 int maxUploadsPerTorrent() const override
;
369 void setMaxUploadsPerTorrent(int max
) override
;
370 int maxActiveDownloads() const override
;
371 void setMaxActiveDownloads(int max
) override
;
372 int maxActiveUploads() const override
;
373 void setMaxActiveUploads(int max
) override
;
374 int maxActiveTorrents() const override
;
375 void setMaxActiveTorrents(int max
) override
;
376 BTProtocol
btProtocol() const override
;
377 void setBTProtocol(BTProtocol protocol
) override
;
378 bool isUTPRateLimited() const override
;
379 void setUTPRateLimited(bool limited
) override
;
380 MixedModeAlgorithm
utpMixedMode() const override
;
381 void setUtpMixedMode(MixedModeAlgorithm mode
) override
;
382 bool isIDNSupportEnabled() const override
;
383 void setIDNSupportEnabled(bool enabled
) override
;
384 bool multiConnectionsPerIpEnabled() const override
;
385 void setMultiConnectionsPerIpEnabled(bool enabled
) override
;
386 bool validateHTTPSTrackerCertificate() const override
;
387 void setValidateHTTPSTrackerCertificate(bool enabled
) override
;
388 bool isSSRFMitigationEnabled() const override
;
389 void setSSRFMitigationEnabled(bool enabled
) override
;
390 bool blockPeersOnPrivilegedPorts() const override
;
391 void setBlockPeersOnPrivilegedPorts(bool enabled
) override
;
392 bool isTrackerFilteringEnabled() const override
;
393 void setTrackerFilteringEnabled(bool enabled
) override
;
394 bool isExcludedFileNamesEnabled() const override
;
395 void setExcludedFileNamesEnabled(bool enabled
) override
;
396 QStringList
excludedFileNames() const override
;
397 void setExcludedFileNames(const QStringList
&excludedFileNames
) override
;
398 bool isFilenameExcluded(const QString
&fileName
) const override
;
399 QStringList
bannedIPs() const override
;
400 void setBannedIPs(const QStringList
&newList
) override
;
401 ResumeDataStorageType
resumeDataStorageType() const override
;
402 void setResumeDataStorageType(ResumeDataStorageType type
) override
;
403 bool isMergeTrackersEnabled() const override
;
404 void setMergeTrackersEnabled(bool enabled
) override
;
406 bool isRestored() const override
;
408 Torrent
*getTorrent(const TorrentID
&id
) const override
;
409 Torrent
*findTorrent(const InfoHash
&infoHash
) const override
;
410 QVector
<Torrent
*> torrents() const override
;
411 qsizetype
torrentsCount() const override
;
412 const SessionStatus
&status() const override
;
413 const CacheStatus
&cacheStatus() const override
;
414 bool isListening() const override
;
416 MaxRatioAction
maxRatioAction() const override
;
417 void setMaxRatioAction(MaxRatioAction act
) override
;
419 void banIP(const QString
&ip
) override
;
421 bool isKnownTorrent(const InfoHash
&infoHash
) const override
;
422 bool addTorrent(const QString
&source
, const AddTorrentParams
¶ms
= {}) override
;
423 bool addTorrent(const MagnetUri
&magnetUri
, const AddTorrentParams
¶ms
= {}) override
;
424 bool addTorrent(const TorrentInfo
&torrentInfo
, const AddTorrentParams
¶ms
= {}) override
;
425 bool deleteTorrent(const TorrentID
&id
, DeleteOption deleteOption
= DeleteTorrent
) override
;
426 bool downloadMetadata(const MagnetUri
&magnetUri
) override
;
427 bool cancelDownloadMetadata(const TorrentID
&id
) override
;
429 void recursiveTorrentDownload(const TorrentID
&id
) override
;
430 void increaseTorrentsQueuePos(const QVector
<TorrentID
> &ids
) override
;
431 void decreaseTorrentsQueuePos(const QVector
<TorrentID
> &ids
) override
;
432 void topTorrentsQueuePos(const QVector
<TorrentID
> &ids
) override
;
433 void bottomTorrentsQueuePos(const QVector
<TorrentID
> &ids
) override
;
436 void handleTorrentNeedSaveResumeData(const TorrentImpl
*torrent
);
437 void handleTorrentSaveResumeDataRequested(const TorrentImpl
*torrent
);
438 void handleTorrentSaveResumeDataFailed(const TorrentImpl
*torrent
);
439 void handleTorrentShareLimitChanged(TorrentImpl
*torrent
);
440 void handleTorrentNameChanged(TorrentImpl
*torrent
);
441 void handleTorrentSavePathChanged(TorrentImpl
*torrent
);
442 void handleTorrentCategoryChanged(TorrentImpl
*torrent
, const QString
&oldCategory
);
443 void handleTorrentTagAdded(TorrentImpl
*torrent
, const QString
&tag
);
444 void handleTorrentTagRemoved(TorrentImpl
*torrent
, const QString
&tag
);
445 void handleTorrentSavingModeChanged(TorrentImpl
*torrent
);
446 void handleTorrentMetadataReceived(TorrentImpl
*torrent
);
447 void handleTorrentPaused(TorrentImpl
*torrent
);
448 void handleTorrentResumed(TorrentImpl
*torrent
);
449 void handleTorrentChecked(TorrentImpl
*torrent
);
450 void handleTorrentFinished(TorrentImpl
*torrent
);
451 void handleTorrentTrackersAdded(TorrentImpl
*torrent
, const QVector
<TrackerEntry
> &newTrackers
);
452 void handleTorrentTrackersRemoved(TorrentImpl
*torrent
, const QStringList
&deletedTrackers
);
453 void handleTorrentTrackersChanged(TorrentImpl
*torrent
);
454 void handleTorrentUrlSeedsAdded(TorrentImpl
*torrent
, const QVector
<QUrl
> &newUrlSeeds
);
455 void handleTorrentUrlSeedsRemoved(TorrentImpl
*torrent
, const QVector
<QUrl
> &urlSeeds
);
456 void handleTorrentResumeDataReady(TorrentImpl
*torrent
, const LoadTorrentParams
&data
);
457 void handleTorrentInfoHashChanged(TorrentImpl
*torrent
, const InfoHash
&prevInfoHash
);
458 void handleTorrentStorageMovingStateChanged(TorrentImpl
*torrent
);
460 bool addMoveTorrentStorageJob(TorrentImpl
*torrent
, const Path
&newPath
, MoveStorageMode mode
, MoveStorageContext context
);
462 void findIncompleteFiles(const TorrentInfo
&torrentInfo
, const Path
&savePath
463 , const Path
&downloadPath
, const PathList
&filePaths
= {}) const;
465 void enablePortMapping();
466 void disablePortMapping();
467 void addMappedPorts(const QSet
<quint16
> &ports
);
468 void removeMappedPorts(const QSet
<quint16
> &ports
);
470 template <typename Func
>
471 void invoke(Func
&&func
)
473 QMetaObject::invokeMethod(this, std::forward
<Func
>(func
), Qt::QueuedConnection
);
476 void invokeAsync(std::function
<void ()> func
);
479 void addTorrentAlertsReceived(qsizetype count
);
482 void configureDeferred();
484 void enqueueRefresh();
485 void processShareLimits();
486 void generateResumeData();
487 void handleIPFilterParsed(int ruleCount
);
488 void handleIPFilterError();
489 void handleDownloadFinished(const Net::DownloadResult
&result
);
490 void fileSearchFinished(const TorrentID
&id
, const Path
&savePath
, const PathList
&fileNames
);
492 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
493 // Session reconfiguration triggers
494 void networkOnlineStateChanged(bool online
);
495 void networkConfigurationChange(const QNetworkConfiguration
&);
499 struct ResumeSessionContext
;
501 struct MoveStorageJob
503 lt::torrent_handle torrentHandle
;
505 MoveStorageMode mode
{};
506 MoveStorageContext context
{};
509 struct RemovingTorrentData
513 DeleteOption deleteOption
{};
516 explicit SessionImpl(QObject
*parent
= nullptr);
519 bool hasPerTorrentRatioLimit() const;
520 bool hasPerTorrentSeedingTimeLimit() const;
521 bool hasPerTorrentInactiveSeedingTimeLimit() const;
523 // Session configuration
524 Q_INVOKABLE
void configure();
525 void configureComponents();
526 void initializeNativeSession();
527 lt::settings_pack
loadLTSettings() const;
528 void applyNetworkInterfacesSettings(lt::settings_pack
&settingsPack
) const;
529 void configurePeerClasses();
531 void applyBandwidthLimits();
532 void processBannedIPs(lt::ip_filter
&filter
);
533 QStringList
getListeningIPs() const;
534 void configureListeningInterface();
535 void enableTracker(bool enable
);
536 void enableBandwidthScheduler();
537 void populateAdditionalTrackers();
538 void enableIPFilter();
539 void disableIPFilter();
540 void processTrackerStatuses();
541 void populateExcludedFileNamesRegExpList();
542 void prepareStartup();
543 void handleLoadedResumeData(ResumeSessionContext
*context
);
544 void processNextResumeData(ResumeSessionContext
*context
);
545 void endStartup(ResumeSessionContext
*context
);
547 LoadTorrentParams
initLoadTorrentParams(const AddTorrentParams
&addTorrentParams
);
548 bool addTorrent_impl(const std::variant
<MagnetUri
, TorrentInfo
> &source
, const AddTorrentParams
&addTorrentParams
);
550 void updateSeedingLimitTimer();
551 void exportTorrentFile(const Torrent
*torrent
, const Path
&folderPath
);
553 void handleAlert(const lt::alert
*a
);
554 void handleAddTorrentAlerts(const std::vector
<lt::alert
*> &alerts
);
555 void dispatchTorrentAlert(const lt::torrent_alert
*a
);
556 void handleStateUpdateAlert(const lt::state_update_alert
*p
);
557 void handleMetadataReceivedAlert(const lt::metadata_received_alert
*p
);
558 void handleFileErrorAlert(const lt::file_error_alert
*p
);
559 void handleTorrentRemovedAlert(const lt::torrent_removed_alert
*p
);
560 void handleTorrentDeletedAlert(const lt::torrent_deleted_alert
*p
);
561 void handleTorrentDeleteFailedAlert(const lt::torrent_delete_failed_alert
*p
);
562 void handlePortmapWarningAlert(const lt::portmap_error_alert
*p
);
563 void handlePortmapAlert(const lt::portmap_alert
*p
);
564 void handlePeerBlockedAlert(const lt::peer_blocked_alert
*p
);
565 void handlePeerBanAlert(const lt::peer_ban_alert
*p
);
566 void handleUrlSeedAlert(const lt::url_seed_alert
*p
);
567 void handleListenSucceededAlert(const lt::listen_succeeded_alert
*p
);
568 void handleListenFailedAlert(const lt::listen_failed_alert
*p
);
569 void handleExternalIPAlert(const lt::external_ip_alert
*p
);
570 void handleSessionErrorAlert(const lt::session_error_alert
*p
) const;
571 void handleSessionStatsAlert(const lt::session_stats_alert
*p
);
572 void handleAlertsDroppedAlert(const lt::alerts_dropped_alert
*p
) const;
573 void handleStorageMovedAlert(const lt::storage_moved_alert
*p
);
574 void handleStorageMovedFailedAlert(const lt::storage_moved_failed_alert
*p
);
575 void handleSocks5Alert(const lt::socks5_alert
*p
) const;
576 void handleI2PAlert(const lt::i2p_alert
*p
) const;
577 void handleTrackerAlert(const lt::tracker_alert
*a
);
578 #ifdef QBT_USES_LIBTORRENT2
579 void handleTorrentConflictAlert(const lt::torrent_conflict_alert
*a
);
582 TorrentImpl
*createTorrent(const lt::torrent_handle
&nativeHandle
, const LoadTorrentParams
¶ms
);
584 void saveResumeData();
585 void saveTorrentsQueue();
586 void removeTorrentsQueue();
588 std::vector
<lt::alert
*> getPendingAlerts(lt::time_duration time
= lt::time_duration::zero()) const;
590 void moveTorrentStorage(const MoveStorageJob
&job
) const;
591 void handleMoveTorrentStorageJobFinished(const Path
&newPath
);
593 void loadCategories();
594 void storeCategories() const;
595 void upgradeCategories();
597 void saveStatistics() const;
598 void loadStatistics();
601 lt::session
*m_nativeSession
= nullptr;
602 NativeSessionExtension
*m_nativeSessionExtension
= nullptr;
604 bool m_deferredConfigureScheduled
= false;
605 bool m_IPFilteringConfigured
= false;
606 mutable bool m_listenInterfaceConfigured
= false;
608 CachedSettingValue
<bool> m_isDHTEnabled
;
609 CachedSettingValue
<bool> m_isLSDEnabled
;
610 CachedSettingValue
<bool> m_isPeXEnabled
;
611 CachedSettingValue
<bool> m_isIPFilteringEnabled
;
612 CachedSettingValue
<bool> m_isTrackerFilteringEnabled
;
613 CachedSettingValue
<Path
> m_IPFilterFile
;
614 CachedSettingValue
<bool> m_announceToAllTrackers
;
615 CachedSettingValue
<bool> m_announceToAllTiers
;
616 CachedSettingValue
<int> m_asyncIOThreads
;
617 CachedSettingValue
<int> m_hashingThreads
;
618 CachedSettingValue
<int> m_filePoolSize
;
619 CachedSettingValue
<int> m_checkingMemUsage
;
620 CachedSettingValue
<int> m_diskCacheSize
;
621 CachedSettingValue
<int> m_diskCacheTTL
;
622 CachedSettingValue
<qint64
> m_diskQueueSize
;
623 CachedSettingValue
<DiskIOType
> m_diskIOType
;
624 CachedSettingValue
<DiskIOReadMode
> m_diskIOReadMode
;
625 CachedSettingValue
<DiskIOWriteMode
> m_diskIOWriteMode
;
626 CachedSettingValue
<bool> m_coalesceReadWriteEnabled
;
627 CachedSettingValue
<bool> m_usePieceExtentAffinity
;
628 CachedSettingValue
<bool> m_isSuggestMode
;
629 CachedSettingValue
<int> m_sendBufferWatermark
;
630 CachedSettingValue
<int> m_sendBufferLowWatermark
;
631 CachedSettingValue
<int> m_sendBufferWatermarkFactor
;
632 CachedSettingValue
<int> m_connectionSpeed
;
633 CachedSettingValue
<int> m_socketSendBufferSize
;
634 CachedSettingValue
<int> m_socketReceiveBufferSize
;
635 CachedSettingValue
<int> m_socketBacklogSize
;
636 CachedSettingValue
<bool> m_isAnonymousModeEnabled
;
637 CachedSettingValue
<bool> m_isQueueingEnabled
;
638 CachedSettingValue
<int> m_maxActiveDownloads
;
639 CachedSettingValue
<int> m_maxActiveUploads
;
640 CachedSettingValue
<int> m_maxActiveTorrents
;
641 CachedSettingValue
<bool> m_ignoreSlowTorrentsForQueueing
;
642 CachedSettingValue
<int> m_downloadRateForSlowTorrents
;
643 CachedSettingValue
<int> m_uploadRateForSlowTorrents
;
644 CachedSettingValue
<int> m_slowTorrentsInactivityTimer
;
645 CachedSettingValue
<int> m_outgoingPortsMin
;
646 CachedSettingValue
<int> m_outgoingPortsMax
;
647 CachedSettingValue
<int> m_UPnPLeaseDuration
;
648 CachedSettingValue
<int> m_peerToS
;
649 CachedSettingValue
<bool> m_ignoreLimitsOnLAN
;
650 CachedSettingValue
<bool> m_includeOverheadInLimits
;
651 CachedSettingValue
<QString
> m_announceIP
;
652 CachedSettingValue
<int> m_maxConcurrentHTTPAnnounces
;
653 CachedSettingValue
<bool> m_isReannounceWhenAddressChangedEnabled
;
654 CachedSettingValue
<int> m_stopTrackerTimeout
;
655 CachedSettingValue
<int> m_maxConnections
;
656 CachedSettingValue
<int> m_maxUploads
;
657 CachedSettingValue
<int> m_maxConnectionsPerTorrent
;
658 CachedSettingValue
<int> m_maxUploadsPerTorrent
;
659 CachedSettingValue
<BTProtocol
> m_btProtocol
;
660 CachedSettingValue
<bool> m_isUTPRateLimited
;
661 CachedSettingValue
<MixedModeAlgorithm
> m_utpMixedMode
;
662 CachedSettingValue
<bool> m_IDNSupportEnabled
;
663 CachedSettingValue
<bool> m_multiConnectionsPerIpEnabled
;
664 CachedSettingValue
<bool> m_validateHTTPSTrackerCertificate
;
665 CachedSettingValue
<bool> m_SSRFMitigationEnabled
;
666 CachedSettingValue
<bool> m_blockPeersOnPrivilegedPorts
;
667 CachedSettingValue
<bool> m_isAddTrackersEnabled
;
668 CachedSettingValue
<QString
> m_additionalTrackers
;
669 CachedSettingValue
<qreal
> m_globalMaxRatio
;
670 CachedSettingValue
<int> m_globalMaxSeedingMinutes
;
671 CachedSettingValue
<int> m_globalMaxInactiveSeedingMinutes
;
672 CachedSettingValue
<bool> m_isAddTorrentToQueueTop
;
673 CachedSettingValue
<bool> m_isAddTorrentPaused
;
674 CachedSettingValue
<Torrent::StopCondition
> m_torrentStopCondition
;
675 CachedSettingValue
<TorrentContentLayout
> m_torrentContentLayout
;
676 CachedSettingValue
<bool> m_isAppendExtensionEnabled
;
677 CachedSettingValue
<int> m_refreshInterval
;
678 CachedSettingValue
<bool> m_isPreallocationEnabled
;
679 CachedSettingValue
<Path
> m_torrentExportDirectory
;
680 CachedSettingValue
<Path
> m_finishedTorrentExportDirectory
;
681 CachedSettingValue
<int> m_globalDownloadSpeedLimit
;
682 CachedSettingValue
<int> m_globalUploadSpeedLimit
;
683 CachedSettingValue
<int> m_altGlobalDownloadSpeedLimit
;
684 CachedSettingValue
<int> m_altGlobalUploadSpeedLimit
;
685 CachedSettingValue
<bool> m_isAltGlobalSpeedLimitEnabled
;
686 CachedSettingValue
<bool> m_isBandwidthSchedulerEnabled
;
687 CachedSettingValue
<bool> m_isPerformanceWarningEnabled
;
688 CachedSettingValue
<int> m_saveResumeDataInterval
;
689 CachedSettingValue
<int> m_port
;
690 CachedSettingValue
<QString
> m_networkInterface
;
691 CachedSettingValue
<QString
> m_networkInterfaceName
;
692 CachedSettingValue
<QString
> m_networkInterfaceAddress
;
693 CachedSettingValue
<int> m_encryption
;
694 CachedSettingValue
<int> m_maxActiveCheckingTorrents
;
695 CachedSettingValue
<bool> m_isProxyPeerConnectionsEnabled
;
696 CachedSettingValue
<ChokingAlgorithm
> m_chokingAlgorithm
;
697 CachedSettingValue
<SeedChokingAlgorithm
> m_seedChokingAlgorithm
;
698 CachedSettingValue
<QStringList
> m_storedTags
;
699 CachedSettingValue
<int> m_maxRatioAction
;
700 CachedSettingValue
<Path
> m_savePath
;
701 CachedSettingValue
<Path
> m_downloadPath
;
702 CachedSettingValue
<bool> m_isDownloadPathEnabled
;
703 CachedSettingValue
<bool> m_isSubcategoriesEnabled
;
704 CachedSettingValue
<bool> m_useCategoryPathsInManualMode
;
705 CachedSettingValue
<bool> m_isAutoTMMDisabledByDefault
;
706 CachedSettingValue
<bool> m_isDisableAutoTMMWhenCategoryChanged
;
707 CachedSettingValue
<bool> m_isDisableAutoTMMWhenDefaultSavePathChanged
;
708 CachedSettingValue
<bool> m_isDisableAutoTMMWhenCategorySavePathChanged
;
709 CachedSettingValue
<bool> m_isTrackerEnabled
;
710 CachedSettingValue
<int> m_peerTurnover
;
711 CachedSettingValue
<int> m_peerTurnoverCutoff
;
712 CachedSettingValue
<int> m_peerTurnoverInterval
;
713 CachedSettingValue
<int> m_requestQueueSize
;
714 CachedSettingValue
<bool> m_isExcludedFileNamesEnabled
;
715 CachedSettingValue
<QStringList
> m_excludedFileNames
;
716 CachedSettingValue
<QStringList
> m_bannedIPs
;
717 CachedSettingValue
<ResumeDataStorageType
> m_resumeDataStorageType
;
718 CachedSettingValue
<bool> m_isMergeTrackersEnabled
;
719 CachedSettingValue
<bool> m_isI2PEnabled
;
720 CachedSettingValue
<QString
> m_I2PAddress
;
721 CachedSettingValue
<int> m_I2PPort
;
722 CachedSettingValue
<bool> m_I2PMixedMode
;
723 CachedSettingValue
<int> m_I2PInboundQuantity
;
724 CachedSettingValue
<int> m_I2POutboundQuantity
;
725 CachedSettingValue
<int> m_I2PInboundLength
;
726 CachedSettingValue
<int> m_I2POutboundLength
;
728 bool m_isRestored
= false;
730 // Order is important. This needs to be declared after its CachedSettingsValue
731 // counterpart, because it uses it for initialization in the constructor
732 // initialization list.
733 const bool m_wasPexEnabled
= m_isPeXEnabled
;
735 int m_numResumeData
= 0;
736 QVector
<TrackerEntry
> m_additionalTrackerList
;
737 QVector
<QRegularExpression
> m_excludedFileNamesRegExpList
;
740 mutable QElapsedTimer m_statisticsLastUpdateTimer
;
741 mutable bool m_isStatisticsDirty
= false;
742 qint64 m_previouslyUploaded
= 0;
743 qint64 m_previouslyDownloaded
= 0;
745 bool m_torrentsQueueChanged
= false;
746 bool m_needSaveTorrentsQueue
= false;
747 bool m_refreshEnqueued
= false;
748 QTimer
*m_seedingLimitTimer
= nullptr;
749 QTimer
*m_resumeDataTimer
= nullptr;
751 QPointer
<FilterParserThread
> m_filterParser
;
752 QPointer
<BandwidthScheduler
> m_bwScheduler
;
754 QPointer
<Tracker
> m_tracker
;
756 Utils::Thread::UniquePtr m_ioThread
;
757 QThreadPool
*m_asyncWorker
= nullptr;
758 ResumeDataStorage
*m_resumeDataStorage
= nullptr;
759 FileSearcher
*m_fileSearcher
= nullptr;
761 QHash
<TorrentID
, lt::torrent_handle
> m_downloadedMetadata
;
763 QHash
<TorrentID
, TorrentImpl
*> m_torrents
;
764 QHash
<TorrentID
, TorrentImpl
*> m_hybridTorrentsByAltID
;
765 QHash
<TorrentID
, LoadTorrentParams
> m_loadingTorrents
;
766 QHash
<QString
, AddTorrentParams
> m_downloadedTorrents
;
767 QHash
<TorrentID
, RemovingTorrentData
> m_removingTorrents
;
768 QSet
<TorrentID
> m_needSaveResumeDataTorrents
;
769 QHash
<TorrentID
, TorrentID
> m_changedTorrentIDs
;
770 QMap
<QString
, CategoryOptions
> m_categories
;
771 QSet
<QString
> m_tags
;
773 // This field holds amounts of peers reported by trackers in their responses to announces
774 // (torrent.tracker_name.tracker_local_endpoint.num_peers)
775 QHash
<lt::torrent_handle
, QHash
<std::string
, QMap
<TrackerEntry::Endpoint
, int>>> m_updatedTrackerEntries
;
777 // I/O errored torrents
778 QSet
<TorrentID
> m_recentErroredTorrents
;
779 QTimer
*m_recentErroredTorrentsTimer
= nullptr;
781 SessionMetricIndices m_metricIndices
;
782 lt::time_point m_statsLastTimestamp
= lt::clock_type::now();
784 SessionStatus m_status
;
785 CacheStatus m_cacheStatus
;
786 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
787 QNetworkConfigurationManager
*m_networkManager
= nullptr;
790 QList
<MoveStorageJob
> m_moveStorageQueue
;
792 QString m_lastExternalIP
;
794 bool m_needUpgradeDownloadPath
= false;
796 // All port mapping related routines are invoked from working thread
797 // so there are no synchronization used. If multithreaded access is
798 // ever required, synchronization should also be provided.
799 bool m_isPortMappingEnabled
= false;
800 QHash
<quint16
, std::vector
<lt::port_mapping_t
>> m_mappedPorts
;
802 QTimer
*m_wakeupCheckTimer
= nullptr;
803 QDateTime m_wakeupCheckTimestamp
;
805 friend void Session::initInstance();
806 friend void Session::freeInstance();
807 friend Session
*Session::instance();
808 static Session
*m_instance
;