Skip to content

Commit

Permalink
Support libtorrent master
Browse files Browse the repository at this point in the history
  • Loading branch information
glassez committed Feb 6, 2025
1 parent 9c2e698 commit 685218e
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 18 deletions.
24 changes: 19 additions & 5 deletions src/base/bittorrent/peerinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

#include "peerinfo.h"

#include <libtorrent/version.hpp>

#include <QBitArray>

#include "base/bittorrent/ltqbitarray.h"
Expand All @@ -36,6 +38,17 @@
#include "base/utils/bytearray.h"
#include "peeraddress.h"

namespace
{
#ifdef QBT_USES_LIBTORRENT2
QString toI2PAddress(const lt::sha256_hash &destHash)
{
const QByteArray base32Dest = Utils::ByteArray::toBase32({destHash.data(), destHash.size()}).replace('=', "").toLower();
return QString::fromLatin1(base32Dest) + u".b32.i2p";
}
#endif
}

using namespace BitTorrent;

PeerInfo::PeerInfo(const lt::peer_info &nativeInfo, const QBitArray &allPieces)
Expand Down Expand Up @@ -172,11 +185,16 @@ PeerAddress PeerInfo::address() const
if (useI2PSocket())
return {};

#if LIBTORRENT_VERSION_NUM < 20100
// fast path for platforms which boost.asio internal struct maps to `sockaddr`
return {QHostAddress(m_nativeInfo.ip.data()), m_nativeInfo.ip.port()};
// slow path for the others
//return {QHostAddress(QString::fromStdString(m_nativeInfo.ip.address().to_string()))
// , m_nativeInfo.ip.port()};
#else
const lt::tcp::endpoint remoteEndpoint = m_nativeInfo.remote_endpoint();
return {QHostAddress(remoteEndpoint.data()), remoteEndpoint.port()};
#endif
}

QString PeerInfo::I2PAddress() const
Expand All @@ -186,11 +204,7 @@ QString PeerInfo::I2PAddress() const

#if defined(QBT_USES_LIBTORRENT2) && TORRENT_USE_I2P
if (m_I2PAddress.isEmpty())
{
const lt::sha256_hash destHash = m_nativeInfo.i2p_destination();
const QByteArray base32Dest = Utils::ByteArray::toBase32({destHash.data(), destHash.size()}).replace('=', "").toLower();
m_I2PAddress = QString::fromLatin1(base32Dest) + u".b32.i2p";
}
m_I2PAddress = toI2PAddress(m_nativeInfo.i2p_destination());
#endif

return m_I2PAddress;
Expand Down
45 changes: 37 additions & 8 deletions src/base/bittorrent/sessionimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
#include "base/preferences.h"
#include "base/profile.h"
#include "base/unicodestrings.h"
#include "base/utils/bytearray.h"
#include "base/utils/fs.h"
#include "base/utils/io.h"
#include "base/utils/net.h"
Expand Down Expand Up @@ -199,6 +200,10 @@ namespace
#endif
case lt::socket_type_t::i2p:
return u"I2P"_s;
#if LIBTORRENT_VERSION_NUM >= 20100
case lt::socket_type_t::rtc:
return u"WebRTC"_s;
#endif
case lt::socket_type_t::socks5:
return u"SOCKS5"_s;
#ifdef QBT_USES_LIBTORRENT2
Expand Down Expand Up @@ -235,6 +240,14 @@ namespace
return {};
}

#ifdef QBT_USES_LIBTORRENT2
QString toI2PAddress(const lt::sha256_hash &destHash)
{
const QByteArray base32Dest = Utils::ByteArray::toBase32({destHash.data(), destHash.size()}).replace('=', "").toLower();
return QString::fromLatin1(base32Dest) + u".b32.i2p";
}
#endif

