diff --git a/README.md b/README.md index 9ee7ef9..1227fb3 100644 --- a/README.md +++ b/README.md @@ -19,11 +19,11 @@ I have a UniFi Dream Machine Pro (UDM-Pro), and I want to update my Cloudflare d 1. Log on to your [UniFi OS Controller](https://unifi.ui.com/) 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`) +- `Service`: choose dyndns +- `Hostname`: the full subdomain and hostname of the record you want to update (e.g. `subdomain.mydomain.com`, `mydomain.com` for root domain) - `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..workers.dev/update?hostname=%h&ip=%i` +- `Server`: the Cloudflare Worker route `..workers.dev` ## Acknowledgements - [inadyn](https://github.com/troglobit/inadyn) is an open-source application that supports different dynamic DNS providers. It's used by UniFi OS under-the-hood to update your public IP address. diff --git a/index.js b/index.js index f10e26d..75c36ec 100644 --- a/index.js +++ b/index.js @@ -3,7 +3,7 @@ * @param {Request} request * @returns {Promise} */ -async function handleRequest(request) { + async function handleRequest(request) { const { protocol, pathname } = new URL(request.url); // Require HTTPS (TLS) connection to be secure. @@ -15,15 +15,6 @@ async function handleRequest(request) { } switch (pathname) { - case "/": - return new Response("Hello World!", { - status: 200, - headers: { - "Content-Type": "text/plain;charset=UTF-8", - "Cache-Control": "no-store" - }, - }); - case "/nic/update": { if (request.headers.has("Authorization")) { const { username, password } = basicAuthentication(request); @@ -45,7 +36,7 @@ async function handleRequest(request) { return new Response(null, { status: 204 }); } - return new Response("Not Found.", { status: 404 }); + return new Response(null, { status: 404 }); } /** @@ -67,16 +58,28 @@ async function informAPI(url, name, token) { const zone = await cloudflare.findZone(name); const record = await cloudflare.findRecord(zone, hostname); - const result = await cloudflare.updateRecord(record, ip); - - // Only returns this response when no exception is thrown. - return new Response("DNS Record Update Successful!", { - status: 200, - headers: { - "Content-Type": "text/plain;charset=UTF-8", - "Cache-Control": "no-store" - }, - }); + + if (record.content != ip) { + const result = await cloudflare.updateRecord(record, ip); + + // return good if ip was changed + return new Response("good " + ip, { + status: 200, + headers: { + "Content-Type": "text/plain;charset=UTF-8", + "Cache-Control": "no-store" + }, + }); + } + + // return nochg if ip wasn't changed + return new Response("nochg " + ip, { + status: 200, + headers: { + "Content-Type": "text/plain;charset=UTF-8", + "Cache-Control": "no-store" + }, + }); } /** @@ -135,7 +138,7 @@ function basicAuthentication(request) { class UnauthorizedException { constructor(reason) { this.status = 401; - this.statusText = "Unauthorized"; + this.statusText = "badauth"; this.reason = reason; } } @@ -221,4 +224,4 @@ addEventListener("fetch", (event) => { }); }) ); -}); +}); \ No newline at end of file