Skip to content

Commit 9e46be1

Browse files
committed
Scrape extension support
Closes Novage#1
1 parent d41906c commit 9e46be1

2 files changed

Lines changed: 55 additions & 1 deletion

File tree

lib/fast-tracker.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ export class FastTracker implements Tracker {
5252
} else {
5353
throw new TrackerError("unknown announce event");
5454
}
55+
} else if (action === "scrape") {
56+
this.processScrape(json, peer);
5557
} else {
5658
new TrackerError("unknown action");
5759
}
@@ -243,6 +245,58 @@ export class FastTracker implements Tracker {
243245
(peer as InternalPeerContext).swarms = undefined;
244246
}
245247

248+
private processScrape(json: any, peer: PeerContext) {
249+
const infoHash: any = json.info_hash;
250+
const files: {[key: string]: { complete: number, incomplete: number, downloaded: number}} = {};
251+
252+
if (infoHash === undefined) {
253+
for (const swarm of this.swarms.values()) {
254+
files[swarm.infoHash] = {
255+
complete: swarm.completedCount,
256+
incomplete: swarm.peersOrdered.length - swarm.completedCount,
257+
downloaded: swarm.completedCount
258+
};
259+
}
260+
} else if (infoHash instanceof Array) {
261+
for (const singleInfoHash of infoHash) {
262+
const swarm = this.swarms.get(singleInfoHash);
263+
if (swarm !== undefined) {
264+
files[singleInfoHash] = {
265+
complete: swarm.completedCount,
266+
incomplete: swarm.peersOrdered.length - swarm.completedCount,
267+
downloaded: swarm.completedCount
268+
};
269+
} else if (typeof singleInfoHash === "string") {
270+
files[singleInfoHash] = {
271+
complete: 0,
272+
incomplete: 0,
273+
downloaded: 0
274+
};
275+
}
276+
}
277+
} else {
278+
const swarm = this.swarms.get(infoHash);
279+
if (swarm !== undefined) {
280+
files[infoHash] = {
281+
complete: swarm.completedCount,
282+
incomplete: swarm.peersOrdered.length - swarm.completedCount,
283+
downloaded: swarm.completedCount
284+
};
285+
} else if (typeof infoHash === "string") {
286+
files[infoHash] = {
287+
complete: 0,
288+
incomplete: 0,
289+
downloaded: 0
290+
};
291+
}
292+
}
293+
294+
peer.sendMessage({
295+
action: "scrape",
296+
files: files
297+
});
298+
}
299+
246300
public get stats() {
247301
let peersCount = 0;
248302
for (const swarm of this.swarms.values()) {

lib/uws-tracker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export class UWebSocketsTracker {
9292
peer = {
9393
sendMessage: (json: any) => {
9494
ws.send(JSON.stringify(json), false, false);
95-
if (this.logLevel === 2) console.log("out", Buffer.from(peer!.id!).toString("hex"), json);
95+
if (this.logLevel === 2) console.log("out", peer!.id ? Buffer.from(peer!.id).toString("hex") : "unknown peer", json);
9696
}
9797
};
9898
(ws as any).peer = peer;

0 commit comments

Comments
 (0)