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..aa7d68f 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,19 @@ 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 }) => { + const src = `${address?.host}:${address?.port}` + const pubKey = b4a.toString(remotePublicKey, 'hex') + logger.info(`Firewall blocked connection attempt from ${src} (public key ${pubKey})`) + }) +} main()