From 7806daa3c73363bfe512777291d6ddc29a84c267 Mon Sep 17 00:00:00 2001 From: Mathias Buus Date: Tue, 21 Nov 2023 19:59:36 +0100 Subject: [PATCH] fix LAN handshake failure leak --- lib/server.js | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/lib/server.js b/lib/server.js index bda29d6a..156a1f39 100644 --- a/lib/server.js +++ b/lib/server.js @@ -311,7 +311,10 @@ module.exports = class Server extends EventEmitter { return null } - if (this._closing) return null + if (this._closing) { + hs.rawStream.destroy() + return null + } if (this.dht._debugHandshakeLatency !== null) { const [start, end] = this.dht._debugHandshakeLatency @@ -321,6 +324,7 @@ module.exports = class Server extends EventEmitter { const h = handshake.final() if (error !== ERROR.NONE) { + hs.rawStream.destroy() this._clearLater(hs, id, k) return hs } @@ -335,21 +339,25 @@ module.exports = class Server extends EventEmitter { return hs } + const onabort = () => { + if (hs.prepunching) clearTimeout(hs.prepunching) + hs.prepunching = null + hs.rawStream.on('close', () => this._clearLater(hs, id, k)) + if (hs.relayToken === null) hs.rawStream.destroy() + } + if (!direct && clientAddress.host === serverAddress.host) { const clientAddresses = remotePayload.addresses4.filter(onlyPrivateHosts) if (clientAddresses.length > 0 && this._shareLocalAddress) { const myAddresses = await this._localAddresses() const addr = Holepuncher.matchAddress(myAddresses, clientAddresses) - if (addr) return hs - } - } - const onabort = () => { - if (hs.prepunching) clearTimeout(hs.prepunching) - hs.prepunching = null - hs.rawStream.on('close', () => this._clearLater(hs, id, k)) - if (hs.relayToken === null) hs.rawStream.destroy() + if (addr) { + hs.prepunching = setTimeout(onabort, HANDSHAKE_INITIAL_TIMEOUT) + return hs + } + } } if (ourRemoteAddr) {