Fix: restore orphaned active RDP sessions on console reset
An RDP client can disconnect while its session stays WTSActive with no client attached. Without a present surface both GDI and DXGI capture go black, yet IsCurrentSessionDisconnected() stays false so the reset button did nothing. Add IsOrphanedActiveRdpSession() requiring WTSActive, an RDP-Tcp station, not the active console session, and an empty WTSClientName. Feed it into RestoreConsoleSession() so the existing tscon / RestartScreen path handles this case. Real RDP sessions, console sessions and listener pseudo-sessions are excluded by the same checks. Behavior is unchanged elsewhere: the WTSDisconnected and Session 0 branches, the privilege check and the tscon command are untouched. The no-target log now carries the current and console session IDs. Verified: Release x64/x86 ServerDll and ghost build with no new warnings; tscon 2 /dest:console on the target host restored both MCP screenshot and YAMA remote desktop. The server target still hits the known LNK1104 from the running Bin/Yama_x64.exe (unrelated, no server code changed). The rebuilt client DLL has not been deployed. Co-Authored-By: Tencent Hunyuan Hy
This commit is contained in:
1 parent
8c78b44387
commit
d48f236385
1 file changed
+42
-5
@@ -396,9 +396,41 @@ static BOOL IsRunningAsAdmin()
|
||||
return isAdmin;
|
||||
}
|
||||
|
||||
// 恢复控制台会话(解决 RDP 断开后黑屏问题)
|
||||
// 当用户通过 RDP 连接后断开,物理控制台可能处于 Disconnected 状态
|
||||
// 此函数检测并将活跃的 RDP 会话切换回控制台
|
||||
// 判断一个 Active 的 RDP 会话是否「孤立」——客户端已经不在了,但会话状态
|
||||
// 还停留在 WTSActive(异常断线时会出现)。这种会话没有显示输出:GDI BitBlt
|
||||
// 抓到全黑、DXGI Desktop Duplication 拿不到帧,但 WTSDisconnected 判定不成立,
|
||||
// 老逻辑不会触发 tscon,表现为「远程桌面黑屏、点 RDP 会话归位没反应」。
|
||||
// 用 WTSClientName 判断有没有真实客户端:只有查不到客户端名才返回 TRUE,
|
||||
// 真人正在用的 RDP 会话一律不碰。
|
||||
static BOOL IsOrphanedActiveRdpSession(DWORD sid, WTS_CONNECTSTATE_CLASS state, const char* winStation)
|
||||
{
|
||||
if (state != WTSActive || sid == 0 || sid >= 65536) return FALSE;
|
||||
// 只处理 RDP 会话;console / 服务会话不在范围内
|
||||
if (!winStation || _strnicmp(winStation, "RDP-Tcp", 7) != 0) return FALSE;
|
||||
// 已经挂在物理控制台上,说明有真实显示输出,不需要归位
|
||||
const DWORD consoleSession = WTSGetActiveConsoleSessionId();
|
||||
if (consoleSession == sid) return FALSE;
|
||||
|
||||
LPSTR clientName = NULL;
|
||||
DWORD bytes = 0;
|
||||
BOOL hasClient = FALSE;
|
||||
if (WTSQuerySessionInformationA(WTS_CURRENT_SERVER_HANDLE, sid, WTSClientName,
|
||||
&clientName, &bytes)) {
|
||||
// 只要 API 分配了缓冲区就要释放,即使 bytes==0(否则泄漏)
|
||||
if (clientName) {
|
||||
hasClient = bytes > 0 && clientName[0] != '\0';
|
||||
WTSFreeMemory(clientName);
|
||||
}
|
||||
}
|
||||
Mprintf("[RestoreConsole] Session %d active RDP: client=%s, consoleSession=%u\n",
|
||||
sid, hasClient ? "yes" : "no", consoleSession);
|
||||
return !hasClient;
|
||||
}
|
||||
|
||||
// 恢复控制台会话(解决 RDP 断开/孤立后黑屏问题)
|
||||
// 当用户通过 RDP 连接后断开,物理控制台可能处于 Disconnected 状态;
|
||||
// 客户端异常断线时也可能留下「Active 但没有客户端」的孤立会话。
|
||||
// 此函数检测并将当前会话切换回控制台。
|
||||
// 需要 SYSTEM 或管理员权限
|
||||
static BOOL RestoreConsoleSession()
|
||||
{
|
||||
@@ -432,13 +464,17 @@ static BOOL RestoreConsoleSession()
|
||||
|
||||
Mprintf("[RestoreConsole] Session %d (%s): State=%d\n", sid, name, state);
|
||||
|
||||
// 如果当前进程在用户会话中(非Session 0),检查该会话是否断开
|
||||
// 如果当前进程在用户会话中(非Session 0),检查该会话是否断开/孤立
|
||||
if (currentSessionId != 0 && sid == currentSessionId) {
|
||||
if (state == WTSDisconnected) {
|
||||
currentSessionDisconnected = TRUE;
|
||||
targetSessionId = sid;
|
||||
foundTarget = TRUE;
|
||||
Mprintf("[RestoreConsole] Current session %d is disconnected\n", sid);
|
||||
} else if (IsOrphanedActiveRdpSession(sid, state, name)) {
|
||||
targetSessionId = sid;
|
||||
foundTarget = TRUE;
|
||||
Mprintf("[RestoreConsole] Current session %d is orphaned (active RDP without client)\n", sid);
|
||||
}
|
||||
}
|
||||
// 如果进程在Session 0(SYSTEM服务),查找断开的用户会话
|
||||
@@ -476,7 +512,8 @@ static BOOL RestoreConsoleSession()
|
||||
Mprintf("[RestoreConsole] CreateProcess failed: %d\n", GetLastError());
|
||||
}
|
||||
} else {
|
||||
Mprintf("[RestoreConsole] No disconnected session to restore\n");
|
||||
Mprintf("[RestoreConsole] No session to restore (current=%d, console=%u)\n",
|
||||
currentSessionId, WTSGetActiveConsoleSessionId());
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
|
||||
Reference in new issue
Block a user