This is a .NET Core 3.1 Hosted Service that polls https://www.ipify.org/ for your public-facing IP and updates any number of Cloudflare DNS records you nominate.
The service primarily pulls the last known IP address from memory, but it also writes to and reads from lastIp.cfg
in the executable directory, in the event the service is restarted. This is to limit the number of times Cloudflare is updated.
This program sends API messages automatically to Cloudflare. I'm obviously not going to screw with people's DNS records for funsies, but I encourage you to glance through Worker.cs
and other files to get a sense of what this program does. USE AT YOUR OWN RISK
- Install .NET Core runtime on your target machine, whether it's Windows, Linux, or Mac.
- Build & publish for your target environment, or download the latest Release for your system, then deploy to your target machine.
- Set
DOTNET_ENVIRONMENT
environment variable toProduction
on target machine. - (Optional) Install as a Windows Service using sc.exe or as Systemd service (skip to "Service/Daemon setup"). There's a guide to the Linux installation down below.
- Start it.
Settings for this app are stored in appsettings.json
. There are some boilerplate (fake) values in there already to give you a sense of how to set it up.
You can set up one or more Cloudflare zones, each with one or more records you wish to update with your new public IP address.
Zone and Record objects each have a CanUpdate
flag, which allows the service to call the Cloudflare API to update that zone or record when set to true
. The zone setting overrides any child record setting.
You can leave the CloudflareApiBaseUrl
to its default. It's correct as of 2020-10-26.
Change CloudflareApiToken
to your application token from your account, and each of the IDs to the IDs for your specific zones and records.
You can look up your zone fairly easily by calling curl -L -X GET 'https://api.cloudflare.com/client/v4/zones' -H 'Authorization: Bearer {your_api_token}'
where your_api_token
is replaced with the one from your account. Similarly, you can fetch the IDs of your DNS records for a particular zone by calling curl -L -X GET 'https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records' -H 'Authorization: Bearer {your_api_token}'
where zone_id
and your_api_token
are appropriately replaced. I may eventually bake this into the service as an option. Shall see.
A more detailed breakdown of the configuration options can be found in the schema file at the root of the repo. The rest is pretty self-explanatory.
These instructions assume you're using an Ubuntu/Debian-based system. Adjust as necessary for your distribution. Any will work, so long as dotnet is installed and it's 64-bit.
- Create a dedicated user under which the service will Run. I created a user called "dotnetuser" in the "dotnetuser" group using the adduser command:
sudo adduser dotnetuser
. - Extract the linux x64 release to
/var/dotnet/ddnsclient/
. You can place it wherever you like, but this is where I put it, and where the rest of the instructions will assume you put it. - Modify settings in
appsettings.json
to your preferences. - Change the ownership of the folder, and all files within to dotnetuser:
sudo chown dotnetuser:dotnetuser /var/dotnet/ddnsclient && sudo chown dotnetuser:dotnetuser /var/dotnet/ddnsclient/*
- Add execute bit to the main binary:
sudo chmod +x /var/dotnet/ddnsclient/JDMallen.DDNSClient
- Create a service file here:
/etc/systemd/system/dotnet-ddnsclient.service
using your favorite text editor with elevated privileges. I used vim:sudo vim /etc/systemd/system/dotnet-ddnsclient.service
. - Paste in the below service definition. Be sure to replace "{your_Cloudflare_API_token}", then tweak to your liking. It's critical it remain of type "notify"!
[Unit]
Description=Dynamic DNS client for Cloudflare
[Service]
Type=notify
ExecStart= /var/dotnet/ddnsclient/JDMallen.DDNSClient
WorkingDirectory=/var/dotnet/ddnsclient
User=dotnetuser
Group=dotnetuser
Restart=on-failure
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=%n
PrivateTmp=true
Environment=DOTNET_ENVIRONMENT=Production
Environment=Settings__CloudflareApiToken={your_Cloudflare_API_token}
[Install]
WantedBy=multi-user.target
- Reload systemctl config to load this service:
sudo systemctl daemon-reload
- Start the service:
sudo systemctl start dotnet-ddnsclient.service
. Note that the service will always start by setting your server fan control to Automatic mode, to establish a sort of baseline of which mode it's in. Assuming it's not above threshold, it'll quiet down your server after {BackToManualThresholdInSeconds} seconds. - Monitor its status:
sudo systemctl status dotnet-ddnsclient.service
orsudo journalctl -u dotnet-ddnsclient.service -f
- Check Cloudflare to see if it worked!
Enjoy. :)