From abe47696d062c03043eab281cc95d5b549603cbf Mon Sep 17 00:00:00 2001 From: Seth Date: Mon, 15 Jul 2024 15:30:39 +0800 Subject: [PATCH 1/3] feat: separate dns resolvers --- src/index.ts | 36 ++++++------------- .../dns-resolvers/cloudflare-dns-resolver.ts | 11 ++++++ src/util/dns-resolvers/google-dns-resolver.ts | 11 ++++++ src/util/dns-resolvers/index.ts | 2 ++ 4 files changed, 35 insertions(+), 25 deletions(-) create mode 100644 src/util/dns-resolvers/cloudflare-dns-resolver.ts create mode 100644 src/util/dns-resolvers/google-dns-resolver.ts create mode 100644 src/util/dns-resolvers/index.ts diff --git a/src/index.ts b/src/index.ts index 7f13aaa..1c6bf76 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,8 +1,8 @@ -import axios from "axios"; import { OpenAttestationDNSTextRecord, OpenAttestationDNSTextRecordT } from "./records/dnsTxt"; import { OpenAttestationDnsDidRecord, OpenAttestationDnsDidRecordT } from "./records/dnsDid"; import { getLogger } from "./util/logger"; import { CodedError, DnsproveStatusCode } from "./common/error"; +import { cloudflareDnsResolver, googleDnsResolver } from "./util/dns-resolvers"; const { trace } = getLogger("index"); @@ -24,24 +24,7 @@ interface GenericObject { export type CustomDnsResolver = (domain: string) => Promise; -export const defaultDnsResolvers: CustomDnsResolver[] = [ - async (domain) => { - const { data } = await axios({ - method: "GET", - url: `https://dns.google/resolve?name=${domain}&type=TXT`, - }); - - return data; - }, - async (domain) => { - const { data } = await axios({ - method: "GET", - url: `https://cloudflare-dns.com/dns-query?name=${domain}&type=TXT`, - headers: { accept: "application/dns-json", contentType: "application/json", connection: "keep-alive" }, - }); - return data; - }, -]; +export const defaultDnsResolvers: CustomDnsResolver[] = [googleDnsResolver, cloudflareDnsResolver]; /** * Returns true for strings that are openattestation records @@ -176,13 +159,14 @@ export const parseDnsDidResults = (recordSet: IDNSRecord[] = [], dnssec: boolean /** * Queries a given domain and parses the results to retrieve openattestation document store records if any * @param domain e.g: "example.openattestation.com" + * @param customDnsResolvers * @example * > getDocumentStoreRecords("example.openattestation.com") * > [ { type: 'openatts', - net: 'ethereum', - netId: '3', - addr: '0x2f60375e8144e16Adf1979936301D8341D58C36C', - dnssec: true } ] + net: 'ethereum', + netId: '3', + addr: '0x2f60375e8144e16Adf1979936301D8341D58C36C', + dnssec: true } ] */ export const getDocumentStoreRecords = async ( domain: string, @@ -190,7 +174,7 @@ export const getDocumentStoreRecords = async ( ): Promise => { trace(`Received request to resolve ${domain}`); - const dnsResolvers = customDnsResolvers || defaultDnsResolvers; + const dnsResolvers = customDnsResolvers ?? defaultDnsResolvers; const results = await queryDns(domain, dnsResolvers); const answers = results.Answer || []; @@ -206,7 +190,7 @@ export const getDnsDidRecords = async ( ): Promise => { trace(`Received request to resolve ${domain}`); - const dnsResolvers = customDnsResolvers || defaultDnsResolvers; + const dnsResolvers = customDnsResolvers ?? defaultDnsResolvers; const results = await queryDns(domain, dnsResolvers); const answers = results.Answer || []; @@ -217,3 +201,5 @@ export const getDnsDidRecords = async ( }; export { OpenAttestationDNSTextRecord, OpenAttestationDnsDidRecord }; + +export * from "./util/dns-resolvers"; diff --git a/src/util/dns-resolvers/cloudflare-dns-resolver.ts b/src/util/dns-resolvers/cloudflare-dns-resolver.ts new file mode 100644 index 0000000..bea6169 --- /dev/null +++ b/src/util/dns-resolvers/cloudflare-dns-resolver.ts @@ -0,0 +1,11 @@ +import axios from "axios"; +import { CustomDnsResolver } from "../../index"; + +export const cloudflareDnsResolver: CustomDnsResolver = async (domain) => { + const { data } = await axios({ + method: "GET", + url: `https://cloudflare-dns.com/dns-query?name=${domain}&type=TXT`, + headers: { accept: "application/dns-json", contentType: "application/json", connection: "keep-alive" }, + }); + return data; +}; diff --git a/src/util/dns-resolvers/google-dns-resolver.ts b/src/util/dns-resolvers/google-dns-resolver.ts new file mode 100644 index 0000000..96f2b7d --- /dev/null +++ b/src/util/dns-resolvers/google-dns-resolver.ts @@ -0,0 +1,11 @@ +import axios from "axios"; +import { CustomDnsResolver } from "../../index"; + +export const googleDnsResolver: CustomDnsResolver = async (domain) => { + const { data } = await axios({ + method: "GET", + url: `https://dns.google/resolve?name=${domain}&type=TXT`, + }); + + return data; +}; diff --git a/src/util/dns-resolvers/index.ts b/src/util/dns-resolvers/index.ts new file mode 100644 index 0000000..7042eab --- /dev/null +++ b/src/util/dns-resolvers/index.ts @@ -0,0 +1,2 @@ +export * from "./google-dns-resolver"; +export * from "./cloudflare-dns-resolver"; From 315724377aba6d915db9fe7e7c64edd4cd007b96 Mon Sep 17 00:00:00 2001 From: Seth Date: Mon, 15 Jul 2024 15:30:51 +0800 Subject: [PATCH 2/3] feat: add ali dns resolver --- src/util/dns-resolvers/ali-dns-resolver.ts | 11 +++++++++++ src/util/dns-resolvers/index.ts | 1 + 2 files changed, 12 insertions(+) create mode 100644 src/util/dns-resolvers/ali-dns-resolver.ts diff --git a/src/util/dns-resolvers/ali-dns-resolver.ts b/src/util/dns-resolvers/ali-dns-resolver.ts new file mode 100644 index 0000000..18ed703 --- /dev/null +++ b/src/util/dns-resolvers/ali-dns-resolver.ts @@ -0,0 +1,11 @@ +import axios from "axios"; +import { CustomDnsResolver } from "../../index"; + +export const aliDnsResolver: CustomDnsResolver = async (domain) => { + const { data } = await axios({ + method: "GET", + url: `https://dns.alidns.com/resolve?name=${domain}&type=16`, + }); + + return data; +}; diff --git a/src/util/dns-resolvers/index.ts b/src/util/dns-resolvers/index.ts index 7042eab..c622480 100644 --- a/src/util/dns-resolvers/index.ts +++ b/src/util/dns-resolvers/index.ts @@ -1,2 +1,3 @@ export * from "./google-dns-resolver"; export * from "./cloudflare-dns-resolver"; +export * from "./ali-dns-resolver"; From bcbc5698eaba2201444ca9162a68a915dd4b5b22 Mon Sep 17 00:00:00 2001 From: Seth Date: Mon, 15 Jul 2024 16:26:15 +0800 Subject: [PATCH 3/3] fix: update paths --- src/util/dns-resolvers/ali-dns-resolver.ts | 2 +- src/util/dns-resolvers/cloudflare-dns-resolver.ts | 2 +- src/util/dns-resolvers/google-dns-resolver.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/util/dns-resolvers/ali-dns-resolver.ts b/src/util/dns-resolvers/ali-dns-resolver.ts index 18ed703..943c559 100644 --- a/src/util/dns-resolvers/ali-dns-resolver.ts +++ b/src/util/dns-resolvers/ali-dns-resolver.ts @@ -1,5 +1,5 @@ import axios from "axios"; -import { CustomDnsResolver } from "../../index"; +import { CustomDnsResolver } from "../.."; export const aliDnsResolver: CustomDnsResolver = async (domain) => { const { data } = await axios({ diff --git a/src/util/dns-resolvers/cloudflare-dns-resolver.ts b/src/util/dns-resolvers/cloudflare-dns-resolver.ts index bea6169..0f30411 100644 --- a/src/util/dns-resolvers/cloudflare-dns-resolver.ts +++ b/src/util/dns-resolvers/cloudflare-dns-resolver.ts @@ -1,5 +1,5 @@ import axios from "axios"; -import { CustomDnsResolver } from "../../index"; +import { CustomDnsResolver } from "../.."; export const cloudflareDnsResolver: CustomDnsResolver = async (domain) => { const { data } = await axios({ diff --git a/src/util/dns-resolvers/google-dns-resolver.ts b/src/util/dns-resolvers/google-dns-resolver.ts index 96f2b7d..93a5d96 100644 --- a/src/util/dns-resolvers/google-dns-resolver.ts +++ b/src/util/dns-resolvers/google-dns-resolver.ts @@ -1,5 +1,5 @@ import axios from "axios"; -import { CustomDnsResolver } from "../../index"; +import { CustomDnsResolver } from "../.."; export const googleDnsResolver: CustomDnsResolver = async (domain) => { const { data } = await axios({