@@ -0,0 +1,88 @@
|
||||
"""Network settings snapshot for admin (LAN name, public URL, mDNS)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import socket
|
||||
from typing import Any, Dict
|
||||
|
||||
from app.services.lan_network import detect_lan_ip, parse_wesp_listen
|
||||
from app.services.mdns_service import mdns_status
|
||||
from config import (
|
||||
DEFAULT_NETWORK_LOCAL_HOSTNAME,
|
||||
get_network_settings_path,
|
||||
network_env_lock_flags,
|
||||
normalize_local_hostname,
|
||||
read_network_settings_merged,
|
||||
)
|
||||
|
||||
|
||||
def build_public_url_from_hostname(
|
||||
local_hostname: str,
|
||||
port: int,
|
||||
*,
|
||||
scheme: str = "http",
|
||||
) -> str:
|
||||
host = normalize_local_hostname(local_hostname)
|
||||
if not host:
|
||||
return ""
|
||||
sch = (scheme or "http").strip().rstrip(":/") or "http"
|
||||
p = int(port)
|
||||
if p in (80, 443):
|
||||
return f"{sch}://{host}"
|
||||
return f"{sch}://{host}:{p}"
|
||||
|
||||
|
||||
def network_settings_snapshot(app) -> Dict[str, Any]:
|
||||
locks = network_env_lock_flags()
|
||||
merged = read_network_settings_merged()
|
||||
cfg = app.config
|
||||
|
||||
listen_host, listen_port = parse_wesp_listen(str(cfg.get("WESP_LISTEN") or ""))
|
||||
lan_ip = detect_lan_ip()
|
||||
os_hostname = ""
|
||||
try:
|
||||
os_hostname = socket.gethostname()
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
local_hostname = str(cfg.get("WESP_NETWORK_LOCAL_HOSTNAME") or "").strip()
|
||||
if not local_hostname:
|
||||
local_hostname = normalize_local_hostname(
|
||||
merged.get("local_hostname") or DEFAULT_NETWORK_LOCAL_HOSTNAME
|
||||
)
|
||||
else:
|
||||
local_hostname = normalize_local_hostname(local_hostname)
|
||||
|
||||
public_base_url = str(cfg.get("KIOSK_PUBLIC_BASE_URL") or "").strip().rstrip("/")
|
||||
if not public_base_url:
|
||||
public_base_url = str(merged.get("public_base_url") or "").strip().rstrip("/")
|
||||
|
||||
mdns_enabled = bool(cfg.get("WESP_MDNS_ENABLED", False))
|
||||
if "mdns_enabled" in merged and not locks.get("mdns_enabled"):
|
||||
mdns_enabled = bool(merged.get("mdns_enabled"))
|
||||
|
||||
suggested = build_public_url_from_hostname(local_hostname, listen_port)
|
||||
sync_url_by_ip = ""
|
||||
if lan_ip:
|
||||
sync_url_by_ip = build_public_url_from_hostname(lan_ip, listen_port)
|
||||
|
||||
mdns = mdns_status()
|
||||
return {
|
||||
"local_hostname": local_hostname,
|
||||
"local_hostname_locked_by_env": bool(locks.get("local_hostname")),
|
||||
"public_base_url": public_base_url,
|
||||
"public_base_url_locked_by_env": bool(locks.get("public_base_url")),
|
||||
"mdns_enabled": mdns_enabled,
|
||||
"mdns_enabled_locked_by_env": bool(locks.get("mdns_enabled")),
|
||||
"default_local_hostname": DEFAULT_NETWORK_LOCAL_HOSTNAME,
|
||||
"suggested_public_url": suggested,
|
||||
"sync_url_by_ip": sync_url_by_ip,
|
||||
"detected": {
|
||||
"lan_ip": lan_ip or None,
|
||||
"os_hostname": os_hostname or None,
|
||||
"listen_host": listen_host,
|
||||
"listen_port": listen_port,
|
||||
**mdns,
|
||||
},
|
||||
"settings_path": str(get_network_settings_path()),
|
||||
}
|
||||
Reference in New Issue
Block a user