KanoopTorrentQt 0.1.0
Qt6 wrapper library for libtorrent-rasterbar
Loading...
Searching...
No Matches
torrentclient.h
1#ifndef TORRENTCLIENT_H
2#define TORRENTCLIENT_H
3
4#include <Kanoop/torrent/kanooptorrent.h>
5#include <Kanoop/torrent/torrent.h>
6#include <Kanoop/torrent/sessionstats.h>
7#include <Kanoop/utility/loggingbaseclass.h>
8#include <QHostAddress>
9#include <QNetworkProxy>
10#include <QObject>
11
12class QTimer;
13
14/**
15 * @brief Qt wrapper around a libtorrent session.
16 *
17 * TorrentClient owns the underlying libtorrent session and manages the
18 * lifecycle of all Torrent objects added to it. It polls for libtorrent
19 * alerts on a 500 ms timer and translates them into Qt signals.
20 *
21 * Typical usage:
22 * @code
23 * auto* client = new TorrentClient(this);
24 * client->setDefaultDownloadDirectory("/tmp/downloads");
25 * client->setResumeDataDirectory("/tmp/resume");
26 * client->loadResumeData();
27 *
28 * Torrent* t = client->addTorrent(MagnetLink(uri));
29 * connect(t, &Torrent::downloadComplete, this, &MyApp::onDone);
30 * @endcode
31 */
32class LIBKANOOPTORRENT_EXPORT TorrentClient : public QObject,
33 public LoggingBaseClass
34{
35 Q_OBJECT
36public:
37 /**
38 * @brief Peer connection encryption policy.
39 */
41 EncryptionEnabled, ///< Prefer encrypted connections, allow plaintext fallback.
42 EncryptionForced, ///< Require encryption; reject plaintext peers.
43 EncryptionDisabled, ///< Disable encryption entirely.
44 };
45 Q_ENUM(EncryptionMode)
46
47 /**
48 * @brief Construct a TorrentClient with DHT and LSD enabled by default.
49 * @param parent Optional QObject parent.
50 */
51 explicit TorrentClient(QObject* parent = nullptr);
53
54 // ── Torrent management ──────────────────────────────────────────────
55
56 /**
57 * @brief Add a torrent from a magnet link.
58 * @param magnetLink The parsed magnet link.
59 * @param downloadDirectory Save path override. Uses defaultDownloadDirectory() if empty.
60 * @return Pointer to the new Torrent, or @c nullptr if a duplicate or invalid.
61 */
62 Torrent* addTorrent(const MagnetLink& magnetLink, const QString& downloadDirectory = QString());
63
64 /**
65 * @brief Add a torrent from a @c .torrent file on disk.
66 * @param torrentFilePath Path to the @c .torrent file.
67 * @param downloadDirectory Save path override. Uses defaultDownloadDirectory() if empty.
68 * @return Pointer to the new Torrent, or @c nullptr on failure.
69 */
70 Torrent* addTorrent(const QString& torrentFilePath, const QString& downloadDirectory = QString());
71
72 /**
73 * @brief Add a torrent from raw @c .torrent file bytes.
74 * @param torrentData The bencoded torrent data.
75 * @param downloadDirectory Save path override. Uses defaultDownloadDirectory() if empty.
76 * @return Pointer to the new Torrent, or @c nullptr on failure.
77 */
78 Torrent* addTorrent(const QByteArray& torrentData, const QString& downloadDirectory = QString());
79
80 /**
81 * @brief Remove a torrent from the session.
82 * @param torrent The torrent to remove. Becomes invalid after this call.
83 * @param deleteFiles If @c true, also delete downloaded data from disk.
84 */
85 void removeTorrent(Torrent* torrent, bool deleteFiles = false);
86
87 // ── Query ───────────────────────────────────────────────────────────
88
89 /** @brief Return all managed torrents. */
90 QList<Torrent*> torrents() const { return _torrents; }
91
92 /**
93 * @brief Find a torrent by its 20-byte SHA-1 info hash.
94 * @param infoHash Raw 20-byte hash.
95 * @return Matching Torrent pointer, or @c nullptr if not found.
96 */
97 Torrent* findTorrent(const QByteArray& infoHash) const;
98
99 // ── Default download directory ──────────────────────────────────────
100
101 /** @brief Default save path for new torrents when no override is given. */
102 QString defaultDownloadDirectory() const { return _defaultDownloadDirectory; }
103 /** @brief Set the default save path for new torrents. */
104 void setDefaultDownloadDirectory(const QString& value) { _defaultDownloadDirectory = value; }
105
106 // ── SOCKS5 proxy ────────────────────────────────────────────────────
107
108 /** @brief Current SOCKS5 proxy configuration. */
109 QNetworkProxy networkProxy() const { return _networkProxy; }
110
111 /**
112 * @brief Route all libtorrent traffic through a SOCKS5 proxy.
113 *
114 * Supports both plain SOCKS5 and SOCKS5 with username/password
115 * authentication. Pass a default-constructed QNetworkProxy to disable.
116 * @param value The proxy configuration.
117 */
118 void setNetworkProxy(const QNetworkProxy& value);
119
120 // ── Bandwidth limits ────────────────────────────────────────────────
121
122 /** @brief Global download rate limit in bytes/sec. 0 means unlimited. */
123 int downloadRateLimit() const;
124 /** @brief Set the global download rate limit in bytes/sec. Pass 0 for unlimited. */
125 void setDownloadRateLimit(int bytesPerSecond);
126
127 /** @brief Global upload rate limit in bytes/sec. 0 means unlimited. */
128 int uploadRateLimit() const;
129 /** @brief Set the global upload rate limit in bytes/sec. Pass 0 for unlimited. */
130 void setUploadRateLimit(int bytesPerSecond);
131
132 // ── Connection limits ───────────────────────────────────────────────
133
134 /** @brief Maximum number of simultaneous connections across all torrents. 0 = unlimited. */
135 int maxConnections() const;
136 /** @brief Set the global connection limit. */
137 void setMaxConnections(int value);
138
139 /** @brief Maximum number of unchoke slots (upload slots). 0 = unlimited. */
140 int maxUploads() const;
141 /** @brief Set the maximum number of unchoke slots. */
142 void setMaxUploads(int value);
143
144 // ── Listen interface ────────────────────────────────────────────────
145
146 /**
147 * @brief Current listen interface string.
148 *
149 * Format: @c "address:port" pairs, comma-separated.
150 * Example: @c "0.0.0.0:6881,[::]:6881"
151 */
152 QString listenInterfaces() const;
153
154 /**
155 * @brief Set the listen interfaces for incoming connections.
156 * @param value Comma-separated @c "address:port" pairs.
157 */
158 void setListenInterfaces(const QString& value);
159
160 // ── Protocol settings ───────────────────────────────────────────────
161
162 /** @brief Whether the Distributed Hash Table (DHT) is enabled. */
163 bool isDhtEnabled() const;
164 /** @brief Enable or disable DHT. */
165 void setDhtEnabled(bool enabled);
166
167 /** @brief Whether Local Service Discovery (LSD) is enabled. */
168 bool isLsdEnabled() const;
169 /** @brief Enable or disable LSD. */
170 void setLsdEnabled(bool enabled);
171
172 // ── Encryption ──────────────────────────────────────────────────────
173
174 /** @brief Current peer connection encryption policy. */
176 /** @brief Set the peer connection encryption policy. */
178
179 // ── User agent ──────────────────────────────────────────────────────
180
181 /** @brief The user-agent string sent to trackers and peers. */
182 QString userAgent() const;
183 /** @brief Set the user-agent string. */
184 void setUserAgent(const QString& value);
185
186 // ── Session statistics ──────────────────────────────────────────────
187
188 /**
189 * @brief Compute an aggregate snapshot of session statistics.
190 *
191 * Iterates all managed torrents and sums their transfer totals,
192 * rates, and peer counts. Also queries the DHT routing table size.
193 * @return A SessionStats value object.
194 */
196
197 /**
198 * @brief Post an asynchronous session-stats request to libtorrent.
199 *
200 * When the stats are ready, the sessionStatsReceived() signal is emitted.
201 */
203
204 // ── IP filtering ────────────────────────────────────────────────────
205
206 /**
207 * @brief Block a range of IP addresses.
208 *
209 * Peers connecting from addresses in the range will be rejected.
210 * Call with the same start and end to block a single address.
211 * @param first Start of the IP range (inclusive).
212 * @param last End of the IP range (inclusive).
213 */
214 void addIpFilter(const QHostAddress& first, const QHostAddress& last);
215
216 /**
217 * @brief Remove a previously added IP filter range.
218 * @param first Start of the IP range (inclusive).
219 * @param last End of the IP range (inclusive).
220 */
221 void removeIpFilter(const QHostAddress& first, const QHostAddress& last);
222
223 /**
224 * @brief Load a P2P-format blocklist from a file.
225 *
226 * Supports the widely-used P2P plaintext format where each line is:
227 * @c "description:startIP-endIP". Lines starting with @c # are ignored.
228 * @param filePath Path to the blocklist file.
229 * @return Number of ranges loaded, or -1 on file error.
230 */
231 int loadBlocklist(const QString& filePath);
232
233 /** @brief Remove all IP filter rules, allowing all addresses. */
235
236 // ── Resume data ─────────────────────────────────────────────────────
237
238 /** @brief Directory where @c .fastresume files are stored. */
239 QString resumeDataDirectory() const { return _resumeDataDirectory; }
240
241 /**
242 * @brief Set the resume-data directory and create it if it doesn't exist.
243 * @param path Filesystem path for @c .fastresume files.
244 */
245 void setResumeDataDirectory(const QString& path);
246
247 /**
248 * @brief Request resume-data saves for every managed torrent.
249 *
250 * Each torrent's resume data is written asynchronously. The
251 * resumeDataSaved() or resumeDataFailed() signal fires per torrent
252 * once the save completes.
253 */
255
256 /**
257 * @brief Scan the resume-data directory and re-add all found torrents.
258 *
259 * Call this at startup after setResumeDataDirectory() to restore
260 * the previous session state.
261 */
263
264 // ── Global controls ─────────────────────────────────────────────────
265
266 /** @brief Resume all paused/stopped torrents. */
267 void startAll();
268 /** @brief Pause all active torrents. */
269 void stopAll();
270
271 // ── Alert processing ────────────────────────────────────────────────
272
273 /**
274 * @brief Process pending libtorrent alerts.
275 *
276 * Called automatically every 500 ms by an internal timer. Can also
277 * be called manually for tighter polling. Translates libtorrent
278 * alerts into the appropriate Qt signals on TorrentClient and Torrent.
279 */
281
282signals:
283 /** @brief Emitted after a torrent is successfully added to the session. */
284 void torrentAdded(Torrent* torrent);
285 /** @brief Emitted after a torrent is removed. @p infoHash identifies the removed torrent. */
286 void torrentRemoved(const QByteArray& infoHash);
287 /** @brief Emitted when any torrent transitions to a new state. */
289 /** @brief Emitted when a torrent finishes downloading and enters seeding. */
290 void torrentComplete(Torrent* torrent);
291
292 /** @brief Emitted when the session begins listening on an interface. */
293 void listenSucceeded(const QString& address, int port);
294 /** @brief Emitted when binding to a listen interface fails. */
295 void listenFailed(const QString& address, int port, const QString& error);
296 /** @brief Emitted when the session's external (public) IP is detected. */
297 void externalAddressDetected(const QString& address);
298 /** @brief Emitted when the DHT routing table is initially populated. */
300 /** @brief Emitted in response to requestSessionStats(). */
302
303 /** @brief Emitted when a torrent's resume data is successfully written to disk. */
304 void resumeDataSaved(const QByteArray& infoHash);
305 /** @brief Emitted when saving resume data for a torrent fails. */
306 void resumeDataFailed(const QByteArray& infoHash, const QString& error);
307
308private:
309 void configureProxy();
310 void applySetting(int name, int value);
311 void applySetting(int name, bool value);
312 void applySetting(int name, const std::string& value);
313 int getSettingInt(int name) const;
314 bool getSettingBool(int name) const;
315 std::string getSettingString(int name) const;
316 Torrent* findTorrentByHandle(void* alertHandle);
317
318 void* _session = nullptr; // lt::session*
319 QList<Torrent*> _torrents;
320 QNetworkProxy _networkProxy;
321 QString _defaultDownloadDirectory;
322 QString _resumeDataDirectory;
323 QTimer* _alertTimer = nullptr;
324};
325
326#endif // TORRENTCLIENT_H
Aggregate statistics snapshot for a TorrentClient session.
Qt wrapper around a libtorrent session.
void setEncryptionMode(EncryptionMode mode)
Set the peer connection encryption policy.
int loadBlocklist(const QString &filePath)
Load a P2P-format blocklist from a file.
void loadResumeData()
Scan the resume-data directory and re-add all found torrents.
EncryptionMode
Peer connection encryption policy.
@ EncryptionForced
Require encryption; reject plaintext peers.
@ EncryptionDisabled
Disable encryption entirely.
@ EncryptionEnabled
Prefer encrypted connections, allow plaintext fallback.
QString userAgent() const
The user-agent string sent to trackers and peers.
void listenSucceeded(const QString &address, int port)
Emitted when the session begins listening on an interface.
void torrentRemoved(const QByteArray &infoHash)
Emitted after a torrent is removed.
void setUploadRateLimit(int bytesPerSecond)
Set the global upload rate limit in bytes/sec.
void dhtBootstrapComplete()
Emitted when the DHT routing table is initially populated.
void setResumeDataDirectory(const QString &path)
Set the resume-data directory and create it if it doesn't exist.
bool isLsdEnabled() const
Whether Local Service Discovery (LSD) is enabled.
void removeIpFilter(const QHostAddress &first, const QHostAddress &last)
Remove a previously added IP filter range.
SessionStats sessionStats() const
Compute an aggregate snapshot of session statistics.
QString resumeDataDirectory() const
Directory where .fastresume files are stored.
bool isDhtEnabled() const
Whether the Distributed Hash Table (DHT) is enabled.
void requestSessionStats()
Post an asynchronous session-stats request to libtorrent.
QString listenInterfaces() const
Current listen interface string.
void clearIpFilter()
Remove all IP filter rules, allowing all addresses.
void processAlerts()
Process pending libtorrent alerts.
void setDownloadRateLimit(int bytesPerSecond)
Set the global download rate limit in bytes/sec.
void saveAllResumeData()
Request resume-data saves for every managed torrent.
void externalAddressDetected(const QString &address)
Emitted when the session's external (public) IP is detected.
void torrentComplete(Torrent *torrent)
Emitted when a torrent finishes downloading and enters seeding.
void sessionStatsReceived(const SessionStats &stats)
Emitted in response to requestSessionStats().
void stopAll()
Pause all active torrents.
void setMaxUploads(int value)
Set the maximum number of unchoke slots.
void setNetworkProxy(const QNetworkProxy &value)
Route all libtorrent traffic through a SOCKS5 proxy.
void resumeDataFailed(const QByteArray &infoHash, const QString &error)
Emitted when saving resume data for a torrent fails.
void setDhtEnabled(bool enabled)
Enable or disable DHT.
void setDefaultDownloadDirectory(const QString &value)
Set the default save path for new torrents.
int maxConnections() const
Maximum number of simultaneous connections across all torrents.
void listenFailed(const QString &address, int port, const QString &error)
Emitted when binding to a listen interface fails.
void setLsdEnabled(bool enabled)
Enable or disable LSD.
void torrentAdded(Torrent *torrent)
Emitted after a torrent is successfully added to the session.
void setMaxConnections(int value)
Set the global connection limit.
void addIpFilter(const QHostAddress &first, const QHostAddress &last)
Block a range of IP addresses.
void setUserAgent(const QString &value)
Set the user-agent string.
EncryptionMode encryptionMode() const
Current peer connection encryption policy.
void startAll()
Resume all paused/stopped torrents.
QNetworkProxy networkProxy() const
Current SOCKS5 proxy configuration.
void setListenInterfaces(const QString &value)
Set the listen interfaces for incoming connections.
void resumeDataSaved(const QByteArray &infoHash)
Emitted when a torrent's resume data is successfully written to disk.
int maxUploads() const
Maximum number of unchoke slots (upload slots).
Torrent * findTorrent(const QByteArray &infoHash) const
Find a torrent by its 20-byte SHA-1 info hash.
QString defaultDownloadDirectory() const
Default save path for new torrents when no override is given.
void torrentStateChanged(Torrent *torrent, Torrent::State state)
Emitted when any torrent transitions to a new state.
int downloadRateLimit() const
Global download rate limit in bytes/sec.
int uploadRateLimit() const
Global upload rate limit in bytes/sec.
Represents a single torrent within a TorrentClient session.
Definition torrent.h:22
State
Lifecycle state of a torrent.
Definition torrent.h:28