Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return correct response for updates #3

Merged
merged 3 commits into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.<worker-subdomain>.workers.dev/update?hostname=%h&ip=%i`
- `Server`: the Cloudflare Worker route `<worker-name>.<worker-subdomain>.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.
Expand Down
49 changes: 26 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @param {Request} request
* @returns {Promise<Response>}
*/
async function handleRequest(request) {
async function handleRequest(request) {
const { protocol, pathname } = new URL(request.url);

// Require HTTPS (TLS) connection to be secure.
Expand All @@ -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);
Expand All @@ -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 });
}

/**
Expand All @@ -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"
},
});
}

/**
Expand Down Expand Up @@ -135,7 +138,7 @@ function basicAuthentication(request) {
class UnauthorizedException {
constructor(reason) {
this.status = 401;
this.statusText = "Unauthorized";
this.statusText = "badauth";
this.reason = reason;
}
}
Expand Down Expand Up @@ -221,4 +224,4 @@ addEventListener("fetch", (event) => {
});
})
);
});
});