forked from webtorrent/bittorrent-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
191 lines (156 loc) · 5.33 KB
/
server.js
File metadata and controls
191 lines (156 loc) · 5.33 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
var Buffer = require('safe-buffer').Buffer
var Client = require('../')
var common = require('./common')
var test = require('tape')
var electronWebrtc = require('electron-webrtc')
var wrtc
var infoHash = '4cb67059ed6bd08362da625b3ae77f6f4a075705'
var peerId = Buffer.from('01234567890123456789')
var peerId2 = Buffer.from('12345678901234567890')
var peerId3 = Buffer.from('23456789012345678901')
function serverTest (t, serverType, serverFamily) {
t.plan(40)
var hostname = serverFamily === 'inet6'
? '[::1]'
: '127.0.0.1'
var clientIp = serverFamily === 'inet6'
? '::1'
: '127.0.0.1'
var opts = {
serverType: serverType
}
common.createServer(t, opts, function (server) {
// Not using announceUrl param from `common.createServer()` since we
// want to control IPv4 vs IPv6.
var port = server[serverType].address().port
var announceUrl = serverType + '://' + hostname + ':' + port + '/announce'
var client1 = new Client({
infoHash: infoHash,
announce: [ announceUrl ],
peerId: peerId,
port: 6881,
wrtc: wrtc
})
if (serverType === 'ws') common.mockWebsocketTracker(client1)
client1.start()
server.once('start', function () {
t.pass('got start message from client1')
})
client1.once('update', function (data) {
t.equal(data.announce, announceUrl)
t.equal(data.complete, 0)
t.equal(data.incomplete, 1)
server.getSwarm(infoHash, function (err, swarm) {
t.error(err)
t.equal(Object.keys(server.torrents).length, 1)
t.equal(swarm.complete, 0)
t.equal(swarm.incomplete, 1)
t.equal(swarm.peers.length, 1)
var id = serverType === 'ws'
? peerId.toString('hex')
: hostname + ':6881'
var peer = swarm.peers.peek(id)
t.equal(peer.type, serverType)
t.equal(peer.ip, clientIp)
t.equal(peer.peerId, peerId.toString('hex'))
t.equal(peer.complete, false)
if (serverType === 'ws') {
t.equal(typeof peer.port, 'number')
t.ok(peer.socket)
} else {
t.equal(peer.port, 6881)
t.notOk(peer.socket)
}
client1.complete()
client1.once('update', function (data) {
t.equal(data.announce, announceUrl)
t.equal(data.complete, 1)
t.equal(data.incomplete, 0)
client1.scrape()
client1.once('scrape', function (data) {
t.equal(data.announce, announceUrl)
t.equal(data.complete, 1)
t.equal(data.incomplete, 0)
t.equal(typeof data.downloaded, 'number')
var client2 = new Client({
infoHash: infoHash,
announce: [ announceUrl ],
peerId: peerId2,
port: 6882,
wrtc: wrtc
})
if (serverType === 'ws') common.mockWebsocketTracker(client2)
client2.start()
server.once('start', function () {
t.pass('got start message from client2')
})
client2.once('update', function (data) {
t.equal(data.announce, announceUrl)
t.equal(data.complete, 1)
t.equal(data.incomplete, 1)
var client3 = new Client({
infoHash: infoHash,
announce: [ announceUrl ],
peerId: peerId3,
port: 6880,
wrtc: wrtc
})
if (serverType === 'ws') common.mockWebsocketTracker(client3)
client3.start()
server.once('start', function () {
t.pass('got start message from client3')
})
client3.once('update', function (data) {
t.equal(data.announce, announceUrl)
t.equal(data.complete, 1)
t.equal(data.incomplete, 2)
client2.stop()
client2.once('update', function (data) {
t.equal(data.announce, announceUrl)
t.equal(data.complete, 1)
t.equal(data.incomplete, 1)
client2.destroy(function () {
t.pass('client2 destroyed')
client3.stop()
client3.once('update', function (data) {
t.equal(data.announce, announceUrl)
t.equal(data.complete, 1)
t.equal(data.incomplete, 0)
client1.destroy(function () {
t.pass('client1 destroyed')
})
client3.destroy(function () {
t.pass('client3 destroyed')
})
server.close(function () {
t.pass('server destroyed')
})
})
})
})
})
})
})
})
})
})
})
}
test('http ipv4 server', function (t) {
serverTest(t, 'http', 'inet')
})
test('http ipv6 server', function (t) {
serverTest(t, 'http', 'inet6')
})
test('udp server', function (t) {
serverTest(t, 'udp', 'inet')
})
test('ws server', function (t) {
wrtc = electronWebrtc()
wrtc.electronDaemon.once('ready', function () {
serverTest(t, 'ws', 'inet')
})
t.once('end', function () {
wrtc.close()
})
})