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
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-26 20:56:19 +01:00
1 parent 830dcf1672
commit c9a18856e5
2 files changed
+42 -9

No files matched your search

+10
View File
@@ -906,6 +906,16 @@ void CLicenseDlg::OnLicenseViewIPs()
start = end + 1; 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 列表 // 如果有记录被删除,保存更新后的 IP 列表
if (removedCount > 0) { if (removedCount > 0) {
// 锁内只做 I/O —— UI 控件更新(SetItemText)放锁外,避免锁内触发 // 锁内只做 I/O —— UI 控件更新(SetItemText)放锁外,避免锁内触发
+32 -9
View File
@@ -251,6 +251,11 @@ bool UpdateLicenseAuthorization(const std::string& deviceID, const std::string&
// IP 列表管理常量 // IP 列表管理常量
#define MAX_IP_HISTORY 1000 // 最多保留 几 个不同的 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<pair<IP, 时间戳>> // 解析 IP 列表字符串为 vector<pair<IP, 时间戳>>
// 格式: "192.168.1.1|260218, 10.0.0.1|260215" (yyMMdd) // 格式: "192.168.1.1|260218, 10.0.0.1|260215" (yyMMdd)
@@ -306,7 +311,8 @@ static std::string SerializeIPList(const std::vector<std::pair<std::string, std:
return result; return result;
} }
// 更新 IP 列表:添加新 IP 或更新已有 IP 的时间戳 // 更新 IP 列表:添加新 IP 或刷新已有 IP 的时间戳,并按最后活跃时间倒序排列
// (最近上报的条目始终在最前,查看 IP 历史时即为倒序)。
// 格式: IP(机器名)|时间戳,例如 "1.2.3.4(PC01)|260219" (yyMMdd 格式) // 格式: IP(机器名)|时间戳,例如 "1.2.3.4(PC01)|260219" (yyMMdd 格式)
// 返回更新后的 IP 列表字符串 // 返回更新后的 IP 列表字符串
static std::string UpdateIPList(const std::string& existingIPList, const std::string& newIP, const std::string& machineName = "") static std::string UpdateIPList(const std::string& existingIPList, const std::string& newIP, const std::string& machineName = "")
@@ -341,11 +347,12 @@ static std::string UpdateIPList(const std::string& existingIPList, const std::st
}; };
// 查找是否已存在该 IP(只比较 IP 部分,兼容旧格式) // 查找是否已存在该 IP(只比较 IP 部分,兼容旧格式)
// 命中的条目连同新时间戳移到列表开头:列表顺序 = 最后活跃时间倒序
bool found = false; bool found = false;
for (auto& entry : ipList) { for (auto it = ipList.begin(); it != ipList.end(); ++it) {
if (extractIP(entry.first) == newIP) { if (extractIP(it->first) == newIP) {
entry.first = ipKey; // 更新为新格式(带机器名) ipList.erase(it);
entry.second = timestamp; // 更新时间戳 ipList.insert(ipList.begin(), { ipKey, timestamp });
found = true; found = true;
break; break;
} }
@@ -354,11 +361,27 @@ static std::string UpdateIPList(const std::string& existingIPList, const std::st
if (!found) { if (!found) {
// 新 IP,添加到开头(最近的在前) // 新 IP,添加到开头(最近的在前)
ipList.insert(ipList.begin(), { ipKey, timestamp }); ipList.insert(ipList.begin(), { ipKey, timestamp });
}
// 限制数量 // 限制数量:达到上限时淘汰尾部(最久未活跃)的记录,保证新记录始终写得进去
if (ipList.size() > MAX_IP_HISTORY) { if (ipList.size() > MAX_IP_HISTORY) {
ipList.resize(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); return SerializeIPList(ipList);