Files
SimpleRemoter/common/host_endpoint.h
T
yuanyuanxianganddeepseek-flash de9e9a7369 Feature(client): support host transfer in the macOS client
Mirror the Android semantics: move the main link to the assigned master
immediately, persist the record in config so it survives restarts, return
to the original master on expiry or early return, and revert after the new
master stays unreachable for 60s. Share requests are answered as
unsupported, and the capability matrix is updated accordingly.

A target counts as unusable when the connection is refused or when it is
accepted but never passes MasterSettings (a foreign service on that port,
or a master built with another key). The latter used to be retried forever,
stranding the host until expiry; both shapes now count toward the revert
gate and reset only once a connection verifies. Early return clears the
persisted record at once, so a crash before the main loop restores the
original master cannot resurrect the transfer on restart.

Endpoints from the command or the record are bounds-checked before being
written into CONNECT_ADDRESS (strcpy_s is strcpy on POSIX), config access
is serialized across the receive and main threads, the new validTo is
published before the pending target so expiry cannot undo a fresh
transfer, and a stale early-return flag is cleared when a new transfer
applies or one is reverted.

The Linux client was rebuilt with the shared header changes; the macOS
side is not compiled here and still needs a build on a Mac.

Co-Authored-By: deepseek-flash
2026-11-21 10:15:35 +01:00

80 lines
2.9 KiB
C++
Raw 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 <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;
}
// 端点能安全写进 CONNECT_ADDRESS 的边界。非 Windows 上 strcpy_s/sprintf_s 就是无界的
// strcpy/sprintf(见 commands.h),地址超长会直接踩坏 szPort/iType 等后续字段,
// 所以来自命令或配置文件的端点必须先过这一关。
inline bool FitsConnAddress(const Endpoint& ep)
{
return !ep.ip.empty()
&& ep.ip.size() <= sizeof(((CONNECT_ADDRESS*)nullptr)->szServerIP) - 1
&& ep.port > 0 && ep.port <= 65535;
}
// 把端点写进连接参数。只在连接生命周期的持有线程调用(见文件头注释)。
inline void ApplyEndpoint(CONNECT_ADDRESS& conn, const Endpoint& ep)
{
if (!FitsConnAddress(ep))
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