Fix: Report process uptime and harden the login reserved list

The "启动信息" elapsed time was computed by subtracting a client-formatted
wall-clock string from the master's own local time. Windows clients send their
local time, Linux/Android/macOS send Beijing time, so a host whose clock runs
ahead of the master (UTC+8 clients on a UTC+1 master, a fast CMOS clock) gave a
negative difference that was clamped to 0s — permanently, for that host.

Add RES_UPTIME to the login reserved list: the client reports its process
uptime in seconds, a difference between two readings of the same clock, so
timezone and clock skew cancel out. The master adds the connection's alive
time to get the current value, and keeps the old string path as the fallback
for clients that do not send the field. That fallback now also treats MAC as
Beijing time, which was simply missed.

AddReserved() now goes through one bounds-checked append. The separator used
to be written without any length check (an overflow aborts the client, which
installs no invalid parameter handler), and an over-long value now leaves an
empty slot instead of dropping its separator, so positional lookups cannot
shift.

Verified with an ASan/UBSan test over the encoding boundaries and Debug builds
of the client and the master. The Linux/Android/macOS clients only append the
same field at the end of the list; they were not compiled here.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-08 21:49:20 +01:00
1 parent 2e34e49186
commit 2cbed0dcdf
6 files changed
+149 -38

No files matched your search

