From fec6d4fa59cc90715dba2868613096d13a744f57 Mon Sep 17 00:00:00 2001 From: Ben Allfree Date: Thu, 28 Nov 2024 00:41:45 -0800 Subject: [PATCH] fix: move stats initialization into proxyservice --- .../pockethost/src/services/ProxyService.ts | 105 +++++++++--------- 1 file changed, 54 insertions(+), 51 deletions(-) diff --git a/packages/pockethost/src/services/ProxyService.ts b/packages/pockethost/src/services/ProxyService.ts index 6ca82326..1e85ee61 100644 --- a/packages/pockethost/src/services/ProxyService.ts +++ b/packages/pockethost/src/services/ProxyService.ts @@ -27,57 +27,6 @@ export type ProxyMiddleware = ( logger: Logger, ) => boolean | Promise -const stats = (() => { - const metrics = { - requests: 0, - errors: 0, - hosts: new Map(), - ips: new Map(), - countries: new Map(), - } - - setInterval(() => { - const top10Ips = Array.from(metrics.ips.entries()) - .sort(([, a], [, b]) => b - a) - .slice(0, 10) - const topHosts = Array.from(metrics.hosts.entries()) - .sort(([, a], [, b]) => b - a) - .slice(0, 10) - const top10Countries = Array.from(metrics.countries.entries()) - .sort(([, a], [, b]) => b - a) - .slice(0, 10) - console.log({ - ...metrics, - ips: top10Ips, - hosts: topHosts, - countries: top10Countries, - }) - metrics.requests = 0 - metrics.errors = 0 - metrics.ips.clear() - metrics.hosts.clear() - metrics.countries.clear() - }, 10000) - - return { - addRequest: () => { - metrics.requests++ - }, - addError: () => { - metrics.errors++ - }, - addHost: (host: string) => { - metrics.hosts.set(host, (metrics.hosts.get(host) || 0) + 1) - }, - addIp: (ip: string) => { - metrics.ips.set(ip, (metrics.ips.get(ip) || 0) + 1) - }, - addCountry: (country: string) => { - metrics.countries.set(country, (metrics.countries.get(country) || 0) + 1) - }, - } -})() - export type ProxyServiceConfig = SingletonBaseConfig & { coreInternalUrl: string } @@ -90,6 +39,60 @@ export const proxyService = mkSingleton( const { coreInternalUrl } = config + const stats = (() => { + const metrics = { + requests: 0, + errors: 0, + hosts: new Map(), + ips: new Map(), + countries: new Map(), + } + + setInterval(() => { + const top10Ips = Array.from(metrics.ips.entries()) + .sort(([, a], [, b]) => b - a) + .slice(0, 10) + const topHosts = Array.from(metrics.hosts.entries()) + .sort(([, a], [, b]) => b - a) + .slice(0, 10) + const top10Countries = Array.from(metrics.countries.entries()) + .sort(([, a], [, b]) => b - a) + .slice(0, 10) + console.log({ + ...metrics, + ips: top10Ips, + hosts: topHosts, + countries: top10Countries, + }) + metrics.requests = 0 + metrics.errors = 0 + metrics.ips.clear() + metrics.hosts.clear() + metrics.countries.clear() + }, 10000) + + return { + addRequest: () => { + metrics.requests++ + }, + addError: () => { + metrics.errors++ + }, + addHost: (host: string) => { + metrics.hosts.set(host, (metrics.hosts.get(host) || 0) + 1) + }, + addIp: (ip: string) => { + metrics.ips.set(ip, (metrics.ips.get(ip) || 0) + 1) + }, + addCountry: (country: string) => { + metrics.countries.set( + country, + (metrics.countries.get(country) || 0) + 1, + ) + }, + } + })() + const proxy = httpProxy.createProxyServer({}) proxy.on('error', (err, req, res, target) => { warn(`Proxy error ${err} on ${req.url} (${req.headers.host})`)