Make HTTPS LuCI access use HTTP backend semantics

This commit is contained in:
kzolotarev95 committed 2026-07-01 13:50:28 +04:00
1 parent be421c1d8f
commit 7608d5da34
1 file changed
+21 -8
+21 -8
View File
@@ -2522,7 +2522,7 @@ class Handler(BaseHTTPRequestHandler):
headers["Host"] = f"127.0.0.1:{port}" headers["Host"] = f"127.0.0.1:{port}"
headers["X-Forwarded-Host"] = self.headers.get("Host", "") headers["X-Forwarded-Host"] = self.headers.get("Host", "")
headers["X-Forwarded-Prefix"] = f"/access/{urllib.parse.quote(router_id)}" headers["X-Forwarded-Prefix"] = f"/access/{urllib.parse.quote(router_id)}"
headers["X-Forwarded-Proto"] = "https" if getattr(self.server, "is_tls", False) or self.headers.get("X-Forwarded-Proto", "") == "https" else "http" headers["X-Forwarded-Proto"] = "http"
if body is not None: if body is not None:
headers["Content-Length"] = str(len(body)) headers["Content-Length"] = str(len(body))
@@ -2598,20 +2598,25 @@ def proxy_runtime_script(prefix):
const roots = ["/ubus", "/cgi-bin/luci", "/luci-static"]; const roots = ["/ubus", "/cgi-bin/luci", "/luci-static"];
window.__owrtRemotePrefix = prefix; window.__owrtRemotePrefix = prefix;
function fixUrl(url) { function fixUrl(url) {
if (typeof url !== "string" || !url) return url; const original = url;
if (url.startsWith(prefix + "/")) return url; let raw = "";
if (typeof url === "string") raw = url;
else if (url && typeof url.href === "string") raw = url.href;
else if (url && typeof url.url === "string") raw = url.url;
if (!raw) return original;
if (raw.startsWith(prefix + "/")) return raw;
try { try {
const absolute = /^[a-z][a-z0-9+.-]*:/i.test(url) || url.startsWith("//"); const absolute = /^[a-z][a-z0-9+.-]*:/i.test(raw) || raw.startsWith("//");
const parsed = absolute ? new URL(url, location.href) : null; const parsed = absolute ? new URL(raw, location.href) : null;
const value = parsed ? (parsed.pathname + parsed.search + parsed.hash) : url; const value = parsed ? (parsed.pathname + parsed.search + parsed.hash) : raw;
for (const root of roots) { for (const root of roots) {
if (value === root || value.startsWith(root + "/") || value.startsWith(root + "?")) { if (value === root || value.startsWith(root + "/") || value.startsWith(root + "?")) {
return prefix + value; return prefix + value;
} }
} }
if (parsed && parsed.hostname !== location.hostname) return url; if (parsed && parsed.hostname !== location.hostname) return raw;
} catch (e) {} } catch (e) {}
return url; return original;
} }
function fixElementUrl(el, attr) { function fixElementUrl(el, attr) {
if (!el) return; if (!el) return;
@@ -2624,6 +2629,8 @@ def proxy_runtime_script(prefix):
window.fetch = function(input, init) { window.fetch = function(input, init) {
if (typeof input === "string") { if (typeof input === "string") {
input = fixUrl(input); input = fixUrl(input);
} else if (input && typeof input.href === "string") {
input = fixUrl(input.href);
} else if (input && input.url) { } else if (input && input.url) {
const fixed = fixUrl(input.url); const fixed = fixUrl(input.url);
if (fixed !== input.url) input = new Request(fixed, input); if (fixed !== input.url) input = new Request(fixed, input);
@@ -2638,6 +2645,12 @@ def proxy_runtime_script(prefix):
return nativeOpen.apply(this, arguments); return nativeOpen.apply(this, arguments);
}; };
} }
if (navigator.sendBeacon) {
const nativeBeacon = navigator.sendBeacon.bind(navigator);
navigator.sendBeacon = function(url, data) {
return nativeBeacon(fixUrl(url), data);
};
}
document.addEventListener("click", function(ev) { document.addEventListener("click", function(ev) {
const a = ev.target && ev.target.closest ? ev.target.closest("a[href]") : null; const a = ev.target && ev.target.closest ? ev.target.closest("a[href]") : null;
fixElementUrl(a, "href"); fixElementUrl(a, "href");