diff --git a/index.js b/index.js index a43e29af..3fc7d517 100644 --- a/index.js +++ b/index.js @@ -30,6 +30,7 @@ class HyperDHT extends DHT { this.listening = new Set() this.tracer = createTracer(this) this.connectionKeepAlive = opts.connectionKeepAlive || 0 + this.stats = { punches: { consistent: 0, random: 0, open: 0 } } this._router = new Router(this, router) this._socketPool = new SocketPool(this, opts.host || '0.0.0.0') diff --git a/lib/connect.js b/lib/connect.js index c9b8daf7..0f140ef5 100644 --- a/lib/connect.js +++ b/lib/connect.js @@ -206,6 +206,7 @@ async function holepunch (c, opts) { const addr = getFirstRemoteAddress(payload.addresses4, serverAddress) if (addr) { const socket = c.dht.socket + c.dht.stats.punches.open++ c.onsocket(socket, addr.port, addr.host) return } diff --git a/lib/holepuncher.js b/lib/holepuncher.js index b1adbae9..d285a2f2 100644 --- a/lib/holepuncher.js +++ b/lib/holepuncher.js @@ -182,6 +182,7 @@ module.exports = class Holepuncher { } if (local === FIREWALL.CONSISTENT && remote === FIREWALL.CONSISTENT) { + this.dht.stats.punches.consistent++ this._consistentProbe() return true } @@ -189,12 +190,14 @@ module.exports = class Holepuncher { if (!remoteVerifiedAddress) return false if (local === FIREWALL.CONSISTENT && remote >= FIREWALL.RANDOM) { + this.dht.stats.punches.random++ this._incrementRandomized() this._randomProbes(remoteVerifiedAddress) return true } if (local >= FIREWALL.RANDOM && remote === FIREWALL.CONSISTENT) { + this.dht.stats.punches.random++ this._incrementRandomized() await this._openBirthdaySockets(remoteVerifiedAddress) if (this.punching) this._keepAliveRandomNat(remoteVerifiedAddress) diff --git a/lib/server.js b/lib/server.js index 5c3ecc06..cc898004 100644 --- a/lib/server.js +++ b/lib/server.js @@ -388,6 +388,7 @@ module.exports = class Server extends EventEmitter { if (remotePayload.firewall === FIREWALL.OPEN || direct) { const sock = direct ? socket : this.dht.socket + this.dht.stats.punches.open++ hs.onsocket(sock, clientAddress.port, clientAddress.host) return hs }