Rewrite LuCI proxy script paths

This commit is contained in:
kzolotarev95 committed 2026-06-29 04:52:34 +04:00
1 parent 9a0dd5a943
commit e8bca9eddb
1 file changed
+27 -2
+27 -2
View File
@@ -836,7 +836,7 @@ class Handler(BaseHTTPRequestHandler):
body = self.read_body() if self.command in ("POST", "PUT", "PATCH") else None body = self.read_body() if self.command in ("POST", "PUT", "PATCH") else None
headers = {} headers = {}
skip = {"host", "connection", "keep-alive", "proxy-authenticate", "proxy-authorization", "te", "trailers", "transfer-encoding", "upgrade", "content-length"} skip = {"host", "connection", "keep-alive", "proxy-authenticate", "proxy-authorization", "te", "trailers", "transfer-encoding", "upgrade", "content-length", "accept-encoding"}
for key, value in self.headers.items(): for key, value in self.headers.items():
if key.lower() in skip: if key.lower() in skip:
continue continue
@@ -866,7 +866,7 @@ class Handler(BaseHTTPRequestHandler):
if low == "set-cookie": if low == "set-cookie":
value = rewrite_cookie_path(value, prefix + "/") value = rewrite_cookie_path(value, prefix + "/")
resp_headers.append((key, value)) resp_headers.append((key, value))
if "text/html" in content_type: if should_rewrite_body(content_type):
resp_body = rewrite_html(resp_body, prefix) resp_body = rewrite_html(resp_body, prefix)
self.send_bytes(resp.status, resp_body, content_type or "application/octet-stream", resp_headers) self.send_bytes(resp.status, resp_body, content_type or "application/octet-stream", resp_headers)
except Exception as exc: except Exception as exc:
@@ -904,6 +904,7 @@ def rewrite_cookie_path(value, path):
def rewrite_html(body, prefix): def rewrite_html(body, prefix):
text = body.decode("utf-8", errors="ignore") text = body.decode("utf-8", errors="ignore")
escaped_prefix = prefix.replace("/", "\\/")
replacements = { replacements = {
'href="/': f'href="{prefix}/', 'href="/': f'href="{prefix}/',
'src="/': f'src="{prefix}/', 'src="/': f'src="{prefix}/',
@@ -911,12 +912,36 @@ def rewrite_html(body, prefix):
"href='/": f"href='{prefix}/", "href='/": f"href='{prefix}/",
"src='/": f"src='{prefix}/", "src='/": f"src='{prefix}/",
"action='/": f"action='{prefix}/", "action='/": f"action='{prefix}/",
'url("/': f'url("{prefix}/',
"url('/": f"url('{prefix}/",
"url(/": f"url({prefix}/",
'"/cgi-bin/luci': f'"{prefix}/cgi-bin/luci',
'"/ubus/': f'"{prefix}/ubus/',
'"/luci-static/': f'"{prefix}/luci-static/',
"'/cgi-bin/luci": f"'{prefix}/cgi-bin/luci",
"'/ubus/": f"'{prefix}/ubus/",
"'/luci-static/": f"'{prefix}/luci-static/",
'"\\/cgi-bin\\/luci': f'"{escaped_prefix}\\/cgi-bin\\/luci',
'"\\/ubus\\/': f'"{escaped_prefix}\\/ubus\\/',
'"\\/luci-static\\/': f'"{escaped_prefix}\\/luci-static\\/',
"'\\/cgi-bin\\/luci": f"'{escaped_prefix}\\/cgi-bin\\/luci",
"'\\/ubus\\/": f"'{escaped_prefix}\\/ubus\\/",
"'\\/luci-static\\/": f"'{escaped_prefix}\\/luci-static\\/",
} }
for old, new in replacements.items(): for old, new in replacements.items():
text = text.replace(old, new) text = text.replace(old, new)
return text.encode("utf-8") return text.encode("utf-8")
def should_rewrite_body(content_type):
content_type = (content_type or "").lower()
return (
"text/html" in content_type
or "text/css" in content_type
or "javascript" in content_type
)
def cmd_init(args): def cmd_init(args):
with connect(args.db) as conn: with connect(args.db) as conn:
init_db(conn) init_db(conn)