forked from Novage/wt-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfast-tracker.ts
More file actions
542 lines (465 loc) · 14.1 KB
/
fast-tracker.ts
File metadata and controls
542 lines (465 loc) · 14.1 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
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/**
* 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.
*/
import { threadId } from "node:worker_threads";
import Debug from "debug";
import type { Tracker } from "./tracker.ts";
import { TrackerError } from "./tracker.ts";
const debugSuffix = threadId ? `-${threadId}` : "";
const debug = Debug(`wt-tracker:fast-tracker${debugSuffix}`);
const debugEnabled = debug.enabled;
interface Swarm<ConnectionContext extends Record<string, unknown>> {
infoHash: string;
completedPeers?: Set<string>;
peers: PeerContext<ConnectionContext>[];
}
interface PeerContext<ConnectionContext extends Record<string, unknown>> {
peerId: string;
connection: ConnectionContext;
lastAccessed: number;
swarm: Swarm<ConnectionContext>;
}
type UnknownObject = Record<string, unknown>;
export type FastTrackerSettings = {
maxOffers: number;
announceInterval: number;
};
export class FastTracker<ConnectionContext extends Record<string, unknown>>
implements Tracker<ConnectionContext>
{
public readonly settings: FastTrackerSettings;
#sendMessage: (json: UnknownObject, connection: ConnectionContext) => void;
readonly #swarms = new Map<string, Swarm<ConnectionContext>>();
public get swarms() {
return this.#swarms;
}
public getSwarms() {
const result = [];
for (const swarm of this.#swarms.values()) {
result.push({
infoHash: swarm.infoHash,
peersCount: swarm.peers.length,
});
}
return Promise.resolve([result]);
}
readonly #peers = new Map<string, PeerContext<ConnectionContext>>();
public get peers() {
return this.#peers;
}
#clearPeersInterval?: NodeJS.Timeout;
#onRemovePeer?: (peerId: string, connection: ConnectionContext) => void;
set onRemovePeer(
callback:
| ((peerId: string, connection: ConnectionContext) => void)
| undefined,
) {
this.#onRemovePeer = callback ?? undefined;
}
public constructor(
settings: Partial<FastTrackerSettings> | undefined,
sendMessage: (json: UnknownObject, connection: ConnectionContext) => void,
) {
this.#sendMessage = sendMessage;
this.settings = {
maxOffers: 20,
announceInterval: 20,
...settings,
};
this.startClearPeersInterval();
}
private getOrCreateSwarm(infoHash: string) {
let swarm = this.#swarms.get(infoHash);
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 = {
infoHash,
peers: [],
};
this.#swarms.set(infoHash, swarm);
}
return swarm;
}
private addPeerToSwarm(
swarm: Swarm<ConnectionContext>,
peer: PeerContext<ConnectionContext>,
isPeerCompleted: boolean,
) {
swarm.peers.push(peer);
if (isPeerCompleted) {
swarm.completedPeers ??= new Set();
swarm.completedPeers.add(peer.peerId);
}
}
private removePeerFromSwarm(
swarm: Swarm<ConnectionContext>,
peer: PeerContext<ConnectionContext>,
) {
const peerIndex = swarm.peers.indexOf(peer);
swarm.completedPeers?.delete(peer.peerId);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const lastPeer = swarm.peers.pop()!;
if (peerIndex < swarm.peers.length) {
swarm.peers[peerIndex] = lastPeer;
}
if (swarm.peers.length === 0) {
if (debugEnabled) {
debug(
"disconnect peer: swarm removed (empty)",
Buffer.from(swarm.infoHash).toString("hex"),
);
}
this.#swarms.delete(swarm.infoHash);
}
}
private setPeerCompletedInSwarm(
swarm: Swarm<ConnectionContext>,
peer: PeerContext<ConnectionContext>,
) {
swarm.completedPeers ??= new Set();
swarm.completedPeers.add(peer.peerId);
}
private startClearPeersInterval(): void {
this.#clearPeersInterval = setInterval(() => {
const now = performance.now();
for (const peer of this.#peers.values()) {
if (
now - peer.lastAccessed >
this.settings.announceInterval * 2 * 1000
) {
if (debugEnabled) {
debug(
"remove by timeout peer:",
Buffer.from(peer.peerId).toString("hex"),
"swarm:",
Buffer.from(peer.swarm.infoHash).toString("hex"),
);
}
this.removePeer(peer);
}
}
}, this.settings.announceInterval * 1000);
}
private removePeer(peer: PeerContext<ConnectionContext>) {
const { swarm } = peer;
this.removePeerFromSwarm(swarm, peer);
this.#peers.delete(peer.peerId);
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete peer.connection[peer.peerId];
this.#onRemovePeer?.(peer.peerId, peer.connection);
}
public processMessage(
json: UnknownObject,
connection: ConnectionContext,
): void {
const { action } = json;
if (action === "announce") {
const { event } = json;
if (event === undefined) {
if (json.answer === undefined) {
this.processAnnounce(json, connection);
} else {
this.processAnswer(json);
}
} else if (event === "started") {
this.processAnnounce(json, connection);
} else if (event === "stopped") {
this.processStop(json);
} else if (event === "completed") {
this.processAnnounce(json, connection, true);
} else {
throw new TrackerError("unknown announce event");
}
} else if (action === "scrape") {
this.processScrape(json, connection);
} else {
throw new TrackerError("unknown action");
}
}
public disconnect(connection: ConnectionContext): void {
// Connection closed - remove all peers
for (const peerId in connection) {
const peer = connection[peerId] as
| PeerContext<ConnectionContext>
| undefined;
if (!peer?.peerId) continue; // Not a peer property
if (debugEnabled) {
debug(
"disconnect peer:",
Buffer.from(peer.peerId).toString("hex"),
"swarm:",
Buffer.from(peer.swarm.infoHash).toString("hex"),
);
}
this.removePeer(peer);
}
}
private processAnnounce(
json: UnknownObject,
connection: ConnectionContext,
completed = false,
): void {
const infoHash = json.info_hash as string;
const peerId = json.peer_id as string;
let swarm: Swarm<ConnectionContext> | undefined;
const isPeerCompleted = completed || json.left === 0;
let peer = connection[peerId] as PeerContext<ConnectionContext> | undefined;
if (peer === undefined) {
const existingPeer = this.#peers.get(peerId);
if (existingPeer) {
// The peer previously was on a different connection
if (debugEnabled) {
debug(
"peer changed connection:",
Buffer.from(existingPeer.peerId).toString("hex"),
);
}
this.removePeer(existingPeer);
}
swarm = this.getOrCreateSwarm(infoHash);
peer = {
peerId,
connection,
lastAccessed: performance.now(),
swarm,
};
this.addPeerToSwarm(swarm, peer, isPeerCompleted);
(connection as unknown as UnknownObject)[peerId] = peer;
this.#peers.set(peerId, peer);
} else if (peer.peerId === peerId) {
peer.lastAccessed = performance.now();
if (infoHash !== peer.swarm.infoHash) {
// Peer changes swarm
const oldSwarm = peer.swarm;
this.removePeerFromSwarm(oldSwarm, peer);
swarm = this.getOrCreateSwarm(infoHash);
peer.swarm = swarm;
this.addPeerToSwarm(swarm, peer, isPeerCompleted);
} else {
({ swarm } = peer);
if (isPeerCompleted) {
this.setPeerCompletedInSwarm(swarm, peer);
}
}
} else {
throw new TrackerError("announce: peerId mismatch");
}
const complete = swarm.completedPeers?.size ?? 0;
this.#sendMessage(
{
action: "announce",
interval: this.settings.announceInterval,
info_hash: infoHash,
complete,
incomplete: swarm.peers.length - complete,
},
connection,
);
this.sendOffersToPeers(json, swarm.peers, peer, infoHash);
}
private sendOffersToPeers(
json: UnknownObject,
peers: readonly PeerContext<ConnectionContext>[],
peer: PeerContext<ConnectionContext>,
infoHash: string,
): void {
if (peers.length <= 1) {
return;
}
const { offers } = json;
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.values();
for (const toPeer of peers) {
if (toPeer !== peer) {
this.#sendMessage(
getSendOfferJson(
offersIterator.next().value,
peer.peerId,
infoHash,
),
toPeer.connection,
);
}
}
} 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 {
this.#sendMessage(
getSendOfferJson(offers[i], peer.peerId, infoHash),
toPeer.connection,
);
}
peerIndex++;
if (peerIndex === peers.length) {
peerIndex = 0;
}
}
}
if (debugEnabled) {
debug(
"announce: sent offers",
countOffersToSend < 0 ? 0 : countOffersToSend,
);
}
}
private processAnswer(json: UnknownObject): 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");
}
delete json.to_peer_id;
this.#sendMessage(json, toPeer.connection);
if (debugEnabled) {
debug(
"answer: from peer",
Buffer.from(json.peer_id as string).toString("hex"),
"to peer",
Buffer.from(toPeerId).toString("hex"),
);
}
}
private processStop(json: UnknownObject): void {
const peerId = json.peer_id as string;
const peer = this.#peers.get(peerId);
if (peer) {
if (debugEnabled) {
debug(
"stop peer:",
Buffer.from(peer.peerId).toString("hex"),
"swarm:",
Buffer.from(peer.swarm.infoHash).toString("hex"),
);
}
this.removePeer(peer);
}
}
private processScrape(
json: UnknownObject,
connection: ConnectionContext,
): void {
const infoHash = json.info_hash;
const files: Record<
string,
{
complete: number;
incomplete: number;
downloaded: number;
}
> = {};
if (infoHash === undefined) {
for (const swarm of this.#swarms.values()) {
const complete = swarm.completedPeers?.size ?? 0;
files[swarm.infoHash] = {
complete,
incomplete: swarm.peers.length - complete,
downloaded: complete,
};
}
} else if (infoHash instanceof Array) {
for (const singleInfoHash of infoHash as unknown[]) {
const swarm = this.#swarms.get(singleInfoHash as string);
if (swarm !== undefined) {
const complete = swarm.completedPeers?.size ?? 0;
files[singleInfoHash as string] = {
complete,
incomplete: swarm.peers.length - complete,
downloaded: complete,
};
} 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) {
const complete = swarm.completedPeers?.size ?? 0;
files[infoHash as string] = {
complete,
incomplete: swarm.peers.length - complete,
downloaded: complete,
};
} else if (typeof infoHash === "string") {
files[infoHash] = {
complete: 0,
incomplete: 0,
downloaded: 0,
};
}
}
this.#sendMessage({ action: "scrape", files }, connection);
}
public dispose() {
clearInterval(this.#clearPeersInterval);
}
}
function getSendOfferJson(
offerItem: unknown,
fromPeerId: string,
infoHash: string,
) {
if (!(offerItem instanceof Object)) {
throw new TrackerError("announce: wrong offer item format");
}
const { offer } = offerItem as UnknownObject;
const offerId = (offerItem as UnknownObject).offer_id;
if (!(offer instanceof Object)) {
throw new TrackerError("announce: wrong offer item field format");
}
return {
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
},
};
}