Feature: transfer a Go-managed host to the MFC master from the web console

A Go-managed host cannot receive the client upgrade that only the MFC master
can push. Transfer moves its main link there and brings it back after N days;
MFC has no reason to return it.

Transfer is not share: ASSIGN writes the target and drops the current
connection, so the old master sees the host go offline and the new master gets
a normal main link it can remote control and upgrade. While a transfer is live
the client refuses a second transfer and any share, so no host is handed to a
third master that expiry would orphan.

Server: admin-only `transfer_device` WS command, COMMAND_ASSIGN_MASTER +
SHARE_TYPE_YAMA_FOREVER as `host:port:days::transport\0`, byte-compatible with
the MFC dialog. The transport segment is always present (YAMA_TRANSFER_TRANSPORT,
default 0/TCP) because clients keep their current link when it is missing: a
WSS-managed host would otherwise retry WSS against the MFC master's raw TCP
port and never connect. The empty shine segment keeps the host's header
encryption. The button only renders when `device_list.transfer` is present,
which MFC never sends, so no MFC change is needed. devicehistory keeps
transfer_to/transfer_until for the card badge and drops the marker on expiry or
on a fresh login here.

Client: KernelManager.cpp writes the registry and disconnects; ClientDll.cpp
re-reads the registry at the top of every reconnect, so a mid-session transfer
lands on the next round. Expiry is checked before every connect attempt as well
as inside the heartbeat loop, so an unreachable target cannot pin the host to
an expired address until a restart; restoring rebuilds the link captured on
entry (address, header encryption, transport). linux/main.cpp applies the
transfer on the main loop and restores the captured endpoint at expiry; Android
does the same; macOS has neither share nor transfer.
This commit is contained in:
yuanyuanxiang committed 2026-11-20 12:23:08 +01:00
1 parent b466f568ab
commit 63c7cdf588
18 files changed
+1024 -177

No files matched your search

