Add NPM certificate selection and read-only VDS certificate renewal sync
All checks were successful
Test and publish Homegate / checks (push) Successful in 28s
Test and publish Homegate / publish (push) Successful in 12m47s

This commit is contained in:
2026-10-15 05:12:03 +03:00
parent b305cc1ba8
commit eeffcfb616
18 changed files with 838 additions and 49 deletions

View File

@@ -117,8 +117,8 @@ class Panel:
history = h.read_json(self.control / "ui-history.json", [])
connections = h.read_json(self.connections_file, {}) if self.connections_file else None
public_connections = None if connections is None else {
**{key: connections.get(key, "") for key in ("npm_url", "npm_identity", "pihole_url", "ssh_public_url", "api_ca_pem")},
**{key + "_set": bool(connections.get(key)) for key in ("npm_password", "pihole_password")},
**{key: connections.get(key, "") for key in ("npm_url", "npm_identity", "pihole_url", "ssh_public_url", "api_ca_pem", "remote_npm_url", "remote_npm_identity")},
**{key + "_set": bool(connections.get(key)) for key in ("npm_password", "pihole_password", "remote_npm_password")},
}
return {"config": value, "revision": revision, "mode": "local" if self.local else "lan",
"setup_required": self.connections_file is not None and (not self.config.exists() or not self.connections_file.exists()),
@@ -136,7 +136,8 @@ class Panel:
if self.config_data()[1] != revision:
raise UIError("Настройки уже изменились. Обновите страницу.", 409)
values = h.read_json(self.connections_file, {})
allowed = {"npm_url", "npm_identity", "npm_password", "pihole_url", "pihole_password", "ssh_public_url", "api_ca_pem", "clear_pihole_password"}
allowed = {"npm_url", "npm_identity", "npm_password", "pihole_url", "pihole_password", "ssh_public_url", "api_ca_pem", "clear_pihole_password",
"remote_npm_url", "remote_npm_identity", "remote_npm_password"}
h.require(not set(connections) - allowed, "Unknown connection field")
clear_pihole_password = connections.get("clear_pihole_password", False)
h.require(isinstance(clear_pihole_password, bool), "clear_pihole_password must be boolean")
@@ -154,6 +155,15 @@ class Panel:
continue
client = h.API(values.get(key, ""), key.removesuffix("_url").upper())
client.close()
if values.get("remote_npm_url"):
h.require(urlsplit(values["remote_npm_url"]).scheme == "https",
"Для NPM на VDS укажите HTTPS URL панели")
h.require(values.get("remote_npm_identity") and values.get("remote_npm_password"),
"Укажите логин и пароль NPM на VDS")
client = h.RemoteNPM(values["remote_npm_url"], "Remote NPM")
client.close()
h.require(client.base != values["npm_url"].rstrip("/") + "/api",
"Домашний NPM и NPM на VDS должны быть разными серверами")
values.setdefault("pihole_password", "")
if clear_pihole_password:
values["pihole_password"] = ""
@@ -168,6 +178,25 @@ class Panel:
self.request_sync()
return result
def certificates(self, remote=False):
h.require(self.connections_file is not None, "Сначала настройте подключения через UI")
values = h.read_json(self.connections_file, {})
prefix = "remote_npm" if remote else "npm"
h.require(values.get(prefix + "_url"), "Сначала сохраните подключение к NPM в Настройках")
verify = True
if not remote and values.get("api_ca_pem"):
ca_path = self.connections_file.with_name("management-ca.pem")
h.atomic(ca_path, values["api_ca_pem"].encode())
verify = str(ca_path)
client = (h.RemoteNPM if remote else h.NPM)(values[prefix + "_url"], "Remote NPM" if remote else "NPM", verify)
try:
client.login(values[prefix + "_identity"], values[prefix + "_password"])
certs = client.call("GET", "/nginx/certificates")
return {"certificates": [h.certificate_info(c) for c in certs
if not remote or c.get("provider") == "letsencrypt"]}
finally:
client.close()
def save(self, value, revision):
h.validate_config(value)
data = yaml.safe_dump(value, sort_keys=False, allow_unicode=True).encode()
@@ -247,10 +276,10 @@ def handler_for(panel):
path = urlsplit(self.path).path
if self.command == "GET" and path == "/healthz":
return self.respond(200, {"ok": True})
if self.command == "GET" and path in ("/", "/app.js", "/settings.js", "/style.css", "/favicon.svg"):
name = {"/": "index.html", "/app.js": "app.js", "/settings.js": "settings.js", "/style.css": "style.css", "/favicon.svg": "favicon.svg"}[path]
if self.command == "GET" and path in ("/", "/app.js", "/settings.js", "/certificates.js", "/style.css", "/favicon.svg"):
name = {"/": "index.html", "/app.js": "app.js", "/settings.js": "settings.js", "/certificates.js": "certificates.js", "/style.css": "style.css", "/favicon.svg": "favicon.svg"}[path]
content_type = {"index.html": "text/html; charset=utf-8", "app.js": "text/javascript; charset=utf-8",
"settings.js": "text/javascript; charset=utf-8", "style.css": "text/css; charset=utf-8", "favicon.svg": "image/svg+xml"}[name]
"settings.js": "text/javascript; charset=utf-8", "certificates.js": "text/javascript; charset=utf-8", "style.css": "text/css; charset=utf-8", "favicon.svg": "image/svg+xml"}[name]
return self.respond(200, (BASE / "ui" / "dist" / name).read_bytes(), content_type)
token, session = panel.session(self.headers.get("Cookie"))
if self.command == "GET" and path == "/api/session":
@@ -282,6 +311,8 @@ def handler_for(panel):
if self.command == "GET":
if path == "/api/state":
return self.respond(200, panel.status())
if path in ("/api/npm-certificates", "/api/remote-certificates"):
return self.respond(200, panel.certificates(remote=path == "/api/remote-certificates"))
if path == "/api/ssh-config":
text = h.render_ssh(h.ssh_entries(h.load_config(panel.config)))
return self.respond(200, text.encode(), "text/plain; charset=utf-8")