Click here to Skip to main content
15,884,628 members
Articles / Programming Languages / C#
Tip/Trick

Update Google Domains Dynamic DNS IPv4 in C#

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
22 Nov 2018CPOL2 min read 12K   3  
A simple solution to update the IP address that binds to a domain purchased in Google Domains

Introduction

You can easily host a website, FTP server, Email server on your own computer. Then, setup a port forwarding or virtual server at your modem/router so that the services (web server, FTP server, Email server, CCTV, etc.) can be accessed from the outside world. What you need is the public IP of your modem/router.

You can easily know your public IP by typing "my ip" on Google search:

Image 1

Google Domains Dynamic DNS

You can buy domain ($12) at Google Domains. They offer a service (Dynamic DNS) that enable you to bind the domain to a dynamic IP (in this case, refers to your Public IP) which is a common practice by most Internet Service Provider. Your public IP gets changed from time to time by ISP, typically after restarting the modem/router.

Dynamic DNS allows you to direct your domain or a subdomain to a resource that is behind a gateway that has a dynamically assigned IP address.

For more information about Google Domains Dynamic DNS, please refer to the official documentation here.

You can check the IP address of every domain/website: http://domaintoipconverter.com

Updating Google Domains Dynamic DNS IPv4 in C#

You have to make sure your public IP is working in the first place before updating it to Google Domains for the first time.

This is because it proves that you successfully setup your local networking and port forwarding configurations (modem/router/firewall, etc..). You can try to access your public IP of where you setup your web server (or FTP server, Email server, CCTV, etc.) from another computer of different internet source, just to verify everything is running fine.

Here is a simple C# solution for updating IPv4 for a domain purchased in Google Domains (Dynamic DNS).

Getting your public IP. The following code has been obtained from here.

C#
public static string GetExternalIPAddress()
{
    string result = string.Empty;

    string[] checkIPUrl =
    {
    "https://ipinfo.io/ip",
    "https://checkip.amazonaws.com/",
    "https://api.ipify.org",
    "https://icanhazip.com",
    "https://wtfismyip.com/text"
};

    using (var client = new WebClient())
    {
        client.Headers["User-Agent"] = "Mozilla/4.0 (Compatible; Windows NT 5.1; MSIE 6.0) " +
            "(compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";

        foreach (var url in checkIPUrl)
        {
            try
            {
                result = client.DownloadString(url);
            }
            catch
            {
            }

            if (!string.IsNullOrEmpty(result))
                break;
        }
    }

    return result.Replace("\n", "").Trim();
}

Then, you need the following parameters:

  • Hostname: Refers to your domain (xyz.com), or sub-domain (subdomain.xyz.com), according to your setup at Google Domains
  • Your Public IP. Obtained in above code block
  • Username. Provided by Google Domains Dynamic DNS (please refer to the documentation above)
  • Password. Provided by Google Domains Dynamic DNS (please refer to the documentation above)

C# Code Block for Updating the IPv4 of the Domain

C#
string hostname = "xyz.com";
string ipv4 = GetExternalIPAddress();
string url = string.Format("https://domains.google.com/nic/update?hostname={0}&myip={1}",
    hostname, ipv4);
WebClient wc = new WebClient();
wc.Credentials = new NetworkCredential(username, password);
var response = wc.DownloadString(url);

Sample Project (Github)

A sample project created by saudiqbal is available at: SaudsGoogleDomainsDynamicDNSUpdater (Github)

You can download his software (Sauds Google Domains Dynamic DNS Updater, also called: Google Domains Dynamic DNS Updater Windows Client) at:

http://www.saudiqbal.com/blog/google-domains-dynamic-dns-updater-windows-client.php

but, however, the software contains a bug at this moment of writing, which is: In some condition, the software upload IPv6 instead of the required IPv4 to Google Domains.

If this happened to you, you have to patch the code for getting the public IP which demonstrated above to Sauds's source code to fix the bug.

More Google Domains Dynamic DNS Updater at Github

Ok, that's all for the tips. 

Thanks for reading! Happy coding!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Other Other
Programming is an art.

Comments and Discussions

 
-- There are no messages in this forum --