From 0dd090d895708f1a912830173bcc47baa9f53fad Mon Sep 17 00:00:00 2001 From: DimaDemchenko Date: Thu, 15 Feb 2024 18:04:37 +0200 Subject: [PATCH 1/2] added peersCountPerInfoHash in stats.json --- lib/run-uws-tracker.ts | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/run-uws-tracker.ts b/lib/run-uws-tracker.ts index 96ed44a..b0957d6 100644 --- a/lib/run-uws-tracker.ts +++ b/lib/run-uws-tracker.ts @@ -21,7 +21,7 @@ import { HttpResponse, HttpRequest } from "uWebSockets.js"; import Debug from "debug"; import { UWebSocketsTracker } from "./uws-tracker.js"; import { FastTracker } from "./fast-tracker.js"; -import { Tracker } from "./tracker.js"; +import { PeerContext, Tracker } from "./tracker.js"; const debugRequests = Debug("wt-tracker:uws-tracker-requests"); const debugRequestsEnabled = debugRequests.enabled; @@ -204,6 +204,11 @@ async function runServers(tracker: Tracker, settings: Settings): Promise { await Promise.all(serverPromises); } +interface ExtendedSwarm { + peers: readonly PeerContext[]; + infoHash: string; +} + function buildServer({ tracker, serverSettings, @@ -236,10 +241,17 @@ function buildServer({ .get("/stats.json", (response: HttpResponse, request: HttpRequest) => { debugRequest(server, request); - const { swarms } = tracker; + const swarms = tracker.swarms; + const peerCountPerInfoHash: Record = {}; + let peersCount = 0; - for (const swarm of swarms.values()) { + for (const swarm of swarms.values() as unknown as ExtendedSwarm[]) { peersCount += swarm.peers.length; + + const infoHashHex = Buffer.from(swarm.infoHash, "binary").toString( + "hex", + ); + peerCountPerInfoHash[infoHashHex] = peersCount; } const serversStats = new Array<{ @@ -260,6 +272,7 @@ function buildServer({ peersCount: peersCount, servers: serversStats, memory: process.memoryUsage(), + peerCountPerInfoHash, }), ); }) From f5504946ad8f1429dc0ff55c984fa391c5757fc8 Mon Sep 17 00:00:00 2001 From: DimaDemchenko Date: Fri, 16 Feb 2024 09:30:04 +0200 Subject: [PATCH 2/2] improvements --- .eslintrc.cjs | 2 ++ lib/fast-tracker.ts | 2 +- lib/run-uws-tracker.ts | 31 ++++++++++++------------------- 3 files changed, 15 insertions(+), 20 deletions(-) diff --git a/.eslintrc.cjs b/.eslintrc.cjs index ea3556f..5cf0ff8 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -8,6 +8,7 @@ module.exports = { sourceType: "module", }, extends: [ + "eslint:recommended", "plugin:@typescript-eslint/eslint-recommended", "plugin:@typescript-eslint/recommended", ], @@ -16,5 +17,6 @@ module.exports = { "@typescript-eslint/prefer-nullish-coalescing": "error", "spaced-comment": ["warn", "always", { markers: ["/"] }], curly: ["warn", "multi-line", "consistent"], + "object-shorthand": ["error", "always"], }, }; diff --git a/lib/fast-tracker.ts b/lib/fast-tracker.ts index c905003..d213e8f 100644 --- a/lib/fast-tracker.ts +++ b/lib/fast-tracker.ts @@ -394,7 +394,7 @@ export class FastTracker implements Tracker { } } - peer.sendMessage({ action: "scrape", files: files }, peer); + peer.sendMessage({ action: "scrape", files }, peer); } } diff --git a/lib/run-uws-tracker.ts b/lib/run-uws-tracker.ts index b0957d6..06666d7 100644 --- a/lib/run-uws-tracker.ts +++ b/lib/run-uws-tracker.ts @@ -21,7 +21,7 @@ import { HttpResponse, HttpRequest } from "uWebSockets.js"; import Debug from "debug"; import { UWebSocketsTracker } from "./uws-tracker.js"; import { FastTracker } from "./fast-tracker.js"; -import { PeerContext, Tracker } from "./tracker.js"; +import { Tracker } from "./tracker.js"; const debugRequests = Debug("wt-tracker:uws-tracker-requests"); const debugRequestsEnabled = debugRequests.enabled; @@ -167,7 +167,7 @@ function validateSettings(jsonSettings: UnknownObject): Settings | undefined { } return { - servers: servers, + servers, tracker: jsonSettings.tracker, websocketsAccess: jsonSettings.websocketsAccess, }; @@ -188,11 +188,11 @@ async function runServers(tracker: Tracker, settings: Settings): Promise { const serverPromises = settings.servers.map(async (serverSettings) => { const server = buildServer({ - tracker: tracker, - serverSettings: serverSettings, + tracker, + serverSettings, websocketsAccess: settings.websocketsAccess, - indexHtml: indexHtml, - servers: servers, + indexHtml, + servers, }); servers.push(server); await server.run(); @@ -204,11 +204,6 @@ async function runServers(tracker: Tracker, settings: Settings): Promise { await Promise.all(serverPromises); } -interface ExtendedSwarm { - peers: readonly PeerContext[]; - infoHash: string; -} - function buildServer({ tracker, serverSettings, @@ -242,16 +237,14 @@ function buildServer({ debugRequest(server, request); const swarms = tracker.swarms; - const peerCountPerInfoHash: Record = {}; + const peersCountPerInfoHash: Record = {}; let peersCount = 0; - for (const swarm of swarms.values() as unknown as ExtendedSwarm[]) { + for (const [infoHash, swarm] of swarms) { peersCount += swarm.peers.length; - const infoHashHex = Buffer.from(swarm.infoHash, "binary").toString( - "hex", - ); - peerCountPerInfoHash[infoHashHex] = peersCount; + const infoHashHex = Buffer.from(infoHash, "binary").toString("hex"); + peersCountPerInfoHash[infoHashHex] = peersCount; } const serversStats = new Array<{ @@ -269,10 +262,10 @@ function buildServer({ response.writeHeader("Content-Type", "application/json").end( JSON.stringify({ torrentsCount: swarms.size, - peersCount: peersCount, + peersCount, servers: serversStats, memory: process.memoryUsage(), - peerCountPerInfoHash, + peersCountPerInfoHash, }), ); })