Feature: Share a Linux host to another master

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
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-07 23:21:44 +01:00
1 parent 7240879792
commit 2a97471b51
6 files changed
+743 -85

No files matched your search

+8 -56
View File
@@ -1,10 +1,9 @@
#pragma once
#include <string>
#include <cstdlib>
#include <cstring>
#include "commands.h"
#include "host_endpoint.h"
// ============== 主机转移(COMMAND_ASSIGN_MASTER)的跨平台公共部分 ==============
// ============== 主机转移(COMMAND_ASSIGN_MASTER + SHARE_TYPE_YAMA_FOREVER)==============
//
// 服务端「转移主机」下发的载荷是 host:port:days:shine[:transport](见
// server/2015Remote/2015RemoteDlg.cpp assignFunction)。内核部分在
@@ -14,19 +13,14 @@
// 收包:ParseAssignCommand() → 写配置记录 + 交给主线程 ApplyTarget
// 心跳:now > validTo 时 ApplyEndpoint(原主控) 并重连
//
// 注意 CONNECT_ADDRESS 不参与跨线程同步(ServerIP() 读的时候还会就地解密),
// ApplyTarget/ApplyEndpoint 只能在连接生命周期的持有线程(主循环)调用。
// 端点本身(ip/port + 可选的 Shine、传输方式)在 host_endpoint.h,与「分享主机」共用。
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) {}
};
using HostEndpoint::Endpoint;
using HostEndpoint::ParseTransport;
using HostEndpoint::ParseHeaderEnc;
using HostEndpoint::ApplyEndpoint;
using HostEndpoint::CaptureEndpoint;
// 一条转移记录:目标端点 + 到期时间(unix 秒)。
struct Target {
@@ -38,25 +32,6 @@ struct Target {
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 侧同样不会生效。
@@ -122,32 +97,9 @@ inline bool DecodeRecord(const std::string& s, Target& out)
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