dev: log disconnection reason
Massimo Melina committed
Feb 16, 2024 at 19:43 UTC
1ea64a8cee256fa80a41f101a1f0056fcb7b0a2e
5 files changed
+7
-5
src/block.ts
+1
-1
@@ -22,7 +22,7 @@ const block = defineConfig('block', [] as BlockingRule[], rules => {
22
23
export function applyBlock(socket: Socket, ip=normalizeIp(socket.remoteAddress||'')) {
24
if (ip && block.compiled().find(rule => rule(ip)))
25
- return disconnect(socket)
25
+ return disconnect(socket, 'block-ip')
26
}
27
28
setInterval(() => { // twice a minute, check if any block has expired
src/connections.ts
+3
-1
@@ -74,8 +74,10 @@ export function updateConnection(conn: Connection, change: Partial<Connection>,
74
events.emit('connectionUpdated', conn, change)
75
}
76
77
-export function disconnect(what: Context | Socket) {
77
+export function disconnect(what: Context | Socket, debugLog='') {
78
if ('socket' in what)
79
what = what.socket
80
+ if (debugLog)
81
+ console.debug("disconnection:", debugLog, what.remoteAddress)
82
return what.destroy()
83
}
\ No newline at end of file
src/geo.ts
+1
-1
@@ -24,7 +24,7 @@ export const geoFilter: Middleware = async (ctx, next) => {
24
updateConnection(connection, { country })
25
if (!ctx.state.skipFilters && allow.get() !== null)
26
if (country ? list.get().includes(country) !== allow.get() : !allowUnknown.get())
27
- return disconnect(ctx)
27
+ return disconnect(ctx, 'geo-filter')
28
}
29
await next()
30
}
src/middlewares.ts
+1
-1
@@ -77,7 +77,7 @@ export const someSecurity: Koa.Middleware = async (ctx, next) => {
77
return ctx.status = HTTP_FOOL
78
}
79
if (!ctx.state.skipFilters && forceBaseUrl.get() && !isLocalHost(ctx) && ctx.host !== baseUrl.compiled())
80
- return disconnect(ctx)
80
+ return disconnect(ctx, 'force-domain')
81
if (!ctx.secure && forceHttps.get() && getHttpsWorkingPort() && !isLocalHost(ctx)) {
82
const { URL } = ctx
83
URL.protocol = 'https'
src/roots.ts
+1
-1
@@ -33,7 +33,7 @@ export const rootsMiddleware: Koa.Middleware = (ctx, next) =>
33
if (root === '' || root === '/') return
34
if (root === undefined) {
35
if (ctx.state.skipFilters || !rootsMandatory.get() || isLocalHost(ctx)) return
36
- disconnect(ctx)
36
+ disconnect(ctx, 'no-root')
37
return true // true will avoid calling next
38
}
39
if (!params) {