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

fix LAN handshake failure leak #152

Merged
merged 1 commit into from
Dec 1, 2023
Merged
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
26 changes: 17 additions & 9 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -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) {
Expand Down