-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
54 lines (48 loc) · 1.57 KB
/
main.ts
File metadata and controls
54 lines (48 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const hosts = Deno.env.get("HOSTS");
const domain = Deno.env.get("DOMAIN");
const password = Deno.env.get("PASSWORD");
const cron = Deno.env.get("CRON") ?? "0 * * * *";
if (!(hosts && domain && password)) {
console.error(
`Something is missing... see ${
JSON.stringify({ host: hosts, domain, password })
}`,
);
Deno.exit(1);
}
console.log("Server started successfully");
async function updateDnsRecord(
args: { host: string; domain: string; password: string; ip: string },
) {
console.log(`Updating DNS record for ${args.host} ...`);
// https://dynamicdns.park-your-domain.com/update?host=[host]&domain=[domain_name]&password=[ddns_password]&ip=[your_ip]
const url = new URL("https://dynamicdns.park-your-domain.com/update");
Object.entries(args).forEach(([key, value]) =>
url.searchParams.set(key, value)
);
const res = await fetch(url);
if (res.ok) {
console.log("DNS record updated successfully");
}
}
async function getPublicIp() {
console.log("Getting public ip address...");
const res = await fetch("https://api.ipify.org?format=json");
if (res.ok) {
const { ip } = await res.json() as { ip: string };
console.log(`Received public ip address: ${ip}`);
return `${ip}:`;
}
console.error("Something went wrong while getting public ip");
}
async function doUpdate() {
const ip = await getPublicIp();
if (!(hosts && domain && password && ip)) {
Deno.exit(1);
}
for (const host of hosts.split(",")) {
await updateDnsRecord({ host, domain, password, ip });
}
}
Deno.cron("update dns record", cron, doUpdate);
doUpdate();