DEV Community

Cover image for 🌐 How to update a DDNS with NodeJS
Alberto Gómez
Alberto Gómez

Posted on • Edited on

🌐 How to update a DDNS with NodeJS

How to update a DDNS with NodeJS

Hi, my name is Alberto Gómez, I am a web developer, and...
This is my first post!! 🥳

Prologue

Initially, this wasn’t a “programming issue” - I just wanted to play a co-op game with three of my coworkers. I set up a server provided by the game developers, opened a specific port on my router, and sent my public IP to my colleagues so we could start our adventure.

However, I soon encountered a problem: the IP assigned to my router is dynamic, like most IPs. This means that it would eventually change, and when it did, my colleagues would no longer be able to connect using the credentials I had given them. If we wanted to play again the next day, I would have to check my IP again and send it to them. The thought of this happening bothered me.

Clearly, I needed a domain name that would point to that IP! That way, the IP could change without users having to do anything. Then I remembered something I had heard about years ago: DDNS.

What is this?

¯\(ツ)/¯ I’m not an expert on this topic either, so I can’t provide too many details. Essentially, there’s a system that updates the IP address associated with a subdomain using an API provided by the service provider.

From Wikipedia:

Dynamic DNS (DDNS) is a method of automatically updating a name server in the Domain Name Server (DNS), often in real time, with the active DDNS configuration of its configured hostnames, addresses or other information.

That was enough to solve my problem.

An easy way to accomplish this would be to write a script that retrieves my public IP and then passes it on to the Google Domains service.

My solution

First, I went to my DNS provider to create a DDNS subdomain. In my case, I have the domain albertogomez.dev registered with Google Domains. This gave me a username and password to perform operations on the subdomain.

After some research, I found the Google Domains API endpoints that I needed to use:

  • GET https://domains.google.com/checkip to get the current public dynamic IP
  • GET https://username:password@domains.google.com/nic/update to update the subdomain IP. Expect username and password as basic auth and the GET params the subdomain name as hostname and the dynamic IP as myip.

Using Axios, I wrote a simple script to run before starting the game server. This way, whenever I wanted to play with my friends, I could do so without having to check anything, and they could always connect using the same address.



import axios from 'axios';

const username = 'pC6Fmu7k6zhQMvFn'; // fake!
const password = 'u6yMFaj33T56rBdw'; // fake!
const domain = 'lets-play-with.albertogomez.dev'; // this was true

async function getDynamicIp(): Promise<string | null> {
  try {
    const response = await axios.get('https://domains.google.com/checkip');
    return response.data;
  } catch (error) {
    return null;
  }
}

async function updateDdns(ip: string | null): Promise<string> {
  if (ip) {
    try {
      const response = await axios.get(
        `https://${username}:${password}@domains.google.com/nic/update`,
        {
          params: {
            hostname: domain,
            myip: ip
          }
        }
      );
      return `DDNS - ${response}`;
    } catch (error) {
      return `NODE - ERROR ${ip}`;
    }
  } else {
    return 'NODE - MISSING IP';
  }
}

(async () => {
  const ip = await getDynamicIp();
  const status = await updateDdns(ip);
  console.log(status);
})();


Enter fullscreen mode Exit fullscreen mode

And at this point, I’m satisfied with what I have. Not bad at all.

Conclusions

In the end, my coworkers completed the game while I spent time fixing something that nobody had requested. Once again, overengineering got the best of me.

But hey, maybe this experience will come in handy someday. Perhaps in the near future, I’ll decide to continue working on it and make better use of it.

Top comments (0)