feat: 存储设置改为宿主机路径 — relay挂载宿主机根(-v /:/hostfs), 管理后台直接修改宿主机目录(vol1↔vol2), 数据宿主机级自动迁移, 系统目录黑名单

This commit is contained in:
personal82555 committed 2026-10-12 20:49:00 +08:00
1 parent 80a220f386
commit 4281566db9
4 files changed
+52 -27

No files matched your search

+35 -17
View File
@@ -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)