Skip to content

Commit

Permalink
Log failed connection attempts
Browse files Browse the repository at this point in the history
  • Loading branch information
HDegroote committed May 13, 2024
1 parent c9edca5 commit 7d959fa
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
10 changes: 8 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 17 additions & 6 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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()

0 comments on commit 7d959fa

Please sign in to comment.