From 7d959faa801ce4a06d997d78a43a29a261689b3a Mon Sep 17 00:00:00 2001 From: HDegroote <75906619+HDegroote@users.noreply.github.com> Date: Mon, 13 May 2024 21:13:30 +0200 Subject: [PATCH 1/2] Log failed connection attempts --- index.js | 10 ++++++++-- server.js | 23 +++++++++++++++++------ 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 889fa8c..d311b00 100644 --- a/index.js +++ b/index.js @@ -13,11 +13,17 @@ class ProxyServer extends ReadyResource { { bootstrap, connectionKeepAlive: keepAlive, seed } ) - const firewall = (remotePublicKey) => { - return !b4a.equals( + const firewall = (remotePublicKey, payload, address) => { + const shouldBlock = !b4a.equals( remotePublicKey, this.dht.defaultKeyPair.publicKey ) + + if (shouldBlock) { + this.emit('firewall-block', { remotePublicKey, payload, address }) + } + + return shouldBlock } this.connectionCounter = 0 diff --git a/server.js b/server.js index a7786b3..d9bb80f 100755 --- a/server.js +++ b/server.js @@ -55,12 +55,7 @@ async function main () { const proxy = new ProxyServer( seed, port, host, { logger, keepAlive, bootstrap } ) - proxy.on('connection', ({ id, remotePublicKey }) => { - logger.info(`Opened connection ${id} with ${b4a.toString(remotePublicKey, 'hex')}`) - }) - proxy.on('connection-close', ({ id, remotePublicKey }) => { - logger.info(`Closed connection ${id} with ${b4a.toString(remotePublicKey, 'hex')}`) - }) + setupLogging(proxy, logger) goodbye(async () => { logger.info('Shutting down') @@ -77,4 +72,20 @@ async function main () { logger.info(`Address: ${address.host}:${address.port}`) } +function setupLogging (proxy, logger) { + proxy.on('connection', ({ id, remotePublicKey }) => { + logger.info(`Opened connection ${id} with ${b4a.toString(remotePublicKey, 'hex')}`) + }) + + proxy.on('connection-close', ({ id, remotePublicKey }) => { + logger.info(`Closed connection ${id} with ${b4a.toString(remotePublicKey, 'hex')}`) + }) + + proxy.on('firewall-block', ({ remotePublicKey, address }) => { + // TODO: consider rate limiting + const src = `${address?.host}:${address?.port}` + const pubKey = b4a.toString(remotePublicKey, 'hex') + logger.info(`Firewall blocked connection attempt from ${src} (public key ${pubKey})`) + }) +} main() From 2dfcbdaa3d8782a95374db688942657ae3751496 Mon Sep 17 00:00:00 2001 From: HDegroote <75906619+HDegroote@users.noreply.github.com> Date: Mon, 13 May 2024 21:16:26 +0200 Subject: [PATCH 2/2] Rm unneeded comment --- server.js | 1 - 1 file changed, 1 deletion(-) diff --git a/server.js b/server.js index d9bb80f..aa7d68f 100755 --- a/server.js +++ b/server.js @@ -82,7 +82,6 @@ function setupLogging (proxy, logger) { }) proxy.on('firewall-block', ({ remotePublicKey, address }) => { - // TODO: consider rate limiting const src = `${address?.host}:${address?.port}` const pubKey = b4a.toString(remotePublicKey, 'hex') logger.info(`Firewall blocked connection attempt from ${src} (public key ${pubKey})`)