Files
snap-dyndns-server/README.md
T

126 lines
5.2 KiB
Markdown

# Snap DynDNS Server using INWX domains
**Snap DynDNS Server** is a tiny Dynamic DNS service for domains managed by the registrar InterNetworX (inwx.de). It is a single Python CGI script without dependencies beyond the standard library, made for shared hosting such as Uberspace.
The server is a credential broker: your INWX API login only exists on the server, while each device gets its own random token that can update exactly one hostname. A device that gets lost or compromised can be cut off by deleting one section from the config.
It speaks the common dyndns2 protocol (HTTP Basic Auth, `myip`/`myipv6`, `good`/`nochg` answers), so routers like the AVM FritzBox, ddclient, and plain `curl` work. The matching client is [Snap DynDNS client](https://src.mehl.mx/mxmehl/snap-dyndns-client), a small shell script for cron.
## How it works
1. A device sends `GET /update.cgi` with its hostname and token as Basic Auth. Without `myip`/`myipv6`, the server uses the IP the request came from.
2. The server compares the SHA-256 of the token with the config.
3. If the IP equals the last one it stored, it answers `nochg` right away. INWX is not contacted, so polling every minute is cheap.
4. Otherwise it logs in to INWX, looks up the record by name and type, updates it, and only then stores the new IP. If INWX fails, the device gets `911` and retries on its next run.
The record (A and/or AAAA) has to exist at INWX already. The server picks A or AAAA depending on the IP family, and keeps the TTL you set there.
## Requirements
- Apache with CGI and `.htaccess` support (Uberspace 7 has both)
- Python 3.11 or newer, no packages needed
- An INWX account or API sub-account. If 2FA is enabled, the TOTP shared secret is needed as well. IP-restricted API accounts work if the server's IP is allowed.
## Installation on Uberspace
Clone the repository into a web-accessible directory, e.g. the document root of a dedicated (sub)domain:
```
git clone https://src.mehl.mx/mxmehl/snap-dyndns-server /var/www/virtual/myuser/dyndns.mydomain.net
```
The `.htaccess` in the repository enables CGI, passes the `Authorization` header to the script, and denies access to every file except `update.cgi`. If your Python is not at `/usr/bin/python3.14`, adjust the shebang of `update.cgi`.
Create the config **outside** the web root and make it private:
```
mkdir -p ~/etc/snap-dyndns
cp /var/www/virtual/myuser/dyndns.mydomain.net/config.toml.sample ~/etc/snap-dyndns/config.toml
chmod 600 ~/etc/snap-dyndns/config.toml
```
If you prefer another location, uncomment `SetEnv SNAP_DYNDNS_CONFIG` in `.htaccess`. The state file with the last known IPs is written next to the config (`state_file` in the config).
### Adding a device
Generate a token and its hash:
```
python3 -c 'import secrets,hashlib;t=secrets.token_urlsafe(32);print(t, hashlib.sha256(t.encode()).hexdigest())'
```
Put the hash into a new section of `config.toml` and give the token to the device:
```toml
[hosts."user1.example.com"]
token_sha256 = "<hash>"
```
The hostname is the Basic Auth user name, the token the password. The INWX zone is guessed as the last two labels of the hostname. For zones such as `example.co.uk` set `zone = "example.co.uk"` in that section.
## Usage
With curl (the `-u` option keeps the credentials out of the URL):
```
curl -fsS -u user1.example.com:TOKEN "https://dyndns.mydomain.net/update.cgi"
curl -fsS -u user1.example.com:TOKEN "https://dyndns.mydomain.net/update.cgi?myip=1.2.3.4"
```
Parameters, all optional:
- `hostname`: must equal the Basic Auth user name
- `myip`: IPv4 or IPv6 address to set, defaults to the address of the request
- `myipv6`: IPv6 address, to update A and AAAA in one request
Answers (HTTP status in brackets):
- `good <ip>` (200): the record was updated
- `nochg <ip>` (200): the IP is what the server stored last, INWX was not contacted
- `badauth` (401): unknown hostname or wrong token
- `nohost` (404): hostname mismatch, or the record does not exist at INWX
- `badip` (400): the IP could not be parsed
- `911` (502 or 500): INWX or the server config failed, see the Apache error log
### AVM FritzBox
Choose a custom DynDNS provider:
```
Update-URL: https://dyndns.mydomain.net/update.cgi?hostname=<domain>&myip=<ipaddr>&myipv6=<ip6addr>
Domain name: user1.example.com
User name: user1.example.com
Password: TOKEN
```
### ddclient
```
protocol=dyndns2
server=dyndns.mydomain.net
script=/dyn/update.cgi
ssl=yes
login=user1.example.com
password=TOKEN
user1.example.com
```
## Known limits
- The IP is only checked for syntax, private and reserved addresses are accepted.
- Missing records are not created, add them at INWX first.
- If you change a record at INWX by hand, the server does not notice. Delete `state.json` or change the IP to resync.
- Only one request updates at a time (a file lock is held during the INWX call), which is plenty for a personal setup.
## Development
```
uv run --python 3.14 python -m unittest -v
```
The tests use a fake INWX server. An additional smoke test runs against the INWX OT&E test system and is skipped unless `INWX_OTE_USER`, `INWX_OTE_PASS` and `INWX_OTE_HOST` (an existing A record in that account) are set in the environment. It restores the record afterwards.
## License
AGPL-3.0-or-later, see `LICENSE`.