Something went wrong. Try again.
Monorepo for Aesthetic.Computer aesthetic.computer
Something went wrong. Try again.
4.3 kB ยท 143 lines
JavaScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144#!/usr/bin/env node// Update Chat DNS, 25.11.28// Updates Cloudflare DNS records to point chat subdomains to session-server
import dotenv from "dotenv";dotenv.config({ path: "../aesthetic-computer-vault/nanos/conductor.env" });
const CLOUDFLARE_API_TOKEN = process.env.CLOUDFLARE_API_TOKEN;const CLOUDFLARE_BASE_URL = "https://api.cloudflare.com/client/v4";
// Session server IP (DigitalOcean)const SESSION_SERVER_IP = "157.245.134.225";
const headers = { "Authorization": `Bearer ${CLOUDFLARE_API_TOKEN}`, "Content-Type": "application/json",};
// Chat subdomains to migrateconst chatDomains = [ { subdomain: "chat-system.aesthetic.computer", zone: "aesthetic.computer" }, { subdomain: "chat-clock.aesthetic.computer", zone: "aesthetic.computer" }, { subdomain: "chat.sotce.net", zone: "sotce.net" },];
async function fetchZones() { const response = await fetch(`${CLOUDFLARE_BASE_URL}/zones`, { headers }); return response.json();}
async function fetchZone(zoneName) { const zones = await fetchZones(); return zones.result?.find((zone) => zone.name === zoneName);}
async function fetchARecord(zoneId, recordName) { const response = await fetch( `${CLOUDFLARE_BASE_URL}/zones/${zoneId}/dns_records?type=A&name=${recordName}`, { headers } ); return response.json();}
async function updateDNSRecord(zoneId, recordId, data) { const response = await fetch( `${CLOUDFLARE_BASE_URL}/zones/${zoneId}/dns_records/${recordId}`, { method: "PUT", headers, body: JSON.stringify(data) } ); return response.json();}
async function createDNSRecord(zoneId, data) { const response = await fetch( `${CLOUDFLARE_BASE_URL}/zones/${zoneId}/dns_records`, { method: "POST", headers, body: JSON.stringify(data) } ); return response.json();}
async function createOrUpdateARecord(subdomain, zone, ip) { console.log(`\n๐ Processing ${subdomain}...`); const zoneId = (await fetchZone(zone))?.id; if (!zoneId) { console.error(` โ Zone ID not found for ${zone}`); return false; } console.log(` ๐ Zone ID: ${zoneId}`);
const recordResponse = await fetchARecord(zoneId, subdomain); const record = recordResponse.result?.[0];
const data = { type: "A", name: subdomain, content: ip, ttl: 1, // Auto proxied: true, };
let response; if (record) { console.log(` ๐ Updating existing record (current: ${record.content})`); response = await updateDNSRecord(zoneId, record.id, data); } else { console.log(` โ Creating new record`); response = await createDNSRecord(zoneId, data); }
if (response.success) { console.log(` โ
Success: ${subdomain} -> ${ip}`); return true; } else { console.log(` โ Failed:`, response.errors); return false; }}
async function main() { console.log("๐ Chat DNS Migration Tool"); console.log("=========================="); console.log(`Target IP: ${SESSION_SERVER_IP}`); if (!CLOUDFLARE_API_TOKEN) { console.error("\nโ Missing Cloudflare API token!"); console.log("Make sure aesthetic-computer-vault/nanos/conductor.env exists with:"); console.log(" CLOUDFLARE_API_TOKEN=..."); process.exit(1); }
console.log(`\nUsing Bearer token authentication`);
const dryRun = process.argv.includes("--dry-run"); if (dryRun) { console.log("\nโ ๏ธ DRY RUN MODE - No changes will be made\n"); }
let successCount = 0; let failCount = 0;
for (const { subdomain, zone } of chatDomains) { if (dryRun) { console.log(`\n๐ Would update: ${subdomain} -> ${SESSION_SERVER_IP}`); successCount++; } else { const success = await createOrUpdateARecord(subdomain, zone, SESSION_SERVER_IP); if (success) successCount++; else failCount++; } }
console.log("\n=========================="); console.log(`โ
Success: ${successCount}`); console.log(`โ Failed: ${failCount}`); if (!dryRun && successCount === chatDomains.length) { console.log("\n๐ All DNS records updated successfully!"); console.log("\nโณ DNS propagation may take a few minutes."); console.log("Test with: curl https://chat-system.aesthetic.computer"); }}
main().catch(console.error);