Add COMMAND_SHARE (SHARE_TYPE_YAMA) with the Windows semantics: the record goes into share_list and a second connection is opened to the target master while the main link stays where it is. main()'s connection state is all global, so each record is served by a forked "--share <entry>" child that reuses the whole login, heartbeat and command path; the parent only starts, tracks and reaps them. COMMAND_SHARE_CANCEL clears the list and stops the children right away (Linux can; Windows does it on the next start). Transfer now opens its connection immediately too, matching the Windows fallthrough that spawns a shared instance, and that temporary connection is dropped once the main link has actually moved to the target or when the transfer expires. common/host_endpoint.h holds the ip/port/shine/transport word list the two commands share; common/host_share.h holds the share payload, share_list encoding and the per-master dedup. Config writes now take a cross-process flock and replace the file by rename. The parent and every child write the same config.conf from their own snapshot, and a truncating overwrite could drop the other side's keys. Children ignore the commands that only mean something on the main link: share, transfer, upgrade, TLS anchor and group. Verified in WSL against fake masters: share start, cancel and restore, re-sharing with a different transport, share plus transfer on one master, and the transfer child disappearing after the main link moved. Co-Authored-By: deepseek-flash
106 lines
3.8 KiB
C++
106 lines
3.8 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::ParseTransport;
|
||
using HostEndpoint::ParseHeaderEnc;
|
||
using HostEndpoint::ApplyEndpoint;
|
||
using HostEndpoint::CaptureEndpoint;
|
||
|
||
// 一条转移记录:目标端点 + 到期时间(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; }
|
||
};
|
||
|
||
// 解析一条完整的 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 ApplyTarget(CONNECT_ADDRESS& conn, const Target& t)
|
||
{
|
||
ApplyEndpoint(conn, t.ep);
|
||
}
|
||
|
||
} // namespace HostTransfer
|