Files
SimpleRemoter/common/host_share.h
T
yuanyuanxianganddeepseek-flash 2a97471b51 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
2026-11-07 23:21:44 +01:00

146 lines
5.5 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#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