Skip to content

Commit

Permalink
Force relaying on holepunch aborted (#151)
Browse files Browse the repository at this point in the history
* Force relaying on holepunch aborted error

* Force relaying on several hyperdht error codes
  • Loading branch information
andrewosh authored Oct 2, 2023
1 parent 3bf12ef commit d3c1314
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
20 changes: 16 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ module.exports = class Hyperswarm extends EventEmitter {
this.dht.on('network-change', this._handleNetworkChange.bind(this))
}

_maybeRelayConnection () {
_maybeRelayConnection (force) {
if (!this.relayThrough) return null
return this.relayThrough()
return this.relayThrough(force)
}

_enqueue (peerInfo) {
Expand Down Expand Up @@ -157,7 +157,7 @@ module.exports = class Hyperswarm extends EventEmitter {
return
}

const relayThrough = this._maybeRelayConnection()
const relayThrough = this._maybeRelayConnection(peerInfo.forceRelaying)
const conn = this.dht.connect(peerInfo.publicKey, {
relayAddresses: peerInfo.relayAddresses,
keyPair: this.keyPair,
Expand Down Expand Up @@ -185,7 +185,13 @@ module.exports = class Hyperswarm extends EventEmitter {

this.emit('update')
})
conn.on('error', noop)
conn.on('error', err => {
if (this.relayThrough && shouldForceRelaying(err.code)) {
peerInfo.forceRelaying = true
// Reset the attempts in order to fast connect to relay
peerInfo.attempts = 0
}
})
conn.on('open', () => {
opened = true
this._connectDone()
Expand Down Expand Up @@ -499,3 +505,9 @@ function noop () { }
function allowAll () {
return false
}

function shouldForceRelaying (code) {
return (code === 'HOLEPUNCH_ABORTED') ||
(code === 'HOLEPUNCH_DOUBLE_RANDOMIZED_NATS') ||
(code === 'REMOTE_NOT_HOLEPUNCHABLE')
}
1 change: 1 addition & 0 deletions lib/peer-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module.exports = class PeerInfo extends EventEmitter {
this.tried = false
this.explicit = false
this.waiting = false
this.forceRelaying = false

// Set by the Swarm
this.queued = false
Expand Down

0 comments on commit d3c1314

Please sign in to comment.