template <typename T>
struct LowerLimited
{
Expand Down Expand Up @@ -6031,20 +6044,29 @@ void SessionImpl::handlePortmapAlert(const lt::portmap_alert *alert)

void SessionImpl::handlePeerBlockedAlert(const lt::peer_blocked_alert *alert)
{
#if LIBTORRENT_VERSION_NUM < 20100
const lt::tcp::endpoint peerIPEndpoint = alert->endpoint;
const QString peerAddress = toString(peerIPEndpoint.address());
#else
const auto peerIPEndpointPtr = std::get_if<lt::peer_alert::ip_endpoint>(&alert->ep);
const auto peerIPEndpoint = peerIPEndpointPtr ? static_cast<lt::tcp::endpoint>(*peerIPEndpointPtr) : lt::tcp::endpoint();
const QString peerAddress = peerIPEndpointPtr
? toString(peerIPEndpoint.address()) : toI2PAddress(std::get<lt::peer_alert::i2p_endpoint>(alert->ep));
#endif
QString reason;
switch (alert->reason)
{
case lt::peer_blocked_alert::ip_filter:
reason = tr("IP filter", "this peer was blocked. Reason: IP filter.");
break;
case lt::peer_blocked_alert::port_filter:
reason = tr("filtered port (%1)", "this peer was blocked. Reason: filtered port (8899).").arg(QString::number(alert->endpoint.port()));
reason = tr("filtered port (%1)", "this peer was blocked. Reason: filtered port (8899).").arg(QString::number(peerIPEndpoint.port()));
break;
case lt::peer_blocked_alert::i2p_mixed:
reason = tr("%1 mixed mode restrictions", "this peer was blocked. Reason: I2P mixed mode restrictions.").arg(u"I2P"_s); // don't translate I2P
break;
case lt::peer_blocked_alert::privileged_ports:
reason = tr("privileged port (%1)", "this peer was blocked. Reason: privileged port (80).").arg(QString::number(alert->endpoint.port()));
reason = tr("privileged port (%1)", "this peer was blocked. Reason: privileged port (80).").arg(QString::number(peerIPEndpoint.port()));
break;
case lt::peer_blocked_alert::utp_disabled:
reason = tr("%1 is disabled", "this peer was blocked. Reason: uTP is disabled.").arg(C_UTP); // don't translate μTP
Expand All @@ -6054,16 +6076,23 @@ void SessionImpl::handlePeerBlockedAlert(const lt::peer_blocked_alert *alert)
break;
}

const QString ip {toString(alert->endpoint.address())};
if (!ip.isEmpty())
Logger::instance()->addPeer(ip, true, reason);
if (!peerAddress.isEmpty())
Logger::instance()->addPeer(peerAddress, true, reason);
}

void SessionImpl::handlePeerBanAlert(const lt::peer_ban_alert *alert)
{
const QString ip {toString(alert->endpoint.address())};
if (!ip.isEmpty())
Logger::instance()->addPeer(ip, false);
#if LIBTORRENT_VERSION_NUM < 20100
const lt::tcp::endpoint peerIPEndpoint = alert->endpoint;
const QString peerAddress = toString(peerIPEndpoint.address());
#else
const auto peerIPEndpointPtr = std::get_if<lt::peer_alert::ip_endpoint>(&alert->ep);
const auto peerIPEndpoint = peerIPEndpointPtr ? static_cast<lt::tcp::endpoint>(*peerIPEndpointPtr) : lt::tcp::endpoint();
const QString peerAddress = peerIPEndpointPtr
? toString(peerIPEndpoint.address()) : toI2PAddress(std::get<lt::peer_alert::i2p_endpoint>(alert->ep));
#endif
if (!peerAddress.isEmpty())
Logger::instance()->addPeer(peerAddress, false);
}

void SessionImpl::handleUrlSeedAlert(const lt::url_seed_alert *alert)
Expand Down
27 changes: 25 additions & 2 deletions src/base/bittorrent/torrentcreator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <libtorrent/create_torrent.hpp>
#include <libtorrent/file_storage.hpp>
#include <libtorrent/torrent_info.hpp>
#include <libtorrent/version.hpp>

#include <QtSystemDetection>
#include <QDirIterator>
Expand Down Expand Up @@ -114,10 +115,18 @@ void TorrentCreator::run()
const Utils::Compare::NaturalLessThan<Qt::CaseInsensitive> naturalLessThan {};

// Adding files to the torrent
#if LIBTORRENT_VERSION_NUM < 20100
lt::file_storage fs;
#else
std::vector<lt::create_file_entry> fileEntries;
#endif
if (QFileInfo(m_params.sourcePath.data()).isFile())
{
#if LIBTORRENT_VERSION_NUM < 20100
lt::add_files(fs, m_params.sourcePath.toString().toStdString(), fileFilter);
#else
fileEntries = lt::list_files(m_params.sourcePath.toString().toStdString(), fileFilter);
#endif
}
else
{
Expand Down Expand Up @@ -166,13 +175,23 @@ void TorrentCreator::run()
}

for (const QString &fileName : asConst(fileNames))
{
#if LIBTORRENT_VERSION_NUM < 20100
fs.add_file(fileName.toStdString(), fileSizeMap[fileName]);
#else
fileEntries.emplace_back(fileName.toStdString(), fileSizeMap[fileName]);
#endif
}
}