+69 -45
View File
@@ -851,12 +851,42 @@ void ConfirmProtoSwitch(iniFile& cfg)
} // namespace
// 转移临时连接是否已过 valid_to。目标不可达时 StartClient 走的是外层重连循环,
// 进不到内层心跳循环,所以两处都要查。
static bool TransferExpired(iniFile& cfg)
// 「被转移」的主控:注册表里有未过期的 valid_to 就把主连接改到 master:port,
// 并记下到期时刻。KernelManager 的 ASSIGN 分支只写配置 + 断开连接(CONNECT_ADDRESS
// 不参与跨线程同步,不能在收包线程里改),真正的应用在这里:进 StartClient 时一次,
// 之后每轮重连前再看一眼——所以会话中途下发的转移,下一轮就生效,不用等进程重启。
// 返回 true 表示当前处于转移期(assigned)。
static bool ApplyAssignedMaster(iniFile& cfg, CONNECT_ADDRESS& settings, double& valid_to)
{
double validTo = atof(cfg.GetStr("settings", "valid_to").c_str());
return validTo > 0 && time(0) > validTo;
auto now = time(0);
valid_to = atof(cfg.GetStr("settings", "valid_to").c_str());
if (now > valid_to)
return false;
auto saved_ip = cfg.GetStr("settings", "master");
auto saved_port = cfg.GetInt("settings", "port");
if (saved_ip.empty() || saved_port <= 0)
return false;
settings.SetServer(saved_ip.c_str(), saved_port);
auto saved_proto = cfg.GetStr("settings", "master_proto");
if (saved_proto == "0" || saved_proto == "1")
settings.iHeaderEnc = atoi(saved_proto.c_str());
// 转移时一并下发的传输方式(新服务端才有;老记录没有这个键)。没有记录就
// 沿用烘焙值——这里不能写死 PROTO_TCP,否则老转移记录会把原本 WSS 的
// 客户端改回直连。
auto saved_transport = cfg.GetStr("settings", "master_transport");
if (saved_transport == "0" || saved_transport == "5" || saved_transport == "6")
settings.protoType = (char)atoi(saved_transport.c_str());
// 每轮重连都会走到这里,目标没变就不再打日志——否则一个连不上的转移目标会把
// 客户端日志刷成同一条重复行,排查时反而看不到别的。
static std::string logged;
const std::string cur = saved_ip + ":" + std::to_string(saved_port) + "@" +
std::to_string((long long)valid_to);
if (cur != logged) {
logged = cur;
Mprintf("[StartClient] Client is assigned to %s:%d- %ds left.\n", saved_ip.c_str(),
saved_port, int(valid_to - now));
}
return true;
}
DWORD WINAPI StartClient(LPVOID lParam)
@@ -868,28 +898,14 @@ DWORD WINAPI StartClient(LPVOID lParam)
double valid_to = 0;
std::string ip = settings.ServerIP();
int port = settings.ServerPort();
// 转移会把主连接改成目标主控的传输方式(master_transport),复原时要一并还原,
// 否则从 WSS 主控转出去的主机会按目标的传输方式去连原主控。
const char linkProto = settings.protoType;
const int linkHeaderEnc = settings.iHeaderEnc;
Mprintf("StartClient begin[%s:%d]\n", ip.c_str(), port);
ActivityHistory::Instance().Start();
if (!app.m_bShared) {
auto now = time(0);
valid_to = atof(cfg.GetStr("settings", "valid_to").c_str());
if (assigned = now <= valid_to) {
auto saved_ip = cfg.GetStr("settings", "master");
auto saved_port = cfg.GetInt("settings", "port");
settings.SetServer(saved_ip.c_str(), saved_port);
auto saved_proto = cfg.GetStr("settings", "master_proto");
if (saved_proto == "0" || saved_proto == "1")
settings.iHeaderEnc = atoi(saved_proto.c_str());
// 转移时一并下发的传输方式(新服务端才有;老记录没有这个键)。没有记录就
// 沿用烘焙值——这里不能写死 PROTO_TCP,否则老转移记录会把原本 WSS 的
// 客户端改回直连。
auto saved_transport = cfg.GetStr("settings", "master_transport");
if (saved_transport == "0" || saved_transport == "5" || saved_transport == "6")
settings.protoType = (char)atoi(saved_transport.c_str());
Mprintf("[StartClient] Client is assigned to %s:%d- %ds left.\n", saved_ip.c_str(), saved_port,
int(valid_to-now));
}
}
if (!app.m_bShared)
assigned = ApplyAssignedMaster(cfg, settings, valid_to);
auto list = app.GetSharedMasterList();
if (list.size() > 1 && settings.runningType == RUNNING_PARALLEL) {
// 并发上线的并行实例只派生一次:主控端重启重连时会再次进入 StartClient,
@@ -973,12 +989,34 @@ DWORD WINAPI StartClient(LPVOID lParam)
app.SetThreadRun(TRUE);
ThreadInfo* kb = CreateKB(&settings, bExit, pubIP, isAuthKernel);
static auto _ = RestoreMemDLL(&cfg, &settings, app.g_bExit);
// 转移到期复原。必须与连接状态解耦:旧实现只在心跳循环里判断,转移目标不可达时
// 主连接一直重连失败,根本进不了那个循环,主机就会卡在一个已经过期的地址上,
// 只能等进程重启。复原后置 S_CLIENT_UPDATE:StartClientApp 复位状态并重新进入
// 本函数,注册表里的 valid_to 已过期,不会再把地址改回转移目标。
auto restoreExpiredAssignment = [&]() -> bool {
if (!assigned || time(0) <= valid_to)
return false;
// ip/port/proto 是进 StartClient 时记下的、被转移覆盖之前的链路。
app.SetProcessState(S_CLIENT_UPDATE);
settings.SetServer(ip.c_str(), port);
settings.protoType = linkProto;
settings.iHeaderEnc = linkHeaderEnc;
Mprintf("[StartClient] Client is restored to %s:%d\n", ip.c_str(), port);
return true;
};
while (app.m_bIsRunning(&app)) {
if (app.m_sharedKind == SHARED_KIND_TRANSFER && TransferExpired(cfg)) {
Mprintf("[StartClient] Transfer expired; stopping transfer instance\n");
app.SetProcessState(S_CLIENT_EXIT);
// 每轮都从注册表对齐一次转移目标:会话中途被转移时,KernelManager 的 ASSIGN
// 分支写的是注册表并断开连接(CONNECT_ADDRESS 不参与跨线程同步,收包线程改不了),
// 新地址只能在这里读出来再连。
// 不能像以前那样「只在 assigned 为假时读一次」——到期复原后 assigned 已经是真,
// 原主控再次发起转移就会被整条漏掉(注册表改了、会话还连在老地址上)。
// 没有有效记录(从未转移 / 已过期)时本函数一个键都不改,代价只是读几个注册表值。
if (!app.m_bShared && ApplyAssignedMaster(cfg, settings, valid_to))
assigned = TRUE;
// 到期判断放在建连之前:目标不可达时下面的 ConnectServer 会一直失败,
// 心跳循环根本进不去,复原不能只依赖那条路径。
if (restoreExpiredAssignment())
break;
}
ULONGLONG dwTickCount = GetTickCount64();
// 协议切换:按注册表里的入口覆盖建连(分享实例不受主控的切换影响,被分配/转移
// 过主控的入口优先)。
@@ -1008,14 +1046,6 @@ DWORD WINAPI StartClient(LPVOID lParam)
WAIT_n(app.m_bIsRunning(&app)&& ClientObject->IsRunning() && ClientObject->IsConnected(), 10, 200);
do {
// 转移临时连接自己按 valid_to 到期退出。主实例可能一直连着原主控
// (assigned=false),永远走不到下面的到期恢复分支,只能由这条连接自查;
// 否则它会一直挂在转移目标上,直到客户端进程重启。
if (app.m_sharedKind == SHARED_KIND_TRANSFER && TransferExpired(cfg)) {
Mprintf("[StartClient] Transfer expired; stopping transfer instance\n");
app.SetProcessState(S_CLIENT_EXIT);
break;
}
Manager->SendHeartbeat();
SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED);
// 协议切换:新入口「登录被接受」后才撤销自动回退计时(ConfirmProtoSwitch 幂等)。
@@ -1026,14 +1056,8 @@ DWORD WINAPI StartClient(LPVOID lParam)
// 不放在循环前一次判定:MasterSettings 可能比第一轮心跳晚到。
if (ClientObject->IsConnected() && ClientObject->IsLoginAcked())
ConfirmProtoSwitch(cfg);
if (assigned && time(0) > valid_to) {
// 转移到期:主连接回到原主控,同时收掉转移顺带起的那条临时连接,
// 否则它会一直连着转移目标(Linux 到期同样停 KIND_TRANSFER)。
StopSharedInstances(SHARED_KIND_TRANSFER);
app.SetProcessState(S_CLIENT_UPDATE);
settings.SetServer(ip.c_str(), port);
Mprintf("[StartClient] Client is restored to %s:%d\n", ip.c_str(), port);
}
// 连接期间到期也要立刻复原;不可达的情况由外层循环开头兜底。
restoreExpiredAssignment();
} while (ClientObject->IsRunning() && ClientObject->IsConnected() && app.m_bIsRunning(&app));
while (GetTickCount64() - dwTickCount < 5000 && app.m_bIsRunning(&app))
Sleep(200);