Search code examples
bittorrent

UDP torrent trackers not replying


I've finally gotten to the stage of getting a response from a UDP tracker.

Here's an exmaple, that I split into an array:

[ 1, 3765366842, 1908, 0, 2, 0 ]

Action, request ID, interval, leechers, seeders, peers.

No matter which torrent I chose, I get 1/2 seeders, which I'm assuming is the server tracking me, and no peers / leechers.

Am I not using the correct info hash?

This is how I retrieve it from a magnet link:

magnet:?xt=urn:btih:9f9165d9a281a9b8e782cd5176bbcc8256fd1871&dn=Ubuntu+16.04.1+LTS+Desktop+64-bit&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Fzer0day.ch%3A1337&tr=udp%3A%2F%2Fopen.demonii.com%3A1337&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Fexodus.desync.com%3A6969

...

h = 9f9165d9a281a9b8e782cd5176bbcc8256fd1871

Now I split this into chunks of two, and parse them, for hex bytes:

bytes = []; 
for (var i = 0; i < h.length; i++) bytes.push(parseInt((h[i]) + h[i++], 16));

[153, 153, 102, 221, 170, 136, 170, 187, 238, 136, 204, 85, 119, 187, 204, 136, 85, 255, 17, 119]

There's no need to encode this, so I send it along with my request.

This is the only point causing trouble, yet it seems so simple...

http://xbtt.sourceforge.net/udp_tracker_protocol.html


Solution

  • As the8472 says, your decoding is incorrect:

    for (var i = 0; i < h.length; i++) bytes.push(parseInt((h[i]) + h[i++], 16));
    

    i and i++ will have the same value here. (One of the reasons to avoid clever inline stuff.) You can use i and ++i, or maybe expand it all to multiple lines for readability:

    for (var i = 0; i < h.length; i += 2) {
        var hex = h.substr(i, 2);
        bytes.push(parseInt(hex, 16));
    }
    

    And if you’re using Node, just parse it into a Buffer, which can easily be converted to an array if necessary:

    var bytes = Buffer.from(h, 'hex');