Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log failed connection attempts #1

Merged
merged 2 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Loading