Fix LuCI proxy paths and telemetry

This commit is contained in:
kzolotarev95 committed 2026-06-29 12:41:55 +04:00
1 parent a36e688cfa
commit 0e2ef7cbdb
2 files changed
+70 -10

No files matched your search

+33 -10
View File
@@ -130,27 +130,40 @@ memory_text() {
}
flash_text() {
local text
text="$(df -h /overlay 2>/dev/null | awk 'NR == 2 { printf "%s free / %s (%s used)", $4, $2, $5; exit }')"
local mount text
for mount in /overlay /; do
text="$(
df -k "$mount" 2>/dev/null | awk '
function mb(k) {
if (k == "" || k == 0) return "0 MB"
return sprintf("%.1f MB", k / 1024)
}
END {
if (NF >= 5) {
total=$(NF-4); avail=$(NF-2); pct=$(NF-1)
printf "%s free / %s (%s used)", mb(avail), mb(total), pct
}
}
'
)"
if [ -n "$text" ]; then
printf '%s' "$text"
return
fi
text="$(df -h / 2>/dev/null | awk 'NR == 2 { printf "%s free / %s (%s used)", $4, $2, $5; exit }')"
[ -n "$text" ] && {
printf '%s' "$text"
return
}
done
printf 'unknown'
}
temperature_text() {
local file raw max
max=""
for file in /sys/class/thermal/thermal_zone*/temp; do
for file in /sys/class/thermal/thermal_zone*/temp /sys/devices/virtual/thermal/thermal_zone*/temp /sys/class/hwmon/hwmon*/temp*_input; do
[ -r "$file" ] || continue
raw="$(cat "$file" 2>/dev/null | tr -cd '0-9')"
raw="$(cat "$file" 2>/dev/null | tr -cd '0-9-')"
[ -n "$raw" ] || continue
case "$raw" in
-*|'') continue ;;
esac
[ -z "$max" ] && max="$raw"
[ "$raw" -gt "$max" ] && max="$raw"
done
@@ -171,6 +184,10 @@ xray_running() {
ps_all | grep '[x]ray' | grep -F "$config" >/dev/null 2>&1
}
ssh_running() {
ps_all | grep '[d]ropbear' >/dev/null 2>&1
}
xray_bin_path() {
local configured candidate
configured="$(uci_get xray_bin /usr/bin/xray)"
@@ -357,7 +374,7 @@ run_xray() {
}
heartbeat_json() {
local router_id router_name role public_url hub_url xray_state service_state
local router_id router_name role public_url hub_url xray_state service_state ssh_state
router_id="$(safe_id "$(uci_get router_id main)")"
router_name="$(uci_get router_name "$router_id")"
role="$(uci_get role node)"
@@ -374,6 +391,11 @@ heartbeat_json() {
else
service_state="disabled"
fi
if ssh_running; then
ssh_state="running"
else
ssh_state="stopped"
fi
cat <<EOF
{
@@ -394,6 +416,7 @@ heartbeat_json() {
"flash": $(json_string "$(flash_text)"),
"temperature": $(json_string "$(temperature_text)"),
"xray": $(json_string "$xray_state"),
"ssh": $(json_string "$ssh_state"),
"service": $(json_string "$service_state")
}
EOF
+37
View File
@@ -28,6 +28,7 @@ DEFAULT_VLESS_PORT = int(os.environ.get("OWRT_REMOTE_VLESS_PORT", "8443"))
PBKDF2_ITERATIONS = 240000
MIN_PASSWORD_LENGTH = 4
SESSION_COOKIE = "owrt_remote_session"
LUCI_ABSOLUTE_ROOTS = ("/ubus", "/cgi-bin/luci", "/luci-static")
def now_ts():
@@ -713,6 +714,7 @@ function render(list) {{
const model = (r.status && (r.status.model || r.status.board)) || 'OpenWrt';
const release = (r.status && r.status.release) || 'waiting heartbeat';
const xray = (r.status && r.status.xray) || 'unknown';
const ssh = (r.status && r.status.ssh) || 'unknown';
const uptime = r.status && r.status.uptime ? duration(r.status.uptime) : 'unknown';
const load = (r.status && r.status.load) || 'unknown';
const memory = (r.status && r.status.memory) || 'unknown';
@@ -736,6 +738,7 @@ function render(list) {{
metric('Модель', model, 'span2'),
metric('OpenWrt', release),
metric('Xray', xray),
metric('SSH', ssh),
metric('В сети уже', uptime),
metric('Был на связи', ago(r.last_seen_iso)),
metric('RAM', memory),
@@ -1023,6 +1026,16 @@ class Handler(BaseHTTPRequestHandler):
def query(self):
return urllib.parse.parse_qs(self.parsed().query)
def maybe_proxy_luci_absolute(self, path):
if not any(path == root or path.startswith(root + "/") for root in LUCI_ABSOLUTE_ROOTS):
return False
ref_path = urllib.parse.urlsplit(self.headers.get("Referer", "")).path
parts = ref_path.split("/", 3)
if len(parts) >= 3 and parts[1] == "access" and parts[2]:
self.proxy_access(f"/access/{parts[2]}{path}")
return True
return False
def admin_ok(self):
cookies = parse_cookies(self.headers.get("Cookie", ""))
return secrets.compare_digest(cookies.get(SESSION_COOKIE, ""), self.app.session_token)
@@ -1145,6 +1158,8 @@ class Handler(BaseHTTPRequestHandler):
if path.startswith("/access/"):
self.proxy_access(path)
return
if self.maybe_proxy_luci_absolute(path):
return
if not self.require_admin():
return
if path == "/" or path == "":
@@ -1187,6 +1202,8 @@ class Handler(BaseHTTPRequestHandler):
if path.startswith("/access/"):
self.proxy_access(path)
return
if self.maybe_proxy_luci_absolute(path):
return
if not self.require_admin():
return
if path == "/api/auth":
@@ -1357,24 +1374,44 @@ def rewrite_html(body, prefix):
'href="/': f'href="{prefix}/',
'src="/': f'src="{prefix}/',
'action="/': f'action="{prefix}/',
'data-url="/': f'data-url="{prefix}/',
"href='/": f"href='{prefix}/",
"src='/": f"src='{prefix}/",
"action='/": f"action='{prefix}/",
"data-url='/": f"data-url='{prefix}/",
'url("/': f'url("{prefix}/',
"url('/": f"url('{prefix}/",
"url(/": f"url({prefix}/",
'"/ubus"': f'"{prefix}/ubus"',
"'/ubus'": f"'{prefix}/ubus'",
"`/ubus`": f"`{prefix}/ubus`",
'"/cgi-bin/luci"': f'"{prefix}/cgi-bin/luci"',
"'/cgi-bin/luci'": f"'{prefix}/cgi-bin/luci'",
"`/cgi-bin/luci`": f"`{prefix}/cgi-bin/luci`",
'"/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"`{prefix}/cgi-bin/luci",
"`/ubus/": f"`{prefix}/ubus/",
"`/luci-static/": f"`{prefix}/luci-static/",
'"\\/ubus"': f'"{escaped_prefix}\\/ubus"',
"'\\/ubus'": f"'{escaped_prefix}\\/ubus'",
"`\\/ubus`": f"`{escaped_prefix}\\/ubus`",
'"\\/cgi-bin\\/luci"': f'"{escaped_prefix}\\/cgi-bin\\/luci"',
"'\\/cgi-bin\\/luci'": f"'{escaped_prefix}\\/cgi-bin\\/luci'",
"`\\/cgi-bin\\/luci`": f"`{escaped_prefix}\\/cgi-bin\\/luci`",
'"\\/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\\/",
"`\\/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():
text = text.replace(old, new)