AutoUpdate.cpp 9.3 KB

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