From 88f57d85c12b7a8fb9b068d9b7409da77da7895b Mon Sep 17 00:00:00 2001 From: Mathias Buus Date: Tue, 11 May 2021 12:40:05 +0200 Subject: [PATCH] add NAT_OPEN enum --- index.js | 7 +++++-- test.js | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index b519426..b0ba7a6 100644 --- a/index.js +++ b/index.js @@ -303,7 +303,7 @@ class DHT extends EventEmitter { const addr = this.remoteAddress(this._forcePersistent ? 1 : 3) - if (addr.type !== NatAnalyzer.PORT_CONSISTENT) { + if (addr.type !== DHT.NAT_PORT_CONSISTENT && addr.type !== DHT.NAT_OPEN) { this._persistentTicks = MORE_PERSISTENT_TICKS return false } @@ -533,7 +533,9 @@ class DHT extends EventEmitter { } remoteAddress (minSamples) { - return this._nat.analyze(minSamples) + const result = this._nat.analyze(minSamples) + if (result.type === NatAnalyzer.PORT_CONSISTENT && !this.ephemeral) result.type = DHT.NAT_OPEN + return result } _reply (tid, target, status, value, to, addToken, socket = this.rpc.socket) { @@ -565,6 +567,7 @@ DHT.NAT_UNKNOWN = NatAnalyzer.UNKNOWN DHT.NAT_PORT_CONSISTENT = NatAnalyzer.PORT_CONSISTENT DHT.NAT_PORT_INCREMENTING = NatAnalyzer.PORT_INCREMENTING DHT.NAT_PORT_RANDOMIZED = NatAnalyzer.PORT_RANDOMIZED +DHT.NAT_OPEN = Symbol.for('NAT_OPEN') // implies PORT_CONSISTENT module.exports = DHT diff --git a/test.js b/test.js index f55e8bd..5e542e0 100644 --- a/test.js +++ b/test.js @@ -42,7 +42,7 @@ tape('make bigger swarm', async function (t) { const { type, host, port } = swarm[490].remoteAddress() - t.same(type, DHT.NAT_PORT_CONSISTENT) + t.same(type, DHT.NAT_OPEN) t.same(port, swarm[490].address().port) t.ok(host)