#pragma once #include #include #include #include #include #include #include class ExternalServerList { public: typedef struct _Entry { std::string address; uint16_t port; std::string name; } Entry; static const uint32_t MaxEntries = 4096; static bool Read(std::vector& entries) { entries.clear(); FILE* file = nullptr; if (fopen_s(&file, "servers.db", "rb") != 0) return errno == ENOENT; char magic[4] = {}; uint32_t version = 0; uint32_t count = 0; bool success = (fread(magic, 1, sizeof(magic), file) == sizeof(magic) && memcmp(magic, "MCSV", sizeof(magic)) == 0) && (fread(&version, sizeof(version), 1, file) == 1 && fread(&count, sizeof(count), 1, file) == 1) && version == 1 && count <= MaxEntries; std::vector loadedEntries; if (success) { loadedEntries.reserve(count); for (uint32_t index = 0; index < count; ++index) { uint16_t addressLength = 0; uint16_t port = 0; uint16_t nameLength = 0; if (fread(&addressLength, sizeof(addressLength), 1, file) != 1 || addressLength == 0 || addressLength > 256) { success = false; break; } char address[257] = {}; if (fread(address, 1, addressLength, file) != addressLength || fread(&port, sizeof(port), 1, file) != 1 || fread(&nameLength, sizeof(nameLength), 1, file) != 1 || nameLength > 256) { success = false; break; } char name[257] = {}; if ((nameLength != 0 && fread(name, 1, nameLength, file) != nameLength) || port == 0) { success = false; break; } loadedEntries.push_back({ std::string(address, addressLength), port, std::string(name, nameLength) }); } } if (fclose(file) != 0) success = false; if (success) entries.swap(loadedEntries); return success; } static bool Write(const std::vector& entries) { if (entries.size() > MaxEntries) return false; FILE* file = nullptr; if (fopen_s(&file, "servers.db.tmp", "wb") != 0) return false; bool success = fwrite("MCSV", 1, 4, file) == 4; const uint32_t version = 1; const uint32_t count = static_cast(entries.size()); success = success && fwrite(&version, sizeof(version), 1, file) == 1 && fwrite(&count, sizeof(count), 1, file) == 1; for (size_t index = 0; success && index < entries.size(); ++index) { const Entry& entry = entries[index]; uint64_t sessionId = 0; if (entry.address.size() > 256 || entry.name.size() > 256 || !MakeSessionId(entry.address, entry.port, sessionId)) { success = false; break; } const uint16_t addressLength = static_cast(entry.address.size()); const uint16_t nameLength = static_cast(entry.name.size()); success = fwrite(&addressLength, sizeof(addressLength), 1, file) == 1 && fwrite(entry.address.data(), 1, addressLength, file) == addressLength && fwrite(&entry.port, sizeof(entry.port), 1, file) == 1 && fwrite(&nameLength, sizeof(nameLength), 1, file) == 1 && (nameLength == 0 || fwrite(entry.name.data(), 1, nameLength, file) == nameLength); } if (fclose(file) != 0) success = false; if (!success || !MoveFileExA("servers.db.tmp", "servers.db", MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH)) { DeleteFileA("servers.db.tmp"); return false; } return true; } static bool Append(const Entry& entry) { std::vector entries; if (!Read(entries)) return false; entries.push_back(entry); return Write(entries); } static bool MakeSessionId(const std::string& address, uint16_t port, uint64_t& sessionId) { if (address.empty() || address.size() > 253 || port == 0) return false; uint32_t ipAddress = 0; size_t position = 0; bool isIPv4 = true; for (int octetIndex = 0; octetIndex < 4; ++octetIndex) { if (position >= address.size()) { isIPv4 = false; break; } uint32_t octet = 0; size_t digits = 0; while (position < address.size() && address[position] >= '0' && address[position] <= '9') { octet = octet * 10 + static_cast(address[position] - '0'); if (octet > 255) return false; ++position; ++digits; } if (digits == 0) { isIPv4 = false; break; } ipAddress = (ipAddress << 8) | octet; if (octetIndex < 3) { if (position >= address.size() || address[position] != '.') { isIPv4 = false; break; } ++position; } } if (isIPv4 && position == address.size()) { sessionId = 0x4C41000000000000ULL | (static_cast(ipAddress) << 16) | static_cast(port); return true; } uint64_t hash = 14695981039346656037ULL; size_t labelLength = 0; char previous = '\0'; for (size_t index = 0; index < address.size(); ++index) { char character = address[index]; if (character == '.') { if (labelLength == 0 || previous == '-') return false; labelLength = 0; } else { if (!((character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z') || (character >= '0' && character <= '9') || character == '-')) { return false; } if (labelLength == 0 && character == '-') return false; ++labelLength; if (labelLength > 63) return false; if (character >= 'A' && character <= 'Z') character = static_cast(character - 'A' + 'a'); } hash ^= static_cast(character); hash *= 1099511628211ULL; previous = character; } if (labelLength == 0 || previous == '-') return false; hash ^= static_cast(port & 0xFF); hash *= 1099511628211ULL; hash ^= static_cast(port >> 8); hash *= 1099511628211ULL; sessionId = 0x4C53000000000000ULL | (hash & 0x0000FFFFFFFFFFFFULL); return true; } };