Feature: Add the file-write tools behind their own switch
P10-B adds delete_path, move_path and create_folder, and the switch they need. Reusing McpFileTransfer would have been wrong: uploading overwrites a file, deleting destroys a tree, and an operator who wants one may well not want the other. McpFileWrite is therefore a separate key, off by default, wired the way every other switch is - read from settings.ini at startup, reported as get_server_status.switches.file_write, and exposed with its own checkbox in the MCP settings dialog. The tools additionally require readonly to be off: a read-only server has no business modifying a host. No client change was needed: COMMAND_DELETE_FILE, COMMAND_DELETE_DIRECTORY, COMMAND_CREATE_FOLDER and COMMAND_RENAME_FILE already existed in the Windows file manager. The chain reuses the file-manager sub-connection and adds a phase - the main connection asks for the drive list, the sub-connection's first packet hands it to OnDriveList which sends the write command, and the client's TOKEN_*_FINISH ack moves the pending request from phase 0 to 1, where the tool sends COMMAND_LIST_FILES for the parent so the ack and the confirmation share one timeout. Only the IO thread touches the sub-connection; the tool thread just waits on the shared condvar, which keeps the sub-connection's lifetime out of its reach. The confirmation exists because the client answers unconditionally: it ignores the return values of DeleteFile, MoveFile and MakeSureDirectoryPathExists and there is no error token. ok therefore only means "the command arrived", and verified is the real answer - true when the parent listing shows the expected post-state, false when it shows the opposite, null when the listing itself did not come back (timeout or disconnect), since reporting that as false would be a lie. Linux, macOS and Android hosts are refused outright: those clients decode these paths as GBK while the server speaks UTF-8 to them for list_files, so a non-ASCII path would corrupt rather than fail. Paths are validated as UTF-8, converted to GBK exactly once, and reject %VAR%, relative paths, drive and share roots, and - for delete_path and for move_path's source - the Windows, Program Files, ProgramData, $Recycle.Bin and System Volume Information trees. create_folder gets no system-directory ban, because writing there is the same permission upload_file already has. Two defects surfaced while exercising this against a real client: create_folder's wire path has to end with a backslash, because MakeSureDirectoryPathExists treats a non-terminated last component as a file name and only creates the intermediate levels; and ResolveFileOpVerified(LookupInListing(...), isDir, ...) read isDir before the lookup ever set it - argument evaluation order is unspecified and MSVC evaluates right to left - so every create_folder reported verified=false even when the directory existed. Verified on a Debug x64 build against a Windows client over MCP: nested create_folder, upload, rename, delete, delete a tree, the failure shapes (delete with recursive=false on a directory, create_folder onto an existing file name, delete of a non-existent target) and every refusal (drive root, C:\Windows\Temp, C:\Users, %TEMP%, relative, empty, over-long, from==to, offline host, non-numeric id), with a Chinese-named directory created, renamed and deleted end to end; no file-manager window appeared in the server process during any of it. The smoke test gained section [4c], opt-in through -FileWriteDir, which asserts verified and never ok and walks a sandbox through the same path; two runs passed 69 checks with the sandbox removed afterwards. Bin/settings.ini was restored byte-exact and the Release instance was left alone. Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
bb55f52b29
commit
27b5431130
7 files changed
+933
-13
No files matched your search
@@ -270,6 +270,12 @@ public:
|
||||
void SetFileTransferEnabled(bool enabled) { m_fileTransferEnabled = enabled; }
|
||||
bool IsFileTransferEnabled() const { return m_fileTransferEnabled; }
|
||||
|
||||
// ===== P10-B:文件系统写操作(delete_path / move_path / create_folder)=====
|
||||
// 独立开关 McpFileWrite(默认关),并要求 McpReadonly=0。不复用 McpFileTransfer:
|
||||
// 上传是「覆盖」,删除是「毁灭」,风险等级不同,必须能分别关掉。
|
||||
void SetFileWriteEnabled(bool enabled) { m_fileWriteEnabled = enabled; }
|
||||
bool IsFileWriteEnabled() const { return m_fileWriteEnabled; }
|
||||
|
||||
// 该 host 是否有进行中的文件传输会话(MessageHandle 分派守卫)。
|
||||
bool IsFileTransferPending(uint64_t device_id);
|
||||
|
||||
@@ -377,7 +383,28 @@ public:
|
||||
bool OnServiceReady(uint64_t device_id, context* subCtx);
|
||||
|
||||
// 工具线程:等待写确认包(第二个同 token 包);超时/空数据清挂起并 CancelIO 子连接。
|
||||
bool WaitPendingWrite(uint64_t device_id, std::vector<BYTE>& out, int timeoutMs);
|
||||
// phaseOut(可空):返回超时/成功那一刻所处的阶段(见 P10-B 的 phase),供调用方区分
|
||||
// 「连完成包都没收到」与「完成包已到、复核没回来」。
|
||||
bool WaitPendingWrite(uint64_t device_id, std::vector<BYTE>& out, int timeoutMs,
|
||||
int* phaseOut = nullptr);
|
||||
|
||||
// ===== P10-B:文件系统写操作(delete_path / move_path / create_folder)=====
|
||||
// 链路与 P7 同构(主连接 COMMAND_LIST_DRIVE → 子连接首包 TOKEN_DRIVE_LIST → 同一子连接
|
||||
// 上发写命令 → 等 TOKEN_*_FINISH),但多一个阶段:客户端的完成包是无条件回包
|
||||
// (忽略 DeleteFile/MoveFile/MakeSureDirectoryPathExists 的返回值),所以收到后不直接
|
||||
// 收尾,而在同一子连接上补发一次 COMMAND_LIST_FILES 复核(phase 0 = 等完成包,
|
||||
// phase 1 = 等复核列目录,两阶段共用一个超时)。
|
||||
//
|
||||
// verifyPath 是复核要列的父目录(客户端 ANSI/GBK,空 = 不复核,直接投递完成包)。
|
||||
bool BeginPendingFileOp(uint64_t device_id, const std::string& tool,
|
||||
const std::vector<BYTE>& followUp, const std::string& verifyPath);
|
||||
|
||||
// MessageHandle(TOKEN_DELETE_FINISH / TOKEN_RENAME_FINISH / TOKEN_CREATEFOLDER_FINISH) 调用。
|
||||
// NotMine 与本请求无关(迟到的孤儿包)→ 调用方 CancelIO 收尾;
|
||||
// KeepSub 已转入 phase 1、在同一子连接上补发了列目录 → 调用方不关子连接;
|
||||
// CloseSub 已投递最终数据(无复核目标 / 复核包发不出去)→ 调用方 CancelIO 收尾。
|
||||
enum class FileOpAck { NotMine, KeepSub, CloseSub };
|
||||
FileOpAck TakeFileOpFinish(uint64_t device_id, context* subCtx, const BYTE* data, ULONG len);
|
||||
|
||||
// ===== 迟到首包吸收(工具放弃后不再弹出 MFC 对话框)=====
|
||||
// 工具超时/失败清掉挂起后,客户端仍可能才把应答首包发上来(列盘、服务列表、注册表、
|
||||
@@ -602,6 +629,9 @@ private:
|
||||
std::vector<std::vector<BYTE>> searchRecords; // 已收集的 TOKEN_SEARCH_FILE_LIST 原始包
|
||||
size_t searchBytes = 0;
|
||||
bool searchFinished = false; // 已收到 TOKEN_SEARCH_FILE_FINISH
|
||||
// ===== P10-B:文件写工具(delete_path / move_path / create_folder)=====
|
||||
int phase = 0; // 0=等 TOKEN_*_FINISH;1=等复核用的 TOKEN_FILE_LIST
|
||||
std::string verifyPath; // phase 1 要列的父目录(客户端 ANSI/GBK)
|
||||
bool searchStopSent = false; // 已下发 COMMAND_FILES_SEARCH_STOP
|
||||
bool searchTruncated = false; // 命中收集上限,后续匹配被丢弃
|
||||
};
|
||||
@@ -778,6 +808,7 @@ private:
|
||||
bool m_terminalEnabled = false; // 持久终端开关(默认关;要求 m_readonly=false)
|
||||
bool m_remoteControlEnabled = false; // 远程控制开关(默认关;要求 m_readonly=false)
|
||||
bool m_fileTransferEnabled = false; // 文件传输开关(默认关;download_file)
|
||||
bool m_fileWriteEnabled = false; // P10-B 文件写入开关(默认关;要求 m_readonly=false)
|
||||
bool m_manageEnabled = false; // P7 管理控制开关(默认关;要求 m_readonly=false)
|
||||
bool m_machineEnabled = false; // P8 主机管理族总开关(默认关;16 个 host_* 工具)
|
||||
bool m_machineDangerousEnabled = false; // P8 高风险子集(冻结/解冻进程、停止/重启服务)
|
||||
|
||||
Reference in new issue
Block a user