Cotton code
65 lines · 3.8 KB · TypeScriptRaw
/** * WHETHER A DOMAIN IS READY (OWN-MAIL.md "The DNS a person adds"), read over * DNS-over-HTTPS: the proof record that it is the person's, Cloudflare's MX * and SPF for receiving, its DKIM keys for forwarding and for sending, and a * DMARC policy. Each line is said in words. */export const DOH_URL = "https://cloudflare-dns.com/dns-query";export type Resolve = (name: string, type: "MX" | "TXT") => Promise<string[]>;let standIn: Resolve | null = null;/** Tests only: answer DNS from a table. */export function resolverForTests(resolve: Resolve | null): void {  standIn = resolve;}async function doh(name: string, type: "MX" | "TXT"): Promise<string[]> {  const res = await fetch(`${DOH_URL}?name=${encodeURIComponent(name)}&type=${type}`, { headers: { accept: "application/dns-json" } });  if (!res.ok) throw new Error(`DNS answered ${res.status}`);  const body = (await res.json()) as { Answer?: { type: number; data: string }[] };  const want = type === "MX" ? 15 : 16;  return (body.Answer ?? []).filter((a) => a.type === want).map((a) => (type === "TXT" ? a.data.replace(/^"|"$/g, "").replace(/" "/g, "") : a.data.replace(/\.$/, "")));}export function resolver(): Resolve {  return standIn ?? doh;}export interface DomainCheck {  domain: string;  proved: boolean;  lines: { id: string; ok: boolean; said: string }[];  receiving: boolean;  sending: boolean;}export async function checkDomain(domain: string, code: string, resolve: Resolve = resolver()): Promise<DomainCheck> {  const safe = async (name: string, type: "MX" | "TXT"): Promise<string[]> => resolve(name, type).catch(() => []);  const [proof, mx, txt, routingDkim, sendingDkim, dmarc] = await Promise.all([    safe(`_cotton.${domain}`, "TXT"),    safe(domain, "MX"),    safe(domain, "TXT"),    safe(`cf2024-1._domainkey.${domain}`, "TXT"),    safe(`cf-bounce._domainkey.${domain}`, "TXT"),    safe(`_dmarc.${domain}`, "TXT"),  ]);  const proved = proof.some((t) => t.trim() === `cotton-verify=${code}`);  const hosts = mx.map((m) => m.split(/\s+/).pop()!.toLowerCase());  const mxOk = hosts.length > 0 && hosts.every((h) => h.endsWith(".mx.cloudflare.net"));  const spf = txt.filter((t) => t.toLowerCase().startsWith("v=spf1"));  const spfOk = spf.length === 1 && spf[0]!.includes("include:_spf.mx.cloudflare.net");  const dkimIn = routingDkim.some((t) => t.includes("v=DKIM1") || t.includes("p="));  const dkimOut = sendingDkim.some((t) => t.includes("v=DKIM1") || t.includes("p="));  const policy = dmarc.find((t) => t.toLowerCase().startsWith("v=dmarc1"));  const lines = [    { id: "proof", ok: proved, said: proved ? `${domain} is yours: Cotton found its proof record.` : `Add a TXT record at _cotton.${domain} with cotton-verify=${code}.` },    { id: "mx", ok: mxOk, said: mxOk ? "Mail can reach Cotton." : hosts.length ? `Mail goes elsewhere (${hosts.join(", ")}): turn on Email Routing for ${domain} in Cloudflare.` : `${domain} has no MX records: turn on Email Routing for it in Cloudflare.` },    { id: "spf", ok: spfOk, said: spfOk ? "SPF names Cloudflare." : spf.length > 1 ? `${domain} has ${spf.length} SPF records; keep one.` : `The SPF record should include include:_spf.mx.cloudflare.net.` },    { id: "dkim_routing", ok: dkimIn, said: dkimIn ? "Forwarded mail is signed." : "Cloudflare's routing key (cf2024-1._domainkey) is missing." },    { id: "dkim_sending", ok: dkimOut, said: dkimOut ? "Sending is signed." : `${domain} is not set up for sending yet (cf-bounce._domainkey is missing).` },    { id: "dmarc", ok: policy !== undefined, said: policy ? `DMARC: ${/p=(\w+)/i.exec(policy)?.[1] ?? "set"}.` : "DMARC: none yet. Add a TXT record at _dmarc with v=DMARC1; p=none to start." },  ];  return { domain, proved, lines, receiving: proved && mxOk && spfOk, sending: proved && dkimOut };}