Tuesday, July 6, 2010

Info - Bittorrent Tracker (and XPCOM component)

The tracker is a web service that when given an id of a torrent will respond with a list of IP addresses/ports of other peers interested in that torrent.

Torrent Identity
The identity of a torrent (the 'infoHash') is generated from a hash using SHA1 on the section of the .torrent file describing the list of files & checksums. This means that the rest of the .torrent file can be edited, while the torrent identity will remain unchanged. The editing may be the addition or removal of a tracker for the torrent, or some other meta data that itself is not the 'contents' of the torrent.

Finding Trackers
The .torrent file will contain at least one tracker, identified as 'announce'. Additional trackers may appear in the .torrent file as 'announce-list'.

Talking to Trackers
Trackers are given a HTTP GET request with information passed via the query string. The payload in the response is usually a bencoded dictionary listing other peers in the torrent, though this is an optimal response. Sometimes it's a string describing an error such as not accepting requests without compact=1 (the format of the peers list may be a compact string, or a list of dictionaries; the component defaults to compact=1 to avoid this issue). Darn.

Talking to Trackers, again
A subsequent call to a tracker may occur, to get more peers or to update the tracker on progress. The original tracker response may have included a minimum timeout to use before making this follow up call, and also a Tracker Id which should be sent as part of the follow up call.


Implementation of Tracker XPCOM component
IDL for Tracker XPCOM component

Example:
var trackerCaller = Components.
classes["@wikiscraps.com/tracker;1"].
getService(Components.interfaces.btIBittorrentTracker);
// Get a list of peers
trackerCaller.start(
// infohash of http://www.mininova.org/det/3194273
"\x72\xc2\x4e\x55\xc3\x2c\xde\xf7\x23\x78\x12\xf1\x69\x4d\xad\xd1\xe7\x13\x19\x97",
// announceURI
"http://tracker.mininova.org/announce",
"", // tracker Id
"-YY0000-000000000000", // peer id (should've generated & recorded this)
6881, // local bittorrent port
-1, // maximumPeers (-1 means use default)
0, // uploaded bytes
0, // downloaded bytes
0, // remaining bytes...
{
onResponse : function(peers, interval, trackerId) {
// got a list of peers
alert("Look! Peers!\n" +peers);
// the interval before another request to the tracker can be made
alert
("Timeout (in seconds) before next tracker request:\n" +interval);
// the id to use for subsequent calls to the tracker
alert("Tracker's Id:\n" + trackerId);
},
onError : function(serverResponse) {
// darn.
alert("Unknown error, tracker component response was:\n" + serverResponse);
}
});

More information on the .torrent structure (Theory.org) Bittorrent Protocol Specification (BEP0003)
More information on the tracker response (Theory.org) Bittorrent Protocol Spec (BEP0003)
The bencoding XPCOM component.

No comments:

Post a Comment