Add support for SSL torrents
[qBittorrent.git] / src / base / bittorrent / bencoderesumedatastorage.cpp
blob97d80ff0777aaeeefe571774241b518d72c8ee1e
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2015-2022 Vladimir Golovnev <glassez@yandex.ru>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
29 #include "bencoderesumedatastorage.h"
31 #include <libtorrent/bdecode.hpp>
32 #include <libtorrent/entry.hpp>
33 #include <libtorrent/read_resume_data.hpp>
34 #include <libtorrent/torrent_info.hpp>
35 #include <libtorrent/write_resume_data.hpp>
37 #include <QByteArray>
38 #include <QDebug>
39 #include <QFile>
40 #include <QRegularExpression>
41 #include <QThread>
43 #include "base/algorithm.h"
44 #include "base/exceptions.h"
45 #include "base/global.h"
46 #include "base/logger.h"
47 #include "base/preferences.h"
48 #include "base/profile.h"
49 #include "base/tagset.h"
50 #include "base/utils/fs.h"
51 #include "base/utils/io.h"
52 #include "base/utils/sslkey.h"
53 #include "base/utils/string.h"
54 #include "infohash.h"
55 #include "loadtorrentparams.h"
57 namespace BitTorrent
59 class BencodeResumeDataStorage::Worker final : public QObject
61 Q_DISABLE_COPY_MOVE(Worker)
63 public:
64 explicit Worker(const Path &resumeDataDir);
66 void store(const TorrentID &id, const LoadTorrentParams &resumeData) const;
67 void remove(const TorrentID &id) const;
68 void storeQueue(const QVector<TorrentID> &queue) const;
70 private:
71 const Path m_resumeDataDir;
75 namespace
77 const char KEY_SSL_CERTIFICATE[] = "qBt-sslCertificate";
78 const char KEY_SSL_PRIVATE_KEY[] = "qBt-sslPrivateKey";
79 const char KEY_SSL_DH_PARAMS[] = "qBt-sslDhParams";
81 template <typename LTStr>
82 QString fromLTString(const LTStr &str)
84 return QString::fromUtf8(str.data(), static_cast<qsizetype>(str.size()));
87 template <typename LTStr>
88 QByteArray toByteArray(const LTStr &str)
90 return {str.data(), static_cast<qsizetype>(str.size())};
93 using ListType = lt::entry::list_type;
95 ListType setToEntryList(const TagSet &input)
97 ListType entryList;
98 entryList.reserve(input.size());
99 for (const Tag &setValue : input)
100 entryList.emplace_back(setValue.toString().toStdString());
101 return entryList;
105 BitTorrent::BencodeResumeDataStorage::BencodeResumeDataStorage(const Path &path, QObject *parent)
106 : ResumeDataStorage(path, parent)
107 , m_ioThread {new QThread}
108 , m_asyncWorker {new Worker(path)}
110 Q_ASSERT(path.isAbsolute());
112 if (!path.exists() && !Utils::Fs::mkpath(path))
114 throw RuntimeError(tr("Cannot create torrent resume folder: \"%1\"")
115 .arg(path.toString()));
118 const QRegularExpression filenamePattern {u"^([A-Fa-f0-9]{40})\\.fastresume$"_s};
119 const QStringList filenames = QDir(path.data()).entryList({u"*.fastresume"_s}, QDir::Files);
121 m_registeredTorrents.reserve(filenames.size());
122 for (const QString &filename : filenames)
124 const QRegularExpressionMatch rxMatch = filenamePattern.match(filename);
125 if (rxMatch.hasMatch())
126 m_registeredTorrents.append(TorrentID::fromString(rxMatch.captured(1)));
129 loadQueue(path / Path(u"queue"_s));
131 qDebug() << "Registered torrents count: " << m_registeredTorrents.size();
133 m_asyncWorker->moveToThread(m_ioThread.get());
134 connect(m_ioThread.get(), &QThread::finished, m_asyncWorker, &QObject::deleteLater);
135 m_ioThread->start();
138 QVector<BitTorrent::TorrentID> BitTorrent::BencodeResumeDataStorage::registeredTorrents() const
140 return m_registeredTorrents;
143 BitTorrent::LoadResumeDataResult BitTorrent::BencodeResumeDataStorage::load(const TorrentID &id) const
145 const QString idString = id.toString();
146 const Path fastresumePath = path() / Path(idString + u".fastresume");
147 const Path torrentFilePath = path() / Path(idString + u".torrent");
148 const qint64 torrentSizeLimit = Preferences::instance()->getTorrentFileSizeLimit();
150 const auto resumeDataReadResult = Utils::IO::readFile(fastresumePath, torrentSizeLimit);
151 if (!resumeDataReadResult)
152 return nonstd::make_unexpected(resumeDataReadResult.error().message);
154 const auto metadataReadResult = Utils::IO::readFile(torrentFilePath, torrentSizeLimit);
155 if (!metadataReadResult)
157 if (metadataReadResult.error().status != Utils::IO::ReadError::NotExist)
158 return nonstd::make_unexpected(metadataReadResult.error().message);
161 const QByteArray data = resumeDataReadResult.value();
162 const QByteArray metadata = metadataReadResult.value_or(QByteArray());
163 return loadTorrentResumeData(data, metadata);
166 void BitTorrent::BencodeResumeDataStorage::doLoadAll() const
168 qDebug() << "Loading torrents count: " << m_registeredTorrents.size();
170 emit const_cast<BencodeResumeDataStorage *>(this)->loadStarted(m_registeredTorrents);
172 for (const TorrentID &torrentID : asConst(m_registeredTorrents))
173 onResumeDataLoaded(torrentID, load(torrentID));
175 emit const_cast<BencodeResumeDataStorage *>(this)->loadFinished();
178 void BitTorrent::BencodeResumeDataStorage::loadQueue(const Path &queueFilename)
180 const int lineMaxLength = 48;
182 QFile queueFile {queueFilename.data()};
183 if (!queueFile.exists())
184 return;
186 if (!queueFile.open(QFile::ReadOnly))
188 LogMsg(tr("Couldn't load torrents queue: %1").arg(queueFile.errorString()), Log::WARNING);
189 return;
192 const QRegularExpression hashPattern {u"^([A-Fa-f0-9]{40})$"_s};
193 int start = 0;
194 while (true)
196 const auto line = QString::fromLatin1(queueFile.readLine(lineMaxLength).trimmed());
197 if (line.isEmpty())
198 break;
200 const QRegularExpressionMatch rxMatch = hashPattern.match(line);
201 if (rxMatch.hasMatch())
203 const auto torrentID = BitTorrent::TorrentID::fromString(rxMatch.captured(1));
204 const int pos = m_registeredTorrents.indexOf(torrentID, start);
205 if (pos != -1)
207 std::swap(m_registeredTorrents[start], m_registeredTorrents[pos]);
208 ++start;
214 BitTorrent::LoadResumeDataResult BitTorrent::BencodeResumeDataStorage::loadTorrentResumeData(const QByteArray &data, const QByteArray &metadata) const
216 const auto *pref = Preferences::instance();
218 lt::error_code ec;
219 const lt::bdecode_node resumeDataRoot = lt::bdecode(data, ec
220 , nullptr, pref->getBdecodeDepthLimit(), pref->getBdecodeTokenLimit());
221 if (ec)
222 return nonstd::make_unexpected(tr("Cannot parse resume data: %1").arg(QString::fromStdString(ec.message())));
224 if (resumeDataRoot.type() != lt::bdecode_node::dict_t)
225 return nonstd::make_unexpected(tr("Cannot parse resume data: invalid format"));
227 LoadTorrentParams torrentParams;
228 torrentParams.category = fromLTString(resumeDataRoot.dict_find_string_value("qBt-category"));
229 torrentParams.name = fromLTString(resumeDataRoot.dict_find_string_value("qBt-name"));
230 torrentParams.hasFinishedStatus = resumeDataRoot.dict_find_int_value("qBt-seedStatus");
231 torrentParams.firstLastPiecePriority = resumeDataRoot.dict_find_int_value("qBt-firstLastPiecePriority");
232 torrentParams.seedingTimeLimit = resumeDataRoot.dict_find_int_value("qBt-seedingTimeLimit", Torrent::USE_GLOBAL_SEEDING_TIME);
233 torrentParams.inactiveSeedingTimeLimit = resumeDataRoot.dict_find_int_value("qBt-inactiveSeedingTimeLimit", Torrent::USE_GLOBAL_INACTIVE_SEEDING_TIME);
235 torrentParams.savePath = Profile::instance()->fromPortablePath(
236 Path(fromLTString(resumeDataRoot.dict_find_string_value("qBt-savePath"))));
237 torrentParams.useAutoTMM = torrentParams.savePath.isEmpty();
238 if (!torrentParams.useAutoTMM)
240 torrentParams.downloadPath = Profile::instance()->fromPortablePath(
241 Path(fromLTString(resumeDataRoot.dict_find_string_value("qBt-downloadPath"))));
244 // TODO: The following code is deprecated. Replace with the commented one after several releases in 4.4.x.
245 // === BEGIN DEPRECATED CODE === //
246 const lt::bdecode_node contentLayoutNode = resumeDataRoot.dict_find("qBt-contentLayout");
247 if (contentLayoutNode.type() == lt::bdecode_node::string_t)
249 const QString contentLayoutStr = fromLTString(contentLayoutNode.string_value());
250 torrentParams.contentLayout = Utils::String::toEnum(contentLayoutStr, TorrentContentLayout::Original);
252 else
254 const bool hasRootFolder = resumeDataRoot.dict_find_int_value("qBt-hasRootFolder");
255 torrentParams.contentLayout = (hasRootFolder ? TorrentContentLayout::Original : TorrentContentLayout::NoSubfolder);
257 // === END DEPRECATED CODE === //
258 // === BEGIN REPLACEMENT CODE === //
259 // torrentParams.contentLayout = Utils::String::parse(
260 // fromLTString(root.dict_find_string_value("qBt-contentLayout")), TorrentContentLayout::Default);
261 // === END REPLACEMENT CODE === //
263 torrentParams.stopCondition = Utils::String::toEnum(
264 fromLTString(resumeDataRoot.dict_find_string_value("qBt-stopCondition")), Torrent::StopCondition::None);
265 torrentParams.sslParameters =
267 .certificate = QSslCertificate(toByteArray(resumeDataRoot.dict_find_string_value(KEY_SSL_CERTIFICATE))),
268 .privateKey = Utils::SSLKey::load(toByteArray(resumeDataRoot.dict_find_string_value(KEY_SSL_PRIVATE_KEY))),
269 .dhParams = toByteArray(resumeDataRoot.dict_find_string_value(KEY_SSL_DH_PARAMS))
272 const lt::string_view ratioLimitString = resumeDataRoot.dict_find_string_value("qBt-ratioLimit");
273 if (ratioLimitString.empty())
274 torrentParams.ratioLimit = resumeDataRoot.dict_find_int_value("qBt-ratioLimit", Torrent::USE_GLOBAL_RATIO * 1000) / 1000.0;
275 else
276 torrentParams.ratioLimit = fromLTString(ratioLimitString).toDouble();
278 const lt::bdecode_node tagsNode = resumeDataRoot.dict_find("qBt-tags");
279 if (tagsNode.type() == lt::bdecode_node::list_t)
281 for (int i = 0; i < tagsNode.list_size(); ++i)
283 const Tag tag {fromLTString(tagsNode.list_string_value_at(i))};
284 torrentParams.tags.insert(tag);
288 lt::add_torrent_params &p = torrentParams.ltAddTorrentParams;
290 p = lt::read_resume_data(resumeDataRoot, ec);
292 if (!metadata.isEmpty())
294 const auto *pref = Preferences::instance();
295 const lt::bdecode_node torentInfoRoot = lt::bdecode(metadata, ec
296 , nullptr, pref->getBdecodeDepthLimit(), pref->getBdecodeTokenLimit());
297 if (ec)
298 return nonstd::make_unexpected(tr("Cannot parse torrent info: %1").arg(QString::fromStdString(ec.message())));
300 if (torentInfoRoot.type() != lt::bdecode_node::dict_t)
301 return nonstd::make_unexpected(tr("Cannot parse torrent info: invalid format"));
303 const auto torrentInfo = std::make_shared<lt::torrent_info>(torentInfoRoot, ec);
304 if (ec)
305 return nonstd::make_unexpected(tr("Cannot parse torrent info: %1").arg(QString::fromStdString(ec.message())));
307 p.ti = torrentInfo;
309 #ifdef QBT_USES_LIBTORRENT2
310 if (((p.info_hashes.has_v1() && (p.info_hashes.v1 != p.ti->info_hashes().v1))
311 || (p.info_hashes.has_v2() && (p.info_hashes.v2 != p.ti->info_hashes().v2))))
312 #else
313 if (!p.info_hash.is_all_zeros() && (p.info_hash != p.ti->info_hash()))
314 #endif
316 return nonstd::make_unexpected(tr("Mismatching info-hash detected in resume data"));
320 p.save_path = Profile::instance()->fromPortablePath(
321 Path(fromLTString(p.save_path))).toString().toStdString();
323 torrentParams.stopped = (p.flags & lt::torrent_flags::paused) && !(p.flags & lt::torrent_flags::auto_managed);
324 torrentParams.operatingMode = (p.flags & lt::torrent_flags::paused) || (p.flags & lt::torrent_flags::auto_managed)
325 ? TorrentOperatingMode::AutoManaged : TorrentOperatingMode::Forced;
327 if (p.flags & lt::torrent_flags::stop_when_ready)
329 p.flags &= ~lt::torrent_flags::stop_when_ready;
330 torrentParams.stopCondition = Torrent::StopCondition::FilesChecked;
333 const bool hasMetadata = (p.ti && p.ti->is_valid());
334 if (!hasMetadata && !resumeDataRoot.dict_find("info-hash"))
335 return nonstd::make_unexpected(tr("Resume data is invalid: neither metadata nor info-hash was found"));
337 return torrentParams;
340 void BitTorrent::BencodeResumeDataStorage::store(const TorrentID &id, const LoadTorrentParams &resumeData) const
342 QMetaObject::invokeMethod(m_asyncWorker, [this, id, resumeData]()
344 m_asyncWorker->store(id, resumeData);
348 void BitTorrent::BencodeResumeDataStorage::remove(const TorrentID &id) const
350 QMetaObject::invokeMethod(m_asyncWorker, [this, id]()
352 m_asyncWorker->remove(id);
356 void BitTorrent::BencodeResumeDataStorage::storeQueue(const QVector<TorrentID> &queue) const
358 QMetaObject::invokeMethod(m_asyncWorker, [this, queue]()
360 m_asyncWorker->storeQueue(queue);
364 BitTorrent::BencodeResumeDataStorage::Worker::Worker(const Path &resumeDataDir)
365 : m_resumeDataDir {resumeDataDir}
369 void BitTorrent::BencodeResumeDataStorage::Worker::store(const TorrentID &id, const LoadTorrentParams &resumeData) const
371 // We need to adjust native libtorrent resume data
372 lt::add_torrent_params p = resumeData.ltAddTorrentParams;
373 p.save_path = Profile::instance()->toPortablePath(Path(p.save_path))
374 .toString().toStdString();
375 if (resumeData.stopped)
377 p.flags |= lt::torrent_flags::paused;
378 p.flags &= ~lt::torrent_flags::auto_managed;
380 else
382 // Torrent can be actually "running" but temporarily "paused" to perform some
383 // service jobs behind the scenes so we need to restore it as "running"
384 if (resumeData.operatingMode == BitTorrent::TorrentOperatingMode::AutoManaged)
386 p.flags |= lt::torrent_flags::auto_managed;
388 else
390 p.flags &= ~lt::torrent_flags::paused;
391 p.flags &= ~lt::torrent_flags::auto_managed;
395 lt::entry data = lt::write_resume_data(p);
397 // metadata is stored in separate .torrent file
398 if (p.ti)
400 lt::entry::dictionary_type &dataDict = data.dict();
401 lt::entry metadata {lt::entry::dictionary_t};
402 lt::entry::dictionary_type &metadataDict = metadata.dict();
403 metadataDict.insert(dataDict.extract("info"));
404 metadataDict.insert(dataDict.extract("creation date"));
405 metadataDict.insert(dataDict.extract("created by"));
406 metadataDict.insert(dataDict.extract("comment"));
408 const Path torrentFilepath = m_resumeDataDir / Path(u"%1.torrent"_s.arg(id.toString()));
409 const nonstd::expected<void, QString> result = Utils::IO::saveToFile(torrentFilepath, metadata);
410 if (!result)
412 LogMsg(tr("Couldn't save torrent metadata to '%1'. Error: %2.")
413 .arg(torrentFilepath.toString(), result.error()), Log::CRITICAL);
414 return;
418 data["qBt-ratioLimit"] = static_cast<int>(resumeData.ratioLimit * 1000);
419 data["qBt-seedingTimeLimit"] = resumeData.seedingTimeLimit;
420 data["qBt-inactiveSeedingTimeLimit"] = resumeData.inactiveSeedingTimeLimit;
421 data["qBt-category"] = resumeData.category.toStdString();
422 data["qBt-tags"] = setToEntryList(resumeData.tags);
423 data["qBt-name"] = resumeData.name.toStdString();
424 data["qBt-seedStatus"] = resumeData.hasFinishedStatus;
425 data["qBt-contentLayout"] = Utils::String::fromEnum(resumeData.contentLayout).toStdString();
426 data["qBt-firstLastPiecePriority"] = resumeData.firstLastPiecePriority;
427 data["qBt-stopCondition"] = Utils::String::fromEnum(resumeData.stopCondition).toStdString();
429 if (!resumeData.sslParameters.certificate.isNull())
430 data[KEY_SSL_CERTIFICATE] = resumeData.sslParameters.certificate.toPem().toStdString();
431 if (!resumeData.sslParameters.privateKey.isNull())
432 data[KEY_SSL_PRIVATE_KEY] = resumeData.sslParameters.privateKey.toPem().toStdString();
433 if (!resumeData.sslParameters.dhParams.isEmpty())
434 data[KEY_SSL_DH_PARAMS] = resumeData.sslParameters.dhParams.toStdString();
436 if (!resumeData.useAutoTMM)
438 data["qBt-savePath"] = Profile::instance()->toPortablePath(resumeData.savePath).data().toStdString();
439 data["qBt-downloadPath"] = Profile::instance()->toPortablePath(resumeData.downloadPath).data().toStdString();
442 const Path resumeFilepath = m_resumeDataDir / Path(u"%1.fastresume"_s.arg(id.toString()));
443 const nonstd::expected<void, QString> result = Utils::IO::saveToFile(resumeFilepath, data);
444 if (!result)
446 LogMsg(tr("Couldn't save torrent resume data to '%1'. Error: %2.")
447 .arg(resumeFilepath.toString(), result.error()), Log::CRITICAL);
451 void BitTorrent::BencodeResumeDataStorage::Worker::remove(const TorrentID &id) const
453 const Path resumeFilename {u"%1.fastresume"_s.arg(id.toString())};
454 Utils::Fs::removeFile(m_resumeDataDir / resumeFilename);
456 const Path torrentFilename {u"%1.torrent"_s.arg(id.toString())};
457 Utils::Fs::removeFile(m_resumeDataDir / torrentFilename);
460 void BitTorrent::BencodeResumeDataStorage::Worker::storeQueue(const QVector<TorrentID> &queue) const
462 QByteArray data;
463 data.reserve(((BitTorrent::TorrentID::length() * 2) + 1) * queue.size());
464 for (const BitTorrent::TorrentID &torrentID : queue)
465 data += (torrentID.toString().toLatin1() + '\n');
467 const Path filepath = m_resumeDataDir / Path(u"queue"_s);
468 const nonstd::expected<void, QString> result = Utils::IO::saveToFile(filepath, data);
469 if (!result)
471 LogMsg(tr("Couldn't save data to '%1'. Error: %2")
472 .arg(filepath.toString(), result.error()), Log::CRITICAL);