fix: admin/monitoring: wrong connections status when using proxy

Massimo Melina committed Jun 21, 2025 at 00:04 UTC 65f335adc528deedcd435dd4e1cf3aed08156063
3 files changed +10 -3
src/api.monitor.ts
+5 -2
@@ -38,6 +38,9 @@ export default {
38 if (ignore(conn)) return
39 list.remove(getConnAddress(conn))
40 },
41 + connectionNewIp(conn: Connection, oldIp: string, newIp: string) {
42 + list.update(getConnAddress(conn, oldIp), { ip: newIp })
43 + },
44 connectionUpdated(conn: Connection, change: Change) {
45 if (conn.socket.closed || ignore(conn) || ignore(change as any) || _.isEmpty(change)) return
46 if (change.ctx) {
@@ -109,9 +112,9 @@ function ignore(conn: Connection) {
112 return false //conn.socket && isLocalHost(conn)
113 }
114
112 -function getConnAddress(conn: Connection) {
115 +function getConnAddress(conn: Connection, overrideIp?: string) {
116 return {
114 - ip: conn.ip,
117 + ip: overrideIp ?? conn.ip,
118 port: conn.socket.remotePort,
119 }
120 }
src/connections.ts
+4
@@ -28,6 +28,10 @@ export class Connection {
28 }
29
30 get ip() { // prioritize ctx.ip as it supports proxies, but fallback for when ctx is not yet available
31 + if (this._cachedIp && this.ctx && this._cachedIp !== this.ctx.ip) {
32 + events.emit('connectionNewIp', this, this._cachedIp, this.ctx.ip)
33 + this._cachedIp = undefined
34 + }
35 return this.ctx?.ip || (this._cachedIp ??= normalizeIp(this.socket.remoteAddress||''))
36 }
37
src/throttler.ts
+1 -1
@@ -41,7 +41,7 @@ export const throttler: Koa.Middleware = async (ctx, next) => {
41 // we wrap the stream also for unlimited connections to get speed and other features
42 const noLimit = ctx.state.account?.ignore_limits || isLocalHost(ctx)
43 const ipGroup = ip2group[noLimit ? '' : ctx.ip] ||= {
44 - count:0,
44 + count: 0,
45 group: new ThrottleGroup(noLimit ? Infinity : maxKbpsPerIp.get(), noLimit ? undefined : mainThrottleGroup),
46 }
47 const conn = getConnection(ctx)