diff --git a/android/app/src/main/cpp/main.cpp b/android/app/src/main/cpp/main.cpp index 46ec05a..e74347d 100644 --- a/android/app/src/main/cpp/main.cpp +++ b/android/app/src/main/cpp/main.cpp @@ -1993,26 +1993,7 @@ static void ApplyTlsAnchorChange() // 重连会重新登录,登录前刷新该字段,否则列里一直显示旧值。只在主线程调用(同 SendLoginInfo)。 static void RefreshLoginTls(LOGIN_INFOR& info) { - std::vector fields; - std::string s(info.szReserved); - size_t pos = 0; - for (;;) { - size_t bar = s.find('|', pos); - if (bar == std::string::npos) { fields.push_back(s.substr(pos)); break; } - fields.push_back(s.substr(pos, bar - pos)); - pos = bar + 1; - } - while (fields.size() <= (size_t)RES_TLS) - fields.push_back(""); - fields[RES_TLS] = TlsEnabled() ? "1" : "0"; - - std::string joined; - for (size_t i = 0; i < fields.size(); ++i) { - if (i) joined += '|'; - joined += fields[i]; - } - strncpy(info.szReserved, joined.c_str(), sizeof(info.szReserved) - 1); - info.szReserved[sizeof(info.szReserved) - 1] = '\0'; + info.SetReserved(RES_TLS, TlsEnabled() ? "1" : "0"); } #endif // YAMA_TLS_CHANNEL @@ -2558,6 +2539,17 @@ static const auto g_processStartMono = std::chrono::steady_clock::now(); // 的状态清掉(服务重启 CaptureService 时真会撞上)。 static std::atomic g_connGeneration{0}; +// 登录 szReserved[20](RES_UPTIME)是「登录这一刻进程已经运行了多少秒」,服务端再加本次 +// 上线时长才是当前运行时长。logInfo 在重连循环外只构造一次,不刷新的话重连上报的仍是 +// 进程启动那一瞬的 0,服务端算出来就只剩「本次连接时长」(Linux 上实测:进程跑了 6 天, +// 浮窗只显示 1.3h)。只在主线程调用(同 SendLoginInfo)。 +static void RefreshLoginUptime(LOGIN_INFOR& info) +{ + long long up = std::chrono::duration_cast( + std::chrono::steady_clock::now() - g_processStartMono).count(); + info.SetReserved(RES_UPTIME, std::to_string(up > 0 ? up : 0).c_str()); +} + static void ConnectionThread(uint64_t gen) { { @@ -2689,6 +2681,7 @@ static void ConnectionThread(uint64_t gen) #ifdef YAMA_TLS_CHANNEL RefreshLoginTls(logInfo); // 传输方式可能已热切换,登录前刷新 RES_TLS #endif + RefreshLoginUptime(logInfo); // 同一条连接可能是几小时前断掉后重连的,按「现在」重算 // 屏幕预览能力位按运行时状态刷新:无障碍服务在跑才声明(API 30+ 的 takeScreenshot), // 服务端据此放行 get_screenshot;无障碍被关掉后重连会撤回,避免服务端白等抓屏。 // 锁屏同理:无障碍服务在跑且 API 28+ 才声明。 diff --git a/common/commands.h b/common/commands.h index b8dd22e..2fad7b7 100644 --- a/common/commands.h +++ b/common/commands.h @@ -1414,6 +1414,36 @@ typedef struct LOGIN_INFOR { { return StringToVector(szReserved, '|', n); } + // 按位置改写第 index 个字段(服务端用 LOGIN_RES 下标取值)。AddReserved 只能往后追加, + // 改不了已写好的字段;重连重发登录前刷新某个「当前状态」字段(RES_TLS / RES_UPTIME) + // 就得靠这个。 + // 字段数按分隔符数 + 1 数,末尾空槽也算一个字段,拼回去时原样保留——否则少写一个 '|' + // 会让后面的字段整体前移,服务端把用户名读成类型。整体放不下 szReserved 时返回 false + // 且保持原值,绝不写半截。 + bool SetReserved(int index, const char* v) + { + if (index < 0 || index >= (int)RES_MAX) return false; + + std::vector fields; + const char* p = szReserved; + for (;;) { + const char* bar = strchr(p, '|'); + if (bar == nullptr) { + fields.push_back(p); + break; + } + fields.push_back(std::string(p, bar - p)); + p = bar + 1; + } + if (fields.size() < (size_t)index + 1) + fields.resize((size_t)index + 1); // 补空槽,std::string 默认空串 + fields[index] = v ? v : ""; + + const std::string joined = VectorJoin(fields, '|'); + if (joined.size() >= sizeof(szReserved)) return false; + memcpy(szReserved, joined.c_str(), joined.size() + 1); + return true; + } private: // 追加一个保留字段。szReserved 是「按位置」解析的(服务端用 LOGIN_RES 下标取值), diff --git a/linux/main.cpp b/linux/main.cpp index 43c2473..9fc173f 100644 --- a/linux/main.cpp +++ b/linux/main.cpp @@ -1778,30 +1778,17 @@ static void ApplySelfUpdate(const PBYTE szBuffer, ULONG ulLength) // 只在主线程调用(与 SendLoginInfo 同一线程),避免和接收线程抢同一个结构体。 static void RefreshLoginTls(LOGIN_INFOR& info) { - std::vector fields; - std::string s(info.szReserved); - size_t pos = 0; - for (;;) { - size_t bar = s.find('|', pos); - if (bar == std::string::npos) { - fields.push_back(s.substr(pos)); - break; - } - fields.push_back(s.substr(pos, bar - pos)); - pos = bar + 1; - } - while (fields.size() <= (size_t)RES_TLS) - fields.push_back(""); - fields[RES_TLS] = TlsEnabled() ? "1" : "0"; + info.SetReserved(RES_TLS, TlsEnabled() ? "1" : "0"); +} - std::string joined; - for (size_t i = 0; i < fields.size(); ++i) { - if (i) - joined += '|'; - joined += fields[i]; - } - strncpy(info.szReserved, joined.c_str(), sizeof(info.szReserved) - 1); - info.szReserved[sizeof(info.szReserved) - 1] = '\0'; +// 登录信息 szReserved 里的 [20] RES_UPTIME 是「登录这一刻进程已经运行了多少秒」,服务端 +// 再加本次上线时长才是当前运行时长。logInfo 在重连循环外只构造一次(见 main 里的注释), +// 不刷新的话每次重连上报的仍是进程启动那一瞬的 0,服务端算出来就只剩「本次连接时长」—— +// vps 实测:进程从 11-14 06:33 一直跑着,浮窗却只显示 1.3h(正好是当天 09:05 那次重连到 +// 双击时刻的间隔),而启动时间本身是对的。与 RefreshLoginTls 同样只在主线程登录前调用。 +static void RefreshLoginUptime(LOGIN_INFOR& info) +{ + info.SetReserved(RES_UPTIME, std::to_string(getProcessUptimeSeconds()).c_str()); } // 能力位(moduleVersion 的 "-%04X" 后缀)里唯一依赖运行环境的是屏幕预览: @@ -1891,6 +1878,9 @@ int main(int argc, char* argv[]) char hostname[256] = {}; gethostname(hostname, sizeof(hostname)); + // 只构造一次,每次连接复用它发登录。因此凡是「随连接变化」的字段都必须在发送前 + // 用 RefreshLoginTls / RefreshLoginUptime / RefreshLoginCaps 刷新;反过来,属于 + // 「进程启动时的事实」(szStartTime、clientID、安装时间…)就靠复用保持不变。 LOGIN_INFOR logInfo; // 读取分组名称(从配置文件或 g_SETTINGS) @@ -2061,6 +2051,8 @@ int main(int argc, char* argv[]) ClientAuth::OnNewConnection(); // 传输方式可能已被热切换(TLS 启/停),登录上报的 RES_TLS 必须反映本次连接。 RefreshLoginTls(logInfo); + // 同一条连接可能是几小时前断掉后重连的,RES_UPTIME 必须按「现在」重算 + RefreshLoginUptime(logInfo); // 屏幕预览能力取决于本机当前是否有可用桌面(daemon 启动时可能还没有 DISPLAY) RefreshLoginCaps(logInfo); ClientObject->SendLoginInfo(logInfo.Speed(clock() - c)); @@ -2082,6 +2074,7 @@ int main(int argc, char* argv[]) } logInfo.szPCName[sizeof(logInfo.szPCName) - 1] = '\0'; RefreshLoginTls(logInfo); + RefreshLoginUptime(logInfo); RefreshLoginCaps(logInfo); ClientObject->SendLoginInfo(logInfo); Mprintf(">> Resent login info after group change\n"); diff --git a/macos/main.mm b/macos/main.mm index 98a7a2e..e295e89 100644 --- a/macos/main.mm +++ b/macos/main.mm @@ -662,35 +662,19 @@ static void fillLoginInfo(LOGIN_INFOR& info) // [[19] RES_TLS] 是「当前」传输状态的快照,服务端在线列表的 TLS 列就取这个值。 // logInfo 只在启动时填一次、之后每条连接复用,因此热切换后重连时必须刷新它, -// 否则列里会一直显示切换前的状态。szReserved 是按 '|' 位置解析的,这里逐字段 -// 拆开、只改第 [19] 项再拼回去(不能用 AddReserved,它只会往后追加)。 +// 否则列里会一直显示切换前的状态(按位置改写见 LOGIN_INFOR::SetReserved)。 // 只在主线程调用(与 SendLoginInfo 同一线程),避免和接收线程抢同一个结构体。 static void RefreshLoginTls(LOGIN_INFOR& info) { - std::vector fields; - std::string s(info.szReserved); - size_t pos = 0; - for (;;) { - size_t bar = s.find('|', pos); - if (bar == std::string::npos) { - fields.push_back(s.substr(pos)); - break; - } - fields.push_back(s.substr(pos, bar - pos)); - pos = bar + 1; - } - while (fields.size() <= (size_t)RES_TLS) - fields.push_back(""); - fields[RES_TLS] = TlsEnabled() ? "1" : "0"; + info.SetReserved(RES_TLS, TlsEnabled() ? "1" : "0"); +} - std::string joined; - for (size_t i = 0; i < fields.size(); ++i) { - if (i) - joined += '|'; - joined += fields[i]; - } - strncpy(info.szReserved, joined.c_str(), sizeof(info.szReserved) - 1); - info.szReserved[sizeof(info.szReserved) - 1] = '\0'; +// [20] RES_UPTIME 是「登录这一刻进程已经运行了多少秒」,服务端再加本次上线时长才是当前 +// 运行时长。logInfo 在重连循环外只构造一次,不刷新的话重连上报的仍是进程启动那一瞬的 +// 0,服务端算出来就只剩「本次连接时长」(Linux 上实测:进程跑了 6 天,浮窗只显示 1.3h)。 +static void RefreshLoginUptime(LOGIN_INFOR& info) +{ + info.SetReserved(RES_UPTIME, std::to_string(getProcessUptimeSeconds()).c_str()); } // 能力位(moduleVersion 的 "-%04X" 后缀)里唯一依赖运行环境的是屏幕预览: @@ -1469,6 +1453,8 @@ int main(int argc, const char* argv[]) ClientAuth::OnNewConnection(); // 本条连接可能是热切换信任锚后重建的:TLS 标志按当前状态重算,再发登录。 RefreshLoginTls(logInfo); + // 同一条连接可能是几小时前断掉后重连的,RES_UPTIME 必须按「现在」重算 + RefreshLoginUptime(logInfo); // 录屏授权可能刚补齐(看守线程会重启进程,但这一次登录也要报对能力位) RefreshLoginCaps(logInfo); ClientObject->SendLoginInfo(logInfo.Speed(clock() - c));