Connecting to torrent trackers / peers

2019-05-31 16:10发布

问题:

I'm currently trying to implement a minimal torrent client, in nodeJS.

I'm reading through this specification: https://wiki.theory.org/index.php/BitTorrentSpecification

I've got 2 magnet URIs:

magnet:?xt=urn:btih:633ab5b0cc27218bca2f9fec9b68ae4f7cbf0c5f&dn=dmb2017-05-31.dpa4021.flac16


xt=urn:btih:633ab5b0cc27218bca2f9fec9b68ae4f7cbf0c5f
dn=dmb2017-05-31.dpa4021.flac16

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

xt=urn:btih:9f9165d9a281a9b8e782cd5176bbcc8256fd1871
dn=Ubuntu+16.04.1+LTS+Desktop+64-bit
tr=udp://tracker.leechers-paradise.org:6969
tr=udp://zer0day.ch:1337
tr=udp://open.demonii.com:1337
tr=udp://tracker.coppersurfer.tk:6969
tr=udp://exodus.desync.com:6969

From what I've read, the tracker is used to find peers, from which data is downloaded. How would one download the first torrent then? It has no tracker.

How do I actually do conduct this connection?

The specification has nothing on magnet links, and states that trackers can be used via the HTTP(S) protocols, but these are clearly UDP.

I gave a stab at this:

var PORT = 6969 ;
var HOST = 'tracker.leechers-paradise.org';

var dgram = require('dgram');
var message = new Buffer("xt=urn:btih:9f9165d9a281a9b8e782cd5176bbcc8256fd1871");

var client = dgram.createSocket('udp4');

client.on('listening', function () {
    var address = client.address();
    console.log('UDP Server listening on ' + address.address + ":" + address.port);
});

client.on('message', function (message, remote) {

    console.log(remote.address + ':' + remote.port +' - ' + message);

});

client.send(message, 0, message.length, PORT, HOST, function(err, bytes) {

    if (err) throw err;
    console.log('UDP message sent to ' + HOST +':'+ PORT);
    console.log(bytes);

});

Obviously, this doesn't work, but I can't find any documentation to help.

回答1:

The inofficial Wiki.theory.org/BitTorrentSpecification is without doubt the very best place to start learning about the BitTorrent protocol, but it is incomplete. It only covers the base protocol and the extensions that was developed in the early years. That's why you can't find all the information you need there.

Since 2008 the official* documentation of the protocol can be found at BitTorrent.org.
The official version of the base protocol is the terse and dense BEP3 - The BitTorrent Protocol Specification.

Magnet links is covered in BEP9 - Extension for Peers to Send Metadata Files.
There can you read:

If no tracker is specified, the client SHOULD use the DHT to acquire peers.

The DHT is specified in BEP5 - DHT Protocol.

As you have noticed, trackers nowadays use UDP, that is specified in BEP15 - UDP Tracker Protocol.


footnote: * Official only means that it's run by BitTorrentInc, not that it's superior or the only source to use. The BitTorrent protocol is not governed by authority. None client has pledged allegiance to BEP. The protocol is formed by consensus from what clients in the real do.