Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Commit

Permalink
fix: if client mode is specifed, do not auto-switch to server mode (#475
Browse files Browse the repository at this point in the history
)

If the user has specified client mode or not then do not auto-switch
between client or server mode.
  • Loading branch information
achingbrain authored May 9, 2023
1 parent 1fb2df2 commit abe6a25
Showing 1 changed file with 27 additions and 23 deletions.
50 changes: 27 additions & 23 deletions src/dual-kad-dht.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,29 +164,33 @@ export class DefaultDualKadDHT extends EventEmitter<PeerDiscoveryEvents> impleme
}))
})

components.events.addEventListener('self:peer:update', (evt) => {
log('received update of self-peer info')
const hasPublicAddress = evt.detail.peer.addresses
.some(({ multiaddr }) => {
const isPublic = multiaddrIsPublic(multiaddr)

log('%m is public %s', multiaddr, isPublic)

return isPublic
})

this.getMode()
.then(async mode => {
if (hasPublicAddress && mode === 'client') {
await this.setMode('server')
} else if (mode === 'server' && !hasPublicAddress) {
await this.setMode('client')
}
})
.catch(err => {
log.error('error setting dht server mode', err)
})
})
// if client mode has not been explicitly specified, auto-switch to server
// mode when the node's peer data is updated with publicly dialable addresses
if (init.clientMode == null) {
components.events.addEventListener('self:peer:update', (evt) => {
log('received update of self-peer info')
const hasPublicAddress = evt.detail.peer.addresses
.some(({ multiaddr }) => {
const isPublic = multiaddrIsPublic(multiaddr)

log('%m is public %s', multiaddr, isPublic)

return isPublic
})

this.getMode()
.then(async mode => {
if (hasPublicAddress && mode === 'client') {
await this.setMode('server')
} else if (mode === 'server' && !hasPublicAddress) {
await this.setMode('client')
}
})
.catch(err => {
log.error('error setting dht server mode', err)
})
})
}
}

readonly [Symbol.toStringTag] = '@libp2p/dual-kad-dht'
Expand Down

0 comments on commit abe6a25

Please sign in to comment.