Core/Warden: Some Warden refactors lifted from #25286.

(cherry picked from commit 5b4288f05f)
This commit is contained in:
Treeston
2020-08-20 17:35:58 +02:00
committed by Shauren
parent 015cee5f27
commit 317bb198b1
3 changed files with 29 additions and 26 deletions

View File

@@ -53,7 +53,7 @@ void WardenCheckMgr::LoadWardenChecks()
uint16 maxCheckId = fields[0].GetUInt16();
CheckStore.resize(maxCheckId + 1);
_checks.resize(maxCheckId + 1);
// 0 1 2 3 4 5 6 7
result = WorldDatabase.Query("SELECT id, type, data, result, address, length, str, comment FROM warden_checks ORDER BY id ASC");
@@ -64,11 +64,11 @@ void WardenCheckMgr::LoadWardenChecks()
fields = result->Fetch();
uint16 const id = fields[0].GetUInt16();
uint8 const type = fields[1].GetUInt8();
WardenCheckType const type = static_cast<WardenCheckType>(fields[1].GetUInt8());
WardenCheck& wardenCheck = CheckStore[id];
WardenCheck& wardenCheck = _checks[id];
wardenCheck.CheckId = id;
wardenCheck.Type = WardenCheckType(type);
wardenCheck.Type = type;
// Initialize action with default action from config
wardenCheck.Action = WardenActions(sWorld->getIntConfig(CONFIG_WARDEN_CLIENT_FAIL_ACTION));
@@ -77,7 +77,7 @@ void WardenCheckMgr::LoadWardenChecks()
wardenCheck.Data = fields[2].GetBinary();
if (type == MPQ_CHECK || type == MEM_CHECK)
CheckResultStore.emplace(id, fields[3].GetBinary());
_checkResults.emplace(id, fields[3].GetBinary());
if (type == MEM_CHECK || type == PAGE_CHECK_A || type == PAGE_CHECK_B || type == PROC_CHECK)
{
@@ -137,12 +137,12 @@ void WardenCheckMgr::LoadWardenOverrides()
// Check if action value is in range (0-2, see WardenActions enum)
if (action > WARDEN_ACTION_BAN)
TC_LOG_ERROR("warden", "Warden check override action out of range (ID: %u, action: %u)", checkId, action);
// Check if check actually exists before accessing the CheckStore vector
else if (checkId >= CheckStore.size())
// Check if check actually exists before accessing the _checks vector
else if (checkId >= _checks.size())
TC_LOG_ERROR("warden", "Warden check action override for non-existing check (ID: %u, action: %u), skipped", checkId, action);
else
{
CheckStore[checkId].Action = WardenActions(action);
_checks[checkId].Action = WardenActions(action);
++count;
}
}
@@ -157,15 +157,15 @@ WardenCheckMgr* WardenCheckMgr::instance()
return &instance;
}
WardenCheck const& WardenCheckMgr::GetCheckDataById(uint16 Id) const
WardenCheck const& WardenCheckMgr::GetCheckData(uint16 Id) const
{
ASSERT(Id < CheckStore.size(), "Requested Warden data for invalid check ID %u", uint32(Id));
return CheckStore[Id];
ASSERT(Id < _checks.size(), "Requested Warden data for invalid check ID %u", uint32(Id));
return _checks[Id];
}
WardenCheckResult const& WardenCheckMgr::GetCheckResultById(uint16 Id) const
WardenCheckResult const& WardenCheckMgr::GetCheckResult(uint16 Id) const
{
auto it = CheckResultStore.find(Id);
ASSERT(it != CheckResultStore.end(), "Requested Warden result for invalid check ID %u", uint32(Id));
auto it = _checkResults.find(Id);
ASSERT(it != _checkResults.end(), "Requested Warden result for invalid check ID %u", uint32(Id));
return it->second;
}

View File

