Files
NODEDC_MISSION_CORE/apps/vps-host/ndc_vps/configuration.py
T

86 lines
3.3 KiB
Python

"""B2 231 / 0.01: documented SETP #1 only, with a surviving secondary uplink.
Never translate USB field numbers into SETP groups. No generic command escape.
GETP contains secrets; only the explicit endpoint projection may reach Core UI.
"""
import re
import time
def endpoint(value):
if not isinstance(value, dict) or set(value) != {"host", "port"}:
raise ValueError("invalid endpoint")
host, port = value["host"], value["port"]
if (
not isinstance(host, str)
or not 1 <= len(host) <= 32
or not re.fullmatch(r"[A-Za-z0-9]+(?:[.-][A-Za-z0-9]+)*", host)
or type(port) is not int
or not 1 <= port <= 65535
):
raise ValueError("invalid endpoint")
return {"host": host, "port": port}
def groups(reply):
if not isinstance(reply, str) or not reply.startswith("#") or len(reply) > 4096:
raise ValueError("invalid GETP")
result = {}
for part in reply.split("#")[1:]:
key, sep, value = part.partition("=")
if not sep or not key.isdecimal() or key in result:
raise ValueError("invalid GETP group")
result[key] = value
if not {"1", "2", "5", "7", "26"} <= result.keys():
raise ValueError("incomplete GETP")
return result
def server(value):
parts = value.split(",")
# Preserve the undocumented fifth value, accept only the qualified empty form.
if len(parts) != 5 or parts[2:] != ["0", "0", ""]:
raise ValueError("unsupported server settings")
return endpoint({"host": parts[0], "port": int(parts[1])})
def response(operation, reply, tracker):
"""Advance only from complete, scoped read evidence. Caller persists atomically."""
try:
actual = groups(reply)
primary, secondary = server(actual["1"]), server(actual["26"])
except (ValueError, KeyError):
return False
stage = operation["stage"]
if stage == "preflight":
if primary != operation["expected"] or secondary != endpoint(tracker):
operation.update(state="rejected", reason="configuration_changed")
elif primary == operation["target"]:
operation.update(state="verified", verified_at=time.time(), server1=primary)
else:
operation.update(before=actual, stage="write", state="queued")
elif stage == "write":
# SETP acknowledgement is never enough: issue a separate GETP after a drain interval.
operation.update(stage="verify", state="queued", not_before=time.time() + 3)
elif stage == "verify":
before = operation["before"]
preserved = {k: v for k, v in actual.items() if k != "1"} == {
k: v for k, v in before.items() if k != "1"
}
operation["after"] = actual
if primary == operation["target"] and preserved:
operation.update(state="verified", verified_at=time.time(), server1=primary)
elif actual == before:
operation.update(state="not_applied", reason="settings_unchanged")
else:
operation.update(state="uncertain", reason="readback_mismatch")
return True
def payload(operation):
if operation["stage"] == "write":
target = operation["target"]
return f"{operation['password']}*SETP*#1={target['host']},{target['port']}"
return operation["password"] + "*GETP"