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
110 lines
4.1 KiB
C++
110 lines
4.1 KiB
C++
#pragma once
|
||
#include <string>
|
||
#include <cstdlib>
|
||
#include "host_endpoint.h"
|
||
|
||
// ============== 主机转移(COMMAND_ASSIGN_MASTER + SHARE_TYPE_YAMA_FOREVER)==============
|
||
//
|
||
// 服务端「转移主机」下发的载荷是 host:port:days:shine[:transport](见
|
||
// server/2015Remote/2015RemoteDlg.cpp assignFunction)。内核部分在
|
||
// client/KernelManager.cpp 的 Windows 实现里。Linux / macOS 客户端共用这里的
|
||
// 解析、记录编解码和地址应用,平台侧只需要:
|
||
// 启动:CaptureEndpoint(g_SETTINGS) 存下原主控 → 读配置记录 → 未过期就 ApplyTarget
|
||
// 收包:ParseAssignCommand() → 写配置记录 + 交给主线程 ApplyTarget
|
||
// 心跳:now > validTo 时 ApplyEndpoint(原主控) 并重连
|
||
//
|
||
// 端点本身(ip/port + 可选的 Shine、传输方式)在 host_endpoint.h,与「分享主机」共用。
|
||
namespace HostTransfer {
|
||
|
||
using HostEndpoint::Endpoint;
|
||
using HostEndpoint::FitsConnAddress;
|
||
using HostEndpoint::ParseTransport;
|
||
using HostEndpoint::ParseHeaderEnc;
|
||
using HostEndpoint::ApplyEndpoint;
|
||
using HostEndpoint::CaptureEndpoint;
|
||
|
||
// 一条转移记录:目标端点 + 到期时间(unix 秒)。
|
||
struct Target {
|
||
Endpoint ep;
|
||
long long validTo; // <= 0 表示无效
|
||
|
||
Target() : validTo(0) {}
|
||
// 端点容量由 FitsConnAddress 守住:记录里存的是外部输入,直接 ApplyTarget 会越界。
|
||
bool Valid() const { return FitsConnAddress(ep) && validTo > 0; }
|
||
bool Active(long long now) const { return Valid() && now <= validTo; }
|
||
};
|
||
|
||
// 解析一条完整的 COMMAND_ASSIGN_MASTER 命令体(buf[0]=命令,buf[1]=转移类型)。
|
||
// now 由调用方传入,便于测试和统一时间口径。days <= 0 视为无效:服务端只有在
|
||
// 天数 > 0 时才写 valid_to,零天数的记录在 Windows 侧同样不会生效。
|
||
inline bool ParseAssignCommand(const unsigned char* buf, unsigned long len,
|
||
long long now, Target& out)
|
||
{
|
||
if (buf == NULL || len < 3)
|
||
return false;
|
||
if (buf[0] != COMMAND_ASSIGN_MASTER || buf[1] != SHARE_TYPE_YAMA_FOREVER)
|
||
return false;
|
||
|
||
// 服务端发的是整块 sizeof(bToken) 缓冲,尾部零填充;先按 ulLength 截取,
|
||
// 再在第一个 NUL 处截断,两个边界都守住才不会读到缓冲区外。
|
||
std::string payload((const char*)buf + 2, (size_t)len - 2);
|
||
size_t nul = payload.find('\0');
|
||
if (nul != std::string::npos)
|
||
payload.resize(nul);
|
||
|
||
auto v = StringToVector(payload, ':', 5);
|
||
Target t;
|
||
t.ep.ip = v[0];
|
||
t.ep.port = atoi(v[1].c_str());
|
||
// 长度/端口范围都要挡住:服务端只限制整条载荷不超过 250 字节,地址本身可以顶到
|
||
// CONNECT_ADDRESS::szServerIP[100] 之外,写进 g_SETTINGS 时就是堆栈/全局越界。
|
||
if (!FitsConnAddress(t.ep))
|
||
return false;
|
||
|
||
double days = atof(v[2].c_str());
|
||
if (days <= 0)
|
||
return false;
|
||
t.validTo = now + (long long)(days * 86400.0);
|
||
|
||
if (!v[3].empty())
|
||
ParseHeaderEnc(v[3], t.ep.headerEnc); // 非法值保持 -1:沿用现有设置
|
||
if (!v[4].empty())
|
||
ParseTransport(v[4], t.ep.transport);
|
||
out = t;
|
||
return true;
|
||
}
|
||
|
||
// 持久化为单行:ip:port:transport:headerEnc:valid_to
|
||
// 单键存储让平台侧只需定义「键名」和「存哪里」两件事,macOS 后续可直接复用。
|
||
inline std::string EncodeRecord(const Target& t)
|
||
{
|
||
return t.ep.ip + ":" + std::to_string(t.ep.port) + ":" +
|
||
std::to_string(t.ep.transport) + ":" +
|
||
std::to_string(t.ep.headerEnc) + ":" +
|
||
std::to_string(t.validTo);
|
||
}
|
||
|
||
inline bool DecodeRecord(const std::string& s, Target& out)
|
||
{
|
||
if (s.empty())
|
||
return false;
|
||
auto v = StringToVector(s, ':', 5);
|
||
Target t;
|
||
t.ep.ip = v[0];
|
||
t.ep.port = atoi(v[1].c_str());
|
||
ParseTransport(v[2], t.ep.transport);
|
||
ParseHeaderEnc(v[3], t.ep.headerEnc);
|
||
t.validTo = atoll(v[4].c_str());
|
||
if (!t.Valid())
|
||
return false;
|
||
out = t;
|
||
return true;
|
||
}
|
||
|
||
inline void ApplyTarget(CONNECT_ADDRESS& conn, const Target& t)
|
||
{
|
||
ApplyEndpoint(conn, t.ep);
|
||
}
|
||
|
||
} // namespace HostTransfer
|