@@ -47,13 +47,13 @@ enum WardenCheckType : uint8
struct WardenCheck
{
uint16 CheckId;
WardenCheckType Type;
std::vector<uint8> Data;
uint32 Address; // PROC_CHECK, MEM_CHECK, PAGE_CHECK
uint8 Length; // PROC_CHECK, MEM_CHECK, PAGE_CHECK
std::string Str; // LUA, MPQ, DRIVER
std::string Comment;
uint16 CheckId;
WardenActions Action;
};
@@ -67,8 +67,8 @@ class TC_GAME_API WardenCheckMgr
public:
static WardenCheckMgr* instance();
WardenCheck const& GetCheckDataById(uint16 Id) const;
WardenCheckResult const& GetCheckResultById(uint16 Id) const;
WardenCheck const& GetCheckData(uint16 Id) const;
WardenCheckResult const& GetCheckResult(uint16 Id) const;
std::vector<uint16> const& GetAvailableMemoryChecks() const { return MemChecksIdPool; }
std::vector<uint16> const& GetAvailableOtherChecks() const { return OtherChecksIdPool; }
@@ -77,8 +77,8 @@ class TC_GAME_API WardenCheckMgr
void LoadWardenOverrides();
private:
std::vector<WardenCheck> CheckStore;
std::unordered_map<uint16, WardenCheckResult> CheckResultStore;
std::vector<WardenCheck> _checks;
std::unordered_map<uint16, WardenCheckResult> _checkResults;
std::vector<uint16> MemChecksIdPool;
std::vector<uint16> OtherChecksIdPool;
};

View File

@@ -223,7 +223,7 @@ void WardenWin::RequestChecks()
uint16 const id = *(_otherChecksIt++);
WardenCheck const& check = sWardenCheckMgr->GetCheckDataById(id);
WardenCheck const& check = sWardenCheckMgr->GetCheckData(id);
if (!check.Str.empty())
{
buff << uint8(check.Str.size());
@@ -242,7 +242,7 @@ void WardenWin::RequestChecks()
for (uint16 const id : _currentChecks)
{
WardenCheck const& check = sWardenCheckMgr->GetCheckDataById(id);
WardenCheck const& check = sWardenCheckMgr->GetCheckData(id);
WardenCheckType const type = check.Type;
buff << uint8(type ^ xorByte);
@@ -370,7 +370,7 @@ void WardenWin::HandleCheckResult(ByteBuffer &buff)
uint16 checkFailed = 0;
for (uint16 const id : _currentChecks)
{
WardenCheck const& check = sWardenCheckMgr->GetCheckDataById(id);
WardenCheck const& check = sWardenCheckMgr->GetCheckData(id);
switch (check.Type)
{
@@ -389,9 +389,12 @@ void WardenWin::HandleCheckResult(ByteBuffer &buff)
std::vector<uint8> response;
response.resize(check.Length);
buff.read(response.data(), response.size());
if (response != sWardenCheckMgr->GetCheckResultById(id))
WardenCheckResult const& expected = sWardenCheckMgr->GetCheckResult(id);
if (response != expected)
{
TC_LOG_DEBUG("warden", "RESULT MEM_CHECK fail CheckId %u account Id %u", id, _session->GetAccountId());
TC_LOG_DEBUG("warden", "Expected: %s", ByteArrayToHexStr(expected).c_str());
TC_LOG_DEBUG("warden", "Got: %s", ByteArrayToHexStr(response).c_str());
checkFailed = id;
continue;
}
@@ -422,7 +425,7 @@ void WardenWin::HandleCheckResult(ByteBuffer &buff)
if (Lua_Result != 0)
{
uint8 luaStrLen = buff.read<uint8>();
if (luaStrLen != 0)
if (luaStrLen == 0)
{
std::string str;
str.resize(luaStrLen);
@@ -451,7 +454,7 @@ void WardenWin::HandleCheckResult(ByteBuffer &buff)
std::vector<uint8> result;
result.resize(Trinity::Crypto::SHA1::DIGEST_LENGTH);
buff.read(result.data(), result.size());
if (result != sWardenCheckMgr->GetCheckResultById(id)) // SHA1
if (result != sWardenCheckMgr->GetCheckResult(id)) // SHA1
{
TC_LOG_DEBUG("warden", "RESULT MPQ_CHECK fail, CheckId %u account Id %u", id, _session->GetAccountId());
checkFailed = id;
@@ -468,7 +471,7 @@ void WardenWin::HandleCheckResult(ByteBuffer &buff)
if (checkFailed > 0)
{
WardenCheck const& check = sWardenCheckMgr->GetCheckDataById(checkFailed);
WardenCheck const& check = sWardenCheckMgr->GetCheckData(checkFailed);
char const* penalty = ApplyPenalty(&check);
TC_LOG_WARN("warden", "%s failed Warden check %u (%s). Action: %s", _session->GetPlayerInfo().c_str(), checkFailed, EnumUtils::ToConstant(check.Type), penalty);
}