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

Commit

Permalink
feat: add support for custom DNS server IP (#142)
Browse files Browse the repository at this point in the history
I need to test my app for a custom DNS server (that's not the default
one), and `multicastdns()` does not supports it.

I've added the `ip` option according to [the multicast-dns
dependency](https://github.com/mafintosh/multicast-dns#mdns--multicastdnsoptions).
  • Loading branch information
rennokki authored Oct 18, 2022
1 parent 1f2b705 commit 3b6c7db
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface MulticastDNSInit {
interval?: number
serviceTag?: string
port?: number
ip?: string
compat?: boolean
compatQueryPeriod?: number
compatQueryInterval?: number
Expand All @@ -33,6 +34,7 @@ class MulticastDNS extends EventEmitter<PeerDiscoveryEvents> implements PeerDisc
private readonly interval: number
private readonly serviceTag: string
private readonly port: number
private readonly ip: string
private _queryInterval: ReturnType<typeof setInterval> | null
private readonly _goMdns?: GoMulticastDNS
private readonly components: MulticastDNSComponents
Expand All @@ -44,6 +46,7 @@ class MulticastDNS extends EventEmitter<PeerDiscoveryEvents> implements PeerDisc
this.broadcast = init.broadcast !== false
this.interval = init.interval ?? (1e3 * 10)
this.serviceTag = init.serviceTag ?? 'ipfs.local'
this.ip = init.ip ?? '224.0.0.251'
this.port = init.port ?? 5353
this._queryInterval = null
this._onPeer = this._onPeer.bind(this)
Expand Down Expand Up @@ -81,7 +84,7 @@ class MulticastDNS extends EventEmitter<PeerDiscoveryEvents> implements PeerDisc
return
}

this.mdns = multicastDNS({ port: this.port })
this.mdns = multicastDNS({ port: this.port, ip: this.ip })
this.mdns.on('query', this._onMdnsQuery)
this.mdns.on('response', this._onMdnsResponse)

Expand Down
27 changes: 27 additions & 0 deletions test/multicast-dns.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,31 @@ describe('MulticastDNS', () => {

await stop(mdnsA)
})

it('find another peer with different udp4 address', async function () {
this.timeout(40 * 1000)

const mdnsA = mdns({
broadcast: false, // do not talk to ourself
port: 50005,
ip: '224.0.0.252',
compat: false
})(getComponents(pA, aMultiaddrs))

const mdnsB = mdns({
port: 50005, // port must be the same
ip: '224.0.0.252', // ip must be the same
compat: false
})(getComponents(pB, bMultiaddrs))

await start(mdnsA, mdnsB)

const { detail: { id } } = await new Promise<CustomEvent<PeerInfo>>((resolve) => mdnsA.addEventListener('peer', resolve, {
once: true
}))

expect(pB.toString()).to.eql(id.toString())

await stop(mdnsA, mdnsB)
})
})

0 comments on commit 3b6c7db

Please sign in to comment.