2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2015-2024 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 "torrentdescriptor.h"
31 #include <libtorrent/load_torrent.hpp>
32 #include <libtorrent/magnet_uri.hpp>
33 #include <libtorrent/torrent_info.hpp>
34 #include <libtorrent/version.hpp>
35 #include <libtorrent/write_resume_data.hpp>
38 #include <QRegularExpression>
41 #include "base/global.h"
42 #include "base/preferences.h"
43 #include "base/utils/io.h"
45 #include "trackerentry.h"
49 // BEP9 Extension for Peers to Send Metadata Files
51 bool isV1Hash(const QString
&string
)
53 // There are 2 representations for BitTorrent v1 info hash:
54 // 1. 40 chars hex-encoded string
55 // == 20 (SHA-1 length in bytes) * 2 (each byte maps to 2 hex characters)
56 // 2. 32 chars Base32 encoded string
57 // == 20 (SHA-1 length in bytes) * 1.6 (the efficiency of Base32 encoding)
58 const int V1_HEX_SIZE
= SHA1Hash::length() * 2;
59 const int V1_BASE32_SIZE
= SHA1Hash::length() * 1.6;
61 return ((((string
.size() == V1_HEX_SIZE
)) && !string
.contains(QRegularExpression(u
"[^0-9A-Fa-f]"_s
)))
62 || ((string
.size() == V1_BASE32_SIZE
) && !string
.contains(QRegularExpression(u
"[^2-7A-Za-z]"_s
))));
65 bool isV2Hash(const QString
&string
)
67 // There are 1 representation for BitTorrent v2 info hash:
68 // 1. 64 chars hex-encoded string
69 // == 32 (SHA-2 256 length in bytes) * 2 (each byte maps to 2 hex characters)
70 const int V2_HEX_SIZE
= SHA256Hash::length() * 2;
72 return (string
.size() == V2_HEX_SIZE
) && !string
.contains(QRegularExpression(u
"[^0-9A-Fa-f]"_s
));
75 lt::load_torrent_limits
loadTorrentLimits()
77 const auto *pref
= Preferences::instance();
79 lt::load_torrent_limits limits
;
80 limits
.max_buffer_size
= static_cast<int>(pref
->getTorrentFileSizeLimit());
81 limits
.max_decode_depth
= pref
->getBdecodeDepthLimit();
82 limits
.max_decode_tokens
= pref
->getBdecodeTokenLimit();
88 const int TORRENTDESCRIPTOR_TYPEID
= qRegisterMetaType
<BitTorrent::TorrentDescriptor
>();
90 nonstd::expected
<BitTorrent::TorrentDescriptor
, QString
>
91 BitTorrent::TorrentDescriptor::load(const QByteArray
&data
) noexcept
94 return TorrentDescriptor(lt::load_torrent_buffer(lt::span
<const char>(data
.data(), data
.size()), loadTorrentLimits()));
96 catch (const lt::system_error
&err
)
98 return nonstd::make_unexpected(QString::fromLocal8Bit(err
.what()));
101 nonstd::expected
<BitTorrent::TorrentDescriptor
, QString
>
102 BitTorrent::TorrentDescriptor::loadFromFile(const Path
&path
) noexcept
105 return TorrentDescriptor(lt::load_torrent_file(path
.toString().toStdString(), loadTorrentLimits()));
107 catch (const lt::system_error
&err
)
109 return nonstd::make_unexpected(QString::fromLocal8Bit(err
.what()));
112 nonstd::expected
<BitTorrent::TorrentDescriptor
, QString
>
113 BitTorrent::TorrentDescriptor::parse(const QString
&str
) noexcept
116 QString magnetURI
= str
;
118 magnetURI
= u
"magnet:?xt=urn:btmh:1220" + str
; // 0x12 0x20 is the "multihash format" tag for the SHA-256 hashing scheme.
119 else if (isV1Hash(str
))
120 magnetURI
= u
"magnet:?xt=urn:btih:" + str
;
122 return TorrentDescriptor(lt::parse_magnet_uri(magnetURI
.toStdString()));
124 catch (const lt::system_error
&err
)
126 return nonstd::make_unexpected(QString::fromLocal8Bit(err
.what()));
129 nonstd::expected
<void, QString
> BitTorrent::TorrentDescriptor::saveToFile(const Path
&path
) const
132 const lt::entry torrentEntry
= lt::write_torrent_file(m_ltAddTorrentParams
);
133 const nonstd::expected
<void, QString
> result
= Utils::IO::saveToFile(path
, torrentEntry
);
135 return result
.get_unexpected();
139 catch (const lt::system_error
&err
)
141 return nonstd::make_unexpected(QString::fromLocal8Bit(err
.what()));
144 BitTorrent::TorrentDescriptor::TorrentDescriptor(lt::add_torrent_params ltAddTorrentParams
)
145 : m_ltAddTorrentParams
{std::move(ltAddTorrentParams
)}
147 if (m_ltAddTorrentParams
.ti
&& m_ltAddTorrentParams
.ti
->is_valid())
149 m_info
.emplace(*m_ltAddTorrentParams
.ti
);
150 if (m_ltAddTorrentParams
.ti
->creation_date() > 0)
151 m_creationDate
= QDateTime::fromSecsSinceEpoch(m_ltAddTorrentParams
.ti
->creation_date());
152 m_creator
= QString::fromStdString(m_ltAddTorrentParams
.ti
->creator());
153 m_comment
= QString::fromStdString(m_ltAddTorrentParams
.ti
->comment());
157 BitTorrent::InfoHash
BitTorrent::TorrentDescriptor::infoHash() const
159 #ifdef QBT_USES_LIBTORRENT2
160 return m_ltAddTorrentParams
.info_hashes
;
162 return m_ltAddTorrentParams
.info_hash
;
166 QString
BitTorrent::TorrentDescriptor::name() const
168 return m_info
? m_info
->name() : QString::fromStdString(m_ltAddTorrentParams
.name
);
171 QDateTime
BitTorrent::TorrentDescriptor::creationDate() const
173 return m_creationDate
;
176 QString
BitTorrent::TorrentDescriptor::creator() const
181 QString
BitTorrent::TorrentDescriptor::comment() const
186 const std::optional
<BitTorrent::TorrentInfo
> &BitTorrent::TorrentDescriptor::info() const
191 void BitTorrent::TorrentDescriptor::setTorrentInfo(TorrentInfo torrentInfo
)
193 if (!torrentInfo
.isValid())
196 m_ltAddTorrentParams
.ti
.reset();
200 m_info
= std::move(torrentInfo
);
201 m_ltAddTorrentParams
.ti
= m_info
->nativeInfo();
202 #ifdef QBT_USES_LIBTORRENT2
203 m_ltAddTorrentParams
.info_hashes
= m_ltAddTorrentParams
.ti
->info_hashes();
205 m_ltAddTorrentParams
.info_hash
= m_ltAddTorrentParams
.ti
->info_hash();
210 QList
<BitTorrent::TrackerEntry
> BitTorrent::TorrentDescriptor::trackers() const
212 QList
<TrackerEntry
> ret
;
213 ret
.reserve(static_cast<decltype(ret
)::size_type
>(m_ltAddTorrentParams
.trackers
.size()));
215 for (const std::string
&tracker
: m_ltAddTorrentParams
.trackers
)
216 ret
.append({QString::fromStdString(tracker
), m_ltAddTorrentParams
.tracker_tiers
[i
++]});
221 QList
<QUrl
> BitTorrent::TorrentDescriptor::urlSeeds() const
223 QList
<QUrl
> urlSeeds
;
224 urlSeeds
.reserve(static_cast<decltype(urlSeeds
)::size_type
>(m_ltAddTorrentParams
.url_seeds
.size()));
226 for (const std::string
&nativeURLSeed
: m_ltAddTorrentParams
.url_seeds
)
227 urlSeeds
.append(QUrl(QString::fromStdString(nativeURLSeed
)));
232 const libtorrent::add_torrent_params
&BitTorrent::TorrentDescriptor::ltAddTorrentParams() const
234 return m_ltAddTorrentParams
;