Improve "info hash" handling

Define "torrent ID" concept, which is either a SHA1 hash for torrents of version 1,
or a SHA256 hash (truncated to SHA1 hash length) for torrents of version 2.
Add support for native libtorrent2 info hashes.
This commit is contained in:
Vladimir Golovnev (Glassez) 2021-03-05 12:43:58 +03:00
parent 4da4fb0676
commit 561b597031
No known key found for this signature in database
GPG key ID: 52A2C7DEE2DFA6F7
34 changed files with 463 additions and 320 deletions

View file

@ -460,10 +460,10 @@ void SyncController::maindataAction()
QHash<QString, QStringList> trackers;
for (const BitTorrent::Torrent *torrent : asConst(session->torrents()))
{
const BitTorrent::InfoHash torrentHash = torrent->hash();
const BitTorrent::TorrentID torrentID = torrent->id();
QVariantMap map = serialize(*torrent);
map.remove(KEY_TORRENT_HASH);
map.remove(KEY_TORRENT_ID);
// Calculated last activity time can differ from actual value by up to 10 seconds (this is a libtorrent issue).
// So we don't need unnecessary updates of last activity time in response.
@ -471,11 +471,11 @@ void SyncController::maindataAction()
if (iterTorrents != lastResponse.end())
{
const QVariantHash lastResponseTorrents = iterTorrents->toHash();
const auto iterHash = lastResponseTorrents.find(torrentHash.toString());
const auto iterID = lastResponseTorrents.find(torrentID.toString());
if (iterHash != lastResponseTorrents.end())
if (iterID != lastResponseTorrents.end())
{
const QVariantMap torrentData = iterHash->toMap();
const QVariantMap torrentData = iterID->toMap();
const auto iterLastActivity = torrentData.find(KEY_TORRENT_LAST_ACTIVITY_TIME);
if (iterLastActivity != torrentData.end())
@ -488,9 +488,9 @@ void SyncController::maindataAction()
}
for (const BitTorrent::TrackerEntry &tracker : asConst(torrent->trackers()))
trackers[tracker.url] << torrentHash.toString();
trackers[tracker.url] << torrentID.toString();
torrents[torrentHash.toString()] = map;
torrents[torrentID.toString()] = map;
}
data["torrents"] = torrents;
@ -534,15 +534,15 @@ void SyncController::maindataAction()
}
// GET param:
// - hash (string): torrent hash
// - hash (string): torrent hash (ID)
// - rid (int): last response id
void SyncController::torrentPeersAction()
{
auto lastResponse = sessionManager()->session()->getData(QLatin1String("syncTorrentPeersLastResponse")).toMap();
auto lastAcceptedResponse = sessionManager()->session()->getData(QLatin1String("syncTorrentPeersLastAcceptedResponse")).toMap();
const auto hash = BitTorrent::InfoHash::fromString(params()["hash"]);
const BitTorrent::Torrent *torrent = BitTorrent::Session::instance()->findTorrent(hash);
const auto id = BitTorrent::TorrentID::fromString(params()["hash"]);
const BitTorrent::Torrent *torrent = BitTorrent::Session::instance()->findTorrent(id);
if (!torrent)
throw APIError(APIErrorType::NotFound);