From 4281566db9532e9e21a0ca16b8a9844bc2b51967 Mon Sep 17 00:00:00 2001 From: personal82555 Date: Mon, 12 Oct 2026 20:49:00 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AD=98=E5=82=A8=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E6=94=B9=E4=B8=BA=E5=AE=BF=E4=B8=BB=E6=9C=BA=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=20=E2=80=94=20relay=E6=8C=82=E8=BD=BD=E5=AE=BF=E4=B8=BB?= =?UTF-8?q?=E6=9C=BA=E6=A0=B9(-v=20/:/hostfs),=20=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E5=90=8E=E5=8F=B0=E7=9B=B4=E6=8E=A5=E4=BF=AE=E6=94=B9=E5=AE=BF?= =?UTF-8?q?=E4=B8=BB=E6=9C=BA=E7=9B=AE=E5=BD=95(vol1=E2=86=94vol2),=20?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=AE=BF=E4=B8=BB=E6=9C=BA=E7=BA=A7=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E8=BF=81=E7=A7=BB,=20=E7=B3=BB=E7=BB=9F=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=E9=BB=91=E5=90=8D=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docker/it-tools-relay/server.py | 52 ++++++++++++++++++++++----------- src/components/MenuLayout.vue | 5 +++- src/layouts/base.layout.vue | 4 +++ src/pages/Admin.page.vue | 18 ++++++------ 4 files changed, 52 insertions(+), 27 deletions(-) diff --git a/docker/it-tools-relay/server.py b/docker/it-tools-relay/server.py index fc76285..d14013b 100644 --- a/docker/it-tools-relay/server.py +++ b/docker/it-tools-relay/server.py @@ -610,20 +610,30 @@ import sqlite3 import uuid MAX_UPLOAD = int(os.environ.get("MAX_UPLOAD_MB", "50")) * 1024 * 1024 -# storage root: /app/data/config.json may override it (admin panel -> storage settings) +# storage model: admin panel edits the HOST path; the container sees it under HOST_PREFIX (-v /:/hostfs) +HOST_PREFIX = os.environ.get("HOST_PREFIX", "/hostfs") +DEFAULT_HOST_ROOT = os.environ.get("DEFAULT_HOST_ROOT", "/vol1/1000/HD1/docker/it-tools-relay/data") _BOOT_DATA_DIR = "/app/data" +def _host_to_container(host_path: str) -> str: + if os.path.isdir(HOST_PREFIX): + return (HOST_PREFIX + host_path).rstrip("/") + return host_path # fallback: legacy deployment without host root mount + + def _load_storage_root() -> str: - cfg = os.path.join(_BOOT_DATA_DIR, "config.json") + cfg = os.path.join(_host_to_container(DEFAULT_HOST_ROOT), "config.json") try: with open(cfg) as f: root = json.load(f).get("storage_root") - if root and os.path.isdir(root): - return root.rstrip("/") + if root: + container = _host_to_container(root) + if os.path.isdir(container): + return container except Exception: pass - return _BOOT_DATA_DIR + return _host_to_container(DEFAULT_HOST_ROOT) DATA_DIR = _load_storage_root() @@ -1081,23 +1091,30 @@ def admin_api(method: str, sub: str, payload: dict, handler): total += os.path.getsize(fp) except OSError: pass + # current host path (strip prefix) + host_root = DATA_DIR + if os.path.isdir(HOST_PREFIX) and DATA_DIR.startswith(HOST_PREFIX): + host_root = DATA_DIR[len(HOST_PREFIX):] or "/" return 200, {"code": 200, "message": "ok", "data": { - "root": DATA_DIR, + "root": host_root, + "container_root": DATA_DIR, "users_dir": USERS_ROOT, "size": total, - "writable": os.access(os.path.dirname(DATA_DIR) if DATA_DIR != _BOOT_DATA_DIR else _BOOT_DATA_DIR, os.W_OK), - "boot_root": _BOOT_DATA_DIR, + "host_mounted": os.path.isdir(HOST_PREFIX), }} if method == "POST" and sub == "/storage": - new_root = str(payload.get("root") or "").strip().rstrip("/") - if not new_root or not new_root.startswith("/"): - return 400, {"code": 400, "message": "请输入绝对路径"} + new_host_root = str(payload.get("root") or "").strip().rstrip("/") + if not new_host_root or not new_host_root.startswith("/"): + return 400, {"code": 400, "message": "请输入宿主机绝对路径,如 /vol1/xxx 或 /vol2/xxx"} # block dangerous roots - banned = ("/bin", "/sbin", "/usr", "/etc", "/proc", "/sys", "/dev", "/boot", "/lib") + banned = ("/bin", "/sbin", "/usr", "/etc", "/proc", "/sys", "/dev", "/boot", "/lib", "/root", "/home") for b in banned: - if new_root == b or new_root.startswith(b + "/"): + if new_host_root == b or new_host_root.startswith(b + "/"): return 400, {"code": 400, "message": "不允许使用系统目录"} + if not os.path.isdir(HOST_PREFIX): + return 400, {"code": 400, "message": "容器未挂载宿主机根目录(缺少 -v /:/hostfs 挂载)"} + new_root = _host_to_container(new_host_root) if new_root == DATA_DIR: return 400, {"code": 400, "message": "与当前路径相同,无需修改"} try: @@ -1108,7 +1125,7 @@ def admin_api(method: str, sub: str, payload: dict, handler): os.remove(probe) except Exception as e: return 400, {"code": 400, "message": f"目标路径不可写: {e}"} - # migrate existing data (users dir + db files + config) + # migrate existing data (users dir + db files) moved = [] old_root = DATA_DIR for item in ("users",): @@ -1123,11 +1140,12 @@ def admin_api(method: str, sub: str, payload: dict, handler): if os.path.exists(src) and not os.path.exists(dst): shutil.move(src, dst) moved.append(item) - # persist config (always in boot dir so it survives) - cfg = os.path.join(_BOOT_DATA_DIR, "config.json") + # persist config in the default host root (survives data root moves) + cfg = os.path.join(_host_to_container(DEFAULT_HOST_ROOT), "config.json") try: + os.makedirs(os.path.dirname(cfg), exist_ok=True) with open(cfg, "w") as f: - json.dump({"storage_root": new_root}, f) + json.dump({"storage_root": new_host_root}, f) except Exception: return 500, {"code": 500, "message": "配置写入失败"} # restart to take effect (restart always brings the container back) diff --git a/src/components/MenuLayout.vue b/src/components/MenuLayout.vue index 8b32f25..84abf56 100644 --- a/src/components/MenuLayout.vue +++ b/src/components/MenuLayout.vue @@ -1,14 +1,17 @@