feat!: complete refactoring of server (#1)
* make a python server * improve authentication by token/hash * better logging * add helper tool for new hosts Reviewed-on: #1 Co-authored-by: Max Mehl <mail@mehl.mx>
This commit was merged in pull request #1.
This commit is contained in:
1 parent
a1c55f4574
commit
0cd8438ab3
15 files changed
+1485
-327
No files matched your search
@@ -1,71 +1,153 @@
|
||||
# Snap DynDNS Server using INWX domains
|
||||
|
||||
**Snap DynDNS Server** enables you to set up your own simple Dynamic DNS service. It requires you to use domains managed by the registrar InterNetworX (inwx.de) and their API. This server may be simply customised to be able to work with other domain sellers and their specific APIs.
|
||||
**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.
|
||||
|
||||
It can set the IP of any existing domain entry, may it be IPv4 or IPv6 (and theoretically all kinds INWX supports). All domains of the clients have to be part of the same INWX account.
|
||||
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.
|
||||
|
||||
The idea of the service is to enable compatibility with as many client devices as possible. So clients only would need to send a GET or POST request to your service which contains their individual user data (which you can create freely) and their desired IP address for their domain.
|
||||
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](https://src.mehl.mx/mxmehl/snap-dyndns-client), a small shell script for cron.
|
||||
|
||||
This server has a client application as counterpart, named [Snap DynDNS client](https://src.mehl.mx/mxmehl/snap-dyndns-client). Using the latter, you will be able to retrieve the IP of your computer and send it to this service which in return updates the domain for you.
|
||||
## 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; the helper script below creates it for new hosts. The server picks A or AAAA depending on the IP family, and keeps the TTL of the record. 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
|
||||
|
||||
The installation requires the possibility to execute PHP and Bash files. It also requires curl to be installed. There's no need for a database to be installed as everything is handled by flat text files.
|
||||
- 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 code has been tested with PHP 5.5, CentOS 7, and Apache 2.2.15. Please be aware to migrate the `.htaccess` file if you use other webservers so the sensitive files like `config.cfg` and `users.db` are safe.
|
||||
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
|
||||
## Installation on Uberspace
|
||||
|
||||
Clone the git repository to your server and move them to a public web-accessible folder of your choice. Now rename/copy the files `config.cfg.sample` and `users.db.sample` to `config.cfg` and `users.db` respectively. Open them in an editor and modify the settings so they match your data.
|
||||
Clone the repository anywhere outside the web root:
|
||||
|
||||
### config.cfg
|
||||
```
|
||||
git clone https://src.mehl.mx/mxmehl/snap-dyndns-server ~/snap-dyndns-server
|
||||
```
|
||||
|
||||
In this file you have to put your inwx.de username and password. It is needed to login to the API and update the IP of your domain entry.
|
||||
Create the config and make it private:
|
||||
|
||||
### users.db
|
||||
```
|
||||
mkdir -p ~/etc/snap-dyndns
|
||||
cp ~/snap-dyndns-server/config.toml.sample ~/etc/snap-dyndns/config.toml
|
||||
chmod 600 ~/etc/snap-dyndns/config.toml
|
||||
```
|
||||
|
||||
In this file you can create users which can use your service. Unauthorized requests which do not match any of the content here will not work. Each line matches one existing single domain entry in your INWX account. It is not necessary the DynDNS domain names to be part of the same domain (`example.com`), they can also be different ones – they just have to be part of the same INWX account (please tell me if you need this to be improved, it shouldn't be a big deal).
|
||||
Create the service, using a free port between 1024 and 65535 (here 8080), in `~/etc/services.d/snap-dyndns.ini`:
|
||||
|
||||
#### Gather INWX domain ID
|
||||
```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
|
||||
```
|
||||
|
||||
Given you own the domain example.com which you can administrate in your INWX account. For each client, you can create a subdomain, in this example `user1.example.com` and `user2.example.com`. For INWX, each domain has a unique ID. To find out this ID, you will have to use a tool like Firebug or the in-built web inspector tools by Firefox or Chromium. Use the *inspect element* feature, hover over the subdomain line in your nameserver/DNS settings (where you can define the IPs, currently on `https://www.inwx.de/de/nameserver2`), and in the id attribute of the `<div>`-element you should find a string with around 9 numbers. This number is the domain entry's ID. Please see the screenshot for how it should look like.
|
||||
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
|
||||
```
|
||||
|
||||
Back in `users.db`, type in the domain name (`user1.example.com`) and its INWX ID. For username and password, you can freely create one and give to your users. Each request to your service requires the correct domain name, a username and a password. Only if all three match, the IP change request will be executed.
|
||||
Check with `supervisorctl status` and `uberspace web backend list`. Errors and requests are logged to `~/logs/` (or `supervisorctl tail snap-dyndns stderr`).
|
||||
|
||||
The service also answers `/` with a short start page, but with the backend registered for `/nic` only `/nic` and `/nic/` reach it. To show the page on the domain's root as well, register the whole domain instead: `uberspace web backend set yourdyndnsservice.net --http --port 8080`.
|
||||
|
||||
### 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`:
|
||||
|
||||
```toml
|
||||
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, prints the token once, and makes sure the DNS record exists at INWX:
|
||||
|
||||
```
|
||||
python3 ~/snap-dyndns-server/snap_dyndns_host.py user1.example.com
|
||||
```
|
||||
|
||||
If the A record does not exist at INWX yet, it is created with the TTL 300 and a placeholder address, which the first update of the device replaces. An existing record is left alone. Use `-t A,AAAA` to handle both types, `--ttl` for another TTL of new records, and `--no-dns` to skip INWX completely. If INWX cannot be reached, nothing is changed in the config. The helper needs the INWX login from the config and is therefore best run on the server itself, as an IP-restricted INWX account refuses logins from elsewhere.
|
||||
|
||||
Give the printed token to the device, for the [client](https://src.mehl.mx/mxmehl/snap-dyndns-client) it is the `hostname:token` entry shown in the output. For an existing host it asks before replacing the token (`-y` skips the question), and the old token 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
|
||||
|
||||
Please feel free to use [Snap DynDNS client](https://src.mehl.mx/mxmehl/snap-dyndns-client) which is the perfect combination for this server application.
|
||||
|
||||
Please note: You cannot define whether it shall be an A, AAAA or whatever record. You would have to do that in the INWX webpanel once, the ID should stay the same.
|
||||
|
||||
### curl
|
||||
|
||||
You can use curl with a POST request:
|
||||
|
||||
curl -X POST -d "domain=user1.example.com&user=myuser&pass=mypassword&ip=1.2.3.4" "https://yourdyndnsservice.net/update.php"
|
||||
|
||||
You can also use GET requests which is **not** recommended. The advantage of POST requests is that the request is encrypted via SSL if you use a HTTPS domain. GET requests can be intercepted quite easily which is bad because you send sensitive data. A GET request would look like this
|
||||
|
||||
curl -d "domain=user1.example.com&user=myuser&pass=mypassword&ip=1.2.3.4" "https://yourdyndnsservice.net/update.php"
|
||||
|
||||
This will send following data to your **Snap DynDNS server** which is running under the domain `https://yourdyndnsservice.net` in this example:
|
||||
|
||||
* `domain=user1.example.com`: This is the client's personal DynDNS domain
|
||||
* `user=myuser`: This is the client's username which you can define freely
|
||||
* `pass=mypassword`: This is the client's personal password which you can define freely.
|
||||
* `ip=1.2.3.4`: This is the IP which shall be set to this single domain
|
||||
|
||||
## AVM FritzBox
|
||||
|
||||
In the AVM Fritz!Boxes you can choose a custom DynDNS provider. In order to connect it with your Snap DynDNS server, use following data:
|
||||
With curl (the `-u` option keeps the credentials out of the URL):
|
||||
|
||||
```
|
||||
Update-URL: yourdyndnsservice.net/update.php?domain=<domain>&user=<username>&pass=<pass>&ip=<ipaddr>
|
||||
Domainname: user1.example.com
|
||||
Username: myuser
|
||||
Password: mypassword
|
||||
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"
|
||||
```
|
||||
|
||||
Be sure to not add an URI scheme like `https://`, the device will throw an error. The IP address is set automatically by the device. Note that you can also set your IPv6 address by using `<ip6addr>` in the URL.
|
||||
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.
|
||||
- The server itself never creates records, only the helper script does when adding a host.
|
||||
- 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 and removes the record it creates for the helper test.
|
||||
|
||||
## License
|
||||
|
||||
AGPL-3.0-or-later, see `LICENSE`.
|
||||
Reference in new issue
Block a user