Feature: Let the client report structured failures for MCP tools

Four new client-to-server tokens carry a win32/LSTATUS code when a
command fails instead of returning empty data: TOKEN_SERVICE_ERROR
(170), TOKEN_FILE_LIST_ERROR (171), TOKEN_REG_ERROR (172) and
TOKEN_DOWNLOAD_ERROR (173). They sit on byte values that were unused,
so existing numbering is untouched.

The three data paths that used to collapse "failed" and "empty" into
the same packet now distinguish them: OpenSCManager failures, directory
enumeration failures and RegOpenKeyEx failures all report the code that
was actually returned, and UploadToRemoteV2 reports why it found no
file to transfer before the transfer sub-connection is ever created.

Every failure branch still sends the packet the old server expects
(TOKEN_FILE_LIST, the empty TOKEN_REG_PATH/KEY pair, the ClientMsg
popup), so a new client talking to an old server degrades to exactly
today's behaviour instead of timing out. Success paths are byte-for-
byte unchanged.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-10-25 17:59:17 +01:00
1 parent eaa10cc7d3
commit 315a0855dc
8 files changed
+89 -13

No files matched your search

+17 -4
View File
@@ -24,11 +24,20 @@ CServicesManager::~CServicesManager()
VOID CServicesManager::SendServicesList()
{
LPBYTE szBuffer = GetServicesList();
DWORD dwErr = 0;
LPBYTE szBuffer = GetServicesList(&dwErr);
if (szBuffer == NULL) {
if (dwErr == 0) dwErr = ERROR_GEN_FAILURE;
Mprintf("Get service list failed[IP: %s][win32=%lu]\n", m_ClientObject->GetPublicIP().c_str(), dwErr);
// 结构化失败回报:MCP 端据此立即返回「权限不足/SCM 打不开」,不必等 20s 超时。
// 老服务端不认识 TOKEN_SERVICE_ERROR,走 default 分支丢弃,随后仍按原样收到 ClientMsg。
BYTE pkt[1 + sizeof(DWORD)] = { 0 };
pkt[0] = TOKEN_SERVICE_ERROR;
memcpy(pkt + 1, &dwErr, sizeof(DWORD));
m_ClientObject->Send2Server((char*)pkt, sizeof(pkt));
// 保留原有弹窗消息:老服务端与 MFC 服务管理器(新 token 由对话框忽略)行为不变。
char buf[128];
sprintf_s(buf, "Get service list failed[IP: %s]", m_ClientObject->GetPublicIP().c_str());
Mprintf("%s\n", buf);
ClientMsg msg("服务管理", buf);
m_ClientObject->Send2Server((char*)&msg, sizeof(msg));
return;
@@ -42,7 +51,7 @@ VOID CServicesManager::SendServicesList()
#define skCrypt(p) p
#endif
LPBYTE CServicesManager::GetServicesList()
LPBYTE CServicesManager::GetServicesList(DWORD* err)
{
LPENUM_SERVICE_STATUS ServicesStatus = NULL;
LPQUERY_SERVICE_CONFIG ServicesInfor = NULL;
@@ -52,12 +61,14 @@ LPBYTE CServicesManager::GetServicesList()
DWORD dwLength = 0;
DWORD dwOffset = 0;
if((m_hscManager=OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS))==NULL) {
if (err) *err = GetLastError(); // 典型 5=拒绝访问(未提权)
return NULL;
}
ServicesStatus = (LPENUM_SERVICE_STATUS) LocalAlloc(LPTR,64*1024);
if (ServicesStatus==NULL) {
if (err) *err = ERROR_NOT_ENOUGH_MEMORY;
CloseServiceHandle(m_hscManager);
return NULL;
}
@@ -75,8 +86,10 @@ LPBYTE CServicesManager::GetServicesList()
&dwResumeHandle);
szBuffer = (LPBYTE)LocalAlloc(LPTR, MAX_PATH);
if (szBuffer == NULL)
if (szBuffer == NULL) {
if (err) *err = ERROR_NOT_ENOUGH_MEMORY;
return NULL;
}
szBuffer[0] = TOKEN_SERVERLIST;
dwOffset = 1;
for (unsigned long i = 0; i < dwServicesCount; ++i) { // Display The Services,显示所有的服务