checkInterruptionRequested();

#ifdef QBT_USES_LIBTORRENT2
#if LIBTORRENT_VERSION_NUM < 20100
lt::create_torrent newTorrent {fs, m_params.pieceSize, toNativeTorrentFormatFlag(m_params.torrentFormat)};
#else
lt::create_torrent newTorrent {fileEntries, m_params.pieceSize, toNativeTorrentFormatFlag(m_params.torrentFormat)};
#endif
#else
lt::create_torrent newTorrent(fs, m_params.pieceSize, m_params.paddedFileSizeLimit
, (m_params.isAlignmentOptimized ? lt::create_torrent::optimize_alignment : lt::create_flags_t {}));
Expand Down Expand Up @@ -268,13 +287,17 @@ int TorrentCreator::calculateTotalPieces(const Path &inputPath, const int pieceS
if (inputPath.isEmpty())
return 0;

#if LIBTORRENT_VERSION_NUM >= 20100
std::vector<lt::create_file_entry> fileEntries = lt::list_files(inputPath.toString().toStdString(), fileFilter);
return lt::create_torrent {std::move(fileEntries), pieceSize, toNativeTorrentFormatFlag(torrentFormat)}.num_pieces();
#else
lt::file_storage fs;
lt::add_files(fs, inputPath.toString().toStdString(), fileFilter);

#ifdef QBT_USES_LIBTORRENT2
return lt::create_torrent {fs, pieceSize, toNativeTorrentFormatFlag(torrentFormat)}.num_pieces();
#else
return lt::create_torrent(fs, pieceSize, paddedFileSizeLimit
, (isAlignmentOptimized ? lt::create_torrent::optimize_alignment : lt::create_flags_t {})).num_pieces();
, (isAlignmentOptimized ? lt::create_torrent::optimize_alignment : lt::create_flags_t {})).num_pieces();
#endif
#endif
}
12 changes: 9 additions & 3 deletions src/base/bittorrent/torrentimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,11 @@ bool TorrentImpl::connectPeer(const PeerAddress &peerAddress)

bool TorrentImpl::needSaveResumeData() const
{
#if LIBTORRENT_VERSION_NUM < 20100
return m_nativeStatus.need_save_resume;
#else
return static_cast<bool>(m_nativeStatus.need_save_resume_data);
#endif
}

void TorrentImpl::requestResumeData(const lt::resume_data_flags_t flags)
Expand Down Expand Up @@ -1885,9 +1889,11 @@ void TorrentImpl::reload()
m_nativeSession->remove_torrent(m_nativeHandle, lt::session::delete_partfile);

lt::add_torrent_params p = m_ltAddTorrentParams;
p.flags |= lt::torrent_flags::update_subscribe
| lt::torrent_flags::override_trackers
p.flags |= lt::torrent_flags::update_subscribe;
#if LIBTORRENT_VERSION_NUM < 20100
p.flags |= lt::torrent_flags::override_trackers
| lt::torrent_flags::override_web_seeds;
#endif

if (m_isStopped)
{
Expand Down Expand Up @@ -2096,7 +2102,7 @@ void TorrentImpl::handleTorrentCheckedAlert([[maybe_unused]] const lt::torrent_c
}
}

if (m_nativeStatus.need_save_resume)
if (needSaveResumeData())
deferredRequestResumeData();

m_session->handleTorrentChecked(this);
Expand Down

0 comments on commit 685218e

Please sign in to comment.