Skip to content

Commit

Permalink
Allow using an array of relay keys (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperisager authored Oct 17, 2023
1 parent 7087c01 commit 3994426
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
9 changes: 8 additions & 1 deletion lib/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module.exports = function connect (dht, publicKey, opts = {}) {
if (pool && pool.has(publicKey)) return pool.get(publicKey)

const keyPair = opts.keyPair || dht.defaultKeyPair
const relayThrough = typeof opts.relayThrough === 'function' ? opts.relayThrough() : (opts.relayThrough || null)
const relayThrough = selectRelay(opts.relayThrough || null)
const encryptedSocket = (opts.createSecretStream || defaultCreateSecretStream)(true, null, {
publicKey: keyPair.publicKey,
remotePublicKey: publicKey,
Expand Down Expand Up @@ -757,4 +757,11 @@ function isRelay (relaySocket, socket, port, host) {
return port === stream.remotePort && host === stream.remoteHost
}

function selectRelay (relayThrough) {
if (typeof relayThrough === 'function') relayThrough = relayThrough()
if (relayThrough === null) return null
if (Array.isArray(relayThrough)) return relayThrough[Math.floor(Math.random() * relayThrough.length)]
return relayThrough
}

function noop () {}
9 changes: 8 additions & 1 deletion lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ module.exports = class Server extends EventEmitter {
}
}

const relayThrough = typeof this.relayThrough === 'function' ? this.relayThrough() : this.relayThrough
const relayThrough = selectRelay(this.relayThrough)

if (relayThrough) hs.relayToken = relay.token()

Expand Down Expand Up @@ -620,4 +620,11 @@ function isRelay (relaySocket, socket, port, host) {
return port === stream.remotePort && host === stream.remoteHost
}

function selectRelay (relayThrough) {
if (typeof relayThrough === 'function') relayThrough = relayThrough()
if (relayThrough === null) return null
if (Array.isArray(relayThrough)) return relayThrough[Math.floor(Math.random() * relayThrough.length)]
return relayThrough
}

function noop () {}

0 comments on commit 3994426

Please sign in to comment.