fix: admin/monitor: some connections had non-normalized ip
Massimo Melina committed
Aug 8, 2022 at 18:04 UTC
70fceb1c537056de5843fbfae03f51b6e8bbf9f4
3 files changed
+5
-5
server/src/api.monitor.ts
+1
-1
@@ -100,7 +100,7 @@ export default apis
100
101
function getConnAddress(conn: Connection) {
102
return {
103
- ip: conn.ctx?.ip || conn.socket.remoteAddress,
103
+ ip: conn.ip,
104
port: conn.socket.remotePort,
105
}
106
}
server/src/block.ts
+2
-2
@@ -1,5 +1,5 @@
1
import { defineConfig } from './config'
2
-import { getConnections } from './connections'
2
+import { getConnections, normalizeIp } from './connections'
3
import { onlyTruthy, with_ } from './misc'
4
import cidr from 'cidr-tools'
5
import _ from 'lodash'
@@ -29,7 +29,7 @@ function compileBlock(rules: any) {
29
}
30
}
31
32
-export function applyBlock(socket: Socket, ip=socket.remoteAddress) {
32
+export function applyBlock(socket: Socket, ip=normalizeIp(socket.remoteAddress||'')) {
33
if (ip && blockFunctions.find(rule => rule(ip)))
34
return socket.destroy()
35
}
server/src/connections.ts
+2
-2
@@ -22,8 +22,8 @@ export class Connection {
22
events.emit('connection', this)
23
}
24
25
- get ip() {
26
- return this.ctx?.ip ?? (this._cachedIp = (this._cachedIp ?? normalizeIp(this.socket.remoteAddress||'')))
25
+ get ip() { // prioritize ctx.ip as it supports proxies, but fallback for when ctx is not yet available
26
+ return this.ctx?.ip || (this._cachedIp ??= normalizeIp(this.socket.remoteAddress||''))
27
}
28
29
get secure() {