Files
snap-dyndns-server/README.md
T

7.2 KiB

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 file without dependencies beyond the standard library, made to run as a small service on 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) on /nic/update, so routers like the AVM FritzBox, ddclient, and plain curl work. The matching client is Snap DynDNS client, a small shell script for cron.

How it works

  1. A device sends GET /nic/update 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. The connection to INWX uses IPv4 only, so an IP-restricted INWX API account only needs the server's IPv4 address on its allow-list.

Requirements

  • Python 3.11 or newer, no packages needed
  • A way to run a service behind HTTPS, e.g. supervisord plus a reverse proxy (Uberspace has both)
  • 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 IPv4 address is allowed.

The server speaks plain HTTP and expects a reverse proxy in front that terminates TLS. Never expose it without HTTPS, as the token is sent with every request.

Installation on Uberspace

Clone the repository anywhere outside the web root:

git clone https://src.mehl.mx/mxmehl/snap-dyndns-server ~/snap-dyndns-server

Create the config and make it private:

mkdir -p ~/etc/snap-dyndns
cp ~/snap-dyndns-server/config.toml.sample ~/etc/snap-dyndns/config.toml
chmod 600 ~/etc/snap-dyndns/config.toml

Create the service, using a free port between 1024 and 65535 (here 8080), in ~/etc/services.d/snap-dyndns.ini:

[program:snap-dyndns]
command=/usr/bin/python3.14 %(ENV_HOME)s/snap-dyndns-server/snap_dyndns.py --config %(ENV_HOME)s/etc/snap-dyndns/config.toml --port 8080
autostart=yes
autorestart=yes

If your Python is not at /usr/bin/python3.14, adjust the path. Start the service and register it as a web backend for the /nic path of your domain:

supervisorctl reread
supervisorctl update
uberspace web backend set yourdyndnsservice.net/nic --http --port 8080

Check with supervisorctl status and uberspace web backend list. Errors and requests are logged to ~/logs/ (or supervisorctl tail snap-dyndns stderr).

Client IP address

Behind the reverse proxy the TCP peer is the proxy, not the device. On Uberspace the proxy puts the device's address into X-Forwarded-For, which is already set in config.toml.sample:

client_ip_header = "X-Forwarded-For"

The server uses the last entry of that header, the one added by your own proxy. Entries before it are controlled by the client and ignored. If the header is missing, or the address is not a global one, the answer is badip and a log line instead of a wrong DNS record. Check once that the good <ip> answer shows your real public IP.

With another proxy, stop the service (supervisorctl stop snap-dyndns), run nc -l 8080 and open the URL once from outside. nc prints the raw request including all headers, so you can see in which one your public IP appears.

Adding a device

The helper creates a random token, stores only its SHA-256 in the config and prints the token once:

python3 ~/snap-dyndns-server/snap_dyndns_host.py user1.example.com

Give the printed token to the device, for the client it is the hostname:token entry shown in the output. The same command for an existing host replaces its token, and the old one stops working immediately. Use -c for a config in another location and -z example.co.uk for a new host whose INWX zone is not the last two labels of its name. Comments and the rest of the config stay untouched. The config is read on every request, so no restart is needed.

The hostname is the Basic Auth user name, the token the password. Without the helper, generate a token and hash yourself with python3 -c 'import secrets,hashlib;t=secrets.token_urlsafe(32);print(t, hashlib.sha256(t.encode()).hexdigest())' and add a section [hosts."user1.example.com"] with token_sha256 = "<hash>".

Usage

With curl (the -u option keeps the credentials out of the URL):

curl -fsS -u user1.example.com:TOKEN "https://yourdyndnsservice.net/nic/update"
curl -fsS -u user1.example.com:TOKEN "https://yourdyndnsservice.net/nic/update?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, or the address of the request is not a global one
  • 911 (502 or 500): INWX or the server config failed, see the log

AVM FritzBox

Choose a custom DynDNS provider:

Update-URL: https://yourdyndnsservice.net/nic/update?hostname=<domain>&myip=<ipaddr>&myipv6=<ip6addr>
Domain name: user1.example.com
User name: user1.example.com
Password: TOKEN

ddclient

protocol=dyndns2
server=yourdyndnsservice.net
ssl=yes
login=user1.example.com
password=TOKEN
user1.example.com

Known limits

  • Explicit myip/myipv6 values are only checked for syntax, private and reserved addresses are accepted there.
  • 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 lock is held during the INWX call), which is plenty for a personal setup.
  • The built-in HTTP server is meant to sit behind a reverse proxy. Do not expose its port directly.

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.