Download check (#184)

This PR makes it so that the mod hash and download confirmation are
verified before proceeding.

---

By creating this pull request, I understand that code that is AI
generated or otherwise automatically generated may be rejected without
further discussion.
I declare that I fully understand all code I pushed into this PR, and
wrote all this code myself and own the rights to this code.
This commit is contained in:
Tixx 2025-05-05 23:46:32 +02:00 committed by GitHub
commit 0341d401e7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -573,6 +573,13 @@ void NewSyncResources(SOCKET Sock, const std::string& Mods, const std::vector<Mo
break; break;
} }
if (Data != "AG") {
UUl("Received corrupted download confirmation, aborting download.");
debug("Corrupted download confirmation: " + Data);
Terminate = true;
break;
}
std::string Name = std::to_string(ModNo) + "/" + std::to_string(TotalMods) + ": " + FName; std::string Name = std::to_string(ModNo) + "/" + std::to_string(TotalMods) + ": " + FName;
std::vector<char> DownloadedFile = SingleNormalDownload(Sock, ModInfoIter->FileSize, Name); std::vector<char> DownloadedFile = SingleNormalDownload(Sock, ModInfoIter->FileSize, Name);
@ -587,11 +594,16 @@ void NewSyncResources(SOCKET Sock, const std::string& Mods, const std::vector<Mo
OutFile.write(DownloadedFile.data(), DownloadedFile.size()); OutFile.write(DownloadedFile.data(), DownloadedFile.size());
OutFile.flush(); OutFile.flush();
} }
// 2. verify size // 2. verify size and hash
if (std::filesystem::file_size(PathToSaveTo) != DownloadedFile.size()) { if (std::filesystem::file_size(PathToSaveTo) != DownloadedFile.size()) {
error("Failed to write the entire file '" + PathToSaveTo + "' correctly (file size mismatch)"); error("Failed to write the entire file '" + PathToSaveTo + "' correctly (file size mismatch)");
Terminate = true; Terminate = true;
} }
if (GetSha256HashReallyFast(PathToSaveTo) != ModInfoIter->Hash) {
error("Failed to write or download the entire file '" + PathToSaveTo + "' correctly (hash mismatch)");
Terminate = true;
}
} while (fs::file_size(PathToSaveTo) != ModInfoIter->FileSize && !Terminate); } while (fs::file_size(PathToSaveTo) != ModInfoIter->FileSize && !Terminate);
if (!Terminate) { if (!Terminate) {
if (!fs::exists(GetGamePath() + "mods/multiplayer")) { if (!fs::exists(GetGamePath() + "mods/multiplayer")) {