A Go-managed host cannot receive the client upgrade that only the MFC master can push. Transfer moves its main link there and brings it back after N days; MFC has no reason to return it. Transfer is not share: ASSIGN writes the target and drops the current connection, so the old master sees the host go offline and the new master gets a normal main link it can remote control and upgrade. While a transfer is live the client refuses a second transfer and any share, so no host is handed to a third master that expiry would orphan. Server: admin-only `transfer_device` WS command, COMMAND_ASSIGN_MASTER + SHARE_TYPE_YAMA_FOREVER as `host:port:days::transport\0`, byte-compatible with the MFC dialog. The transport segment is always present (YAMA_TRANSFER_TRANSPORT, default 0/TCP) because clients keep their current link when it is missing: a WSS-managed host would otherwise retry WSS against the MFC master's raw TCP port and never connect. The empty shine segment keeps the host's header encryption. The button only renders when `device_list.transfer` is present, which MFC never sends, so no MFC change is needed. devicehistory keeps transfer_to/transfer_until for the card badge and drops the marker on expiry or on a fresh login here. Client: KernelManager.cpp writes the registry and disconnects; ClientDll.cpp re-reads the registry at the top of every reconnect, so a mid-session transfer lands on the next round. Expiry is checked before every connect attempt as well as inside the heartbeat loop, so an unreachable target cannot pin the host to an expired address until a restart; restoring rebuilds the link captured on entry (address, header encryption, transport). linux/main.cpp applies the transfer on the main loop and restores the captured endpoint at expiry; Android does the same; macOS has neither share nor transfer.
147 lines
5.6 KiB
C++
147 lines
5.6 KiB
C++
#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
|