Skip to content

Commit

Permalink
Merge pull request #1 from HDegroote/log-failed-conn-attempts
Browse files Browse the repository at this point in the history
Log failed connection attempts
  • Loading branch information
HDegroote authored May 13, 2024
2 parents c9edca5 + 2dfcbda commit c6a6b32
Show file tree
Hide file tree
Showing 2 changed files with 24 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
22 changes: 16 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,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()

0 comments on commit c6a6b32

Please sign in to comment.