added hash
This commit is contained in:
1 parent
2c1c077611
commit
251a59ddbe
5 files changed
+58
-7
No files matched your search
+34
-3
@@ -129,6 +129,28 @@ public:
|
||||
});
|
||||
}
|
||||
|
||||
bool IsMessageValid(const std::string& fullMessage) {
|
||||
#ifdef ENABLE_NETWORK_CHECKSUM
|
||||
size_t hashPos = fullMessage.find("#hash:");
|
||||
if (hashPos == std::string::npos) {
|
||||
return false; // Хеша нет, хотя он ожидался
|
||||
}
|
||||
|
||||
std::string originalContent = fullMessage.substr(0, hashPos);
|
||||
std::string receivedHashStr = fullMessage.substr(hashPos + 6); // 6 — длина "#hash:"
|
||||
|
||||
// Вычисляем ожидаемый хеш от контента
|
||||
size_t expectedHash = std::hash<std::string>{}(originalContent + NET_SECRET);
|
||||
|
||||
std::stringstream ss;
|
||||
ss << std::hex << expectedHash;
|
||||
|
||||
return ss.str() == receivedHashStr;
|
||||
#else
|
||||
return true; // В режиме отладки пропускаем всё
|
||||
#endif
|
||||
}
|
||||
|
||||
private:
|
||||
/*
|
||||
void init() {
|
||||
@@ -299,8 +321,17 @@ private:
|
||||
}
|
||||
|
||||
void process_message(const std::string& msg) {
|
||||
std::cout << "Received from player " << id_ << ": " << msg << std::endl;
|
||||
auto parts = split(msg, ':');
|
||||
if (!IsMessageValid(msg)) {
|
||||
// Логируем попытку подмены и просто выходим из обработки
|
||||
std::cout << "[Security] Invalid packet hash. Dropping message." << std::endl;
|
||||
return;
|
||||
}
|
||||
std::string cleanMessage = msg.substr(0, msg.find("#hash:"));
|
||||
|
||||
|
||||
|
||||
std::cout << "Received from player " << id_ << ": " << cleanMessage << std::endl;
|
||||
auto parts = split(cleanMessage, ':');
|
||||
|
||||
if (parts.empty()) return;
|
||||
|
||||
@@ -326,7 +357,7 @@ private:
|
||||
receivedState.handle_full_sync(parts, 2);
|
||||
timedClientStates.add_state(receivedState);
|
||||
|
||||
retranslateMessage(msg);
|
||||
retranslateMessage(cleanMessage);
|
||||
}
|
||||
else if (parts[0] == "RESPAWN") {
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user