|
| 1 | +import { SigInfo } from "@ndn/packet"; |
| 2 | +import { fromUtf8, toHex, toUtf8 } from "@ndn/util"; |
| 3 | +import type { DOHResponse } from "cf-doh"; |
| 4 | +import isValidHostname from "is-valid-hostname"; |
| 5 | + |
| 6 | +import { type ChallengeRequest, ErrorCode } from "../packet/mod"; |
| 7 | +import type { ServerChallenge, ServerChallengeContext, ServerChallengeResponse } from "./challenge"; |
| 8 | + |
| 9 | +interface State { |
| 10 | + record: string; |
| 11 | + token: string; |
| 12 | +} |
| 13 | + |
| 14 | +/** The "dns" challenge where client creates a DNS TXT record. */ |
| 15 | +export class ServerDnsChallenge implements ServerChallenge<State> { |
| 16 | + public readonly challengeId = "dns"; |
| 17 | + public readonly timeLimit = 300000; |
| 18 | + public readonly retryLimit = 3; |
| 19 | + |
| 20 | + private readonly dohServer: string; |
| 21 | + |
| 22 | + constructor({ |
| 23 | + dohServer = "https://cloudflare-dns.com/dns-query", |
| 24 | + }: ServerDnsChallenge.Options = {}) { |
| 25 | + this.dohServer = dohServer; |
| 26 | + } |
| 27 | + |
| 28 | + public async process(request: ChallengeRequest, context: ServerChallengeContext<State>): Promise<ServerChallengeResponse> { |
| 29 | + if (!context.challengeState) { |
| 30 | + return this.process0(request, context); |
| 31 | + } |
| 32 | + return this.process1(request, context); |
| 33 | + } |
| 34 | + |
| 35 | + private async process0(request: ChallengeRequest, context: ServerChallengeContext<State>): Promise<ServerChallengeResponse> { |
| 36 | + const { domain: domainWire } = request.parameters; |
| 37 | + let domain: string; |
| 38 | + if (!domainWire || !isValidHostname(domain = fromUtf8(domainWire))) { |
| 39 | + return { fail: ErrorCode.InvalidParameters }; |
| 40 | + } |
| 41 | + |
| 42 | + const record = `_ndncert-challenge.${domain}`; |
| 43 | + const token = toHex(SigInfo.generateNonce(16)); |
| 44 | + context.challengeState = { record, token }; |
| 45 | + return { |
| 46 | + challengeStatus: "need-record", |
| 47 | + parameters: { |
| 48 | + "record-name": toUtf8(record), |
| 49 | + "expected-value": toUtf8(token), |
| 50 | + }, |
| 51 | + }; |
| 52 | + } |
| 53 | + |
| 54 | + private async process1( |
| 55 | + request: ChallengeRequest, |
| 56 | + { challengeState }: ServerChallengeContext<State>, |
| 57 | + ): Promise<ServerChallengeResponse> { |
| 58 | + const { confirmation } = request.parameters; |
| 59 | + if (!confirmation) { |
| 60 | + return { fail: ErrorCode.InvalidParameters }; |
| 61 | + } |
| 62 | + |
| 63 | + let ok: boolean; |
| 64 | + try { |
| 65 | + ok = await this.checkRecord(challengeState!); |
| 66 | + } catch { |
| 67 | + ok = false; |
| 68 | + } |
| 69 | + return ok ? { success: true } : { |
| 70 | + decrementRetry: true, |
| 71 | + challengeStatus: "wrong-record", |
| 72 | + }; |
| 73 | + } |
| 74 | + |
| 75 | + private async checkRecord({ record, token }: State): Promise<boolean> { |
| 76 | + const url = new URL(this.dohServer); |
| 77 | + url.searchParams.set("name", record); |
| 78 | + url.searchParams.set("type", "TXT"); |
| 79 | + |
| 80 | + console.log("sdccr", url); |
| 81 | + const res = await fetch(url, { headers: { Accept: "application/dns-json" } }); |
| 82 | + console.log("sdccr", res); |
| 83 | + if (res.status !== 200) { |
| 84 | + return false; |
| 85 | + } |
| 86 | + const j: DOHResponse = await res.json(); |
| 87 | + console.log("sdccr", j); |
| 88 | + |
| 89 | + if (Number(j.Status) !== 0) { |
| 90 | + return false; |
| 91 | + } |
| 92 | + for (const answer of j.Answer ?? []) { |
| 93 | + if ( |
| 94 | + [record, `${record}.`].includes(answer.name) && |
| 95 | + Number(answer.type) === 16 && |
| 96 | + [token, `"${token}"`].includes(answer.data) |
| 97 | + ) { |
| 98 | + return true; |
| 99 | + } |
| 100 | + } |
| 101 | + return false; |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +export namespace ServerDnsChallenge { |
| 106 | + export interface Options { |
| 107 | + /** |
| 108 | + * DNS-over-HTTPS server with application/dns-json capability. |
| 109 | + * Common choices includes: |
| 110 | + * - https://cloudflare-dns.com/dns-query |
| 111 | + * - https://dns.google/resolve |
| 112 | + */ |
| 113 | + dohServer?: string; |
| 114 | + } |
| 115 | +} |
0 commit comments