| 1 | // Copyright 2018 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "UICommon/AutoUpdate.h" |
| 5 | |
| 6 | #include <atomic> |
| 7 | #include <cstdlib> |
| 8 | #include <string> |
| 9 | |
| 10 | #include <fmt/format.h> |
| 11 | #include <picojson.h> |
| 12 | |
| 13 | #include "Common/HttpRequest.h" |
| 14 | #include "Common/Logging/Log.h" |
| 15 | #include "Common/MsgHandler.h" |
| 16 | #include "Common/ScopeGuard.h" |
| 17 | #include "Common/StringUtil.h" |
| 18 | #include "Common/Version.h" |
| 19 | |
| 20 | #ifdef _WIN32 |
| 21 | #include <Windows.h> |
| 22 | #else |
| 23 | #include <unistd.h> |
| 24 | #endif |
| 25 | |
| 26 | #ifdef __APPLE__ |
| 27 | #include <sys/stat.h> |
| 28 | #endif |
| 29 | |
| 30 | #if defined(_WIN32) || defined(__APPLE__) |
| 31 | #define OS_SUPPORTS_UPDATER |
| 32 | #include "Common/CommonFuncs.h" |
| 33 | #include "Common/CommonPaths.h" |
| 34 | #include "Common/FileUtil.h" |
| 35 | #endif |
| 36 | |
| 37 | // Refer to docs/autoupdate_overview.md for a detailed overview of the autoupdate process |
| 38 | |
| 39 | namespace |
| 40 | { |
| 41 | std::atomic_bool s_check_in_progress = false; |
| 42 | bool s_update_triggered = false; |
| 43 | |
| 44 | #ifdef __APPLE__ |
| 45 | const char UPDATER_CONTENT_PATH[] = "/Contents/MacOS/Dolphin Updater"; |
| 46 | #endif |
| 47 | |
| 48 | #ifdef OS_SUPPORTS_UPDATER |
| 49 | |
| 50 | const char UPDATER_LOG_FILE[] = "Updater.log"; |
| 51 | |
| 52 | std::string UpdaterPath(bool relocated = false) |
| 53 | { |
| 54 | #ifdef __APPLE__ |
| 55 | if (relocated) |
| 56 | return File::GetExeDirectory() + DIR_SEP + ".Dolphin Updater.2.app"; |
| 57 | else |
| 58 | return File::GetBundleDirectory() + DIR_SEP + "Contents/Helpers/Dolphin Updater.app"; |
| 59 | #else |
| 60 | return File::GetExeDirectory() + DIR_SEP + "Updater.exe"; |
| 61 | #endif |
| 62 | } |
| 63 | |
| 64 | std::string MakeUpdaterCommandLine(const std::map<std::string, std::string>& flags) |
| 65 | { |
| 66 | #ifdef __APPLE__ |
| 67 | std::string cmdline = "\"" + UpdaterPath(true) + UPDATER_CONTENT_PATH + "\""; |
| 68 | #else |
| 69 | std::string cmdline = UpdaterPath(); |
| 70 | #endif |
| 71 | |
| 72 | cmdline += " "; |
| 73 | |
| 74 | for (const auto& pair : flags) |
| 75 | { |
| 76 | std::string value = "--" + pair.first + "=" + pair.second; |
| 77 | value = ReplaceAll(value, "\"", "\\\""); // Escape double quotes. |
| 78 | value = "\"" + value + "\" "; |
| 79 | cmdline += value; |
| 80 | } |
| 81 | return cmdline; |
| 82 | } |
| 83 | |
| 84 | void CleanupFromPreviousUpdate() |
| 85 | { |
| 86 | #ifdef __APPLE__ |
| 87 | // Remove the relocated updater file. |
| 88 | File::DeleteDirRecursively(UpdaterPath(true)); |
| 89 | |
| 90 | // Remove the old (non-embedded) updater app bundle. |
| 91 | // While the update process will delete the files within the old bundle after updating to a |
| 92 | // version with an embedded updater, it won't delete the folder structure of the bundle, so |
| 93 | // we should clean those leftovers up. |
| 94 | File::DeleteDirRecursively(File::GetExeDirectory() + DIR_SEP + "Dolphin Updater.app"); |
| 95 | #endif |
| 96 | |
| 97 | // Updater.log was moved from GetExeDirectory() to GetUserPath(D_LOGS_IDX) in 5.0-14529. |
| 98 | File::Delete(File::GetExeDirectory() + DIR_SEP + "Updater.log", |
| 99 | File::IfAbsentBehavior::NoConsoleWarning); |
| 100 | } |
| 101 | #endif |
| 102 | |
| 103 | // This ignores i18n because most of the text in there (change descriptions) is only going to be |
| 104 | // written in english anyway. |
| 105 | std::string GenerateChangelog(const picojson::array& versions) |
| 106 | { |
| 107 | std::string changelog; |
| 108 | for (const auto& ver : versions) |
| 109 | { |
| 110 | if (!ver.is<picojson::object>()) |
| 111 | continue; |
| 112 | picojson::object ver_obj = ver.get<picojson::object>(); |
| 113 | |
| 114 | if (ver_obj["changelog_html"].is<picojson::null>()) |
| 115 | { |
| 116 | if (!changelog.empty()) |
| 117 | changelog += "<div style=\"margin-top: 0.4em;\"></div>"; // Vertical spacing. |
| 118 | |
| 119 | // Try to link to the PR if we have this info. Otherwise just show shortrev. |
| 120 | if (ver_obj["pr_url"].is<std::string>()) |
| 121 | { |
| 122 | changelog += "<a href=\"" + ver_obj["pr_url"].get<std::string>() + "\">" + |
| 123 | ver_obj["shortrev"].get<std::string>() + "</a>"; |
| 124 | } |
| 125 | else |
| 126 | { |
| 127 | changelog += ver_obj["shortrev"].get<std::string>(); |
| 128 | } |
| 129 | const std::string escaped_description = |
| 130 | Common::GetEscapedHtml(ver_obj["short_descr"].get<std::string>()); |
| 131 | changelog += " by <a href = \"" + ver_obj["author_url"].get<std::string>() + "\">" + |
| 132 | ver_obj["author"].get<std::string>() + "</a> — " + escaped_description; |
| 133 | } |
| 134 | else |
| 135 | { |
| 136 | if (!changelog.empty()) |
| 137 | changelog += "<hr>"; |
| 138 | changelog += "<b>Dolphin " + ver_obj["shortrev"].get<std::string>() + "</b>"; |
| 139 | changelog += "<p>" + ver_obj["changelog_html"].get<std::string>() + "</p>"; |
| 140 | } |
| 141 | } |
| 142 | return changelog; |
| 143 | } |
| 144 | } // namespace |
| 145 | |
| 146 | bool AutoUpdateChecker::SystemSupportsAutoUpdates() |
| 147 | { |
| 148 | #if defined(AUTOUPDATE) && defined(OS_SUPPORTS_UPDATER) |
| 149 | return true; |
| 150 | #else |
| 151 | return false; |
| 152 | #endif |
| 153 | } |
| 154 | |
| 155 | static std::string GetPlatformID() |
| 156 | { |
| 157 | #if defined(_WIN32) |
| 158 | #if defined(_M_ARM_64) |
| 159 | return "win-arm64"; |
| 160 | #else |
| 161 | return "win"; |
| 162 | #endif |
| 163 | #elif defined(__APPLE__) |
| 164 | #if defined(MACOS_UNIVERSAL_BUILD) |
| 165 | return "macos-universal"; |
| 166 | #else |
| 167 | return "macos"; |
| 168 | #endif |
| 169 | #else |
| 170 | return "unknown"; |
| 171 | #endif |
| 172 | } |
| 173 | |
| 174 | static std::string GetUpdateServerUrl() |
| 175 | { |
| 176 | auto server_url = std::getenv("DOLPHIN_UPDATE_SERVER_URL"); |
| 177 | if (server_url) |
| 178 | return server_url; |
| 179 | return "https://dolphin-emu.org"; |
| 180 | } |
| 181 | |
| 182 | static u32 GetOwnProcessId() |
| 183 | { |
| 184 | #ifdef _WIN32 |
| 185 | return GetCurrentProcessId(); |
| 186 | #else |
| 187 | return getpid(); |
| 188 | #endif |
| 189 | } |
| 190 | |
| 191 | void AutoUpdateChecker::CheckForUpdate(std::string_view update_track, |
| 192 | std::string_view hash_override, const CheckType check_type) |
| 193 | { |
| 194 | bool expected_check_in_progress = false; |
| 195 | if (!s_check_in_progress.compare_exchange_strong(expected_check_in_progress, true)) |
| 196 | return; |
| 197 | |
| 198 | Common::ScopeGuard guard([]() { s_check_in_progress.store(false); }); |
| 199 | |
| 200 | if (s_update_triggered) |
| 201 | { |
| 202 | if (check_type == CheckType::Manual) |
| 203 | SuccessAlertFmtT("A Dolphin update is already scheduled for the next time it closes."); |
| 204 | |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | // Don't bother checking if updates are not supported or not enabled. |
| 209 | if (!SystemSupportsAutoUpdates() || update_track.empty()) |
| 210 | return; |
| 211 | |
| 212 | #ifdef OS_SUPPORTS_UPDATER |
| 213 | CleanupFromPreviousUpdate(); |
| 214 | #endif |
| 215 | |
| 216 | std::string_view version_hash = hash_override.empty() ? Common::GetScmRevGitStr() : hash_override; |
| 217 | std::string url = fmt::format("{}/update/check/v1/{}/{}/{}", GetUpdateServerUrl(), update_track, |
| 218 | version_hash, GetPlatformID()); |
| 219 | |
| 220 | const bool is_manual_check = check_type == CheckType::Manual; |
| 221 | |
| 222 | Common::HttpRequest req{std::chrono::seconds{10}}; |
| 223 | auto resp = req.Get(url); |
| 224 | if (!resp) |
| 225 | { |
| 226 | if (is_manual_check) |
| 227 | CriticalAlertFmtT("Unable to contact update server."); |
| 228 | return; |
| 229 | } |
| 230 | const std::string contents(reinterpret_cast<char*>(resp->data()), resp->size()); |
| 231 | INFO_LOG_FMT(COMMON, "Auto-update JSON response: {}", contents); |
| 232 | |
| 233 | picojson::value json; |
| 234 | const std::string err = picojson::parse(json, contents); |
| 235 | if (!err.empty()) |
| 236 | { |
| 237 | CriticalAlertFmtT("Invalid JSON received from auto-update service : {0}", err); |
| 238 | return; |
| 239 | } |
| 240 | picojson::object obj = json.get<picojson::object>(); |
| 241 | |
| 242 | if (obj["status"].get<std::string>() != "outdated") |
| 243 | { |
| 244 | if (is_manual_check) |
| 245 | SuccessAlertFmtT("You are running the latest version available on this update track."); |
| 246 | INFO_LOG_FMT(COMMON, "Auto-update status: we are up to date."); |
| 247 | return; |
| 248 | } |
| 249 | |
| 250 | NewVersionInformation nvi; |
| 251 | nvi.this_manifest_url = obj["old"].get<picojson::object>()["manifest"].get<std::string>(); |
| 252 | nvi.next_manifest_url = obj["new"].get<picojson::object>()["manifest"].get<std::string>(); |
| 253 | nvi.content_store_url = obj["content-store"].get<std::string>(); |
| 254 | nvi.new_shortrev = obj["new"].get<picojson::object>()["name"].get<std::string>(); |
| 255 | nvi.new_hash = obj["new"].get<picojson::object>()["hash"].get<std::string>(); |
| 256 | |
| 257 | // TODO: generate the HTML changelog from the JSON information. |
| 258 | nvi.changelog_html = GenerateChangelog(obj["changelog"].get<picojson::array>()); |
| 259 | |
| 260 | if (std::getenv("DOLPHIN_UPDATE_TEST_DONE")) |
| 261 | { |
| 262 | // We are at end of updater test flow, send a message to server, which will kill us. |
| 263 | req.Get(fmt::format("{}/update-test-done/{}", GetUpdateServerUrl(), GetOwnProcessId())); |
| 264 | } |
| 265 | else |
| 266 | { |
| 267 | OnUpdateAvailable(nvi); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | void AutoUpdateChecker::TriggerUpdate(const AutoUpdateChecker::NewVersionInformation& info, |
| 272 | const AutoUpdateChecker::RestartMode restart_mode) |
| 273 | { |
| 274 | // Check to make sure we don't already have an update triggered |
| 275 | if (s_update_triggered) |
| 276 | { |
| 277 | WARN_LOG_FMT(COMMON, "Auto-update: received a redundant trigger request, ignoring"); |
| 278 | return; |
| 279 | } |
| 280 | |
| 281 | #ifdef OS_SUPPORTS_UPDATER |
| 282 | std::map<std::string, std::string> updater_flags; |
| 283 | updater_flags["this-manifest-url"] = info.this_manifest_url; |
| 284 | updater_flags["next-manifest-url"] = info.next_manifest_url; |
| 285 | updater_flags["content-store-url"] = info.content_store_url; |
| 286 | updater_flags["parent-pid"] = std::to_string(GetOwnProcessId()); |
| 287 | updater_flags["install-base-path"] = File::GetExeDirectory(); |
| 288 | updater_flags["log-file"] = File::GetUserPath(D_LOGS_IDX) + UPDATER_LOG_FILE; |
| 289 | |
| 290 | if (restart_mode == RestartMode::RESTART_AFTER_UPDATE) |
| 291 | updater_flags["binary-to-restart"] = File::GetExePath(); |
| 292 | |
| 293 | #ifdef __APPLE__ |
| 294 | // Copy the updater so it can update itself if needed. |
| 295 | const std::string reloc_updater_path = UpdaterPath(true); |
| 296 | if (!File::Copy(UpdaterPath(), reloc_updater_path)) |
| 297 | { |
| 298 | CriticalAlertFmtT("Unable to create updater copy."); |
| 299 | return; |
| 300 | } |
| 301 | if (chmod((reloc_updater_path + UPDATER_CONTENT_PATH).c_str(), 0700) != 0) |
| 302 | { |
| 303 | CriticalAlertFmtT("Unable to set permissions on updater copy."); |
| 304 | return; |
| 305 | } |
| 306 | #endif |
| 307 | |
| 308 | // Run the updater! |
| 309 | std::string command_line = MakeUpdaterCommandLine(updater_flags); |
| 310 | INFO_LOG_FMT(COMMON, "Updater command line: {}", command_line); |
| 311 | |
| 312 | #ifdef _WIN32 |
| 313 | STARTUPINFO sinfo{.cb = sizeof(sinfo)}; |
| 314 | sinfo.dwFlags = STARTF_FORCEOFFFEEDBACK; // No hourglass cursor after starting the process. |
| 315 | PROCESS_INFORMATION pinfo; |
| 316 | if (CreateProcessW(UTF8ToWString(UpdaterPath()).c_str(), UTF8ToWString(command_line).data(), |
| 317 | nullptr, nullptr, FALSE, 0, nullptr, nullptr, &sinfo, &pinfo)) |
| 318 | { |
| 319 | CloseHandle(pinfo.hThread); |
| 320 | CloseHandle(pinfo.hProcess); |
| 321 | s_update_triggered = true; |
| 322 | } |
| 323 | else |
| 324 | { |
| 325 | const std::string error = Common::GetLastErrorString(); |
| 326 | CriticalAlertFmtT("Could not start updater process: {0}", error); |
| 327 | } |
| 328 | #else |
| 329 | if (popen(command_line.c_str(), "r") == nullptr) |
| 330 | { |
| 331 | const std::string error = Common::LastStrerrorString(); |
| 332 | CriticalAlertFmtT("Could not start updater process: {0}", error); |
| 333 | } |
| 334 | else |
| 335 | { |
| 336 | s_update_triggered = true; |
| 337 | } |
| 338 | #endif |
| 339 | |
| 340 | #endif |
| 341 | } |