-
Notifications
You must be signed in to change notification settings - Fork 452
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support custom DNS resolvers (#2435)
Adds a `dns` config key that lets users specify an instance of the `DNS` interface from `@multiformats/dns`. This instance will be passed through to DNSADDR `multiaddr`s for use when the DNSLink TXT record is resolved.
- Loading branch information
1 parent
25c8f93
commit f39ce5f
Showing
8 changed files
with
66 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,30 @@ | ||
import { type AbortOptions, multiaddr, type Multiaddr } from '@multiformats/multiaddr' | ||
import { resolvers } from '@multiformats/multiaddr' | ||
import type { LoggerOptions } from '@libp2p/interface' | ||
import type { Multiaddr, ResolveOptions } from '@multiformats/multiaddr' | ||
|
||
/** | ||
* Resolve multiaddr recursively | ||
* Recursively resolve DNSADDR multiaddrs | ||
*/ | ||
export async function resolveMultiaddrs (ma: Multiaddr, options: AbortOptions & LoggerOptions): Promise<Multiaddr[]> { | ||
// TODO: recursive logic should live in multiaddr once dns4/dns6 support is in place | ||
// Now only supporting resolve for dnsaddr | ||
const resolvableProto = ma.protoNames().includes('dnsaddr') | ||
export async function resolveMultiaddrs (ma: Multiaddr, options: ResolveOptions & LoggerOptions): Promise<Multiaddr[]> { | ||
// check multiaddr resolvers | ||
let resolvable = false | ||
|
||
// Multiaddr is not resolvable? End recursion! | ||
if (!resolvableProto) { | ||
return [ma] | ||
for (const key of resolvers.keys()) { | ||
resolvable = ma.protoNames().includes(key) | ||
|
||
if (resolvable) { | ||
break | ||
} | ||
} | ||
|
||
const resolvedMultiaddrs = await resolveRecord(ma, options) | ||
const recursiveMultiaddrs = await Promise.all(resolvedMultiaddrs.map(async (nm) => { | ||
return resolveMultiaddrs(nm, options) | ||
})) | ||
// return multiaddr if it is not resolvable | ||
if (!resolvable) { | ||
return [ma] | ||
} | ||
|
||
const addrs = recursiveMultiaddrs.flat() | ||
const output = addrs.reduce<Multiaddr[]>((array, newM) => { | ||
if (array.find(m => m.equals(newM)) == null) { | ||
array.push(newM) | ||
} | ||
return array | ||
}, ([])) | ||
const output = await ma.resolve(options) | ||
|
||
options.log('resolved %s to', ma, output.map(ma => ma.toString())) | ||
|
||
return output | ||
} | ||
|
||
/** | ||
* Resolve a given multiaddr. If this fails, an empty array will be returned | ||
*/ | ||
async function resolveRecord (ma: Multiaddr, options: AbortOptions & LoggerOptions): Promise<Multiaddr[]> { | ||
try { | ||
ma = multiaddr(ma.toString()) // Use current multiaddr module | ||
const multiaddrs = await ma.resolve(options) | ||
return multiaddrs | ||
} catch (err) { | ||
options.log.error(`multiaddr ${ma.toString()} could not be resolved`, err) | ||
return [] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters