- 插件执行超时默认延长至 6 小时,支持插件单独配置超时时间 - 新增插件根目录迁移逻辑,解决旧版权限问题 - 新增多个服务端实体、DTO、配置模块等基础代码 - 新增插件 UI 模板及相关静态资源
217 lines
8.7 KiB
JavaScript
217 lines
8.7 KiB
JavaScript
// 37du Electron 壳:配置固化(.env 加载 + 路径 + 常量)
|
||
"use strict";
|
||
|
||
const fs = require("fs");
|
||
const path = require("path");
|
||
|
||
// 打包态判定:Electron 运行时 + resources 下存在 app.asar(打包版独有,dev `electron .` 无 asar)。
|
||
// 不能依赖 ELECTRON_RENDERER_URL(那是 electron-vite 等 dev-server 工具的变量,本工程从不设置)。
|
||
const IS_FROZEN =
|
||
!!process.versions &&
|
||
!!process.versions.electron &&
|
||
!!process.resourcesPath &&
|
||
!process.env.ELECTRON_RUN_AS_NODE &&
|
||
fs.existsSync(path.join(process.resourcesPath, "app.asar"));
|
||
|
||
// 本模块目录:desktop/electron-shell/electron → parents
|
||
const SHELL_ROOT = path.resolve(__dirname, "..");
|
||
|
||
// 仓库根:electron/../.. = 37duAutomation(dev 模式定位 plugins/)
|
||
const REPO_ROOT = path.resolve(SHELL_ROOT, "..", "..");
|
||
|
||
// ==================== 轻量 .env 加载(仅本模块目录,变量已存在则不覆盖) ====================
|
||
function loadDotEnv() {
|
||
// 开发/CLI:读本模块 .env;打包态额外读 resources/.env(electron-builder extraResources 带入,
|
||
// 位于 asar 外的真实文件,可随包分发或按机器覆盖)
|
||
const candidates = [path.join(SHELL_ROOT, ".env")];
|
||
if (IS_FROZEN) candidates.push(path.join(process.resourcesPath, ".env"));
|
||
for (const envPath of candidates) {
|
||
if (!fs.existsSync(envPath)) continue;
|
||
try {
|
||
const text = fs.readFileSync(envPath, "utf-8");
|
||
for (const rawLine of text.split(/\r?\n/)) {
|
||
const line = rawLine.trim();
|
||
if (!line || line.startsWith("#") || !line.includes("=")) continue;
|
||
const idx = line.indexOf("=");
|
||
const key = line.slice(0, idx).trim();
|
||
const value = line.slice(idx + 1).trim();
|
||
if (key && process.env[key] === undefined) process.env[key] = value;
|
||
}
|
||
} catch (e) {
|
||
// 读取失败静默(与 Python 实现一致)
|
||
}
|
||
}
|
||
}
|
||
loadDotEnv();
|
||
|
||
// ==================== 基础常量 ====================
|
||
const PORT = 8799;
|
||
|
||
// 数据根目录:与影刀版 / 旧 Python 底座共享 %LOCALAPPDATA%\37duAutomation(跨端契约)
|
||
function localAppData() {
|
||
return process.env.LOCALAPPDATA || process.env.APPDATA || require("os").homedir();
|
||
}
|
||
const APP_DATA = path.join(localAppData(), "37duAutomation");
|
||
|
||
// 插件本地配置目录:per-plugin JSON 持久化
|
||
const PLUGIN_CONFIG_DIR = path.join(APP_DATA, "plugin_configs");
|
||
|
||
// 插件源码/安装根:dev = 仓库根 plugins/;打包 = %LOCALAPPDATA%\37duAutomation\plugins。
|
||
// 不能放 exe 同级:装到 C:\Program Files 时普通权限进程连 mkdir 都被拒(EPERM,插件全部装不上),
|
||
// 且 NSIS 重装会清空安装目录、插件陪葬(与环境包同因,见 envRootDir 注释)。
|
||
function pluginRoot() {
|
||
if (process.env.SHELL_PLUGIN_ROOT) return process.env.SHELL_PLUGIN_ROOT;
|
||
if (IS_FROZEN) return path.join(APP_DATA, "plugins");
|
||
return path.join(REPO_ROOT, "plugins");
|
||
}
|
||
|
||
// 运行时版本:打包态读 exe 同级 shell.version(发版文件);开发/CLI 回退 package.json 版本。
|
||
// 插件 plugin.json 可声明 minRuntimeVersion 做版本兼容校验(不满足则拒绝安装)。
|
||
function runtimeVersion() {
|
||
try {
|
||
if (!process.env.ELECTRON_RUN_AS_NODE && process.resourcesPath) {
|
||
const vf = path.join(path.dirname(process.execPath), "shell.version");
|
||
if (fs.existsSync(vf)) {
|
||
const v = fs.readFileSync(vf, "utf-8").trim();
|
||
if (v) return v;
|
||
}
|
||
}
|
||
const pkg = JSON.parse(fs.readFileSync(path.join(SHELL_ROOT, "package.json"), "utf-8"));
|
||
return String(pkg.version || "").trim();
|
||
} catch (e) {
|
||
return "";
|
||
}
|
||
}
|
||
|
||
// ==================== 控制面配置 ====================
|
||
const CONTROLPLANE_URL = (process.env.CONTROLPLANE_URL || "").replace(/\/+$/, "");
|
||
const CONTROLPLANE_ACCOUNT = process.env.CONTROLPLANE_ACCOUNT || "";
|
||
const CONTROLPLANE_TOKEN = process.env.CONTROLPLANE_TOKEN || "";
|
||
|
||
// ==================== 插件执行子进程配置 ====================
|
||
// Python 解释器:优先 env,默认 python(PATH 中的 venv/系统 Python)
|
||
const PYTHON_EXE = process.env.PYTHON_EXE || "python";
|
||
// 插件执行超时(秒,默认 21600=6 小时):长流程(耗时数小时)不再被 1800s 默认强杀。
|
||
// 可整体覆盖(.env / 环境变量 RUN_TIMEOUT,0=全局不限时);单个插件可用 plugin.json 的
|
||
// runTimeoutSeconds 覆盖(见 runTimeoutFor)。两处都按 0=不限时、缺省回退壳默认处理。
|
||
const RUN_TIMEOUT = parseInt(process.env.RUN_TIMEOUT || "21600", 10);
|
||
const RUN_MAX_MEM_MB = parseInt(process.env.RUN_MAX_MEM_MB || "2048", 10);
|
||
|
||
// 插件执行超时(秒):插件 plugin.json 声明 runTimeoutSeconds 优先(0=不限时),
|
||
// 否则回退壳默认 RUN_TIMEOUT。传入 registry.find() 产出的插件对象。
|
||
function runTimeoutFor(plugin) {
|
||
if (plugin && typeof plugin === "object") {
|
||
const raw = plugin.timeout_seconds;
|
||
// 注意不能用 Number(raw) 直接判:null/undefined 会被转成 0(=不限时),必须显式排除
|
||
if (raw !== undefined && raw !== null) {
|
||
const v = Number(raw);
|
||
if (Number.isFinite(v) && v >= 0) return v;
|
||
}
|
||
}
|
||
return RUN_TIMEOUT;
|
||
}
|
||
|
||
// ==================== 便携 Python 环境包(python-env) ====================
|
||
// 版本化目录(python-env/<version>/)+ current.json 指向当前版本,保留上一版供回滚。
|
||
// 目录位置:打包态 = %LOCALAPPDATA%\37duAutomation\python-env——NSIS 重装/升级会清空安装目录,
|
||
// 放 exe 同级会导致环境包每次陪葬重新下载解压(其余持久化数据本就都在 APP_DATA);
|
||
// 开发态 = electron-shell/python-env/(可本地构建联调)。
|
||
function envRootDir() {
|
||
if (process.env.SHELL_PYTHON_ENV_ROOT) return path.resolve(process.env.SHELL_PYTHON_ENV_ROOT);
|
||
if (IS_FROZEN) return path.join(APP_DATA, "python-env");
|
||
return path.join(SHELL_ROOT, "python-env");
|
||
}
|
||
|
||
// 当前环境包目录(current.json → 版本目录);无环境包返回 ""
|
||
function pythonEnvRoot() {
|
||
const dir = envRootDir();
|
||
try {
|
||
const raw = fs.readFileSync(path.join(dir, "current.json"), "utf-8").replace(/^\uFEFF/, "");
|
||
const cur = JSON.parse(raw);
|
||
const ver = String((cur || {}).version || "").trim();
|
||
if (ver && fs.existsSync(path.join(dir, ver, "python.exe"))) return path.join(dir, ver);
|
||
} catch (e) {
|
||
/* 尚无环境包 */
|
||
}
|
||
return "";
|
||
}
|
||
|
||
// 环境包解析的 Python 解释器:显式 PYTHON_EXE 优先,其次环境包,最后系统 python
|
||
function pythonExe() {
|
||
if (process.env.PYTHON_EXE) return process.env.PYTHON_EXE;
|
||
const envRoot = pythonEnvRoot();
|
||
if (envRoot) return path.join(envRoot, "python.exe");
|
||
return "python";
|
||
}
|
||
|
||
// 运行时根(webhost 包所在目录):环境包模式 = 环境包根;否则 = 壳内置 python/(vendor 子集)
|
||
function runtimeRoot() {
|
||
const envRoot = pythonEnvRoot();
|
||
if (envRoot) return envRoot;
|
||
return path.join(SHELL_ROOT, "python");
|
||
}
|
||
|
||
// 插件执行入口:环境包模式 = 环境包内 plugin_runner.py;打包态 = exe 同级;dev = 壳内置 python/
|
||
function runnerScript() {
|
||
if (process.env.SHELL_RUNNER_SCRIPT) return process.env.SHELL_RUNNER_SCRIPT;
|
||
const envRoot = pythonEnvRoot();
|
||
if (envRoot) return path.join(envRoot, "webhost", "backend", "core", "plugin_runner.py");
|
||
if (IS_FROZEN) {
|
||
return path.join(path.dirname(process.execPath), "plugin_runner.py");
|
||
}
|
||
return path.join(SHELL_ROOT, "python", "webhost", "backend", "core", "plugin_runner.py");
|
||
}
|
||
|
||
// Job Object 守护(环境包模式启用内存上限 + 整树托管):无环境包返回 ""
|
||
function jobGuardScript() {
|
||
const envRoot = pythonEnvRoot();
|
||
if (envRoot) return path.join(envRoot, "webhost", "backend", "core", "job_guard.py");
|
||
return "";
|
||
}
|
||
|
||
// 前端静态资源:壳自带 renderer/dist(自包含;缺失时先跑 scripts/copy-renderer.ps1 构建)
|
||
function frontendDist() {
|
||
return path.join(SHELL_ROOT, "renderer", "dist");
|
||
}
|
||
|
||
// 敏感凭证中心化下发:进程级覆盖存储(唯一来源:中心机下发 / 本地加密缓存)
|
||
const _CREDS_OVERRIDE = {};
|
||
|
||
function applyCreds(values) {
|
||
if (values && typeof values === "object") {
|
||
Object.assign(_CREDS_OVERRIDE, values);
|
||
}
|
||
}
|
||
|
||
function getCred(name) {
|
||
if (_CREDS_OVERRIDE[name] !== undefined) return _CREDS_OVERRIDE[name];
|
||
return process.env[name] !== undefined ? process.env[name] : "";
|
||
}
|
||
|
||
module.exports = {
|
||
SHELL_ROOT,
|
||
REPO_ROOT,
|
||
PORT,
|
||
APP_DATA,
|
||
PLUGIN_CONFIG_DIR,
|
||
CONTROLPLANE_URL,
|
||
CONTROLPLANE_ACCOUNT,
|
||
CONTROLPLANE_TOKEN,
|
||
PYTHON_EXE,
|
||
RUN_TIMEOUT,
|
||
RUN_MAX_MEM_MB,
|
||
runTimeoutFor,
|
||
IS_FROZEN,
|
||
pluginRoot,
|
||
runnerScript,
|
||
frontendDist,
|
||
runtimeVersion,
|
||
applyCreds,
|
||
getCred,
|
||
envRootDir,
|
||
pythonEnvRoot,
|
||
pythonExe,
|
||
runtimeRoot,
|
||
jobGuardScript,
|
||
};
|