Cloudflare DDNS (Dynamic DNS) support for UniFi OS

Overview

Cloudflare DDNS for UniFi OS

A Cloudflare Worker script that exposes a UniFi-compatible DDNS API to dynamically update the IP address of a DNS A record.

Why?

I have a UniFi Dream Machine Pro (UDM-Pro), and I want to update my Cloudflare domain name DNS records when my public IP address changes. Unfortunately, UniFi does not come pre-configured to support Cloudflare as one of its DDNS providers.

Configuring Cloudflare

  1. Create a new Cloudflare Worker
  2. 'Quick Edit' the worker within your browser.
  3. Copy and paste the contents of index.js into the code editor for your worker. Ensure that you are replacing any boilerplate/code that is currently there.
  4. Once you have created the worker, take note of it's *.workers.dev route. More on routes for Cloudflare Workers here.
  5. Create an API token so the Worker can update your DNS records. Go to https://dash.cloudflare.com/profile/api-tokens and select "Create custom token". Enable permissions for both Zone:Read and DNS:Edit. Copy your API Key - you will need it later when configuring your UniFi OS Controller.

Configuring UniFi OS

  1. Log on to your UniFi OS Controller
  2. Navigate to Settings > Internet > WAN and scroll down to Dynamic DNS.
  3. Click Create New Dynamic DNS and enter the following information:
  • Service: choose any service from the drop-down menu
  • Hostname: the full subdomain and hostname of the record you want to update (e.g. subdomain.mydomain.com)
  • Username: the domain name containing the record (e.g. mydomain.com)
  • Password: the Cloudflare API Token you created earlier
  • Server: the Cloudflare Worker route ddns.<worker-subdomain>.workers.dev/update?hostname=%h&ip=%i

Acknowledgements

