86 lines
2.6 KiB
JavaScript
86 lines
2.6 KiB
JavaScript
import { SESv2Client, GetEmailIdentityCommand } from "@aws-sdk/client-sesv2";
|
||
|
||
const token = process.env.CLOUDFLARE_API_TOKEN;
|
||
const zoneName = process.env.CLOUDFLARE_ZONE_NAME || "hubsport.club";
|
||
const domain = process.env.MAIL_FROM_DOMAIN || zoneName;
|
||
const region = process.env.AWS_REGION || "eu-west-1";
|
||
|
||
if (!token) {
|
||
console.error("CLOUDFLARE_API_TOKEN fehlt (Zone → DNS → Edit für " + zoneName + ").");
|
||
process.exit(1);
|
||
}
|
||
|
||
const ses = new SESv2Client({ region });
|
||
const identity = await ses.send(new GetEmailIdentityCommand({ EmailIdentity: domain }));
|
||
const tokens = identity.DkimAttributes?.Tokens ?? [];
|
||
if (tokens.length !== 3) {
|
||
console.error(`Erwartet 3 DKIM-Tokens, bekommen: ${tokens.length}`);
|
||
process.exit(1);
|
||
}
|
||
const mailFrom = identity.MailFromAttributes?.MailFromDomain ?? `bounce.${domain}`;
|
||
|
||
const api = "https://api.cloudflare.com/client/v4";
|
||
async function call(method, path, body) {
|
||
const response = await fetch(api + path, {
|
||
method,
|
||
headers: { authorization: `Bearer ${token}`, "content-type": "application/json" },
|
||
body: body ? JSON.stringify(body) : undefined,
|
||
});
|
||
const data = await response.json();
|
||
if (!data.success) throw new Error(`${method} ${path}: ${JSON.stringify(data.errors)}`);
|
||
return data.result;
|
||
}
|
||
|
||
const zones = await call("GET", `/zones?name=${encodeURIComponent(zoneName)}`);
|
||
const zone = zones[0];
|
||
if (!zone) throw new Error(`Zone ${zoneName} nicht gefunden oder Token ohne Zugriff`);
|
||
|
||
const records = [];
|
||
for (const value of tokens) {
|
||
records.push({
|
||
type: "CNAME",
|
||
name: `${value}._domainkey.${domain}`,
|
||
content: `${value}.dkim.amazonses.com`,
|
||
ttl: 3600,
|
||
proxied: false,
|
||
});
|
||
}
|
||
records.push({
|
||
type: "MX",
|
||
name: mailFrom,
|
||
content: `feedback-smtp.${region}.amazonses.com`,
|
||
priority: 10,
|
||
ttl: 3600,
|
||
proxied: false,
|
||
});
|
||
records.push({
|
||
type: "TXT",
|
||
name: mailFrom,
|
||
content: "v=spf1 include:amazonses.com ~all",
|
||
ttl: 3600,
|
||
proxied: false,
|
||
});
|
||
records.push({
|
||
type: "TXT",
|
||
name: `_dmarc.${domain}`,
|
||
content: "v=DMARC1; p=none;",
|
||
ttl: 3600,
|
||
proxied: false,
|
||
});
|
||
|
||
for (const record of records) {
|
||
const existing = await call(
|
||
"GET",
|
||
`/zones/${zone.id}/dns_records?type=${record.type}&name=${encodeURIComponent(record.name)}`,
|
||
);
|
||
const match = existing.find((item) => String(item.content).replace(/^"|"$/g, "") === record.content);
|
||
if (match) {
|
||
console.log(`ok ${record.type.padEnd(5)} ${record.name}`);
|
||
continue;
|
||
}
|
||
await call("POST", `/zones/${zone.id}/dns_records`, record);
|
||
console.log(`created ${record.type.padEnd(5)} ${record.name}`);
|
||
}
|
||
|
||
console.log("\nDNS gesetzt. SES braucht für die Verifikation meist 1–5 Minuten.");
|