Welcome to mirror list, hosted at ThFree Co, Russian Federation.

blocklist-tracker.js « test - github.com/webtorrent/webtorrent.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e60d02d958732182baa9e8b1cb9c63cc73d4ccff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
var auto = require('run-auto')
var fs = require('fs')
var parseTorrent = require('parse-torrent')
var test = require('tape')
var TrackerServer = require('bittorrent-tracker/server')
var WebTorrent = require('../')

var leavesTorrent = fs.readFileSync(__dirname + '/torrents/leaves.torrent')
var leavesParsed = parseTorrent(leavesTorrent)

test('blocklist blocks peers discovered via tracker', function (t) {
  t.plan(8)

  auto({
    tracker: function (cb) {
      var tracker = new TrackerServer({ udp: false })

      tracker.listen(function () {
        var port = tracker.http.address().port
        var announceUrl = 'http://127.0.0.1:' + port + '/announce'

        // Overwrite announce with our local tracker
        leavesParsed.announce = [ announceUrl ]
        leavesParsed.announceList = [[ announceUrl ]]

        cb(null, tracker)
      })

      tracker.on('start', function () {
        t.pass('client connected to tracker') // 2x, once for each client
      })
    },

    client1: ['tracker', function (cb) {
      var client1 = new WebTorrent({ dht: false })
      client1.on('error', function (err) { t.fail(err) })

      var torrent1 = client1.add(leavesParsed)

      torrent1.on('peer', function () {
        t.pass('client1 found itself')
        cb(null, client1)
      })

      torrent1.on('blockedPeer', function () {
        t.fail('client1 should not block any peers')
      })
    }],

    client2: ['client1', function (cb) {
      var client2 = new WebTorrent({
        dht: false,
        blocklist: [ '127.0.0.1' ]
      })
      client2.on('error', function (err) { t.fail(err) })

      var torrent2 = client2.add(leavesParsed)

      torrent2.on('blockedPeer', function () {
        t.pass('client2 blocked connection') // 2x, once for each client
        cb(null, client2)
      })

      torrent2.on('peer', function () {
        t.fail('client2 should not find any peers')
      })
    }]

  }, function (err, r) {
    if (err) throw err

    r.tracker.close(function () {
      t.pass('tracker closed')
    })
    r.client1.destroy(function () {
      t.pass('client1 destroyed')
    })
    r.client2.destroy(function () {
      t.pass('client2 destroyed')
    })
  })
})