From ea9dfffcec8d8ff3aa00d9af2599a71743450cff Mon Sep 17 00:00:00 2001 From: kiskaadee Date: Thu, 25 Jun 2026 17:09:18 -0500 Subject: [PATCH] chore(dev): lint monitor.py and update flake dependencies - Refactor `hosts/desktop/monitor.py` to resolve PEP8 violations (line lengths, trailing whitespace, blank line structure). - Update `flake.lock` to incorporate the latest upstream changes for `antigravity`, `DankMaterialShell`, and `home-manager`. --- flake.lock | 18 +++++++-------- hosts/desktop/monitor.py | 49 ++++++++++++++++++++-------------------- 2 files changed, 33 insertions(+), 34 deletions(-) diff --git a/flake.lock b/flake.lock index e7770d6..fa0e01b 100644 --- a/flake.lock +++ b/flake.lock @@ -8,11 +8,11 @@ ] }, "locked": { - "lastModified": 1782288772, - "narHash": "sha256-cLZ/6ZCuX5kyU/g/ppmGYGC5SYDQnwEcBCg934bU0Mo=", + "lastModified": 1782375110, + "narHash": "sha256-VgQr+Y1b3C9bCn68DVyrzJiLvF14Cxpcf66HV6MNkVM=", "owner": "jacopone", "repo": "antigravity-nix", - "rev": "9339da3b94d8691a92e101e7be45f83ea5012f69", + "rev": "738d61a4b7d88473adedda7b5dbc7b6bc5704020", "type": "github" }, "original": { @@ -49,11 +49,11 @@ ] }, "locked": { - "lastModified": 1782274482, - "narHash": "sha256-QzEtKa5ROX8cNYDymcjB6oTfRBpEApGQ7PFsF85Gl5s=", + "lastModified": 1782412048, + "narHash": "sha256-iLCNbmbH1ltOrqBUf8ccN0DaPkFr8JrnwnEtoy1grog=", "owner": "AvengeMedia", "repo": "DankMaterialShell", - "rev": "1a39b7f66ca2b8df23a546fe312619e05c61a13c", + "rev": "016527fda910f21b36c079051def38b12c6d0aab", "type": "github" }, "original": { @@ -103,11 +103,11 @@ ] }, "locked": { - "lastModified": 1782233665, - "narHash": "sha256-h/xOtrByoA/Ak1lWHn0O1lVZz4qWYbwOSLQ8YSwQO0I=", + "lastModified": 1782423922, + "narHash": "sha256-qPNd6lUohHP5gcJhqQ7rLV87RwIx0xYR2A4Frb9Zjc4=", "owner": "nix-community", "repo": "home-manager", - "rev": "062581938b4a378a82dfbb294b494808157153a1", + "rev": "5d320ab301cfaaca7d32514f13815d19d109f5f4", "type": "github" }, "original": { diff --git a/hosts/desktop/monitor.py b/hosts/desktop/monitor.py index 229a119..964ba00 100644 --- a/hosts/desktop/monitor.py +++ b/hosts/desktop/monitor.py @@ -1,13 +1,13 @@ """ Dynu DDNS Smart IP Change Monitor and Updater. -This script determines the current public IPv4 address by querying multiple external -resolvers sequentially. It compares the discovered IP against the last recorded -successful IP in a local history log. If a change is detected, it triggers the -`ddclient.service` on-demand to perform the DNS record update. +This script determines the current public IPv4 address by querying multiple +external resolvers sequentially. It compares the discovered IP against the last +recorded successful IP in a local history log. If a change is detected, it +triggers the `ddclient.service` on-demand to perform the DNS record update. -This local-first state verification prevents making redundant API requests to the -DNS provider, avoiding potential rate-limiting, IP bans, or API abuse flags. +This local-first state verification prevents making redundant API requests to +the DNS provider, avoiding rate-limiting, IP bans, or API abuse flags. """ import urllib.request @@ -22,7 +22,7 @@ from datetime import datetime # Defaults to a state directory managed securely by the systemd service unit. HISTORY_FILE = os.environ.get("HISTORY_FILE", "/var/lib/dynu/ip_history.jsonl") -# Fallback sequence of external reflection services to discover the public WAN IP. +# Fallback sequence of external services to discover the public WAN IP. PROVIDERS = [ "https://api.ipify.org", "https://ifconfig.me/ip", @@ -34,9 +34,9 @@ PROVIDERS = [ def is_valid_public_ipv4(ip): """ Validates that a string is a properly formatted public IPv4 address. - - This function excludes standard private, loopback, link-local, and CGNAT blocks - to avoid recording temporary, local, or invalid network states. + + This function excludes standard private, loopback, link-local, and CGNAT + blocks to avoid recording temporary, local, or invalid network states. """ match = re.match(r"^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$", ip) if not match: @@ -45,7 +45,7 @@ def is_valid_public_ipv4(ip): if any(x > 255 for x in octets): return False a, b, c, d = octets - + # RFC 1918 Private ranges: 10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12 if a == 10: return False @@ -53,26 +53,26 @@ def is_valid_public_ipv4(ip): return False if a == 172 and 16 <= b <= 31: return False - + # Carrier-Grade NAT (CGNAT) RFC 6598: 100.64.0.0/10 if a == 100 and 64 <= b <= 127: return False - + # Loopback address range: 127.0.0.0/8 if a == 127: return False - + # Link-local addresses: 169.254.0.0/16 if a == 169 and b == 254: return False - + return True def get_public_ip(): """ - Queries public IP providers sequentially until a valid public IPv4 is returned. - + Queries public IP providers until a valid public IPv4 is returned. + Returns: str: The validated public IP string, or None if all providers fail. """ @@ -85,7 +85,7 @@ def get_public_ip(): 'User-Agent': 'Mozilla/5.0 (NixOS WAN Monitor)' } ) - # Fetch content with a strict 5 second timeout to prevent hanging the systemd task + # Fetch with 5s timeout to avoid hanging the systemd service with urllib.request.urlopen(req, timeout=5) as response: ip = response.read().decode('utf-8').strip() if is_valid_public_ipv4(ip): @@ -94,7 +94,7 @@ def get_public_ip(): errors.append(f"{provider}: invalid public IPv4 '{ip}'") except Exception as e: errors.append(f"{provider}: {str(e)}") - + print( f"Error: All IP discovery providers failed. Details: {errors}", file=sys.stderr @@ -104,8 +104,8 @@ def get_public_ip(): def get_last_recorded_ip(): """ - Reads the history log from the bottom up to locate the last successful IP address. - + Reads the history log to locate the last successful IP address. + Returns: str: The last successfully updated IP, or None if no log exists yet. """ @@ -151,12 +151,12 @@ def record_ip(ip, status, details=None): def trigger_ddclient(): """ Invokes the systemd service unit for `ddclient` on-demand. - + Returns: tuple: (bool, str) representing (success_boolean, error_details_string) """ try: - # Run ddclient as a one-shot activation via systemd to perform the API call + # Run ddclient as a one-shot activation via systemd to perform API call res = subprocess.run( ["systemctl", "start", "ddclient.service"], capture_output=True, @@ -184,7 +184,7 @@ def main(): last_ip = get_last_recorded_ip() if current_ip == last_ip: - # IP matches the last recorded working IP; do nothing to conserve API quota + # IP matches last recorded working IP; do nothing to conserve quota sys.exit(0) print(f"IP rotation detected! Old: {last_ip}, New: {current_ip}") @@ -203,4 +203,3 @@ def main(): if __name__ == "__main__": main() -