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

+69
View File
@@ -0,0 +1,69 @@
#pragma once
#include <string>
#include <cstdlib>
#include "commands.h"
// ============== 主控端点的跨平台公共部分 ==============
//
// 「转移主机」和「分享主机」下发的是同一个概念:一个 ip:port,外加可选的
// Shine/HELL 头加密变体和传输方式(明文 / WS / WSS)。这段词表与具体命令无关,
// 单独放在这里,Linux 和 macOS 客户端的两个命令都复用(见 host_transfer.h、
// host_share.h)。
//
// 注意 CONNECT_ADDRESS 不参与跨线程同步(ServerIP() 读的时候还会就地解密),
// ApplyEndpoint/CaptureEndpoint 只能在连接生命周期的持有线程(主循环)调用。
namespace HostEndpoint {
// 一个连接端点。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) {}
};
// 只认合法值。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;
}
// 把端点写进连接参数。只在连接生命周期的持有线程调用(见文件头注释)。
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 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 HostEndpoint
+145
View File
@@ -0,0 +1,145 @@
#pragma once
#include <string>
#include <vector>
#include <cstdlib>
#include "host_endpoint.h"
// ============== 主机分享(COMMAND_SHARE + SHARE_TYPE_YAMA)的跨平台公共部分 ==============
//
// 服务端「分享主机」下发的载荷是 host:port:shine[:transport](见
// server/2015Remote/2015RemoteDlg.cpp ShareClient)。命令字节是 COMMAND_SHARE(59),
// 不是「转移主机」用的 COMMAND_ASSIGN_MASTER(207);Windows 客户端的 case 把两者
// 并在一起、靠 buf[1] 的类型字节区分,这里同样两个都收。和「转移主机」的区别:
// 转移改的是本实例的主连接,分享另起一条通往目标主控的连接,主连接原样保留。
// Windows 侧是同一进程里再起一个 ClientApp 线程(client/KernelManager.cpp、
// client/ClientDll.cpp 的 m_bShared 分支);Linux 侧由主实例 fork+exec 出一个
// 分享子进程,复用同一份登录/心跳/命令处理代码。
//
// 这里只放与平台无关的那一半:命令解析、share_list 的编解码与去重。平台侧只需要:
// 启动:读 share_list,每条起一个分享实例
// 收包:ParseCommand() → 持久化这条记录 → 起分享实例
// 取消:清空 share_list → 停掉所有分享实例
//
// 端点本身(ip/port + 可选的 Shine、传输方式)在 host_endpoint.h,与「转移主机」共用。
namespace HostShare {
using HostEndpoint::Endpoint;
// 把已拆好的 ip/port[/shine[/transport]] 字段转成端点。命令载荷和 share_list
// 记录用的是同一套字段顺序,解析逻辑共用一份,免得两处跑偏。
inline bool DecodeFields(const std::vector<std::string>& v, Endpoint& out)
{
if (v.size() < 2)
return false;
Endpoint ep;
ep.ip = v[0];
ep.port = atoi(v[1].c_str());
if (ep.ip.empty() || ep.port <= 0 || ep.port > 65535)
return false;
if (v.size() > 2 && !v[2].empty())
HostEndpoint::ParseHeaderEnc(v[2], ep.headerEnc); // 非法值保持 -1:沿用现有设置
if (v.size() > 3 && !v[3].empty())
HostEndpoint::ParseTransport(v[3], ep.transport);
out = ep;
return true;
}
// 解析分享载荷(buf[1] = SHARE_TYPE_YAMA)。命令字节收 COMMAND_SHARE 和
// COMMAND_ASSIGN_MASTER 两种:前者是现在服务端发的,后者是老服务端/转移命令共用
// 的那个值(转移用 SHARE_TYPE_YAMA_FOREVER,靠 buf[1] 区分)。
inline bool ParseCommand(const unsigned char* buf, unsigned long len, Endpoint& out)
{
if (buf == NULL || len < 3)
return false;
if ((buf[0] != COMMAND_SHARE && buf[0] != COMMAND_ASSIGN_MASTER)
|| buf[1] != SHARE_TYPE_YAMA)
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);
return DecodeFields(StringToVector(payload, ':', 4), out);
}
// share_list 的单条记录:ip:port[:shine[:transport]],与 Windows 客户端写进
// settings/share_list 的格式一致(第 3 段是 Shine,第 4 段才是传输方式——顺序不能反,
// 反了重启后会被当成 Shine 解析)。
inline std::string EncodeEntry(const Endpoint& ep)
{
std::string s = ep.ip + ":" + std::to_string(ep.port);
if (ep.headerEnc < 0 && ep.transport < 0)
return s;
// 传输方式落在第 4 段,第 3 段(Shine)缺省时必须占位,否则重启后
// "ip:port:2" 会被当成 Shine=2 而不是传输方式 2。
s += ":";
if (ep.headerEnc >= 0)
s += std::to_string(ep.headerEnc);
if (ep.transport >= 0)
s += ":" + std::to_string(ep.transport);
return s;
}
inline bool DecodeEntry(const std::string& s, Endpoint& out)
{
if (s.empty())
return false;
return DecodeFields(StringToVector(s, ':', 4), out);
}
// "ip:port" —— 去重只比这两段。同一条分享目标换了传输方式(重新分享成 WSS)时
// 第 3/4 段会变,按整条比会被判成「新条目」,同一个主控留下两条记录 → 同一 clientID
// 起两个分享实例,登录时互相踢。
inline std::string MasterKey(const Endpoint& ep)
{
return ep.ip + ":" + std::to_string(ep.port);
}
inline std::vector<std::string> Split(const std::string& list)
{
std::vector<std::string> items;
if (list.empty())
return items;
for (auto& s : StringToVector(list, '|'))
if (!s.empty())
items.push_back(s);
return items;
}
inline std::string Join(const std::vector<std::string>& items)
{
return VectorJoin(items, '|');
}
inline size_t Count(const std::string& list)
{
return Split(list).size();
}
// 追加一条记录:同一 ip:port 的旧记录被替换(传输方式以最新一次下发为准)。
// 返回新的 share_list 字符串。
inline std::string AddEntry(const std::string& list, const std::string& entry)
{
Endpoint add;
if (!DecodeEntry(entry, add))
return list;
const std::string key = MasterKey(add);
std::vector<std::string> items = Split(list);
bool replaced = false;
for (auto& item : items) {
Endpoint old;
if (DecodeEntry(item, old) && MasterKey(old) == key) {
item = entry;
replaced = true;
}
}
if (!replaced)
items.push_back(entry);
return Join(items);
}
} // namespace HostShare
+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