forked from Novage/wt-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfast-tracker.ts
447 lines (375 loc) · 14.4 KB
/
fast-tracker.ts
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/**
* Copyright 2019 Novage LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* eslint-disable camelcase */
import * as Debug from "debug";
import { Tracker, PeerContext, TrackerError } from "./tracker";
// eslint-disable-next-line new-cap
const debug = Debug("wt-tracker:fast-tracker");
const debugEnabled = debug.enabled;
interface UnknownObject {
[key: string]: unknown;
}
interface Settings {
maxOffers: number;
announceInterval: number;
}
export class FastTracker implements Tracker {
public readonly settings: Settings;
// eslint-disable-next-line @typescript-eslint/explicit-member-accessibility
readonly #swarms = new Map<string, Swarm>();
// eslint-disable-next-line @typescript-eslint/explicit-member-accessibility
readonly #peers = new Map<string, PeerContext>();
public constructor(settings?: Partial<Settings>) {
this.settings = {
maxOffers: 20,
announceInterval: 120,
...settings,
};
}
public get swarms(): ReadonlyMap<string, { peers: readonly PeerContext[] }> {
return this.#swarms;
}
public processMessage(jsonObject: object, peer: PeerContext): void {
const json = jsonObject as UnknownObject;
const action = json.action;
if (action === "announce") {
const event = json.event;
if (event === undefined) {
if (json.answer === undefined) {
this.processAnnounce(json, peer);
} else {
this.processAnswer(json, peer);
}
} else if (event === "started") {
this.processAnnounce(json, peer);
} else if (event === "stopped") {
this.processStop(json, peer);
} else if (event === "completed") {
this.processAnnounce(json, peer, true);
} else {
throw new TrackerError("unknown announce event");
}
} else if (action === "scrape") {
this.processScrape(json, peer);
} else {
throw new TrackerError("unknown action");
}
}
public disconnectPeer(peer: PeerContext): void {
const peerId = peer.id;
if (peerId === undefined) {
return;
}
if (debugEnabled) {
debug("disconnect peer:", Buffer.from(peerId).toString("hex"));
}
// eslint-disable-next-line guard-for-in
for (const infoHash in peer) {
const swarm = (peer as unknown as UnknownObject)[infoHash];
if (!(swarm instanceof Swarm)) {
continue;
}
swarm.removePeer(peer);
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete (peer as unknown as UnknownObject)[infoHash];
if (debugEnabled) {
debug("disconnect peer: peer", Buffer.from(peerId).toString("hex"), "removed from swarm", Buffer.from(infoHash).toString("hex"));
}
if (swarm.peers.length === 0) {
if (debugEnabled) {
debug("disconnect peer: swarm removed (empty)", Buffer.from(swarm.infoHash).toString("hex"));
}
this.#swarms.delete(swarm.infoHash);
}
}
this.#peers.delete(peerId);
peer.id = undefined;
}
private processAnnounce(json: UnknownObject, peer: PeerContext, completed = false): void {
const infoHash = json.info_hash;
const peerId = json.peer_id;
let swarm: unknown = undefined;
if (peer.id === undefined) {
if (typeof peerId !== "string") {
throw new TrackerError("announce: peer_id field is missing or wrong");
}
peer.id = peerId;
const oldPeer = this.#peers.get(peerId);
if (oldPeer !== undefined) {
this.disconnectPeer(oldPeer);
}
this.#peers.set(peerId, peer);
} else if (peer.id === peerId) {
swarm = (peer as unknown as UnknownObject)[infoHash as string];
} else {
throw new TrackerError("announce: different peer_id on the same connection");
}
const isPeerCompleted = (completed || json.left === 0);
if (swarm === undefined) {
swarm = this.addPeerToSwarm(peer, infoHash, isPeerCompleted);
} else if (swarm instanceof Swarm) {
if (debugEnabled) {
debug(
"announce: peer",
Buffer.from(peer.id).toString("hex"),
"in swarm",
Buffer.from(infoHash as string).toString("hex"),
);
}
if (isPeerCompleted) {
swarm.setCompleted(peer);
}
} else {
throw new TrackerError("announce: illegal info_hash field");
}
peer.sendMessage({
action: "announce",
interval: this.settings.announceInterval,
info_hash: infoHash,
complete: (swarm as Swarm).completedCount,
incomplete: (swarm as Swarm).peers.length - (swarm as Swarm).completedCount,
}, peer);
this.sendOffersToPeers(json, (swarm as Swarm).peers, peer, infoHash as string);
}
private addPeerToSwarm(peer: PeerContext, infoHash: unknown, completed: boolean): Swarm {
let swarm = this.#swarms.get(infoHash as string);
if (swarm === undefined) {
if (typeof infoHash !== "string") {
throw new TrackerError("announce: info_hash field is missing or wrong");
}
if (debugEnabled) {
debug("announce: swarm created:", Buffer.from(infoHash).toString("hex"));
}
swarm = new Swarm(infoHash);
this.#swarms.set(infoHash, swarm);
}
if (debugEnabled) {
debug(
"announce: peer",
Buffer.from(peer.id!).toString("hex"),
"added to swarm",
Buffer.from(infoHash as string).toString("hex"),
);
}
swarm.addPeer(peer, completed);
(peer as unknown as UnknownObject)[infoHash as string] = swarm;
return swarm;
}
private sendOffersToPeers(
json: UnknownObject,
peers: readonly PeerContext[],
peer: PeerContext,
infoHash: string,
): void {
if (peers.length <= 1) {
return;
}
const offers = json.offers;
if (offers === undefined) {
return;
} else if (!(offers instanceof Array)) {
throw new TrackerError("announce: offers field is not an array");
}
const numwant = json.numwant as number;
if (!Number.isInteger(numwant)) {
return;
}
const countPeersToSend = peers.length - 1;
const countOffersToSend = Math.min(countPeersToSend, offers.length, this.settings.maxOffers, numwant);
if (countOffersToSend === countPeersToSend) {
// we have offers for all the peers from the swarm - send offers to all
const offersIterator = (offers as unknown[]).values();
for (const toPeer of peers) {
if (toPeer !== peer) {
sendOffer(offersIterator.next().value, peer.id!, toPeer, infoHash);
}
}
} else {
// send offers to random peers
let peerIndex = Math.floor(Math.random() * peers.length);
for (let i = 0; i < countOffersToSend; i++) {
const toPeer = peers[peerIndex];
if (toPeer === peer) {
i--; // do one more iteration
} else {
sendOffer(offers[i], peer.id!, toPeer, infoHash);
}
peerIndex++;
if (peerIndex === peers.length) {
peerIndex = 0;
}
}
}
debug("announce: sent offers", (countOffersToSend < 0) ? 0 : countOffersToSend);
}
private processAnswer(json: UnknownObject, peer: PeerContext): void {
const toPeerId = json.to_peer_id as string;
const toPeer = this.#peers.get(toPeerId);
if (toPeer === undefined) {
throw new TrackerError("answer: to_peer_id is not in the swarm");
}
json.peer_id = peer.id;
delete json.to_peer_id;
toPeer.sendMessage(json, toPeer);
if (debugEnabled) {
debug(
"answer: from peer",
Buffer.from(peer.id!).toString("hex"),
"to peer",
Buffer.from(toPeerId).toString("hex"),
);
}
}
private processStop(json: UnknownObject, peer: PeerContext): void {
const infoHash = json.info_hash;
const swarm = (peer as unknown as UnknownObject)[infoHash as string];
if (!(swarm instanceof Swarm)) {
debug("stop event: peer not in the swarm");
return;
}
if (debugEnabled) {
debug(
"stop event: peer",
Buffer.from(peer.id!).toString("hex"),
"removed from swarm",
Buffer.from(infoHash as string).toString("hex"),
);
}
swarm.removePeer(peer);
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete (peer as unknown as UnknownObject)[infoHash as string];
if (swarm.peers.length === 0) {
if (debugEnabled) {
debug("stop event: swarm removed (empty)", Buffer.from(infoHash as string).toString("hex"));
}
this.#swarms.delete(infoHash as string);
}
}
private processScrape(json: UnknownObject, peer: PeerContext): void {
const infoHash = json.info_hash;
const files: { [key: string]: { complete: number; incomplete: number; downloaded: number } } = {};
if (infoHash === undefined) {
for (const swarm of this.#swarms.values()) {
files[swarm.infoHash] = {
complete: swarm.completedCount,
incomplete: swarm.peers.length - swarm.completedCount,
downloaded: swarm.completedCount,
};
}
} else if (infoHash instanceof Array) {
for (const singleInfoHash of infoHash as unknown[]) {
const swarm = this.#swarms.get(singleInfoHash as string);
if (swarm !== undefined) {
files[singleInfoHash as string] = {
complete: swarm.completedCount,
incomplete: swarm.peers.length - swarm.completedCount,
downloaded: swarm.completedCount,
};
} else if (typeof singleInfoHash === "string") {
files[singleInfoHash] = {
complete: 0,
incomplete: 0,
downloaded: 0,
};
}
}
} else {
const swarm = this.#swarms.get(infoHash as string);
if (swarm !== undefined) {
files[infoHash as string] = {
complete: swarm.completedCount,
incomplete: swarm.peers.length - swarm.completedCount,
downloaded: swarm.completedCount,
};
} else if (typeof infoHash === "string") {
files[infoHash] = {
complete: 0,
incomplete: 0,
downloaded: 0,
};
}
}
peer.sendMessage({ action: "scrape", files: files }, peer);
}
}
class Swarm {
public completedCount = 0;
// eslint-disable-next-line @typescript-eslint/explicit-member-accessibility
readonly #peers: PeerContext[] = [];
private completedPeers?: Set<string>;
public constructor(public readonly infoHash: string) { }
public addPeer(peer: PeerContext, completed: boolean): void {
this.#peers.push(peer);
if (completed) {
if (this.completedPeers === undefined) {
this.completedPeers = new Set();
}
this.completedPeers.add(peer.id!);
this.completedCount++;
}
}
public removePeer(peer: PeerContext): void {
const index = this.#peers.indexOf(peer);
if (this.completedPeers?.delete(peer.id!) === true) {
this.completedCount--;
}
// Delete peerId from array without calling splice
const last = this.#peers.pop()!;
if (index < this.#peers.length) {
this.#peers[index] = last;
}
}
public setCompleted(peer: PeerContext): void {
if (this.completedPeers === undefined) {
this.completedPeers = new Set();
}
if (!this.completedPeers.has(peer.id!)) {
this.completedPeers.add(peer.id!);
this.completedCount++;
}
}
public get peers(): readonly PeerContext[] {
return this.#peers;
}
}
function sendOffer(
offerItem: unknown,
fromPeerId: string,
toPeer: PeerContext,
infoHash: string,
): void {
if (!(offerItem instanceof Object)) {
throw new TrackerError("announce: wrong offer item format");
}
const offer = (offerItem as UnknownObject).offer;
const offerId = (offerItem as UnknownObject).offer_id;
if (!(offer instanceof Object)) {
throw new TrackerError("announce: wrong offer item field format");
}
toPeer.sendMessage(
{
action: "announce",
info_hash: infoHash,
offer_id: offerId, // offerId is not validated to be a string
peer_id: fromPeerId,
offer: {
type: "offer",
sdp: (offer as UnknownObject).sdp, // offer.sdp is not validated to be a string
},
},
toPeer,
);
}