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:
1 parent
b466f568ab
commit
63c7cdf588
18 files changed
+1024
-177
No files matched your search
+51
-92
@@ -53,15 +53,10 @@
|
||||
|
||||
int DataProcess(void* user, PBYTE szBuffer, ULONG ulLength);
|
||||
static void ApplySelfUpdate(const PBYTE szBuffer, ULONG ulLength);
|
||||
// 主机分享/转移的子进程管理(实现在进程辅助函数之后,DataProcess/main 里都要用)
|
||||
// 分享子进程与转移子进程都走同一套 fork+exec 管理,只是归属不同:
|
||||
// KIND_SHARE 「取消分享」只停这些
|
||||
// KIND_TRANSFER 转移到期时停这些(提前建立的那条连接)
|
||||
// KIND_ALL 主实例退出时全停
|
||||
enum ManagedKind { KIND_SHARE, KIND_TRANSFER, KIND_ALL };
|
||||
// 主机分享的子进程管理(实现在进程辅助函数之后,DataProcess/main 里都要用)。
|
||||
// 转移不在这里:转移是主连接自己换地址(见下面的 g_needReconnect),不起子进程。
|
||||
static bool StartShareInstance(const std::string& entry);
|
||||
static bool StartTransferInstance(const std::string& entry);
|
||||
static void StopChildren(ManagedKind kind);
|
||||
static void StopChildren();
|
||||
static void StartShareInstancesFromConfig();
|
||||
|
||||
// 远程地址:当前为写死状态,如需调试,请按实际情况修改
|
||||
@@ -76,8 +71,9 @@ static std::atomic<bool> g_needResendLogin(false); // 分组变更后需要重
|
||||
static std::atomic<bool> g_needReconnect(false);
|
||||
|
||||
// ============== 主机转移(COMMAND_ASSIGN_MASTER)==============
|
||||
// 服务端「转移主机」把目标写进 config.conf 的 transfer 键,之后每次重连都连新主控;
|
||||
// valid_to 到期后恢复本进程启动时的原主控。原主控只存在内存里(argv 或编译期默认),
|
||||
// 转移 = 主连接真的搬到新主控:收包后立刻断开当前主控、连到目标,旧主控上这台主机
|
||||
// 随即离线。目标写进 config.conf 的 transfer 键,之后每次重连都连新主控;valid_to
|
||||
// 到期后恢复本进程启动时的原主控。原主控只存在内存里(argv 或编译期默认),
|
||||
// 不能写回配置——恢复时读到的若还是转移目标,就成了原地打转。
|
||||
// 解析/编解码/地址应用在 common/host_transfer.h(macOS 客户端可直接复用)。
|
||||
static HostTransfer::Endpoint g_transferOrig; // 启动时的主控,过期恢复用
|
||||
@@ -96,14 +92,11 @@ static bool g_bShareInstance = false; // 本进程是分享子
|
||||
static std::mutex g_shareMu; // 保护 g_shareProcs
|
||||
// 子进程 pid 和它启动时用的那条记录:判断「同一条分享」只能比这条记录,不能比配置
|
||||
// 里的——命令处理是先写配置再起进程,配置里已经是新记录了。
|
||||
// transfer=true 表示这是「转移主机」立即建立的那条连接(见 StartTransferInstance),
|
||||
// 它不进 share_list、不受「取消分享」影响。
|
||||
struct ShareProc {
|
||||
pid_t pid;
|
||||
std::string entry;
|
||||
bool transfer;
|
||||
};
|
||||
static std::map<std::string, ShareProc> g_shareProcs; // "ip:port" / "transfer:ip:port" -> 子进程
|
||||
static std::map<std::string, ShareProc> g_shareProcs; // "ip:port" -> 子进程
|
||||
// 分享条目上限:每条都对应一个常驻子进程,服务端误发/恶意刷下发不能把进程表打爆。
|
||||
static const size_t kMaxShareEntries = 16;
|
||||
static std::atomic<bool> g_shareReaperStop(false);
|
||||
@@ -574,8 +567,8 @@ static void InitTransferState()
|
||||
}
|
||||
|
||||
// 把接收线程记下的目标应用到全局连接参数。只在主线程调用:CONNECT_ADDRESS 没有
|
||||
// 跨线程同步,ConnectServer 读的就是它。只改地址、不主动断开——与 Windows 一致,
|
||||
// 下一次重连才生效。
|
||||
// 跨线程同步,ConnectServer 读的就是它。收包时已经置了 g_needReconnect,心跳循环
|
||||
// 会立刻断开重连,所以这里的地址在下一秒就生效——不是「等下次自然重连」。
|
||||
static void ApplyPendingTransfer()
|
||||
{
|
||||
if (!g_transferPending.load(std::memory_order_acquire))
|
||||
@@ -588,7 +581,7 @@ static void ApplyPendingTransfer()
|
||||
}
|
||||
HostTransfer::ApplyTarget(g_SETTINGS, t);
|
||||
long long left = t.validTo - (long long)time(nullptr);
|
||||
Mprintf(">>> Transfer armed: next reconnect -> %s:%d (transport=%d), %llds left\n",
|
||||
Mprintf(">>> Transfer applied: -> %s:%d (transport=%d), %llds left\n",
|
||||
t.ep.ip.c_str(), t.ep.port, t.ep.transport, left);
|
||||
}
|
||||
|
||||
@@ -606,8 +599,6 @@ static void CheckTransferExpiry()
|
||||
g_needReconnect.store(true, std::memory_order_relaxed);
|
||||
Mprintf(">>> Transfer expired: restoring %s:%d\n",
|
||||
g_transferOrig.ip.c_str(), g_transferOrig.port);
|
||||
// 转移时提前建立的那条连接也要收掉,否则过期后主机仍挂在新主控上。
|
||||
StopChildren(KIND_TRANSFER);
|
||||
}
|
||||
|
||||
// ============== 历史活动(COMMAND_QUERY_ACTIVITY → TOKEN_REPORT_ACTIVITY) ==============
|
||||
@@ -861,6 +852,14 @@ int DataProcess(void* user, PBYTE szBuffer, ULONG ulLength)
|
||||
Mprintf("** [%p] SHARE ignored: running as share instance ***\n", user);
|
||||
return TRUE;
|
||||
}
|
||||
// 转移期间不接受分享:主机此刻只连着新主控,新主控无权再把它分出去。
|
||||
// 放行的话,到期按原主控复原后这条分享连接还挂在别处,等于凭空多出
|
||||
// 一条谁也不认的链路。
|
||||
if ((long long)time(nullptr) <= g_transferValidTo.load(std::memory_order_relaxed)) {
|
||||
Mprintf("** [%p] SHARE ignored: host is transferred until %lld ***\n",
|
||||
user, g_transferValidTo.load(std::memory_order_relaxed));
|
||||
return TRUE;
|
||||
}
|
||||
HostShare::Endpoint ep;
|
||||
if (!HostShare::ParseCommand(szBuffer, ulLength, ep)) {
|
||||
Mprintf("** [%p] SHARE: malformed payload, ignored ***\n", user);
|
||||
@@ -898,10 +897,19 @@ int DataProcess(void* user, PBYTE szBuffer, ULONG ulLength)
|
||||
}
|
||||
// 有效期内重复下发忽略(与 Windows KernelManager 的 now <= valid_to 判断一致)。
|
||||
// 用原子变量而不是再读一次配置:状态以本进程为准,避免和主线程的写入交错。
|
||||
// 这条同时挡住了「转移目标主控再转一手」:主机被转移到 B 之后只连着 B,B 发来的
|
||||
// 转移命令一律落在这里被丢掉——只有原主控(主机复原后才连回去的那一台)还有
|
||||
// 机会发起下一次转移。不这么挡的话,B 把主机转到 C,到期却按原主控复原,
|
||||
// 中间那一跳就成了谁也不认的孤儿。
|
||||
long long active = g_transferValidTo.load(std::memory_order_relaxed);
|
||||
if (now <= active) {
|
||||
Mprintf("** [%p] ASSIGN to %s:%d ignored: already assigned until %lld ***\n",
|
||||
user, t.ep.ip.c_str(), t.ep.port, active);
|
||||
// 主控那边点完看不到任何反馈会以为下发丢了,回一条消息进消息栏。
|
||||
if (g_pMainClient && g_pMainClient->IsConnected()) {
|
||||
ClientMsg msg("Transfer host", "Host is already transferred, ignored");
|
||||
g_pMainClient->Send2Server((char*)&msg, sizeof(msg));
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
{
|
||||
@@ -913,18 +921,18 @@ int DataProcess(void* user, PBYTE szBuffer, ULONG ulLength)
|
||||
g_transferValidTo.store(t.validTo, std::memory_order_relaxed);
|
||||
LinuxConfig cfg;
|
||||
cfg.SetStr("transfer", HostTransfer::EncodeRecord(t));
|
||||
// 立即再起一条通往新主控的连接,新主控马上就能看到这台主机上线;本实例与老主控
|
||||
// 的主连接原样保留,下次重连(含重启)才轮到主连接过去。这与 Windows 客户端
|
||||
// 现有语义一致:KernelManager 的转移分支末尾没有 break,会贯穿到分享分支,
|
||||
// 额外起一个 shared 实例连到同一个目标地址。
|
||||
Mprintf("** [%p] ASSIGN to %s:%d for %llds, main link moves on next reconnect ***\n",
|
||||
// 真转移:断掉当前主控,主连接立刻连到新主控,旧主控上这台主机随即离线。
|
||||
// 不能另起一条临时连接——那只是「分享」,新主控拿到的是一条不能升级、
|
||||
// 也不真正属于它的旁路连接(升级命令按设计只认主连接)。
|
||||
// 目标由主线程的 ApplyPendingTransfer 落到 g_SETTINGS,本标志让它马上重连。
|
||||
g_needReconnect.store(true, std::memory_order_relaxed);
|
||||
Mprintf("** [%p] ASSIGN to %s:%d for %llds, moving the main link now ***\n",
|
||||
user, t.ep.ip.c_str(), t.ep.port, t.validTo - now);
|
||||
StartTransferInstance(HostShare::EncodeEntry(t.ep)); // 新主控立即看到上线
|
||||
} else if (szBuffer[0] == COMMAND_SHARE_CANCEL) {
|
||||
// 「取消分享」:清掉持久化列表并立刻收掉分享子进程。Windows 客户端在
|
||||
// COMMAND_SHARE_CANCEL 里也改成停掉同进程的分享线程(StopSharedInstances),
|
||||
// 两端都是立即断开;Linux 的分享实例是独立进程,直接 stop。转移顺带起的
|
||||
// 实例(KIND_TRANSFER / SHARED_KIND_TRANSFER)两端都不动。
|
||||
// 两端都是立即断开;Linux 的分享实例是独立进程,直接 stop。转移不涉及子进程,
|
||||
// 取消分享自然也撤销不了转移。
|
||||
if (g_bShareInstance) {
|
||||
Mprintf("** [%p] SHARE_CANCEL ignored: running as share instance ***\n", user);
|
||||
return TRUE;
|
||||
@@ -932,7 +940,7 @@ int DataProcess(void* user, PBYTE szBuffer, ULONG ulLength)
|
||||
LinuxConfig cfg;
|
||||
cfg.SetStr("share_list", "");
|
||||
Mprintf("** [%p] SHARE_CANCEL: stopping all share instances ***\n", user);
|
||||
StopChildren(KIND_SHARE);
|
||||
StopChildren();
|
||||
} else if (szBuffer[0] == COMMAND_UPDATE) {
|
||||
// 服务端"升级程序"菜单下发新版本 ELF。
|
||||
// 分享实例不升级:二进制是主实例和所有分享实例共用的,好几个进程同时替换
|
||||
@@ -1316,15 +1324,14 @@ static void ShareReaperLoop()
|
||||
int st = 0;
|
||||
pid_t r = waitpid(it->second.pid, &st, WNOHANG);
|
||||
if (r == it->second.pid) {
|
||||
const char* kind = it->second.transfer ? "Transfer instance" : "Share instance";
|
||||
if (WIFEXITED(st))
|
||||
Mprintf(">>> %s %s exited (code %d)\n",
|
||||
kind, it->first.c_str(), WEXITSTATUS(st));
|
||||
Mprintf(">>> Share instance %s exited (code %d)\n",
|
||||
it->first.c_str(), WEXITSTATUS(st));
|
||||
else if (WIFSIGNALED(st))
|
||||
Mprintf(">>> %s %s exited by signal %d\n",
|
||||
kind, it->first.c_str(), WTERMSIG(st));
|
||||
Mprintf(">>> Share instance %s exited by signal %d\n",
|
||||
it->first.c_str(), WTERMSIG(st));
|
||||
else
|
||||
Mprintf(">>> %s %s exited\n", kind, it->first.c_str());
|
||||
Mprintf(">>> Share instance %s exited\n", it->first.c_str());
|
||||
it = g_shareProcs.erase(it);
|
||||
} else if (r < 0 && errno == ECHILD) {
|
||||
it = g_shareProcs.erase(it); // 已被别处收走(防御)
|
||||
@@ -1421,9 +1428,8 @@ static void TerminateChildLocked(pid_t pid)
|
||||
Mprintf(">>> Child pid %d still alive after SIGKILL, giving up on it\n", (int)pid);
|
||||
}
|
||||
|
||||
// 起一个受管子进程并登记。key 在本进程内唯一:分享用 "ip:port",转移用 "transfer:ip:port",
|
||||
// 这样转移目标与分享目标相同也不会互相顶掉。
|
||||
static bool SpawnManagedChild(const std::string& key, const std::string& entry, bool transfer)
|
||||
// 起一个受管子进程并登记。key 是目标主控 "ip:port"。
|
||||
static bool SpawnManagedChild(const std::string& key, const std::string& entry)
|
||||
{
|
||||
EnsureShareReaper();
|
||||
|
||||
@@ -1448,9 +1454,8 @@ static bool SpawnManagedChild(const std::string& key, const std::string& entry,
|
||||
pid_t pid = SpawnShareProcess(entry);
|
||||
if (pid <= 0)
|
||||
return false;
|
||||
g_shareProcs[key] = ShareProc{ pid, entry, transfer };
|
||||
Mprintf(">>> %s instance started: %s (pid %d)\n",
|
||||
transfer ? "Transfer" : "Share", entry.c_str(), (int)pid);
|
||||
g_shareProcs[key] = ShareProc{ pid, entry };
|
||||
Mprintf(">>> Share instance started: %s (pid %d)\n", entry.c_str(), (int)pid);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1460,61 +1465,19 @@ static bool StartShareInstance(const std::string& entry)
|
||||
if (!HostShare::DecodeEntry(entry, ep))
|
||||
return false;
|
||||
const std::string shareKey = HostShare::MasterKey(ep);
|
||||
{
|
||||
// 同一主控上已有一条「转移」临时连接:分享是长期连接,接管它的职责。
|
||||
// 不收掉的话,这个主控上会挂着同一 clientID 的两条连接,互踢。
|
||||
const std::string transferKey = "transfer:" + shareKey;
|
||||
std::lock_guard<std::mutex> lk(g_shareMu);
|
||||
auto it = g_shareProcs.find(transferKey);
|
||||
if (it != g_shareProcs.end()) {
|
||||
Mprintf(">>> Share to %s replaces the transfer instance (pid %d)\n",
|
||||
shareKey.c_str(), (int)it->second.pid);
|
||||
TerminateChildLocked(it->second.pid);
|
||||
g_shareProcs.erase(it);
|
||||
}
|
||||
}
|
||||
return SpawnManagedChild(shareKey, entry, false);
|
||||
return SpawnManagedChild(shareKey, entry);
|
||||
}
|
||||
|
||||
// 「转移主机」立即生效的那条连接:不进 share_list(重启后由主连接自己过去),
|
||||
// 也不受「取消分享」影响;目标可以暂时不可达,子进程有自己的重连循环。
|
||||
static bool StartTransferInstance(const std::string& entry)
|
||||
{
|
||||
HostShare::Endpoint ep;
|
||||
if (!HostShare::DecodeEntry(entry, ep))
|
||||
return false;
|
||||
const std::string shareKey = HostShare::MasterKey(ep);
|
||||
{
|
||||
// 同一个主控已经有分享连接时不必再起一条:目标那边已经能看到这台主机,
|
||||
// 两条同 clientID 的连接只会互相踢。
|
||||
std::lock_guard<std::mutex> lk(g_shareMu);
|
||||
auto it = g_shareProcs.find(shareKey);
|
||||
if (it != g_shareProcs.end() && !it->second.transfer
|
||||
&& ChildAliveLocked(it->second.pid)) {
|
||||
Mprintf(">>> Transfer target %s already reachable via share instance "
|
||||
"(pid %d), extra connection skipped\n",
|
||||
shareKey.c_str(), (int)it->second.pid);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return SpawnManagedChild("transfer:" + shareKey, entry, true);
|
||||
}
|
||||
|
||||
// 停掉指定归属的子进程:SIGTERM 让子进程走自己的 signal handler 正常退出,
|
||||
// 3 秒还不走就 SIGKILL(再等 1 秒)。
|
||||
// 停掉所有子进程(分享实例;转移已不产生子进程):SIGTERM 让子进程走自己的
|
||||
// signal handler 正常退出,3 秒还不走就 SIGKILL(再等 1 秒)。
|
||||
// 等待阶段不持 g_shareMu:调用点就在接收线程上,持锁睡会把 reaper 和其它起停一起堵住。
|
||||
static void StopChildren(ManagedKind kind)
|
||||
static void StopChildren()
|
||||
{
|
||||
auto match = [kind](const ShareProc& p) {
|
||||
return kind == KIND_ALL || p.transfer == (kind == KIND_TRANSFER);
|
||||
};
|
||||
|
||||
std::vector<pid_t> pids;
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(g_shareMu);
|
||||
for (auto& kv : g_shareProcs)
|
||||
if (match(kv.second))
|
||||
pids.push_back(kv.second.pid);
|
||||
pids.push_back(kv.second.pid);
|
||||
for (pid_t pid : pids)
|
||||
kill(pid, SIGTERM); // 只发信号,不等待
|
||||
}
|
||||
@@ -2042,10 +2005,6 @@ int main(int argc, char* argv[])
|
||||
Sleep(5000);
|
||||
continue;
|
||||
}
|
||||
// 主连接已经搬到转移目标:那条「提前建立」的临时连接完成使命。留着就是同一
|
||||
// clientID 在这个主控上的第二条连接,服务端会互踢。
|
||||
if (g_transferValidTo.load(std::memory_order_relaxed) > 0)
|
||||
StopChildren(KIND_TRANSFER);
|
||||
|
||||
// 进入新连接,重置服务端身份校验状态
|
||||
ClientAuth::OnNewConnection();
|
||||
@@ -2174,7 +2133,7 @@ int main(int argc, char* argv[])
|
||||
g_shareReaperStop.store(true, std::memory_order_relaxed);
|
||||
if (g_shareReaperThread.joinable())
|
||||
g_shareReaperThread.join();
|
||||
StopChildren(KIND_ALL);
|
||||
StopChildren();
|
||||
}
|
||||
|
||||
// 采样线程要在 main 的局部对象(historyChecker)析构前停掉
|
||||
|
||||
Reference in new issue
Block a user