I wanted to host this blog on a real domain. No offense to DuckDNS, but I am committing to this project so I want Source to Sink to have source-to-sink.com to call home!

I setup the domain on Cloudflare. The domain is only $10 a year, and it comes with a lot of great features. This blog will focus on the API and Dynamic DNS.

Setup Cloudflare DNS Entry

First I will show how to set the DNS entry in Cloudflare.

1 - Login to Cloudflare 2 - Select the domain you want to update 3 - Select ‘Update DNS configuration’ 4 - Add a new A record

Here is where things get a interesting.

There are three kinds of DNS records that you can setup in Cloudflare.

  • A - Resolves to an IPv4 address
  • AAAA - Resolves to an IPv6 address
  • CNAME - Resolves to another domain

I am using an A record because I want my domain to resolve to my public IP address.

You can find your public IP address by searching “What is my IP” in Google, or using the command curl -4 ifconfig.me.

Great, now your record should look something like this:

Cloudflare DNS Entry

Dynamic DNS

Since the blog is self hosted on home internet, I need to update my domain name to point to my home public IP address. My ISP can change my IP address at any time, so y’all need to know where to find me. That is where Dynamic DNS comes in.

Cloudflare Dynamic DNS

I am going to show you how to set your DNS record using the Cloudflare API, and how to use the OpenWRT Cloudflare DDNS package.

First we need to gather a few things:

Cloudflare API

Cloudflare API Token

To get your API key, login to Cloudflare and go to your profile. Select API key. Create a new API key with the following permissions:

  • Zone - DNS - Edit
  • Zone - Zone - Read

Zone ID

To get your zone ID, login to Cloudflare and go to your domain.

  • Select Overview
  • Should be on the right side of the page.

Record ID

To get the record ID we can use the Cloudflare API. Here is a curl command to get a list of records:

Replace $ZONE with your zone ID and $API_KEY with your API key.

curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$ZONE/dns_records/" \
-H "Authorization: Bearer $API_KEY" \

You should see a list of records. Find the record you want to update and copy the ID.'

Update Record

Now we can update the record with our public IP address. The API documentation can be found here: Cloudflare DNS API

Replace $ZONE, $API_KEY, $RECORD_ID, and $PUBLIC_IP with your information.

curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$ZONE/dns_records/$RECORD_ID"
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" 
--data '{"type":"A","name":"source-to-sink.com","content":"${IP_ADDRESS}","ttl":120,"proxied":false}'

Using this API call, you can update your DNS record with your public IPv4 address. From here you can make a script to update your IP address, like so:

#!/bin/bash

API_KEY="your_api_key"
ZONE="your_zone_id"
RECORD_ID="your_record_id"
IP=$(curl -4 ifconfig.me)
curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$ZONE/dns_records/$RECORD_ID"
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
--data '{"type":"A","name":"source-to-sink.com","content":"${IP}","ttl":120,"proxied":false}'

OpenWRT

Alternatively, you can use the OpenWRT Cloudflare DDNS package to set your DNS record.

1 - Install required packages

  • ddns-scripts-cloudflare
  • ca-bundle
  • ca-certificates
  • curl

2 - Create a new DDNS entry in the OpenWRT LuCI interface

  • Select Services -> Dynamic DNS
  • Add a new service.
  • Select cloudflare.com-v4

Here is my setup:

OpenWRT DDNS

  • Username: Bearer
  • Password: Your API key with the ZONE - Read and DNS - Edit permissions
  • Lookup Hostname: Your domain: eg. source-to-sink.com. For a subdomain, you can use subdomain.source-to-sink.com
  • Domain: Your domain: eg. source-to-sink.com. If you want to use a subdomain, you can use [email protected]
  • HTTP Secure: Checked
  • Path to CA-Certificate: /etc/ssl/certs/

3 - Select Save. Then save and apply. Then Reload to start the service.

Now your OpenWRT router will update your DNS record with your public IP address.

Conclusion

I hope you learned more about the Cloudflare API and DNS records. Send a message if you found this helpful. In a future blog I would like to do more research into the Cloudflare API and see what other cool things I can do with it.

References

A useful blog post on OpenWRT DDNS

OpenWRT Cloudflare DDNS Source Code