feat!: complete refactoring of server #1

Merged
mxmehl merged 10 commits from v2 into master 2026-10-22 15:35:55 +02:00
3 changed files with 30 additions and 4 deletions
Showing only changes of commit 186fe7f9ee - Show all commits

No files matched your search

+2
View File
@@ -58,6 +58,8 @@ 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`). 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 ### 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`: 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`:
+20 -3
View File
@@ -224,11 +224,28 @@ def handle(env, cfg, update=inwx_update):
return 200, "\n".join(lines) return 200, "\n".join(lines)
INDEX_HTML = """<!DOCTYPE html>
<html lang="en-GB">
<head>
<meta charset="utf-8">
<title>Snap DynDNS Server</title>
</head>
<body>
<h1>Snap DynDNS Server</h1>
<p>This is a Dynamic DNS service. It is running with the <a href="https://src.mehl.mx/mxmehl/snap-dyndns-server">Snap DynDNS Server</a> software.</p>
<p>Please contact the site owner for more information on this specific service.</p>
</body>
</html>
"""
class RequestHandler(http.server.BaseHTTPRequestHandler): class RequestHandler(http.server.BaseHTTPRequestHandler):
timeout = 30 # do not let stalled connections hold a thread forever timeout = 30 # do not let stalled connections hold a thread forever
def do_GET(self): def do_GET(self):
url = urllib.parse.urlsplit(self.path) url = urllib.parse.urlsplit(self.path)
if url.path in ("/", "/nic", "/nic/"):
return self._reply(200, INDEX_HTML, "text/html")
if url.path != "/nic/update": if url.path != "/nic/update":
return self._reply(404, "not found") return self._reply(404, "not found")
try: try:
@@ -253,10 +270,10 @@ class RequestHandler(http.server.BaseHTTPRequestHandler):
return self.headers.get(name, "").split(",")[-1].strip() return self.headers.get(name, "").split(",")[-1].strip()
return self.client_address[0] return self.client_address[0]
def _reply(self, status, body): def _reply(self, status, body, content_type="text/plain"):
data = (body + "\n").encode() data = (body if body.endswith("\n") else body + "\n").encode()
self.send_response(status) self.send_response(status)
self.send_header("Content-Type", "text/plain; charset=utf-8") self.send_header("Content-Type", f"{content_type}; charset=utf-8")
self.send_header("Content-Length", str(len(data))) self.send_header("Content-Length", str(len(data)))
self.send_header("Cache-Control", "no-store") self.send_header("Cache-Control", "no-store")
if status == 401: if status == 401:
+8 -1
View File
@@ -334,9 +334,16 @@ token_sha256 = "{hashlib.sha256(TOKEN.encode()).hexdigest()}"
status, challenge, body = self.get(server, Authorization=basic(HOST, "bad")) status, challenge, body = self.get(server, Authorization=basic(HOST, "bad"))
self.assertEqual((status, challenge, body), (401, 'Basic realm="Snap DynDNS"', "badauth\n")) self.assertEqual((status, challenge, body), (401, 'Basic realm="Snap DynDNS"', "badauth\n"))
def test_start_page_without_credentials(self):
server = self.start()
for path in ("/", "/nic", "/nic/"):
status, challenge, body = self.get(server, path, Authorization="")
self.assertEqual((status, challenge), (200, None), path)
self.assertIn("<h1>Snap DynDNS Server</h1>", body)
def test_other_paths_are_not_found(self): def test_other_paths_are_not_found(self):
server = self.start() server = self.start()
self.assertEqual(self.get(server, "/")[0], 404) self.assertEqual(self.get(server, "/elsewhere")[0], 404)
def test_unreachable_inwx_is_911(self): def test_unreachable_inwx_is_911(self):
server = self.start(client_ip_header="X-Forwarded-For") server = self.start(client_ip_header="X-Forwarded-For")