Comments
  • dyndns no longer available as a service in UDM-P

    dyndns no longer available as a service in UDM-P

    Hi and thanks for this great guide, but I'm wondering which of the available services Unify supports on the latest version of the controller software I should use?

    Choices are:

    • afraid
    • dnsspark
    • dslreports
    • easydns
    • namecheap
    • noip
    • sitelutions
    • zoneedit

    Thanks!

    opened by bar2sek 6
  • [

    ["400 Bad Request"]

    Hi guys,

    just tried to setup my UDMB (UnifiOS 1.12.15, Network 7.1.59) to directly update its public ip to cloudflare.

    Currently I am using affraid for that and this is working.

    I created a new A record with my root domain, which I will call "example.de".

    My config in the UDMB looks like this: 2022-03-28 18_55_45-UniFi Network - RT-UDM

    My cloudflare dns like that: CloudflareDNS

    But when the UDMB tries to update the ip, I just get this:

    [400 Bad Request] <html> <head><title>400 Bad Request</title></head> <body> <center><h1>400 Bad Request</h1></center> <hr><center>cloudflare</center> </body> </html>

    I tried to access the worker url with the cloudflare dev tools and I can access at least the url till https://ddnsapi.example.workers.dev/nic/update and get the

    400 Bad Request
    cache-control:
        no-store
    content-length:
        33
    content-type:
        text/plain;charset=UTF-8
    Please provide valid credentials.
    

    What I am doing wrong?

    Thanks in advance!

    opened by TheUntouchable 6
  • Not working on USG 3P

    Not working on USG 3P

    I'm not sure if the USG 3P is different from the UDM in protocol version, and if this is related/caused by #6 or #1.

    From a bit of troubleshooting, it appears configuring the Server entry as in the docs that the request to the worker has the following:

    "event": {
        "request": {
          "url": "https://ddns-updater.example.workers.dev/update?ip=%i&hostname=/nic/update?system=dyndns&hostname=mydomain.example.com&myip=111.111.111.111",
    

    (Domain & IP replaced)

    From a guess it looks like the USG doesn't support using a custom path on the URL.

    Have I setup something wrong or is there a version difference somewhere?

    opened by dhutchison 5
  • Doesn’t work with Synology

    Doesn’t work with Synology

    I do have a Unifi system, but given the flakiness of its dyndns updating (you can’t even force an update) I want to use this via my Synology.

    I did the full setup, and in the DDNS service provider setup, I made a new provider called “Cloudflare” with the URL: https://xxx.xxx.workers.dev/update?hostname=__HOSTNAME__&ip=__MYIP__

    I filled in the hostname, username, and password fields. I clicked the “Test Connection” button, which spun for a few seconds and said that it failed.

    All that shows up in the logs is “System failed to register [IP] to [domain] in DDNS server [USER_Cloudflare] because of [service_ddns_error_unknown].”

    However, if I go to the Cloudflare DNS page, it worked! The DNS IP is set successfully.

    So it seems like the script isn’t correctly returning a success value that the Synology client likes.

    opened by avidrissman 4
  • Issues using UDMSE 2.4.10

    Issues using UDMSE 2.4.10

    I am leveraging the index.js script which is reporting no hostname when the script is being sent to a cloudflareworker. Getting the following:

    You must specify a hostname inadyn[13240]: Fatal error in DDNS server response: inadyn[13240]: [400 Bad Request] You must specify a hostname inadyn[13240]: Error response from DDNS server, exiting! inadyn[13240]: Error code 48: DDNS server response not OK

    Command being sent from debug:

    inadyn[13240]: Sending alias table update to DDNS server: GET /update?ip=47.185.98.12&hostname= HTTP/1.0 Host: dyndns.cowtownbells.workers.dev

    Here is the snippit from the config:

    iface = eth8

    custom dyndns.cowtownbells.workers.dev:1 { hostname = "ha.cowtownbells.com" username = "cowtownbells.com" password = "" ddns-server = "dyndns.cowtownbells.workers.dev" ddns-path = "/update?ip=%i&hostname=" }

    opened by nathanlbell 3
  • Invalid Value for keyword 'ip' = '

    Invalid Value for keyword 'ip' = '

    I am following your guide exactly but the DNS record on Cloudflare never updates. Model: USG-Pro-4 Version: 4.4.56.5449062

    relevant log in /var/log/messages: WARNING: file /var/cache/ddclient/ddclient_eth2.cache, line 3: Invalid Value for keyword 'ip' = '

    opened by bencdll 3
  • Need additional documentation for UDM Pro SE

    Need additional documentation for UDM Pro SE

    I found that the instructions provided here work as expected for the USG. However, when I replicated the steps for the UDM Pro SE (UnifFi OS 2.4.8, which is EA), DNS was not updating. To resolve it, I SSH'd into it and ran this command:

    /usr/sbin/inadyn -n -C -f /run/ddns-eth8-inadyn.conf --no-pidfile -l debug

    I found that the HTTP request it was making to Cloudflare was malformed because it was missing a leading folder path:

    inadyn[20183]: Sending alias table update to DDNS server:

    GET foobar.workers.dev HTTP/1.0
    Host: foobar.workers.dev
    Authorization: Basic foobar
    User-Agent: inadyn/2.8.1 https://github.com/troglobit/inadyn/issues
    
    inadyn[20183]: Successfully sent HTTPS request!
    inadyn[20183]: Successfully received HTTPS response (316/8191 bytes)!
    inadyn[20183]: DDNS server response: HTTP/1.1 400 Bad Request
    Server: cloudflare
    Date: Thu, 28 Apr 2022 16:22:43 GMT
    Content-Type: text/html
    Content-Length: 155
    Connection: close
    CF-RAY: -
    
    <html>
    <head><title>400 Bad Request</title></head>
    <body>
    <center><h1>400 Bad Request</h1></center>
    <hr><center>cloudflare</center>
    </body>
    </html>
    

    Notice the malformed GET subdomain.domain.com HTTP/1.0

    Long story short, thanks to this blog post, I found i needed to append /nic/update?hostname=%h&myip=%i at the end of the Server entry in the DDNS configuration. So, if your Server entry is subdomain.workers.dev, make it this: workers.dev/nic/update?hostname=%h&myip=%i.

    opened by absane 2
  • Change 200 OK return string to `good`

    Change 200 OK return string to `good`

    inadyn parses the 200 return string for "good", "nochg", "dnserr", "911", "badauth", and "nohost". All other strings result in "NOTOK". This small change fixes this.

    The UDM log before the change: May 22 01:34:18 udm inadyn[10905]: Update forced for alias my.example.com, new IP# 1.2.3.4 May 22 01:34:18 udm inadyn[10905]: Fatal error in DDNS server response: May 22 01:34:18 udm inadyn[10905]: [200 OK] DNS Record Update Successful! May 22 01:34:18 udm inadyn[10905]: Error response from DDNS server, ignoring ...

    After the change: May 22 10:20:14 udm inadyn[18317]: Update forced for alias my.example.com, new IP# 1.2.3.4 May 22 10:20:14 udm inadyn[18317]: Updating cache for my.example.com

    opened by rotx 1
  • Using a similar setup for UDM Pro VPN?

    Using a similar setup for UDM Pro VPN?

    Thank you for the useful script and instructions. I'm wondering if we could use a similar approach for UDM Pro's VPN? My goal is to use the Cloudflare proxied DNS, instead of a public address.

    enhancement 
    opened by fmunteanu 1
  • Return correct response for updates

    Return correct response for updates

    As discussed in #1. Status codes returned were not according to dyndns spec, which resulted in the Unifi Gateway sending an update every 5 minutes opposed to every IP change only.

    This also updates the README, since with the latest changes only allow for dyndns.

    opened by UnchartedBull 1
  • Improve exception handling

    Improve exception handling

    As i was tracking down a problem in my configuration i modified your script a little bit. Maybe this helps someone else too.

    After those modifications all errors are visible in the cloudflare dashboard (Workers -> Real-time Logs): worker-log

    opened by davidlueder 0
  • UDM not sending correct IP

    UDM not sending correct IP

    Leaving this in case anyone else is stuck where I'm at.

    Set everything up, verified it was sending requests to worker, failed to update IP.

    After putting a crap ton of logs throughout the code I found two issues with Ubiquiti's software:

    1. For whatever reason sometimes it sends the domain twice, eg a.b.coma.b.com
    2. It's sending the wrong IP

    I believe the first one is a straight up bug, if you just send a request without the query this sometimes gets appended.

    The second was a bit of a doozy. The only feedback the logs were giving was a 500 and the message wasn't visible (only current real issue I have with this script). Threw logs in all the exceptions to narrow it down and came out with the duplicate bug and a failure in the actual PUT. A lot more logs added later and the exact failure was the PUT was being rejected with a 9004 exception, quick google said whatever IP I was supplying was not eligible for proxy.

    Couple logs later I grabbed the IP Ubiquiti was pushing, 100.x.x.x. Sure enough it was rejected with a 9004. Confused I quickly looked up my IP and got a 204.x.x.x, CF did not reject that IP.

    The hell.

    Network 7.2.95

    opened by Cohors1316 2
  • UDM Pro is not updating my DynDNS config automatically

    UDM Pro is not updating my DynDNS config automatically

    The Dyn DNS service on the UDM is not updating my IP Address automatically. If I trigger the update manually with a command via ssh everything works fine. (The first-time registration was successful even without the command) Command:"/usr/sbin/inadyn --foreground --config /run/ddns-ppp0-inadyn.conf --once --loglevel debug --force"

    opened by L5onidas 0
  • not found

    not found

    I followed the steps in the readme to configure Cloudflare for my Dream Machine. When I go to my worker url, I see 'not found'. Is that the expected behavior?

    opened by andrew-davis 2
  • UDM Pro not updating DNS for my cloudflare

    UDM Pro not updating DNS for my cloudflare

    Hi,

    I followed all the steps but somehow its not updating. Do i need to create specific kind of worker? i selected default "HTTP Handler" worker and not the "HTTP Router" kind.

    Beyond that i have reched everything still no update. Only thing is that earlier i had dyndns configured to update my google domain. Which i removed and added new one. Might be its not triggering dyndns in unifi?

    opened by JaswinderJohal 4
  • dyndns was removed on latest Network update

    dyndns was removed on latest Network update

    On latest official Network release 7.2.92, dyndns is gone from the list of services. Is there an alternative service we can use? They are now running inadyn 2.8.1 into console, I think we can pick any service from the list.

    image

    opened by fmunteanu 1
  • TypeError: Cannot set properties of undefined (setting 'content') when record does not exist

    TypeError: Cannot set properties of undefined (setting 'content') when record does not exist

    If you try and use this before creating a record, it returns an error:

    $ curl -u "example.com:redacted" "https://ddns.example.workers.dev/update?ip=192.168.0.1&hostname=ddns.example.com"
    TypeError: Cannot set properties of undefined (setting 'content')
        at Cloudflare.updateRecord (worker.js:183:22)
        at informAPI (worker.js:65:35)
        at async handleRequest (worker.js:30:26)
    

    May just want to update the README.md to indicate the dns record you are using must be created or already exist with a previous value.

    Other than that, this worked great. Thanks!

    opened by gbrayut 1
Owner
William Walker
William Walker
Functional-style Cloudflare Durable Objects with direct API calls from Cloudflare Workers and TypeScript support.

durable-apis Simplifies usage of Cloudflare Durable Objects, allowing a functional programming style or class style, lightweight object definitions, a

Dabble 12 Jan 2, 2023
Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

Svante Jonsson IT-Högskolan 3 May 18, 2022
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
DNS-Blocklists: For a better internet - keep the internet clean!

DNS Blocklists - For a better internet! Multi - Cleans the Internet and protects your privacy! An all in one blocklist based on the OISD blocklist, ba

Gerd 280 Jan 2, 2023
Node.js package with a customized HTTP and HTTPS agents to prevent SSRF with hosts validations and custom DNS feature.

http-agent-dns This is a Node.js package with a customized HTTP and HTTPS agents to prevent SSRF with hosts validations with a possibility to use a cu

Bruno Germano 4 Jul 21, 2022
A (multi) DNS-over-HTTPS resolver for Node.js

doh-resolver A DNS-over-HTTPS resolver for Node.js. Install $ npm install doh-resolver --save Usage It can be used as dns.resolve4 and/or dns.resolve6

Kiko Beats 6 Jul 20, 2022
A set of useful helper methods for writing functions to handle Cloudflare Pub/Sub messages (https://developers.cloudflare.com/pub-sub/)

pubsub A set of useful helper methods for writing functions to handle Cloudflare Pub/Sub messages. This includes: A isValidBrokerRequest helper for au

Cloudflare 18 Dec 4, 2022
Connect to a Postgres database from a Cloudflare Worker, using Cloudflare Tunnel

Cloudflare Workers Postgres Client This is an experimental module. Heavily based on cloudflare/worker-template-postgres, but cleaned up and bundled in

BubblyDoo 17 Dec 22, 2022
Dynamic-web-development - Dynamic web development used CSS and HTML

Dynamic-web-development ASSISNMENT I just used CSS and HTML to make a mobile int

null 1 Feb 8, 2022
dynamic-component-app is an angular application for dynamic component template creation

MyApp This project was generated with Angular CLI version 14.1.0. Development server Run ng serve for a dev server. Navigate to http://localhost:4200/

Aniket Muruskar 7 Aug 26, 2022
Query for CSS brower support data, combined from caniuse and MDN, including version support started and global support percentages.

css-browser-support Query for CSS browser support data, combined from caniuse and MDN, including version support started and global support percentage

Stephanie Eckles 65 Nov 2, 2022
Use Cloudflare Pages Functions as a reverse proxy with custom domain support.

cf-page-func-proxy Use Cloudflare Pages Functions as a reverse proxy with custom domain support. Getting Start 1.下载或是Fork本仓库 2.修改_worker.js中的url.hostn

null 121 Dec 23, 2022
Dynamic (Per line/paragraph depend on language you type) RTL/LTR support plugin for Obsidian.md

In the name of Allah Obsidian Dynamic RTL Dynamic (Per line/paragraph depending on the language you type) RTL/LTR support plugin for Obsidian.md Previ

Amirreza Aliakbari 33 Jan 2, 2023
A website for tracking community support for BIP21 QR codes that support on-chain and lightning bitcoin payments.

BIP21 Microsite This is a WIP microsite to promote the usage of a BIP21 payment URI QR code that can include lightning invoices or offers. Wallet supp

Stephen DeLorme 16 Nov 27, 2022
Persistent key/value data storage for your Browser and/or PWA, promisified, including file support and service worker support, all with IndexedDB. Perfectly suitable for your next (PWA) app.

BrowstorJS ?? ?? ?? Persistent key/value data storage for your Browser and/or PWA, promisified, including file support and service worker support, all

Nullix 8 Aug 5, 2022
基于 gh-proxy + Jsdelivr+ cnpmjs + cloudflare workers 的 GitHub Serverless API 工具。

better-github-api Better, Eazy, Access Anywhere 介绍 基于 gh-proxy + Jsdelivr + cnpmjs + cloudflare workers 的 GitHub Serverless API 工具。 cdn.js:仅含 gh-proxy

One Studio 11 Nov 23, 2022
A Cloudflare Worker for fetching data from Twitter User API.

cloudflare-worker-twitter This is a Cloudflare Worker for fetching data from Twitter User API. ❔ About this project I created this Worker to hide my A

Arda Soytürk 12 Oct 1, 2022
A URL shortener that runs on Cloudflare Workers

ITP Works A URL shortener that runs on Cloudflare Workers. It stores the rules in Cloudflare KV storage and sends a 301 redirect when a matched pathna

Yifei Gao 3 Mar 4, 2022