Files
dynv6/index.ts
2026-02-25 16:50:38 +01:00

48 lines
1.2 KiB
TypeScript

import { createServer } from "node:http";
import { networkInterfaces } from "node:os";
const IONOS_TOKEN = process.env.IONOS_TOKEN;
const IPV4 = process.env.IPV4;
if (!IONOS_TOKEN || !IPV4) {
console.error("Missing IONOS_TOKEN or IPV4 environment variable.");
process.exit(1);
}
const IONOS_ENDPOINT = "https://ipv4.api.hosting.ionos.com/dns/v1/dyndns?q=" + IONOS_TOKEN + "&ipv4=" + IPV4;
function getMyIPv6() {
const ifs = networkInterfaces();
for (const name in ifs) {
for (const iface of ifs[name]!) {
if (iface.family === "IPv6" && !iface.internal) {
return iface.address;
}
}
}
return null;
}
const server = createServer(async (req, res) => {
const ipv6 = getMyIPv6();
if (ipv6) {
console.log(`My IPv6 address: ${ipv6}`);
} else {
console.error("No IPv6 address found.");
res.writeHead(503);
res.end("");
return;
}
const ionos = await fetch(IONOS_ENDPOINT + "&ipv6=" + ipv6);
if (!ionos.ok) {
console.error("Failed to update IONOS DNS record:", ionos.statusText);
res.writeHead(502);
res.end("");
return;
}
res.writeHead(200);
res.end("");
});
const PORT = 10205;
server.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});