+51 -26
View File
@@ -10026,33 +10026,52 @@ CString FormatValue(double dValue, LPCTSTR szUnit)
return str;
}
// 计算「启动信息」里的运行时长。客户端上报的时间字符串没有时区标记:Windows 客户端
// 发的是本机本地时间(client/LoginServer.cpp getProcessTime),Linux/Android 发的是
// 北京时间(linux/main.cpp 与 common/commands.h 的 ToPekingTimeAsString,恒为 UTC+8)。
// 因此基准「现在」必须按客户端类型取:LNX/APK 用北京时间的当前时刻,其余用本地时间。
// 否则服务端不在 UTC+8 时,北京时间会被当成本地时间,得到的启动时间「在未来」——早先
// 取绝对值后就成了恒定的时区差(UTC+1 服务端上 Linux 主机永远显示 7h)。
CString GetElapsedTime(LPCTSTR szBaseTime, bool pekingBase)
// 计算「启动信息」里的运行时长。有两个数据来源:
//
// 1. 新客户端的 RES_UPTIME:登录时上报「进程已运行秒数」。它是个差值而不是时刻,
// 客户端用同一块时钟的两次读数相减,时区与整机时钟偏移都抵消了(Windows 见
// client/LoginServer.cpp getProcessUptime)。叠加本次上线时长即为当前运行时长,
// 与主控在哪个时区、客户端时钟准不准都无关。
//
// 2. 老客户端只有 szStartTime 字符串,没有时区标记:Windows 客户端发的是本机本地
// 时间(getProcessTime),Linux/Android/macOS 发的是北京时间(恒为 UTC+8,macOS
// 见 getTimeString 里的 timeZoneForSecondsFromGMT)。因此基准「现在」必须按客户端
// 类型取:LNX/APK/MAC 用北京时间的当前时刻,其余用本地时间。否则服务端不在 UTC+8
// 时,北京时间会被当成本地时间,得到的启动时间「在未来」。
// 这类错值服务端修正不了,只能显示 0s 等客户端升级——上线后就会走第 1 条。
CString GetElapsedTime(LPCTSTR szBaseTime, bool pekingBase, LPCTSTR szUptime, ULONGLONG ulOnlineSec)
{
int nYear, nMonth, nDay, nHour, nMin, nSec;
if (_stscanf_s(szBaseTime, _T("%d-%d-%d %d:%d:%d"),
&nYear, &nMonth, &nDay, &nHour, &nMin, &nSec) != 6) {
return _T("0s");
__int64 nSeconds = -1;
if (szUptime != NULL && szUptime[0] != _T('\0')) {
__int64 nUptime = _ttoi64(szUptime);
if (nUptime >= 0) {
nSeconds = nUptime + (__int64)ulOnlineSec;
// 主控自己的时钟回拨会让上线时长算出负数,此时只用客户端上报的值
if (nSeconds < nUptime) nSeconds = nUptime;
}
}
CTime tmBase(nYear, nMonth, nDay, nHour, nMin, nSec);
CTime tmNow;
if (pekingBase) {
auto t = ToPekingTime(nullptr);
tmNow = CTime(t.tm_year + 1900, t.tm_mon + 1, t.tm_mday,
t.tm_hour, t.tm_min, t.tm_sec);
} else {
tmNow = CTime::GetCurrentTime();
if (nSeconds < 0) { // 老客户端:退回按时间字符串计算
int nYear, nMonth, nDay, nHour, nMin, nSec;
if (_stscanf_s(szBaseTime, _T("%d-%d-%d %d:%d:%d"),
&nYear, &nMonth, &nDay, &nHour, &nMin, &nSec) != 6) {
return _T("0s");
}
CTime tmBase(nYear, nMonth, nDay, nHour, nMin, nSec);
CTime tmNow;
if (pekingBase) {
auto t = ToPekingTime(nullptr);
tmNow = CTime(t.tm_year + 1900, t.tm_mon + 1, t.tm_mday,
t.tm_hour, t.tm_min, t.tm_sec);
} else {
tmNow = CTime::GetCurrentTime();
}
nSeconds = (tmNow - tmBase).GetTotalSeconds();
// 启动时间「在未来」只可能是时区约定/客户端时钟不一致(如老客户端),此时运行时长
// 不可知,钳成 0 而不是取绝对值,免得把时钟偏差当成运行时长显示出来。
if (nSeconds < 0) nSeconds = 0;
}
__int64 nSeconds = (tmNow - tmBase).GetTotalSeconds();
// 启动时间「在未来」只可能是时区约定/客户端时钟不一致(如老客户端),此时运行时长
// 不可知,钳成 0 而不是取绝对值,免得把时钟偏差当成运行时长显示出来。
if (nSeconds < 0) nSeconds = 0;
if (nSeconds < 60)
return FormatValue((double)nSeconds, _T("s"));
@@ -10117,12 +10136,18 @@ void CMy2015RemoteDlg::OnListClick(NMHDR* pNMHDR, LRESULT* pResult)
CString strText;
std::string expired = res[RES_EXPIRED_DATE];
expired = expired.empty() ? "" : " Expired on " + expired;
// LNX/APK 客户端上报的启动时间是北京时间,基准时刻按此选(见 GetElapsedTime)
const bool pekingBase = (res[RES_CLIENT_TYPE] == "LNX" || res[RES_CLIENT_TYPE] == "APK");
// LNX/APK 客户端上报的启动时间是北京时间,基准时刻按此选(见 GetElapsedTime);
// macOS 的 getTimeString() 同样固定按 UTC+8 格式化,一并按北京时间解释。
const bool pekingBase = (res[RES_CLIENT_TYPE] == "LNX" || res[RES_CLIENT_TYPE] == "APK" ||
res[RES_CLIENT_TYPE] == "MAC");
// 新客户端带 RES_UPTIME(登录时已运行秒数),加上本次上线时长即为当前运行时长;
// 老客户端该字段为空,GetElapsedTime 内部退回按启动时间字符串计算。
CString elapsed = GetElapsedTime(startTime, pekingBase, res[RES_UPTIME],
(ULONGLONG)ctx->GetAliveTime());
strText.FormatL(_T("文件路径: %s%s %s%s\r\n系统信息: %s 位 %s 核心 %s GB %s\r\n启动信息: %s %s %s%s %s\r\n上线信息: %s %d %s Master-%s\r\n客户信息: %s"),
res[RES_PROGRAM_BITS].IsEmpty() ? "" : res[RES_PROGRAM_BITS] + _L(" 位 "), res[RES_FILE_PATH], res[RES_EXE_VERSION], processInfo,
res[RES_SYSTEM_BITS], res[RES_SYSTEM_CPU], res[RES_SYSTEM_MEM], res[RES_RESOLUTION], startTime, expired.c_str(),
res[RES_USERNAME], res[RES_ISADMIN] == "1" ? _L("[管理员]") : res[RES_ISADMIN].IsEmpty() ? "" : _L("[非管理员]"), GetElapsedTime(startTime, pekingBase),
res[RES_USERNAME], res[RES_ISADMIN] == "1" ? _L("[管理员]") : res[RES_ISADMIN].IsEmpty() ? "" : _L("[非管理员]"), elapsed,
ctx->GetProtocol().c_str(), ctx->GetServerPort(), typMap[type].c_str(), ctx->GetMasterID().c_str(), res[RES_CLIENT_ID]);
// 地理信息只在加载了本地 IP 库(QQWry/ip2region)时才查询并展示。
// 未装库时 m_IPConverter 是网络版 GetGeoLocation(同步外网 HTTP,可达数秒),