The Linux client ignored COMMAND_ASSIGN_MASTER, so a host could not be moved to a different controller the way the Windows client allows. The payload is host:port:days:shine[:transport]; the parsing, record encoding and address application live in a shared header so a macOS client can reuse them unchanged. An assignment only rewrites the connection target. The running session stays up and the next reconnect - a dropped link or a restart - goes to the new master, matching the Windows behaviour. The target and its expiry are stored in a single "transfer" config key, while the address the process started with is kept in memory only: the config's own master/port entries are never overwritten, so restarting mid-transfer still has the original to fall back to. When the expiry passes the client restores that address, clears the key and reconnects. Malformed payloads and zero-day assignments are rejected outright. The way back depends on an expiry time, so honouring a permanent move would strand a host on an address it cannot leave. Give the POSIX connect() a timeout as well (non-blocking connect plus poll, 10 seconds). A blocking connect waiting for SYN retransmission froze the main loop for about two minutes when the port was filtered, delaying both heartbeats and the expiry restore. The transfer state is initialised after daemonize() for the same reason it is logged: logging before the double fork can leave the child deadlocked on the logger mutex. Co-Authored-By: deepseek-flash
154 lines
5.4 KiB
C++
154 lines
5.4 KiB
C++
#pragma once
|
||
#include <string>
|
||
#include <cstdlib>
|
||
#include <cstring>
|
||
#include "commands.h"
|
||
|
||
// ============== 主机转移(COMMAND_ASSIGN_MASTER)的跨平台公共部分 ==============
|
||
//
|
||
// 服务端「转移主机」下发的载荷是 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(原主控) 并重连
|
||
//
|
||
// 注意 CONNECT_ADDRESS 不参与跨线程同步(ServerIP() 读的时候还会就地解密),
|
||
// ApplyTarget/ApplyEndpoint 只能在连接生命周期的持有线程(主循环)调用。
|
||
namespace HostTransfer {
|
||
|
||
// 一个连接端点。transport/headerEnc 为 -1 表示「不改变现有值」。
|
||
struct Endpoint {
|
||
std::string ip;
|
||
int port;
|
||
int transport; // PROTO_TCP / PROTO_WS / PROTO_WSS
|
||
int headerEnc; // PROTOCOL_SHINE / PROTOCOL_HELL(Windows 头加密变体)
|
||
|
||
Endpoint() : port(0), transport(-1), headerEnc(-1) {}
|
||
};
|
||
|
||
// 一条转移记录:目标端点 + 到期时间(unix 秒)。
|
||
struct Target {
|
||
Endpoint ep;
|
||
long long validTo; // <= 0 表示无效
|
||
|
||
Target() : validTo(0) {}
|
||
bool Valid() const { return !ep.ip.empty() && ep.port > 0 && validTo > 0; }
|
||
bool Active(long long now) const { return Valid() && now <= validTo; }
|
||
};
|
||
|
||
// 只认合法值。atoi 对非法串返回 0(正是 PROTO_TCP),不校验就会把一条本该走
|
||
// WSS 的链路静默降级成明文直连;headerEnc 同理由服务端决定,不能随意采信。
|
||
inline bool ParseTransport(const std::string& s, int& out)
|
||
{
|
||
if (s != std::to_string(PROTO_TCP) && s != std::to_string(PROTO_WS)
|
||
&& s != std::to_string(PROTO_WSS))
|
||
return false;
|
||
out = atoi(s.c_str());
|
||
return true;
|
||
}
|
||
|
||
inline bool ParseHeaderEnc(const std::string& s, int& out)
|
||
{
|
||
if (s != std::to_string(PROTOCOL_SHINE) && s != std::to_string(PROTOCOL_HELL))
|
||
return false;
|
||
out = atoi(s.c_str());
|
||
return true;
|
||
}
|
||
|
||
// 解析一条完整的 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());
|
||
if (t.ep.ip.empty() || t.ep.port <= 0 || t.ep.port > 65535)
|
||
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 ApplyEndpoint(CONNECT_ADDRESS& conn, const Endpoint& ep)
|
||
{
|
||
if (ep.ip.empty() || ep.port <= 0)
|
||
return;
|
||
conn.SetServer(ep.ip.c_str(), ep.port);
|
||
if (ep.transport >= 0)
|
||
conn.protoType = (char)ep.transport;
|
||
if (ep.headerEnc >= 0)
|
||
conn.iHeaderEnc = ep.headerEnc;
|
||
}
|
||
|
||
inline void ApplyTarget(CONNECT_ADDRESS& conn, const Target& t)
|
||
{
|
||
ApplyEndpoint(conn, t.ep);
|
||
}
|
||
|
||
// 记下当前连接参数作为「原主控」,转移过期后靠它恢复。
|
||
inline Endpoint CaptureEndpoint(CONNECT_ADDRESS& conn)
|
||
{
|
||
Endpoint ep;
|
||
ep.ip = conn.ServerIP();
|
||
ep.port = conn.ServerPort();
|
||
ep.transport = (int)(unsigned char)conn.protoType;
|
||
ep.headerEnc = conn.iHeaderEnc;
|
||
return ep;
|
||
}
|
||
|
||
} // namespace HostTransfer
|