Skip to content

Commit

Permalink
filter out bad messages and fix wrap not fwding
Browse files Browse the repository at this point in the history
  • Loading branch information
mafintosh committed Sep 15, 2021
1 parent 9503e88 commit 57a678a
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions lib/holepuncher.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ module.exports = class Holepuncher {
this.destroyed = false
this.punching = false

const self = this

this.onmessagebound = function (buf, rinfo) {
if (buf.byteLength > 1 && this === self.socket) dht.onmessage(this, buf, rinfo)
else self.onmessage(this, buf, rinfo)
}

this.reset()
}

Expand All @@ -120,14 +127,8 @@ module.exports = class Holepuncher {
}

const dht = this.dht
const self = this

this.socket = this._makeSocket()
this.socket.on('message', function (buf, rinfo) {
if (buf.byteLength > 1) dht.onmessage(this, buf, rinfo)
else self.onmessage(this, buf, rinfo)
})

this.allSockets.push(this.socket)
this.nat = new Nat(this.dht, this.socket)

Expand All @@ -137,6 +138,10 @@ module.exports = class Holepuncher {

// Note that this never throws so it is safe to run in the background
async onmessage (socket, buf, rinfo) {
// try to filter out spoofed messages
if (rinfo.address !== this.remoteAddress.host) return
if (this.remoteAddress.port && rinfo.port !== this.remoteAddress.port) return

if (!this.punching) return
this.punching = false

Expand Down Expand Up @@ -227,7 +232,9 @@ module.exports = class Holepuncher {
_makeSocket () {
const socket = utp()
socket.bind(0)
return new SocketWrap(socket)
const wrap = new SocketWrap(socket)
wrap.on('message', this.onmessagebound)
return wrap
}

ping (addr, socket = this.socket) {
Expand Down

0 comments on commit 57a678a

Please sign in to comment.