Skip to content

Commit

Permalink
Explicit dns servers (#1281)
Browse files Browse the repository at this point in the history
* add ability to setup explicit dns servers

* cleanup

* fix

* reorder

* pr feedback
  • Loading branch information
dholms authored Jul 4, 2023
1 parent dd4d91c commit 0ceed96
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 8 deletions.
56 changes: 49 additions & 7 deletions packages/identity/src/handle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ const PREFIX = 'did='

export class HandleResolver {
public timeout: number
private backupNameservers: string[] | undefined
private backupNameserverIps: string[] | undefined

constructor(opts: HandleResolverOpts = {}) {
this.timeout = opts.timeout ?? 3000
this.backupNameservers = opts.backupNameservers
}

async resolve(handle: string): Promise<string | undefined> {
Expand All @@ -23,7 +26,11 @@ export class HandleResolver {
httpAbort.abort()
return dnsRes
}
return httpPromise
const res = await httpPromise
if (res) {
return res
}
return this.resolveDnsBackup(handle)
}

async resolveDns(handle: string): Promise<string | undefined> {
Expand All @@ -33,12 +40,7 @@ export class HandleResolver {
} catch (err) {
return undefined
}
const results = chunkedResults.map((chunks) => chunks.join(''))
const found = results.filter((i) => i.startsWith(PREFIX))
if (found.length !== 1) {
return undefined
}
return found[0].slice(PREFIX.length)
return this.parseDnsResult(chunkedResults)
}

async resolveHttp(
Expand All @@ -57,4 +59,44 @@ export class HandleResolver {
return undefined
}
}

async resolveDnsBackup(handle: string): Promise<string | undefined> {
let chunkedResults: string[][]
try {
const backupIps = await this.getBackupNameserverIps()
if (!backupIps || backupIps.length < 1) return undefined
const resolver = new dns.Resolver()
resolver.setServers(backupIps)
chunkedResults = await resolver.resolveTxt(`${SUBDOMAIN}.${handle}`)
} catch (err) {
return undefined
}
return this.parseDnsResult(chunkedResults)
}

parseDnsResult(chunkedResults: string[][]): string | undefined {
const results = chunkedResults.map((chunks) => chunks.join(''))
const found = results.filter((i) => i.startsWith(PREFIX))
if (found.length !== 1) {
return undefined
}
return found[0].slice(PREFIX.length)
}

private async getBackupNameserverIps(): Promise<string[] | undefined> {
if (!this.backupNameservers) {
return undefined
} else if (!this.backupNameserverIps) {
const responses = await Promise.allSettled(
this.backupNameservers.map((h) => dns.lookup(h)),
)
for (const res of responses) {
if (res.status === 'fulfilled') {
this.backupNameserverIps ??= []
this.backupNameserverIps.push(res.value.address)
}
}
}
return this.backupNameserverIps
}
}
2 changes: 2 additions & 0 deletions packages/identity/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ export type IdentityResolverOpts = {
timeout?: number
plcUrl?: string
didCache?: DidCache
backupNameservers?: string[]
}

export type HandleResolverOpts = {
timeout?: number
backupNameservers?: string[]
}

export type DidResolverOpts = {
Expand Down
10 changes: 10 additions & 0 deletions packages/pds/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface ServerConfigValues {
databaseLocation?: string

availableUserDomains: string[]
handleResolveNameservers?: string[]

imgUriSalt: string
imgUriKey: string
Expand Down Expand Up @@ -136,6 +137,10 @@ export class ServerConfig {
? process.env.AVAILABLE_USER_DOMAINS.split(',')
: []

const handleResolveNameservers = process.env.HANDLE_RESOLVE_NAMESERVERS
? process.env.HANDLE_RESOLVE_NAMESERVERS.split(',')
: []

const imgUriSalt =
process.env.IMG_URI_SALT || '9dd04221f5755bce5f55f47464c27e1e'
const imgUriKey =
Expand Down Expand Up @@ -215,6 +220,7 @@ export class ServerConfig {
termsOfServiceUrl,
databaseLocation,
availableUserDomains,
handleResolveNameservers,
imgUriSalt,
imgUriKey,
imgUriEndpoint,
Expand Down Expand Up @@ -368,6 +374,10 @@ export class ServerConfig {
return this.cfg.availableUserDomains
}

get handleResolveNameservers() {
return this.cfg.handleResolveNameservers
}

get imgUriSalt() {
return this.cfg.imgUriSalt
}
Expand Down
6 changes: 5 additions & 1 deletion packages/pds/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ export class PDS {
config.didCacheStaleTTL,
config.didCacheMaxTTL,
)
const idResolver = new IdResolver({ plcUrl: config.didPlcUrl, didCache })
const idResolver = new IdResolver({
plcUrl: config.didPlcUrl,
didCache,
backupNameservers: config.handleResolveNameservers,
})

const messageDispatcher = new MessageDispatcher()
const sequencer = new Sequencer(db)
Expand Down

0 comments on commit 0ceed96

Please sign in to comment.