diff --git a/src/models/site.model.ts b/src/models/site.model.ts index 15a9bbb3..8f658caf 100644 --- a/src/models/site.model.ts +++ b/src/models/site.model.ts @@ -720,19 +720,30 @@ export async function getMiraBalance(characterId: string, contract: Contract) { return result } -export async function checkDomainServer(domain: string, handle: string) { - const dns = await ( - await fetch( - `https://cloudflare-dns.com/dns-query?name=_xlog-challenge.${domain}&type=TXT`, - { - headers: { - accept: "application/dns-json", - }, +export async function fetchTenant( + host: string, + retries: number, +): Promise { + const res = await fetch( + `https://cloudflare-dns.com/dns-query?name=_xlog-challenge.${host}&type=TXT`, + { + headers: { + accept: "application/dns-json", }, - ) - ).json() + }, + ) + const txt = await res.json() + if (txt.Status === 5 && retries > 0) { + console.log("retrying", host, retries - 1) + return await fetchTenant(host, retries - 1) + } else { + return txt?.Answer?.[0]?.data.replace(/^"|"$/g, "") + } +} + +export async function checkDomainServer(domain: string, handle: string) { + const tenant = await fetchTenant(domain, 5) - const tenant = dns?.Answer?.[0]?.data.replace(/^"|"$/g, "") if (!tenant || tenant !== handle) { return false } else { diff --git a/src/pages/api/host2handle.ts b/src/pages/api/host2handle.ts index a1c9a049..3bc9148b 100644 --- a/src/pages/api/host2handle.ts +++ b/src/pages/api/host2handle.ts @@ -1,7 +1,7 @@ import { NextApiRequest, NextApiResponse } from "next" import { OUR_DOMAIN } from "~/lib/env" import { cacheGet } from "~/lib/redis.server" -import { checkDomainServer } from "~/models/site.model" +import { checkDomainServer, fetchTenant } from "~/models/site.model" export default async function handler( req: NextApiRequest, @@ -48,16 +48,7 @@ export default async function handler( } } } else { - const res = await fetch( - `https://cloudflare-dns.com/dns-query?name=_xlog-challenge.${realHost}&type=TXT`, - { - headers: { - accept: "application/dns-json", - }, - }, - ) - const txt = await res.json() - const tenant = txt?.Answer?.[0]?.data.replace(/^"|"$/g, "") + const tenant = await fetchTenant(realHost, 5) return { subdomain: tenant, }