Add UI DNS challenge issuance and automatic home NPM renewal
All checks were successful
Test and publish Homegate / checks (push) Successful in 29s
Test and publish Homegate / publish (push) Successful in 12m43s

This commit is contained in:
2026-10-15 05:42:15 +03:00
parent eeffcfb616
commit 5661de72b7
22 changed files with 1051 additions and 43 deletions

View File

@@ -18,6 +18,7 @@ from urllib.parse import urlsplit
import yaml
import homegate as h
from dns_challenge import DNSChallenge, providers as dns_providers
LOG = logging.getLogger("homegate.ui")
BASE = Path(__file__).resolve().parent
@@ -52,6 +53,7 @@ class Panel:
raise UIError("UI password must contain at least 12 characters; run setup.sh")
if self.config.exists() or not self.connections_file:
h.load_config(self.config)
self.dns_challenge = DNSChallenge(self.control, self.connect_npm)
def config_data(self):
if self.connections_file and not self.config.exists():
@@ -178,7 +180,7 @@ class Panel:
self.request_sync()
return result
def certificates(self, remote=False):
def connect_npm(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"
@@ -191,12 +193,27 @@ class Panel:
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"])
return client
except Exception:
client.close()
raise
def certificates(self, remote=False):
client = self.connect_npm(remote)
try:
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 dns_providers(self):
client = self.connect_npm()
try:
return dns_providers(client)
finally:
client.close()
def save(self, value, revision):
h.validate_config(value)
data = yaml.safe_dump(value, sort_keys=False, allow_unicode=True).encode()
@@ -276,10 +293,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", "/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]
if self.command == "GET" and path in ("/", "/app.js", "/settings.js", "/certificates.js", "/dns-challenge.js", "/style.css", "/favicon.svg"):
name = {"/": "index.html", "/app.js": "app.js", "/settings.js": "settings.js", "/certificates.js": "certificates.js", "/dns-challenge.js": "dns-challenge.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", "certificates.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", "dns-challenge.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":
@@ -313,6 +330,10 @@ def handler_for(panel):
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/dns-providers":
return self.respond(200, panel.dns_providers())
if path == "/api/dns-challenge":
return self.respond(200, panel.dns_challenge.status())
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")
@@ -337,6 +358,10 @@ def handler_for(panel):
return self.respond(200, panel.save_settings(body["config"], body["connections"], body["revision"]))
if path == "/api/sync":
return self.respond(202, panel.request_sync())
if path == "/api/dns-challenge":
return self.respond(202, panel.dns_challenge.start(body))
if path == "/api/dns-challenge/check":
return self.respond(202, panel.dns_challenge.check())
if path == "/api/logout":
panel.sessions.pop(token, None)
return self.respond(200, {"ok": True}, cookie="")