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

blocklist-tracker.js « node « test - github.com/webtorrent/webtorrent.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 080f36229540c5791e8c3af9dfbc5b5e253a5a84 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
const fixtures = require('webtorrent-fixtures')
const series = require('run-series')
const test = require('tape')
const TrackerServer = require('bittorrent-tracker/server')
const WebTorrent = require('../../index.js')
const common = require('../common.js')

test('blocklist blocks peers discovered via tracker', t => {
  t.plan(9)

  const parsedTorrent = Object.assign({}, fixtures.leaves.parsedTorrent)
  let tracker, client1, client2

  series([
    cb => {
      tracker = new TrackerServer({ udp: false, ws: false })

      tracker.listen(() => {
        const port = tracker.http.address().port
        const announceUrl = `http://127.0.0.1:${port}/announce`

        // Overwrite announce with our local tracker
        parsedTorrent.announce = announceUrl

        cb(null)
      })

      tracker.once('start', () => {
        t.pass('client1 connected to tracker')

        tracker.once('start', () => {
          t.pass('client2 connected to tracker')
        })
      })
    },

    cb => {
      client1 = new WebTorrent({ dht: false, lsd: false })
      client1.on('error', err => { t.fail(err) })
      client1.on('warning', err => { t.fail(err) })

      const torrent1 = client1.add(parsedTorrent, {
        path: common.getDownloadPath('client_1', parsedTorrent.infoHash)
      })

      torrent1.on('invalidPeer', () => {
        t.pass('client1 found itself')
        cb(null)
      })

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

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

      const torrent2 = client2.add(parsedTorrent, {
        path: common.getDownloadPath('client_2', parsedTorrent.infoHash)
      })

      torrent2.once('blockedPeer', () => {
        t.pass('client2 blocked first peer')

        torrent2.once('blockedPeer', () => {
          t.pass('client2 blocked second peer')
          cb(null)
        })
      })

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

  ], err => {
    t.error(err)
    tracker.close(() => {
      t.pass('tracker closed')
    })
    client1.destroy(err => {
      t.error(err, 'client1 destroyed')
    })
    client2.destroy(err => {
      t.error(err, 'client2 destroyed')
    })
  })
})