mirror of
https://github.com/pockethost/pockethost.git
synced 2025-11-24 06:25:48 +00:00
fix: move stats initialization into proxyservice
This commit is contained in:
parent
00f3a4dc0a
commit
fec6d4fa59
@ -27,57 +27,6 @@ export type ProxyMiddleware = (
|
||||
logger: Logger,
|
||||
) => boolean | Promise<boolean>
|
||||
|
||||
const stats = (() => {
|
||||
const metrics = {
|
||||
requests: 0,
|
||||
errors: 0,
|
||||
hosts: new Map<string, number>(),
|
||||
ips: new Map<string, number>(),
|
||||
countries: new Map<string, number>(),
|
||||
}
|
||||
|
||||
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<string, number>(),
|
||||
ips: new Map<string, number>(),
|
||||
countries: new Map<string, number>(),
|
||||
}
|
||||
|
||||
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})`)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user