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:
1 parent
830dcf1672
commit
c9a18856e5
2 files changed
+42
-9
No files matched your search
@@ -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<pair<IP, 时间戳>>
|
||||
// 格式: "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;
|
||||
}
|
||||
|
||||
// 更新 IP 列表:添加新 IP 或更新已有 IP 的时间戳
|
||||
// 更新 IP 列表:添加新 IP 或刷新已有 IP 的时间戳,并按最后活跃时间倒序排列
|
||||
// (最近上报的条目始终在最前,查看 IP 历史时即为倒序)。
|
||||
// 格式: IP(机器名)|时间戳,例如 "1.2.3.4(PC01)|260219" (yyMMdd 格式)
|
||||
// 返回更新后的 IP 列表字符串
|
||||
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 部分,兼容旧格式)
|
||||
// 命中的条目连同新时间戳移到列表开头:列表顺序 = 最后活跃时间倒序
|
||||
bool found = false;
|
||||
for (auto& entry : ipList) {
|
||||
if (extractIP(entry.first) == newIP) {
|
||||
entry.first = ipKey; // 更新为新格式(带机器名)
|
||||
entry.second = timestamp; // 更新时间戳
|
||||
for (auto it = ipList.begin(); it != ipList.end(); ++it) {
|
||||
if (extractIP(it->first) == 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);
|
||||
|
||||
Reference in new issue
Block a user