feat(device): 文件通道 pull/push
core 与设备复用同一条 WS 搬文件:256KiB 分块、base64、50MiB 上限, 写盘先 .part 再校验 size/sha256 后 rename;device 工具新增 pull/push 两个动作。
This commit is contained in:
+127
-1
@@ -7,6 +7,7 @@
|
||||
// 启用条件:有公网前缀(config.yml public_link.base_url 或 env PUBLIC_BASE_URL)
|
||||
// 且配了 DEVICE_TOKEN。本地部署两者都没有,设备也连不进来,端点干脆不挂。
|
||||
const crypto = require('crypto');
|
||||
const fs = require('fs');
|
||||
const WebSocket = require('ws');
|
||||
const log = require('./log');
|
||||
|
||||
@@ -106,6 +107,16 @@ function onConnection(ws) {
|
||||
if (p && p.onExit) p.onExit(m);
|
||||
return;
|
||||
}
|
||||
if (m.t === 'file.chunk') {
|
||||
const p = state.pending.get(m.id);
|
||||
if (p && p.onFileChunk) p.onFileChunk(m);
|
||||
return;
|
||||
}
|
||||
if (m.t === 'file.done') {
|
||||
const p = state.pending.get(m.id);
|
||||
if (p && p.onFileDone) p.onFileDone(m);
|
||||
return;
|
||||
}
|
||||
if (m.t === 'health') {
|
||||
const p = state.pending.get(m.id);
|
||||
if (p && p.onHealth) p.onHealth(m);
|
||||
@@ -273,4 +284,119 @@ function exec({ device, command, cwd, timeout_ms }) {
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { init, attach, enabled, list, health, exec, PATH };
|
||||
|
||||
// ---------- 文件通道 ----------
|
||||
// core 与设备之间拉/推单个文件,复用同一条 WS:256 KiB 一块,base64 传输。
|
||||
// 写盘一律先落 .part,size 与 sha256 都对上才 rename——不留半截文件。
|
||||
const FILE_CHUNK = 256 * 1024;
|
||||
const MAX_FILE = 50 * 1024 * 1024;
|
||||
|
||||
function fileTimeout(bytes) {
|
||||
return Math.max(61000, Math.ceil(bytes / FILE_CHUNK) * 3000);
|
||||
}
|
||||
|
||||
// pull:设备 → 会话目录
|
||||
function pull({ device, remotePath, localPath }) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const dev = pick(device);
|
||||
if (!dev) return reject(new Error(device ? `设备 ${device} 不在线` : '当前没有设备在线'));
|
||||
if (!remotePath) return reject(new Error('remote_path 不能为空'));
|
||||
|
||||
const id = newJobId();
|
||||
const tmp = localPath + '.part';
|
||||
const out = fs.createWriteStream(tmp);
|
||||
const hash = crypto.createHash('sha256');
|
||||
let got = 0;
|
||||
let settled = false;
|
||||
|
||||
const finish = () => {
|
||||
if (settled) return false;
|
||||
settled = true;
|
||||
clearTimeout(timer);
|
||||
state.pending.delete(id);
|
||||
return true;
|
||||
};
|
||||
const fail = msg => {
|
||||
if (!finish()) return;
|
||||
try { out.destroy(); } catch (_) {}
|
||||
try { fs.unlinkSync(tmp); } catch (_) {}
|
||||
reject(new Error(msg));
|
||||
};
|
||||
const timer = setTimeout(() => fail('拉取超时(60s 无完成)'), 60000);
|
||||
|
||||
state.pending.set(id, {
|
||||
onFileChunk(m) {
|
||||
if (settled) return;
|
||||
let b;
|
||||
try { b = Buffer.from(String(m.data || ''), 'base64'); } catch (_) { return fail('数据块解码失败'); }
|
||||
got += b.length;
|
||||
if (got > MAX_FILE) return fail('文件超过 50MiB 上限');
|
||||
hash.update(b);
|
||||
out.write(b);
|
||||
},
|
||||
onFileDone(m) {
|
||||
if (settled) return;
|
||||
if (m.err) return fail(String(m.err));
|
||||
out.end(() => {
|
||||
if (!finish()) return;
|
||||
const sum = hash.digest('hex');
|
||||
if (typeof m.size === 'number' && m.size !== got) {
|
||||
try { fs.unlinkSync(tmp); } catch (_) {}
|
||||
return reject(new Error(`大小不符:收到 ${got},设备报 ${m.size}`));
|
||||
}
|
||||
if (m.sha256 && String(m.sha256).toLowerCase() !== sum) {
|
||||
try { fs.unlinkSync(tmp); } catch (_) {}
|
||||
return reject(new Error('sha256 不符'));
|
||||
}
|
||||
try { fs.renameSync(tmp, localPath); } catch (e) { return reject(e); }
|
||||
resolve({ device: dev.name, bytes: got, sha256: sum, path: localPath });
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
dev.ws.send(JSON.stringify({ t: 'file', id, op: 'read', path: remotePath }));
|
||||
});
|
||||
}
|
||||
|
||||
// push:会话目录 → 设备
|
||||
async function push({ device, localPath, remotePath }) {
|
||||
const dev = pick(device);
|
||||
if (!dev) throw new Error(device ? `设备 ${device} 不在线` : '当前没有设备在线');
|
||||
if (!remotePath) throw new Error('remote_path 不能为空');
|
||||
|
||||
const st = fs.statSync(localPath);
|
||||
if (!st.isFile()) throw new Error('只能推文件,不能推目录:' + localPath);
|
||||
if (st.size > MAX_FILE) throw new Error('文件超过 50MiB 上限');
|
||||
const buf = fs.readFileSync(localPath);
|
||||
const sum = crypto.createHash('sha256').update(buf).digest('hex');
|
||||
const id = newJobId();
|
||||
|
||||
const done = new Promise((resolve, reject) => {
|
||||
const timer = setTimeout(() => {
|
||||
state.pending.delete(id);
|
||||
reject(new Error('推送超时'));
|
||||
}, fileTimeout(buf.length));
|
||||
state.pending.set(id, {
|
||||
onFileDone(m) {
|
||||
clearTimeout(timer);
|
||||
state.pending.delete(id);
|
||||
if (m.err) return reject(new Error(String(m.err)));
|
||||
resolve({ device: dev.name, bytes: buf.length, sha256: sum, path: remotePath });
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
dev.ws.send(JSON.stringify({ t: 'file', id, op: 'write', path: remotePath, size: buf.length, sha256: sum }));
|
||||
for (let off = 0; off < buf.length; off += FILE_CHUNK) {
|
||||
dev.ws.send(JSON.stringify({
|
||||
t: 'file.chunk', id,
|
||||
data: buf.subarray(off, off + FILE_CHUNK).toString('base64'),
|
||||
}));
|
||||
// 简单背压:发送缓冲堆到 4MB 就等一等,避免把内存打满
|
||||
while (dev.ws.bufferedAmount > 4 * 1024 * 1024) await new Promise(r => setTimeout(r, 10));
|
||||
}
|
||||
dev.ws.send(JSON.stringify({ t: 'file.done', id }));
|
||||
return done;
|
||||
}
|
||||
|
||||
module.exports = { init, attach, enabled, list, health, exec, pull, push, PATH };
|
||||
|
||||
@@ -436,7 +436,7 @@
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "device",
|
||||
"description": "Operate the remote devices (other machines) connected to this server. Devices dial out to this server, so they need no inbound port; they stay connected until closed.\n\n- action=list: no other params. Returns every online device with os/arch/shell/root/health. Always call this first when you don't know device names.\n- action=health: requires device. Returns host uptime, load, memory, disk and temperature (fields the platform can't provide are -1).\n- action=exec: requires device and command. Runs the command through a shell on that device (sh -c on Unix, cmd /c on Windows) and returns stdout, stderr and the exit code. `cwd` must be inside that device's work root; omit it to use the root.\n\nUse it when the work has to happen on the user's own machine (laptop, box, Windows PC). For the machine running this server use `terminal` instead. If a device is offline, say so explicitly — do not silently run the command elsewhere.",
|
||||
"description": "Operate the remote devices (other machines) connected to this server. Devices dial out to this server, so they need no inbound port; they stay connected until closed.\n\n- action=list: no other params. Returns every online device with os/arch/shell/root/health. Always call this first when you don't know device names.\n- action=health: requires device. Returns host uptime, load, memory, disk and temperature (fields the platform can't provide are -1).\n- action=exec: requires device and command. Runs the command through a shell on that device (sh -c on Unix, cmd /c on Windows) and returns stdout, stderr and the exit code. `cwd` must be inside that device's work root; omit it to use the root.\n\n- action=pull: requires device and remote_path; copies a file from the device into the session directory so read_file / skills can use it.\n- action=push: requires device, local_path and remote_path; sends a session file to the device.\n\nUse it when the work has to happen on the user's own machine (laptop, box, Windows PC). For the machine running this server use `terminal` instead. If a device is offline, say so explicitly — do not silently run the command elsewhere.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"required": [
|
||||
@@ -448,7 +448,9 @@
|
||||
"enum": [
|
||||
"list",
|
||||
"health",
|
||||
"exec"
|
||||
"exec",
|
||||
"pull",
|
||||
"push"
|
||||
],
|
||||
"description": "Operation to perform."
|
||||
},
|
||||
@@ -467,6 +469,14 @@
|
||||
"timeout_ms": {
|
||||
"type": "integer",
|
||||
"description": "Default 120000, max 600000 (action=exec)."
|
||||
},
|
||||
"remote_path": {
|
||||
"type": "string",
|
||||
"description": "设备上的绝对路径,必须在设备工作根内(pull/push)。"
|
||||
},
|
||||
"local_path": {
|
||||
"type": "string",
|
||||
"description": "会话目录内的相对路径。pull 省略则用远端文件名;push 必填(源文件)。"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -834,7 +834,39 @@ async function toolDevice(ctx, args) {
|
||||
}
|
||||
}
|
||||
|
||||
return 'Error: action 必须是 list / health / exec';
|
||||
if (action === 'pull') {
|
||||
const name = String(args.device || '').trim();
|
||||
const remote = String(args.remote_path || '').trim();
|
||||
if (!name) return 'Error: action=pull 需要 device 参数(先调 action=list 看设备名)';
|
||||
if (!remote) return 'Error: action=pull 需要 remote_path 参数';
|
||||
try {
|
||||
const rel = String(args.local_path || '').trim() || path.basename(remote);
|
||||
const abs = resolveInTask(ctx.taskDir, rel, fsRoot(ctx));
|
||||
const r = await devices.pull({ device: name, remotePath: remote, localPath: abs });
|
||||
recordFile(ctx, abs);
|
||||
return `已拉到 ${path.relative(ctx.taskDir, abs) || path.basename(abs)}(${r.bytes} 字节,sha256 ${r.sha256.slice(0, 16)}…)`;
|
||||
} catch (e) {
|
||||
return 'Error: ' + e.message;
|
||||
}
|
||||
}
|
||||
|
||||
if (action === 'push') {
|
||||
const name = String(args.device || '').trim();
|
||||
const local = String(args.local_path || '').trim();
|
||||
const remote = String(args.remote_path || '').trim();
|
||||
if (!name) return 'Error: action=push 需要 device 参数(先调 action=list 看设备名)';
|
||||
if (!local) return 'Error: action=push 需要 local_path 参数(会话目录内的相对路径)';
|
||||
if (!remote) return 'Error: action=push 需要 remote_path 参数(设备上的绝对路径,须在工作根内)';
|
||||
try {
|
||||
const abs = resolveInTask(ctx.taskDir, local, fsRoot(ctx));
|
||||
const r = await devices.push({ device: name, localPath: abs, remotePath: remote });
|
||||
return `已推到 ${name}:${remote}(${r.bytes} 字节,sha256 ${r.sha256.slice(0, 16)}…)`;
|
||||
} catch (e) {
|
||||
return 'Error: ' + e.message;
|
||||
}
|
||||
}
|
||||
|
||||
return 'Error: action 必须是 list / health / exec / pull / push';
|
||||
}
|
||||
|
||||
async function toolTerminal(ctx, args) {
|
||||
|
||||
Reference in New Issue
Block a user