From d97d272653269cc63133f901cd2fe77668c06ac6 Mon Sep 17 00:00:00 2001 From: orignal Date: Mon, 13 Jul 2026 18:23:58 -0400 Subject: [PATCH] parse compact tracker response --- libi2pd_client/Torrents.cpp | 42 +++++++++++-------------------------- libi2pd_client/Torrents.h | 5 ++--- 2 files changed, 14 insertions(+), 33 deletions(-) diff --git a/libi2pd_client/Torrents.cpp b/libi2pd_client/Torrents.cpp index 3d111b44..d3b7f9a1 100644 --- a/libi2pd_client/Torrents.cpp +++ b/libi2pd_client/Torrents.cpp @@ -295,7 +295,7 @@ namespace torrents return len; } - std::string Torrent::GetHexStraingInfoHash () const + std::string Torrent::GetHexStringInfoHash () const { std::string infoHash; BIGNUM * bn = BN_bin2bn (m_InfoHash.data (), SHA_DIGEST_LENGTH, nullptr); @@ -313,7 +313,7 @@ namespace torrents { i2p::http::HTTPReq req; req.parse (m_Announce); - req.AddHeader ("info_hash", GetHexStraingInfoHash ()); + req.AddHeader ("info_hash", GetHexStringInfoHash ()); return req; } @@ -336,32 +336,14 @@ namespace torrents size_t Torrent::ParsePeers (std::string_view buf) { m_Peers.clear (); - return ParseList (buf, std::bind (&Torrent::ParsePeer, this, std::placeholders::_1)); - } - - size_t Torrent::ParsePeer (std::string_view buf) - { - std::string peerID; - std::shared_ptr addr; - size_t ret = ParseDictionary (buf, [&peerID, &addr](std::string_view key, std::string_view buf)->size_t - { - if (key == "id") // peer_id - { - auto [id, l] = ExtractByteString (buf); - if (l) peerID = id; - return l; - } - else if (key == "ip") // address - { - auto [address, l] = ExtractByteString (buf); - if (l) addr = i2p::client::context.GetAddressBook ().GetAddress (address); - return l; - } - return 0; - }); - if (ret && addr) - m_Peers.emplace_back (std::pair{ peerID, addr }); - return ret; + auto [hashes, len] = ExtractByteString (buf); + while (!hashes.empty ()) + { + m_Peers.emplace_back (std::make_shared( + i2p::data::IdentHash((const uint8_t *)hashes.substr (0, SHA256_DIGEST_LENGTH).data ()))); + hashes = hashes.substr (SHA256_DIGEST_LENGTH); + } + return len; } PeerConnection::PeerConnection (i2p::client::I2PService * owner, std::shared_ptr stream): @@ -745,12 +727,12 @@ namespace torrents return; } req.AddHeader ("peer_id", m_PeerID); - req.AddHeader ("ip", GetLocalDestination ()->GetIdentHash ().ToBase32 () + ".b32.i2p"); + req.AddHeader ("ip", GetLocalDestination ()->GetIdentity ()->ToBase64 ()); req.AddHeader ("port", std::to_string (6881)); + req.AddHeader ("compact", "1"); req.AddHeader ("uploaded", "0"); // TODO req.AddHeader ("downloaded", "0"); // TODO req.AddHeader ("left", "1"); // TODO - req.AddHeader ("compact", "1"); // TODO req.AddHeader ("numwant", "0"); // TODO CreateStream ([this, req, torrent](std::shared_ptr stream) { diff --git a/libi2pd_client/Torrents.h b/libi2pd_client/Torrents.h index 0a293d32..e8dc7983 100644 --- a/libi2pd_client/Torrents.h +++ b/libi2pd_client/Torrents.h @@ -91,12 +91,11 @@ namespace torrents private: - std::string GetHexStraingInfoHash () const; + std::string GetHexStringInfoHash () const; size_t ParsePieces (std::string_view buf); size_t ParseInfo (std::string_view buf); size_t ParsePeers (std::string_view buf); - size_t ParsePeer (std::string_view buf); private: @@ -105,7 +104,7 @@ namespace torrents int m_Interval; InfoHash m_InfoHash; // SHA1 std::vector m_Pieces; - std::list > > m_Peers; + std::list > m_Peers; }; class TorrentsTunnel;