From c9a18856e5b1d1b3facd45db802c91b2885ce5e5 Mon Sep 17 00:00:00 2001 From: yuanyuanxiang <962914132@qq.com> Date: Thu, 26 Nov 2026 20:56:19 +0100 Subject: [PATCH] Fix: order IP history by last activity and cap the stored value The IP list behind "View IP history" was kept in first-seen order: an existing IP only refreshed its timestamp in place, so a host that kept reconnecting for months stayed wherever it originally landed. Updates now move the entry to the front, making the stored list last-active first; the dialog also sorts by the recorded date so legacy lists show the correct order without waiting for every host to reconnect. At the cap, trimming now runs on every update and evicts the tail (the least recently active entry), so a new IP always fits. A 60 KB byte cap protects the single value: the Win32 profile API mishandles a value over ~64 KB (GetPrivateProfileStringA returns only the tail, while WritePrivateProfileStringA appends the whole line again and the file doubles), and the live licenses.ini already carries a 33.5 KB list. Verified: the Release server builds; the value limit was measured against kernel32 - 65,500 bytes round-trips, 66,000 corrupts the file. Co-Authored-By: deepseek-flash --- server/2015Remote/CLicenseDlg.cpp | 10 ++++++++ server/2015Remote/CPasswordDlg.cpp | 41 +++++++++++++++++++++++------- 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/server/2015Remote/CLicenseDlg.cpp b/server/2015Remote/CLicenseDlg.cpp index 67ccbd8..cdc90be 100644 --- a/server/2015Remote/CLicenseDlg.cpp +++ b/server/2015Remote/CLicenseDlg.cpp @@ -906,6 +906,16 @@ void CLicenseDlg::OnLicenseViewIPs() start = end + 1; } + // 按最后活跃时间倒序展示(新的在前);无时间戳/无法解析的记录排在最后。 + // 存储侧已保证最近上报的条目在前,这里再做一次稳定排序,兼容旧版本写下的 + // "首次出现倒序" 数据,打开对话框即可看到正确的顺序。 + std::stable_sort(records.begin(), records.end(), + [](const IPRecord& a, const IPRecord& b) { + int da = (a.daysAgo < 0) ? 0x7FFFFFFF : a.daysAgo; + int db = (b.daysAgo < 0) ? 0x7FFFFFFF : b.daysAgo; + return da < db; + }); + // 如果有记录被删除,保存更新后的 IP 列表 if (removedCount > 0) { // 锁内只做 I/O —— UI 控件更新(SetItemText)放锁外,避免锁内触发 diff --git a/server/2015Remote/CPasswordDlg.cpp b/server/2015Remote/CPasswordDlg.cpp index d3a5775..7404deb 100644 --- a/server/2015Remote/CPasswordDlg.cpp +++ b/server/2015Remote/CPasswordDlg.cpp @@ -251,6 +251,11 @@ bool UpdateLicenseAuthorization(const std::string& deviceID, const std::string& // IP 列表管理常量 #define MAX_IP_HISTORY 1000 // 最多保留 几 个不同的 IP +// 单个 IP 值总字节数上限。Win32 profile API 对单个值有约 64KB 的硬限制: +// 值超过后 GetPrivateProfileStringA 只返回被截断的尾部,而 +// WritePrivateProfileStringA 会把整行再追加一遍(实测 66KB 的值读回 464 字节、 +// 文件膨胀到两倍),之后的读改写会永久写坏历史。留出余量按 60KB 控制。 +#define MAX_IP_LIST_BYTES 60000 // 解析 IP 列表字符串为 vector> // 格式: "192.168.1.1|260218, 10.0.0.1|260215" (yyMMdd) @@ -306,7 +311,8 @@ static std::string SerializeIPList(const std::vectorfirst) == newIP) { + ipList.erase(it); + ipList.insert(ipList.begin(), { ipKey, timestamp }); found = true; break; } @@ -354,11 +361,27 @@ static std::string UpdateIPList(const std::string& existingIPList, const std::st if (!found) { // 新 IP,添加到开头(最近的在前) ipList.insert(ipList.begin(), { ipKey, timestamp }); + } - // 限制数量 - if (ipList.size() > MAX_IP_HISTORY) { - ipList.resize(MAX_IP_HISTORY); - } + // 限制数量:达到上限时淘汰尾部(最久未活跃)的记录,保证新记录始终写得进去 + if (ipList.size() > MAX_IP_HISTORY) { + ipList.resize(MAX_IP_HISTORY); + } + + // 限制字节数:尾部(最久未活跃)继续淘汰,避免单个值越过 INI 的 64KB 上限后 + // 读写整体失效(见 MAX_IP_LIST_BYTES 注释)。机器名很长时条数没到上限也会触发。 + size_t totalBytes = 0; + for (size_t i = 0; i < ipList.size(); ++i) { + totalBytes += ipList[i].first.size(); + if (!ipList[i].second.empty()) totalBytes += 1 + ipList[i].second.size(); + if (i > 0) totalBytes += 2; // ", " 分隔符 + } + while (ipList.size() > 1 && totalBytes > MAX_IP_LIST_BYTES) { + const auto& last = ipList.back(); + totalBytes -= last.first.size(); + if (!last.second.empty()) totalBytes -= 1 + last.second.size(); + totalBytes -= 2; // 去掉它前面的 ", " + ipList.pop_back(); } return SerializeIPList(ipList);