chore: improve logging and type fixes

This commit is contained in:
mxmehl committed 2026-10-22 14:09:43 +02:00
1 parent 8bc1cfaf27
commit c43f0dd699
2 files changed
+106 -34

No files matched your search

+25 -6
View File
@@ -27,6 +27,7 @@ import time
import tomllib
import urllib.parse
import urllib.request
from typing import cast
API_LIVE = "https://api.domrobot.com/jsonrpc/"
DEFAULT_CONFIG = "~/etc/snap-dyndns/config.toml"
@@ -191,7 +192,10 @@ def handle(env, cfg, update=inwx_update):
return 400, "badip"
# Guards against publishing the proxy's address if client_ip_header is wrong
if not given and not ip.is_global:
print(f"snap-dyndns: {host}: connection address {ip} is not global, check client_ip_header", file=sys.stderr)
print(
f"snap-dyndns: {host}: connection address {ip} is not global, check client_ip_header",
file=sys.stderr,
)
return 400, "badip"
# shortcut: explicit myip/myipv6 are only syntax-checked (private ranges accepted), tighten if clients misbehave
ips["A" if ip.version == 4 else "AAAA"] = str(ip)
@@ -218,6 +222,8 @@ def handle(env, cfg, update=inwx_update):
print(f"snap-dyndns: {host}: {e}", file=sys.stderr)
return 502, "911"
# shortcut: cache goes stale if the record is edited by hand at INWX, delete state.json to resync
old_ip = state.get(host, {}).get(rtype, "unknown")
print(f"snap-dyndns: {host}: {rtype} {old_ip} -> {ip}", file=sys.stderr)
state.setdefault(host, {})[rtype] = ip
_save_state(state_path, state)
lines.append(f"good {ip}")
@@ -239,6 +245,10 @@ INDEX_HTML = """<!DOCTYPE html>
"""
class Server(http.server.ThreadingHTTPServer):
config_path: str
class RequestHandler(http.server.BaseHTTPRequestHandler):
timeout = 30 # do not let stalled connections hold a thread forever
@@ -250,7 +260,7 @@ class RequestHandler(http.server.BaseHTTPRequestHandler):
return self._reply(404, "not found")
try:
# Read per request, so devices can be added without restarting
with open(self.server.config_path, "rb") as f:
with open(cast(Server, self.server).config_path, "rb") as f:
cfg = tomllib.load(f)
env = {
"QUERY_STRING": url.query,
@@ -283,15 +293,24 @@ class RequestHandler(http.server.BaseHTTPRequestHandler):
def make_server(config_path, port, host="0.0.0.0"):
server = http.server.ThreadingHTTPServer((host, port), RequestHandler)
server = Server((host, port), RequestHandler)
server.config_path = config_path
return server
def main(argv=None):
parser = argparse.ArgumentParser(description="Snap DynDNS Server (serves /nic/update)")
parser.add_argument("-c", "--config", default=DEFAULT_CONFIG, help=f"config file (default: {DEFAULT_CONFIG})")
parser.add_argument("-p", "--port", type=int, default=8080, help="port to listen on (default: 8080)")
parser = argparse.ArgumentParser(
description="Snap DynDNS Server (serves /nic/update)"
)
parser.add_argument(
"-c",
"--config",
default=DEFAULT_CONFIG,
help=f"config file (default: {DEFAULT_CONFIG})",
)
parser.add_argument(
"-p", "--port", type=int, default=8080, help="port to listen on (default: 8080)"
)
args = parser.parse_args(argv)
path = os.path.expanduser(args.config)
try: # fail fast on a broken config; it is re-read on every request later