feat: also add page for /
This commit is contained in:
3 files changed
+30
-4
No files matched your search
@@ -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`).
|
||||
|
||||
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`:
|
||||
|
||||
+20
-3
@@ -224,11 +224,28 @@ def handle(env, cfg, update=inwx_update):
|
||||
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):
|
||||
timeout = 30 # do not let stalled connections hold a thread forever
|
||||
|
||||
def do_GET(self):
|
||||
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":
|
||||
return self._reply(404, "not found")
|
||||
try:
|
||||
@@ -253,10 +270,10 @@ class RequestHandler(http.server.BaseHTTPRequestHandler):
|
||||
return self.headers.get(name, "").split(",")[-1].strip()
|
||||
return self.client_address[0]
|
||||
|
||||
def _reply(self, status, body):
|
||||
data = (body + "\n").encode()
|
||||
def _reply(self, status, body, content_type="text/plain"):
|
||||
data = (body if body.endswith("\n") else body + "\n").encode()
|
||||
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("Cache-Control", "no-store")
|
||||
if status == 401:
|
||||
|
||||
+8
-1
@@ -334,9 +334,16 @@ token_sha256 = "{hashlib.sha256(TOKEN.encode()).hexdigest()}"
|
||||
status, challenge, body = self.get(server, Authorization=basic(HOST, "bad"))
|
||||
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):
|
||||
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):
|
||||
server = self.start(client_ip_header="X-Forwarded-For")
|
||||
|
||||
Reference in new issue
Block a user