Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bittorrent tracker seeder and leecher in nodejs

I need to set up a sample bittorrent tracker, seeder and leecher in nodejs. I have written all code but it is not working and I don't know why. I booted up a tracker using bittorrent-tracker, wrote the torrent file using nt, connected as a seeder to the tracker using bittorrent-tracker as well (bt-tracker has both client and server).

Finally I launched another client that only had the torrent file and connected to the tracker. I am able to see the files in the torrent (in the downloading/leecher client). But the file download itself won't start.


Code being used: // Tracker:

var Server = require('bittorrent-tracker').Server
var port=6881

var server = new Server({
  udp: true, // enable udp server? [default=true]
  http: true // enable http server? [default=true]
})

server.on('error', function (err) {
  // fatal server error!
  console.log(err.message)
})

server.on('warning', function (err) {
  // client sent bad data. probably not a problem, just a buggy client.

  console.log(err.message)
})

server.on('listening', function () {
  console.log('tracker server is listening!')
})

// start tracker server listening!
server.listen(port)

// listen for individual tracker messages from peers:

server.on('start', function (addr, params) {
  console.log('got start message from ' + addr)
  console.log('params in the message: ' + JSON.stringify(params))
})

server.on('complete', function (addr, params) {})
server.on('update', function (addr, params) {})
server.on('stop', function (addr, params) {})

// get info hashes for all torrents in the tracker server
console.log(Object.keys(server.torrents))

// Code for torrent file writer and seeder

var nt=require('nt');
var fs=require('fs');

//var rs=nt.make('udp://tracker.publicbt.com:80');
//rs.pipe(fs.createWriteStream('param.torrent'));

function postWrite(){
  var cl=require('bittorrent-tracker').Client;
  var parseTorrent=require('parse-torrent');
  var torrent=fs.readFileSync(__dirname + '/param.torrent');
  var parsedTorrent=parseTorrent(torrent);
  console.log(parsedTorrent);

  var peerId = new Buffer('81276382172123141133')
  var port = 6882

  var client = new cl(peerId, port, parsedTorrent)

  client.on('error', function (err) {
    console.log(err.message)
    // a tracker was unavailable or sent bad data to the client. you can probably ignore it
  })

  client.start()

  client.on('update', function (data) {
    console.log('got an announce response from tracker: ' + data.announce)
    console.log('number of seeders in the swarm: ' + data.complete)
    console.log('number of leechers in the swarm: ' + data.incomplete)
  })

  client.once('peer', function (addr) {
    console.log('found a peer: ' + addr) // 85.10.239.191:48623
  })

  // announce that download has completed (and you are now a seeder)
  client.complete();

  client.update()
}

function writeTorrentFile() {
  nt.makeWrite('param.torrent', 'udp://hola.127.0.0.1.xip.io:6881', '/Users/param/personal/nodejs/uploader/files', 
  //  ['hello-world.txt'], function(err, torrent){
    ['hello-world.txt'], {}, function(err, torrent){
      console.log(err);
      console.log(torrent);
      nt.read('param.torrent', function(err, torrent) {
        if (err) throw err;
        console.log('Info hash:', torrent.metadata.info);
      });

      postWrite();
    });
}
writeTorrentFile();

// Code for leecher

var BitTorrentClient = require('bittorrent-client');
var fs = require('fs');

var file = fs.readFileSync(__dirname + '/param.torrent')

var client = BitTorrentClient({
  maxPeers: 100,          // Max number of peers to connect to (per torrent)
  path: __dirname, // Where to save the torrent file data
  dht: true,              // Whether or not to enable DHT
  verify: true            // Verify previously stored data before starting
});

client.add(file);

client.on('torrent', function (torrent) {
  // torrent metadata has been fetched
  console.log(torrent.name)

  torrent.files.forEach(function (file) {
    console.log("selecting "+file.name+" for download");
    console.log(file.path)
    st=file.createReadStream()
    st.on('data', function(chunk){
      console.log(chunk)
    });
  })
})

The data event on leecher is never called - even though it goes into the files loop of the torrent!

like image 880
Param Avatar asked Jun 18 '14 23:06

Param


2 Answers

You need to use an actual torrent client to seed. Right now, you're just using bittorrent-tracker which just tells the tracker server that you're a seeder, but doesn't actually contain any code to send files to peers, and in fact, doesn't even listen on any ports. To actually seed, you should use a complete torrent client.

In your example, you're already using bittorrent-client (authored by me), but I suggest you move to using webtorrent since I deprecated bittorrent-client a while ago.

Here's some code to seed files:

var WebTorrent = require('webtorrent')
var client = new WebTorrent()
client.seed('/path/to/file', function (torrent) {
  console.log('Client is seeding:', torrent.magnetUri)
})

Here are full docs on client.seed: https://github.com/feross/webtorrent/blob/master/docs/api.md#clientseedinput-opts-function-onseed-torrent-

like image 88
Feross Avatar answered Nov 18 '22 09:11

Feross


You want to pass a filename to createReadStream() I think. In any case, check if the 'error' event is emitted.

// ...
var st = file.createReadStream(file)
st.on('data', console.log);
st.on('error', console.error);
like image 1
wires Avatar answered Nov 18 '22 11:11

wires