Feature: Add a get_server_status tool to the MCP server
Answering "did my rebuild actually take effect" or "why was that tool refused" used to mean reading list_online_hosts and get_audit_log and inferring the rest. A single read-only call now returns the server's own state: brand and version, the exe link time (taken from the file's last write, which unlike __DATE__ reflects the whole build), how long the process has been up, the address and port MCP is actually listening on, every MCP switch, online and offline host counts, the number of sessions in flight per family, and how many source IPs are currently blocked. The bind and port are recorded by Start rather than re-read from the configuration, so the tool reports what the listener really got. Session counters take each subsystem's lock in turn and release it immediately, so the numbers are an estimate of one moment, not a consistent snapshot. Nothing sensitive is exposed: the token and the command whitelist never appear, and the switches are reported as booleans only. The tool is registered as read-only, non-destructive, idempotent and server-local, which brings tools/list to 53 entries. The smoke test gains three checks for it, including one that fails if a token-shaped field ever appears in the output. Verified on the Debug instance: the summary line reports version, uptime and listener correctly, sessions.terminal goes 0 to 1 while a persistent terminal is open and back to 0 after terminal_close, and the smoke test passes 26/26. Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
ae37e812ee
commit
5ce453797c
5 files changed
+231
-2
No files matched your search
@@ -1133,6 +1133,36 @@ Json::Value BuildAuditLogOutputSchema() {
|
||||
return schema;
|
||||
}
|
||||
|
||||
// get_server_status 的 outputSchema(服务端自身状态;嵌套对象只声明类型,不逐字段展开)
|
||||
Json::Value BuildServerStatusOutputSchema() {
|
||||
Json::Value props(Json::objectValue);
|
||||
|
||||
const char* strFields[] = { "app_name", "version", "exe_build_time", "bind" };
|
||||
for (const char* f : strFields) {
|
||||
Json::Value s(Json::objectValue);
|
||||
s["type"] = "string";
|
||||
props[f] = s;
|
||||
}
|
||||
const char* intFields[] = { "uptime_sec", "port", "blocked_auth_ips" };
|
||||
for (const char* f : intFields) {
|
||||
Json::Value n(Json::objectValue);
|
||||
n["type"] = "integer";
|
||||
props[f] = n;
|
||||
}
|
||||
const char* objFields[] = { "switches", "hosts", "sessions" };
|
||||
for (const char* f : objFields) {
|
||||
Json::Value o(Json::objectValue);
|
||||
o["type"] = "object";
|
||||
props[f] = o;
|
||||
}
|
||||
|
||||
Json::Value schema(Json::objectValue);
|
||||
schema["type"] = "object";
|
||||
schema["properties"] = props;
|
||||
schema["required"] = Json::Value(Json::arrayValue);
|
||||
return schema;
|
||||
}
|
||||
|
||||
// get_screenshot 的 outputSchema(image 元数据;base64 数据在 content 的 image 块中)
|
||||
Json::Value BuildScreenshotOutputSchema() {
|
||||
Json::Value props(Json::objectValue);
|
||||
@@ -1552,6 +1582,23 @@ std::string BuildToolsListResult(const Json::Value& id) {
|
||||
tools.append(tool);
|
||||
}
|
||||
|
||||
// 11b) get_server_status(服务端自身状态:版本/运行时长/开关/监听地址/计数,纯内存)
|
||||
{
|
||||
Json::Value tool(Json::objectValue);
|
||||
tool["name"] = "get_server_status";
|
||||
tool["description"] = u8"获取 YAMA 服务端自身的状态:程序版本与 exe 构建时间、进程已运行秒数、MCP 监听地址与端口、各功能开关(只读/终端/远程控制/文件传输/管理/主机管理/高风险操作/键盘记录/远程交谈)、在线与离线主机数、各类在飞会话数、当前被封禁的来源 IP 数。只读、纯内存,不返回 token。";
|
||||
|
||||
Json::Value inputSchema(Json::objectValue);
|
||||
inputSchema["type"] = "object";
|
||||
inputSchema["properties"] = Json::Value(Json::objectValue);
|
||||
inputSchema["required"] = Json::Value(Json::arrayValue);
|
||||
tool["inputSchema"] = inputSchema;
|
||||
|
||||
tool["outputSchema"] = BuildServerStatusOutputSchema();
|
||||
|
||||
tools.append(tool);
|
||||
}
|
||||
|
||||
// 12) list_registry(P3:注册表查询,列根键/列子键+值,仅 Windows,一次性子链接)
|
||||
{
|
||||
Json::Value tool(Json::objectValue);
|
||||
@@ -2763,6 +2810,109 @@ std::string BuildGetAuditLog(const Json::Value& id, const Json::Value& args, CMy
|
||||
return BuildResult(id, result);
|
||||
}
|
||||
|
||||
// tools/call:get_server_status(服务端自身状态:版本/构建时间/运行时长/开关/监听地址/计数)
|
||||
std::string BuildGetServerStatus(const Json::Value& id, CMy2015RemoteDlg* parent) {
|
||||
CMcpServer& srv = CMcpServer::Instance();
|
||||
|
||||
Json::Value status(Json::objectValue);
|
||||
status["app_name"] = BRAND_APP_NAME;
|
||||
status["version"] = VERSION_STR;
|
||||
|
||||
// exe 的最后写入时间就是链接时间,比 __DATE__/__TIME__ 可靠(后者是编译本 .cpp 的时间)。
|
||||
{
|
||||
char exe[MAX_PATH] = { 0 };
|
||||
if (GetModuleFileNameA(NULL, exe, MAX_PATH) > 0) {
|
||||
WIN32_FILE_ATTRIBUTE_DATA fad;
|
||||
if (GetFileAttributesExA(exe, GetFileExInfoStandard, &fad)) {
|
||||
FILETIME local = fad.ftLastWriteTime;
|
||||
FileTimeToLocalFileTime(&fad.ftLastWriteTime, &local);
|
||||
SYSTEMTIME st = { 0 };
|
||||
if (FileTimeToSystemTime(&local, &st)) {
|
||||
char buf[32] = { 0 };
|
||||
wsprintfA(buf, "%04d-%02d-%02d %02d:%02d:%02d",
|
||||
(int)st.wYear, (int)st.wMonth, (int)st.wDay,
|
||||
(int)st.wHour, (int)st.wMinute, (int)st.wSecond);
|
||||
status["exe_build_time"] = buf;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 进程运行时长:从进程创建时间算起(不是系统开机时间,也不受 MCP 启停影响)。
|
||||
{
|
||||
FILETIME createTime, exitTime, kernelTime, userTime, nowFt;
|
||||
if (GetProcessTimes(GetCurrentProcess(), &createTime, &exitTime, &kernelTime, &userTime)) {
|
||||
GetSystemTimeAsFileTime(&nowFt);
|
||||
ULARGE_INTEGER c, n;
|
||||
c.LowPart = createTime.dwLowDateTime; c.HighPart = createTime.dwHighDateTime;
|
||||
n.LowPart = nowFt.dwLowDateTime; n.HighPart = nowFt.dwHighDateTime;
|
||||
LONGLONG ticks100ns = (LONGLONG)(n.QuadPart - c.QuadPart);
|
||||
if (ticks100ns > 0) status["uptime_sec"] = (Json::Int64)(ticks100ns / 10000000LL);
|
||||
}
|
||||
}
|
||||
|
||||
status["bind"] = srv.GetBind();
|
||||
status["port"] = srv.GetPort();
|
||||
|
||||
// 开关只报「开/关」,不输出 token、白名单内容等敏感配置。
|
||||
Json::Value switches(Json::objectValue);
|
||||
switches["readonly"] = srv.IsReadonly();
|
||||
switches["terminal"] = srv.IsTerminalEnabled();
|
||||
switches["remote_control"] = srv.IsRemoteControlEnabled();
|
||||
switches["file_transfer"] = srv.IsFileTransferEnabled();
|
||||
switches["manage"] = srv.IsManageEnabled();
|
||||
switches["machine"] = srv.IsMachineEnabled();
|
||||
switches["machine_dangerous"] = srv.IsMachineDangerousEnabled();
|
||||
switches["keylog"] = srv.IsKeylogEnabled();
|
||||
switches["chat"] = srv.IsChatEnabled();
|
||||
status["switches"] = switches;
|
||||
|
||||
int onlineCount = 0, offlineCount = 0;
|
||||
if (parent) {
|
||||
Json::Value all(Json::arrayValue);
|
||||
CollectAllHosts(parent, all, true, true);
|
||||
for (Json::Value::ArrayIndex i = 0; i < all.size(); ++i) {
|
||||
if (all[i]["online"].asBool()) onlineCount++;
|
||||
else offlineCount++;
|
||||
}
|
||||
}
|
||||
Json::Value hosts(Json::objectValue);
|
||||
hosts["online"] = onlineCount;
|
||||
hosts["offline"] = offlineCount;
|
||||
status["hosts"] = hosts;
|
||||
|
||||
int pending = 0, terminal = 0, screenCtrl = 0, fileXfer = 0, chat = 0, blockedIps = 0;
|
||||
srv.GetRuntimeCounters(pending, terminal, screenCtrl, fileXfer, chat, blockedIps);
|
||||
Json::Value sessions(Json::objectValue);
|
||||
sessions["pending"] = pending; // 一次性/管理/键盘记录挂起会话
|
||||
sessions["terminal"] = terminal;
|
||||
sessions["remote_control"] = screenCtrl;
|
||||
sessions["file_transfer"] = fileXfer;
|
||||
sessions["chat"] = chat;
|
||||
status["sessions"] = sessions;
|
||||
status["blocked_auth_ips"] = blockedIps;
|
||||
|
||||
Json::Value result(Json::objectValue);
|
||||
Json::Value structuredContent(Json::objectValue);
|
||||
structuredContent["status"] = status;
|
||||
result["structuredContent"] = structuredContent;
|
||||
|
||||
std::string text = std::string(BRAND_APP_NAME) + " v" + VERSION_STR +
|
||||
std::string(u8",已运行 ") + std::to_string((long long)status["uptime_sec"].asInt64()) +
|
||||
std::string(u8" 秒,监听 ") + srv.GetBind() + ":" + std::to_string(srv.GetPort()) +
|
||||
std::string(u8";在线主机 ") + std::to_string(onlineCount) +
|
||||
std::string(u8" 台、离线 ") + std::to_string(offlineCount) + std::string(u8" 台。");
|
||||
Json::Value content(Json::arrayValue);
|
||||
Json::Value item(Json::objectValue);
|
||||
item["type"] = "text";
|
||||
item["text"] = text;
|
||||
content.append(item);
|
||||
result["content"] = content;
|
||||
result["isError"] = false;
|
||||
|
||||
return BuildResult(id, result);
|
||||
}
|
||||
|
||||
// ===== P4:持久远程终端(terminal_open / terminal_exec / terminal_close)=====
|
||||
|
||||
static const int kTermIdleTimeoutSec = 300; // 持久终端 idle 回收超时(秒)
|
||||
@@ -5844,6 +5994,7 @@ std::string DispatchSingleHostTool(const Json::Value& id, const std::string& too
|
||||
if (toolName == "get_client_log") return BuildGetClientLog(id, args, parent);
|
||||
if (toolName == "get_keylog") return BuildGetKeylog(id, args, parent);
|
||||
if (toolName == "get_audit_log") return BuildGetAuditLog(id, args, parent);
|
||||
if (toolName == "get_server_status") return BuildGetServerStatus(id, parent);
|
||||
if (toolName == "list_registry") return BuildListRegistry(id, args, parent);
|
||||
if (toolName == "exec_command") return BuildExecCommand(id, args, parent);
|
||||
if (toolName == "terminal_open") return BuildTerminalOpen(id, args, parent);
|
||||
@@ -6309,6 +6460,9 @@ bool CMcpServer::Start(const std::string& bind, int port) {
|
||||
m_server.set_write_timeout(60, 0);
|
||||
m_server.new_task_queue = []() { return new httplib::ThreadPool(32); };
|
||||
|
||||
m_bind = bind;
|
||||
m_port = port;
|
||||
|
||||
m_thread = std::thread([this, bind, port]() {
|
||||
m_server.listen(bind, port);
|
||||
});
|
||||
@@ -6327,6 +6481,38 @@ void CMcpServer::Stop() {
|
||||
m_running.store(false);
|
||||
}
|
||||
|
||||
// get_server_status:各类在飞会话 + 当前被封禁的来源 IP 计数。
|
||||
// 每类只取自己的锁且立刻释放,不跨锁持有一致性(这些数字只作观测用)。
|
||||
void CMcpServer::GetRuntimeCounters(int& pending, int& terminal, int& screenCtrl,
|
||||
int& fileXfer, int& chat, int& blockedIps) {
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(m_PendingMutex);
|
||||
pending = (int)m_Pending.size();
|
||||
}
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(m_TermMutex);
|
||||
terminal = (int)m_TermSessions.size();
|
||||
}
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(m_ScreenCtrlMutex);
|
||||
screenCtrl = (int)m_ScreenCtrlSessions.size();
|
||||
}
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(m_FileXferMutex);
|
||||
fileXfer = (int)m_FileXferSessions.size();
|
||||
}
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(m_ChatMutex);
|
||||
chat = (int)m_ChatSessions.size();
|
||||
}
|
||||
blockedIps = 0;
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
std::lock_guard<std::mutex> lk(m_AuthMutex);
|
||||
for (const auto& kv : m_AuthFails) {
|
||||
if (kv.second.blockedUntil > now) blockedIps++;
|
||||
}
|
||||
}
|
||||
|
||||
// ===== 鉴权加固(N4)=====
|
||||
//
|
||||
// 两件事:
|
||||
|
||||
Reference in new issue
Block a user