|
| 1 | +#include "update.hpp" |
| 2 | + |
| 3 | +#include "config.hpp" |
| 4 | +#include "globals.hpp" |
| 5 | +#include "log.hpp" |
| 6 | +#include "utils.hpp" |
| 7 | +#include "version.hpp" |
| 8 | + |
| 9 | +#include <curl/curl.h> |
| 10 | +#include <curl/easy.h> |
| 11 | +#include "yaml-cpp/yaml.h" |
| 12 | + |
| 13 | +#include <filesystem> |
| 14 | +#include <fstream> |
| 15 | +#include <map> |
| 16 | +#include <map> |
| 17 | +#include <string> |
| 18 | + |
| 19 | +static CURL* curl = nullptr; |
| 20 | + |
| 21 | +std::map<uint64_t, std::unordered_set<std::string>> Updater::clientHashMap = std::map<uint64_t, std::unordered_set<std::string>>(); |
| 22 | + |
| 23 | +static size_t writeCallback(const char* content, size_t size, size_t memberSize, std::string* data) |
| 24 | +{ |
| 25 | + data->append(content, size * memberSize); |
| 26 | + return size * memberSize; |
| 27 | +} |
| 28 | + |
| 29 | +bool Updater::init() |
| 30 | +{ |
| 31 | + std::string data; |
| 32 | + |
| 33 | + curl = curl_easy_init(); |
| 34 | + curl_easy_setopt(curl, CURLOPT_URL, "https://raw.githubusercontent.com/AceSLS/SLSsteam/refs/heads/main/res/updates.yaml"); |
| 35 | + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback); |
| 36 | + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data); |
| 37 | + |
| 38 | + auto res = curl_easy_perform(curl); |
| 39 | + g_pLog->info("Curl Res: %u\n", res); |
| 40 | + |
| 41 | + curl_easy_cleanup(curl); |
| 42 | + |
| 43 | + if(res != 0) |
| 44 | + { |
| 45 | + data = loadFromCache(); |
| 46 | + if(data.size() < 1) |
| 47 | + { |
| 48 | + return false; |
| 49 | + } |
| 50 | + |
| 51 | + g_pLog->info("Using cached updates.yaml\n"); |
| 52 | + } |
| 53 | + |
| 54 | + g_pLog->debug("updates.yaml:\n%s\n", data.c_str()); |
| 55 | + |
| 56 | + try |
| 57 | + { |
| 58 | + YAML::Node node = YAML::Load(data); |
| 59 | + for (const auto& sub : node["SafeModeHashes"]) |
| 60 | + { |
| 61 | + uint64_t version = sub.first.as<uint64_t>(); |
| 62 | + clientHashMap[version] = std::unordered_set<std::string>(); |
| 63 | + |
| 64 | + g_pLog->debug("Parsing version %llu\n", version); |
| 65 | + |
| 66 | + for(const auto& hash : sub.second) |
| 67 | + { |
| 68 | + auto str = hash.as<std::string>(); |
| 69 | + clientHashMap[version].emplace(str); |
| 70 | + |
| 71 | + g_pLog->debug("Added %s to SLSsteam version %llu\n", str.c_str(), version); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + catch(...) |
| 76 | + { |
| 77 | + g_pLog->info("Failed to parse updates!\n"); |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + saveToCache(data); |
| 82 | + return true; |
| 83 | +} |
| 84 | + |
| 85 | +std::string Updater::getCacheFilePath() |
| 86 | +{ |
| 87 | + auto path = g_config.getDir().append("/.updates.yaml"); |
| 88 | + return path; |
| 89 | +} |
| 90 | + |
| 91 | +void Updater::saveToCache(std::string yaml) |
| 92 | +{ |
| 93 | + auto path = Updater::getCacheFilePath(); |
| 94 | + |
| 95 | + std::ofstream stream = std::ofstream(path.c_str()); |
| 96 | + stream << yaml; |
| 97 | + stream.close(); |
| 98 | + |
| 99 | + g_pLog->debug("Cached res/updates.yaml!\n"); |
| 100 | +} |
| 101 | + |
| 102 | +std::string Updater::loadFromCache() |
| 103 | +{ |
| 104 | + auto path = Updater::getCacheFilePath(); |
| 105 | + if (!std::filesystem::exists(path)) |
| 106 | + { |
| 107 | + return std::string(); |
| 108 | + } |
| 109 | + |
| 110 | + g_pLog->debug("Loading updates.ymal from disk!\n"); |
| 111 | + |
| 112 | + std::ifstream fstream = std::ifstream(path.c_str()); |
| 113 | + std::stringstream buf; |
| 114 | + buf << fstream.rdbuf(); |
| 115 | + |
| 116 | + fstream.close(); |
| 117 | + return buf.str(); |
| 118 | +} |
| 119 | + |
| 120 | +bool Updater::verifySafeModeHash() |
| 121 | +{ |
| 122 | + auto path = std::filesystem::path(g_modSteamClient.path); |
| 123 | + |
| 124 | + try |
| 125 | + { |
| 126 | + std::string sha256 = Utils::getFileSHA256(path.c_str()); |
| 127 | + g_pLog->info("steamclient.so hash is %s\n", sha256.c_str()); |
| 128 | + |
| 129 | + for (const auto& safeHash : clientHashMap) |
| 130 | + { |
| 131 | + g_pLog->debug("SafeHash %llu -> %llu\n", safeHash.first, VERSION); |
| 132 | + if (VERSION > safeHash.first) |
| 133 | + { |
| 134 | + continue; |
| 135 | + } |
| 136 | + |
| 137 | + if (safeHash.second.contains(sha256)) |
| 138 | + { |
| 139 | + return true; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + return false; |
| 144 | + } |
| 145 | + catch(std::runtime_error& err) |
| 146 | + { |
| 147 | + g_pLog->debug("Unable to read steamclient.so hash!\n"); |
| 148 | + return false; |
| 149 | + } |
| 150 | + |
| 151 | + return true; |
| 152 | +} |
0 commit comments