Skip to content

Instantly share code, notes, and snippets.

@EduApps-CDG
Last active July 20, 2022 22:27
Show Gist options
  • Save EduApps-CDG/c5d6ebc76b4694689adc5e914fe8f553 to your computer and use it in GitHub Desktop.
Save EduApps-CDG/c5d6ebc76b4694689adc5e914fe8f553 to your computer and use it in GitHub Desktop.
Code that updates FreeDNS.

Usage

First of all, add to your .env file tis code:

FREEDNS_URL=PUT YOUR URL HERE!!!

Then you can run it:

//check IP changes every 1 sec:
let service = FreeDNS.startService(); //start service
clearInterval(service); //close service

//update just one time:
FreeDNS.updateDns();

You can also use it alongside express.

import { exec } from "child_process";
import { promisify } from "util";
export default class FreeDNS {
/**
* Update FreeDNS.
*/
static async updateDns() {
let exe = promisify(exec);
let dns = await exe(`curl ${process.env["FREEDNS_URL"]}`); //alternatively put your update url here.
console.log("[FreeDNS] ", dns.stdout ? dns.stdout : dns.stderr);
}
/**
* Start a service that updates FreeDNS every time the public IP changes.
*
* @param {number} timeout default to 1000
* @returns {setInterval} a setInterval event
*/
static startService(timeout=1000) {
if (!__RAM) {
globalThis.__RAM = {};
}
return setInterval(async () => {
let exe = promisify(exec);
let ipv6 = "";
try {
ipv6 = await exe(`curl https://api6.my-ip.io/ip.txt`); //replace 6 for 4 if you want IPv4
ipv6 = ipv6.stdout;
} catch (e) {
console.error("[FreeDNS]",e,"(Try to check your connection).");
}
if (ipv6 && __RAM.IPv6 !== ipv6) {
FreeDNS.updateDns();
__RAM.IPv6 = ipv6;
}
},timeout);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment