ui: align app_key canonicalization with backend

This commit is contained in:
beckline
2026-02-16 01:41:40 +03:00
parent b617963034
commit a5e93888a5
2 changed files with 129 additions and 13 deletions

View File

@@ -102,16 +102,74 @@ def get_profile(profile_id: str) -> dict:
def infer_app_key(cmdline: str) -> str: def infer_app_key(cmdline: str) -> str:
return canonicalize_app_key("", cmdline)
def canonicalize_app_key(app_key: str, cmdline: str) -> str:
key = (app_key or "").strip()
cmd = (cmdline or "").strip() cmd = (cmdline or "").strip()
if not cmd:
return ""
try: try:
args = shlex.split(cmd) tokens = shlex.split(cmd) if cmd else []
if args:
return str(args[0] or "").strip()
except Exception: except Exception:
pass tokens = cmd.split() if cmd else []
return (cmd.split() or [""])[0].strip() if not tokens and key:
tokens = [key]
tokens = [str(x or "").strip() for x in tokens if str(x or "").strip()]
if not tokens:
return ""
def base(t: str) -> str:
return os.path.basename(str(t or "").strip())
def extract_run_target(toks: list[str]) -> str:
idx = -1
for i, t in enumerate(toks):
if t == "run":
idx = i
break
if idx < 0:
return ""
for j in range(idx + 1, len(toks)):
t = toks[j].strip()
if not t or t == "--":
continue
if t.startswith("-"):
continue
return t
return ""
primary = tokens[0]
b = base(primary).lower()
if b == "env":
for j in range(1, len(tokens)):
t = tokens[j].strip()
if not t or t == "--":
continue
if t.startswith("-"):
continue
if "=" in t:
continue
return canonicalize_app_key("", " ".join(tokens[j:]))
return "env"
if b == "flatpak":
appid = extract_run_target(tokens)
return f"flatpak:{appid}" if appid else "flatpak"
if b == "snap":
name = extract_run_target(tokens)
return f"snap:{name}" if name else "snap"
if b == "gtk-launch" and len(tokens) >= 2:
did = tokens[1].strip()
if did and not did.startswith("-"):
return f"desktop:{did}"
if "/" in primary:
return base(primary) or primary
return primary
def systemctl_user(args: list[str], *, timeout: float = 4.0) -> tuple[int, str]: def systemctl_user(args: list[str], *, timeout: float = 4.0) -> tuple[int, str]:
@@ -268,7 +326,8 @@ def main(argv: list[str]) -> int:
if target not in ("vpn", "direct"): if target not in ("vpn", "direct"):
target = "vpn" target = "vpn"
app_key = str(prof.get("app_key") or "").strip() or infer_app_key(cmd) app_key_raw = str(prof.get("app_key") or "").strip()
app_key = canonicalize_app_key(app_key_raw, cmd) or canonicalize_app_key("", cmd)
ttl = int(prof.get("ttl_sec", 0) or 0) ttl = int(prof.get("ttl_sec", 0) or 0)
if ttl <= 0: if ttl <= 0:
ttl = 24 * 60 * 60 ttl = 24 * 60 * 60

View File

@@ -1108,17 +1108,74 @@ RU: Применяет policy-rules и проверяет health. При оши
self._emit_log(line) self._emit_log(line)
def _infer_app_key_from_cmdline(self, cmdline: str) -> str: def _infer_app_key_from_cmdline(self, cmdline: str) -> str:
# Keep this aligned with backend canonicalization (flatpak/snap/env/path).
cmd = (cmdline or "").strip() cmd = (cmdline or "").strip()
if not cmd: if not cmd:
return "" return ""
try: try:
args = shlex.split(cmd) args = shlex.split(cmd)
if args:
return str(args[0] or "").strip()
except Exception: except Exception:
pass args = cmd.split()
# Fallback: first token return self._canonical_app_key_from_tokens(args)
return (cmd.split() or [""])[0].strip()
def _canonical_app_key_from_tokens(self, tokens: list[str]) -> str:
toks = [str(x or "").strip() for x in (tokens or []) if str(x or "").strip()]
if not toks:
return ""
def base(t: str) -> str:
return os.path.basename(str(t or "").strip())
def extract_run_target(toks2: list[str]) -> str:
idx = -1
for i, t in enumerate(toks2):
if t == "run":
idx = i
break
if idx < 0:
return ""
for j in range(idx + 1, len(toks2)):
t = toks2[j].strip()
if not t or t == "--":
continue
if t.startswith("-"):
continue
return t
return ""
primary = toks[0]
b = base(primary).lower()
if b == "env":
# env VAR=1 /usr/bin/app ...
for j in range(1, len(toks)):
t = toks[j].strip()
if not t or t == "--":
continue
if t.startswith("-"):
continue
if "=" in t: # VAR=VAL
continue
return self._canonical_app_key_from_tokens(toks[j:])
return "env"
if b == "flatpak":
appid = extract_run_target(toks)
return f"flatpak:{appid}" if appid else "flatpak"
if b == "snap":
name = extract_run_target(toks)
return f"snap:{name}" if name else "snap"
if b == "gtk-launch" and len(toks) >= 2:
did = toks[1].strip()
if did and not did.startswith("-"):
return f"desktop:{did}"
if "/" in primary:
return base(primary) or primary
return primary
def _launch_and_mark( def _launch_and_mark(
self, self,