Add HTTPS onboarding page; prefer .local host for service URLs
This commit is contained in:
32
pikit-api.py
32
pikit-api.py
@@ -174,6 +174,14 @@ def normalize_path(path: str | None) -> str:
|
||||
return p
|
||||
|
||||
|
||||
def default_host():
|
||||
"""Return preferred hostname (append .local if bare)."""
|
||||
host = socket.gethostname()
|
||||
if "." not in host:
|
||||
host = f"{host}.local"
|
||||
return host
|
||||
|
||||
|
||||
def dbg(msg):
|
||||
# Legacy debug file logging (when /boot/pikit-debug exists)
|
||||
if DEBUG_FLAG:
|
||||
@@ -226,7 +234,7 @@ def load_services():
|
||||
try:
|
||||
data = json.loads(SERVICE_JSON.read_text())
|
||||
# Normalize entries: ensure url built from port if missing
|
||||
host = socket.gethostname()
|
||||
host = default_host()
|
||||
for svc in data:
|
||||
svc_path = normalize_path(svc.get("path"))
|
||||
if svc_path:
|
||||
@@ -506,16 +514,8 @@ def set_updates_config(opts: dict):
|
||||
|
||||
|
||||
def detect_https(host, port):
|
||||
try:
|
||||
import ssl
|
||||
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
|
||||
ctx.check_hostname = False
|
||||
ctx.verify_mode = ssl.CERT_NONE
|
||||
with socket.create_connection((host, int(port)), timeout=1.5) as sock:
|
||||
with ctx.wrap_socket(sock, server_hostname=host):
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
"""Heuristic: known HTTPS ports or .local certs."""
|
||||
return int(port) in HTTPS_PORTS or str(host).lower().endswith(".local") or str(host).lower() == "pikit"
|
||||
|
||||
|
||||
def factory_reset():
|
||||
@@ -1176,6 +1176,12 @@ class Handler(BaseHTTPRequestHandler):
|
||||
if port:
|
||||
svc["online"] = port_online("127.0.0.1", port)
|
||||
svc["firewall_open"] = ufw_status_allows(port)
|
||||
# Rebuild URL with preferred host (adds .local)
|
||||
host = default_host()
|
||||
path = normalize_path(svc.get("path"))
|
||||
scheme = svc.get("scheme") or ("https" if detect_https(host, port) else "http")
|
||||
svc["scheme"] = scheme
|
||||
svc["url"] = f"{scheme}://{host}:{port}{path}"
|
||||
services.append(svc)
|
||||
self._send(200, {"services": services})
|
||||
elif self.path.startswith("/api/updates/auto"):
|
||||
@@ -1294,7 +1300,7 @@ class Handler(BaseHTTPRequestHandler):
|
||||
services = load_services()
|
||||
if any(s.get("port") == port for s in services):
|
||||
return self._send(400, {"error": "port already exists"})
|
||||
host = socket.gethostname()
|
||||
host = default_host()
|
||||
scheme = payload.get("scheme")
|
||||
if scheme not in ("http", "https"):
|
||||
scheme = "https" if detect_https(host, port) else "http"
|
||||
@@ -1368,7 +1374,7 @@ class Handler(BaseHTTPRequestHandler):
|
||||
svc["port"] = new_port_int
|
||||
target_port = new_port_int
|
||||
port_changed = True
|
||||
host = socket.gethostname()
|
||||
host = default_host()
|
||||
if new_path is not None:
|
||||
path = normalize_path(new_path)
|
||||
if path:
|
||||
|
||||
Reference in New Issue
Block a user