Feature(client): support host transfer in the macOS client

Mirror the Android semantics: move the main link to the assigned master
immediately, persist the record in config so it survives restarts, return
to the original master on expiry or early return, and revert after the new
master stays unreachable for 60s. Share requests are answered as
unsupported, and the capability matrix is updated accordingly.

A target counts as unusable when the connection is refused or when it is
accepted but never passes MasterSettings (a foreign service on that port,
or a master built with another key). The latter used to be retried forever,
stranding the host until expiry; both shapes now count toward the revert
gate and reset only once a connection verifies. Early return clears the
persisted record at once, so a crash before the main loop restores the
original master cannot resurrect the transfer on restart.

Endpoints from the command or the record are bounds-checked before being
written into CONNECT_ADDRESS (strcpy_s is strcpy on POSIX), config access
is serialized across the receive and main threads, the new validTo is
published before the pending target so expiry cannot undo a fresh
transfer, and a stale early-return flag is cleared when a new transfer
applies or one is reverted.

The Linux client was rebuilt with the shared header changes; the macOS
side is not compiled here and still needs a build on a Mac.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-21 10:15:35 +01:00
1 parent 913b74097b
commit de9e9a7369
5 files changed
+342 -12

No files matched your search

+7
View File
@@ -56,6 +56,13 @@ inline bool IsCommandAllowed(unsigned char cmd)
return g_settingsVerified.load(std::memory_order_acquire) || cmd == CMD_MASTERSETTING;
}
// 本连接是否已通过 MasterSettings 校验。转移主机用它判断「目标主控是否真的可用」:
// 连得上但校验一直不通过(对端不是主控,或不是本构建的主控)与连不上等价。
inline bool IsVerified()
{
return g_settingsVerified.load(std::memory_order_acquire);
}
// 处理 CMD_MASTERSETTING(payload = szBuffer + 1,payloadLen = ulLength - 1):
// 强制要求完整 MasterSettings(包含 Signature 字段);不完整 / 签名失败 → 不更新
// g_settingsVerified,让心跳循环 30s 超时自然把本次连接断开重连。
+11 -1
View File
@@ -43,10 +43,20 @@ inline bool ParseHeaderEnc(const std::string& s, int& out)
return true;
}
// 端点能安全写进 CONNECT_ADDRESS 的边界。非 Windows 上 strcpy_s/sprintf_s 就是无界的
// strcpy/sprintf(见 commands.h),地址超长会直接踩坏 szPort/iType 等后续字段,
// 所以来自命令或配置文件的端点必须先过这一关。
inline bool FitsConnAddress(const Endpoint& ep)
{
return !ep.ip.empty()
&& ep.ip.size() <= sizeof(((CONNECT_ADDRESS*)nullptr)->szServerIP) - 1
&& ep.port > 0 && ep.port <= 65535;
}
// 把端点写进连接参数。只在连接生命周期的持有线程调用(见文件头注释)。
inline void ApplyEndpoint(CONNECT_ADDRESS& conn, const Endpoint& ep)
{
if (ep.ip.empty() || ep.port <= 0)
if (!FitsConnAddress(ep))
return;
conn.SetServer(ep.ip.c_str(), ep.port);
if (ep.transport >= 0)
+6 -2
View File
@@ -17,6 +17,7 @@
namespace HostTransfer {
using HostEndpoint::Endpoint;
using HostEndpoint::FitsConnAddress;
using HostEndpoint::ParseTransport;
using HostEndpoint::ParseHeaderEnc;
using HostEndpoint::ApplyEndpoint;
@@ -28,7 +29,8 @@ struct Target {
long long validTo; // <= 0 表示无效
Target() : validTo(0) {}
bool Valid() const { return !ep.ip.empty() && ep.port > 0 && validTo > 0; }
// 端点容量由 FitsConnAddress 守住:记录里存的是外部输入,直接 ApplyTarget 会越界。
bool Valid() const { return FitsConnAddress(ep) && validTo > 0; }
bool Active(long long now) const { return Valid() && now <= validTo; }
};
@@ -54,7 +56,9 @@ inline bool ParseAssignCommand(const unsigned char* buf, unsigned long len,
Target t;
t.ep.ip = v[0];
t.ep.port = atoi(v[1].c_str());
if (t.ep.ip.empty() || t.ep.port <= 0 || t.ep.port > 65535)
// 长度/端口范围都要挡住:服务端只限制整条载荷不超过 250 字节,地址本身可以顶到
// CONNECT_ADDRESS::szServerIP[100] 之外,写进 g_SETTINGS 时就是堆栈/全局越界。
if (!FitsConnAddress(t.ep))
return false;
double days = atof(v[2].c_str());