win-update.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. #include "../win-update/updater/manifest.hpp"
  2. #include "update-helpers.hpp"
  3. #include "shared-update.hpp"
  4. #include "update-window.hpp"
  5. #include "remote-text.hpp"
  6. #include "win-update.hpp"
  7. #include "obs-app.hpp"
  8. #include <qt-wrappers.hpp>
  9. #include <QMessageBox>
  10. #include <string>
  11. #include <mutex>
  12. #define WIN32_LEAN_AND_MEAN
  13. #include <windows.h>
  14. #include <shellapi.h>
  15. #include <util/windows/WinHandle.hpp>
  16. #include <util/util.hpp>
  17. #ifdef BROWSER_AVAILABLE
  18. #include <browser-panel.hpp>
  19. #endif
  20. using namespace std;
  21. using namespace updater;
  22. /* ------------------------------------------------------------------------ */
  23. #ifndef WIN_MANIFEST_URL
  24. #define WIN_MANIFEST_URL "https://obsproject.com/update_studio/manifest.json"
  25. #endif
  26. #ifndef WIN_MANIFEST_BASE_URL
  27. #define WIN_MANIFEST_BASE_URL "https://obsproject.com/update_studio/"
  28. #endif
  29. #ifndef WIN_BRANCHES_URL
  30. #define WIN_BRANCHES_URL "https://obsproject.com/update_studio/branches.json"
  31. #endif
  32. #ifndef WIN_DEFAULT_BRANCH
  33. #define WIN_DEFAULT_BRANCH "stable"
  34. #endif
  35. #ifndef WIN_UPDATER_URL
  36. #define WIN_UPDATER_URL "https://obsproject.com/update_studio/updater.exe"
  37. #endif
  38. /* ------------------------------------------------------------------------ */
  39. static bool ParseUpdateManifest(const char *manifest_data, bool *updatesAvailable, string &notes, string &updateVer,
  40. const string &branch)
  41. try {
  42. constexpr uint64_t currentVersion = (uint64_t)LIBOBS_API_VER << 16ULL | OBS_RELEASE_CANDIDATE << 8ULL |
  43. OBS_BETA;
  44. constexpr bool isPreRelease = currentVersion & 0xffff || std::char_traits<char>::length(OBS_COMMIT);
  45. json manifestContents = json::parse(manifest_data);
  46. Manifest manifest = manifestContents.get<Manifest>();
  47. if (manifest.version_major == 0 && manifest.commit.empty())
  48. throw strprintf("Invalid version number: %d.%d.%d", manifest.version_major, manifest.version_minor,
  49. manifest.version_patch);
  50. notes = manifest.notes;
  51. if (manifest.commit.empty()) {
  52. uint64_t new_ver = MAKE_SEMANTIC_VERSION((uint64_t)manifest.version_major,
  53. (uint64_t)manifest.version_minor,
  54. (uint64_t)manifest.version_patch);
  55. new_ver <<= 16;
  56. /* RC builds are shifted so that rc1 and beta1 versions do not result
  57. * in the same new_ver. */
  58. if (manifest.rc > 0)
  59. new_ver |= (uint64_t)manifest.rc << 8;
  60. else if (manifest.beta > 0)
  61. new_ver |= (uint64_t)manifest.beta;
  62. updateVer = to_string(new_ver);
  63. /* When using a pre-release build or non-default branch we only check if
  64. * the manifest version is different, so that it can be rolled back. */
  65. if (branch != WIN_DEFAULT_BRANCH || isPreRelease)
  66. *updatesAvailable = new_ver != currentVersion;
  67. else
  68. *updatesAvailable = new_ver > currentVersion;
  69. } else {
  70. /* Test or nightly builds may not have a (valid) version number,
  71. * so compare commit hashes instead. */
  72. updateVer = manifest.commit.substr(0, 8);
  73. *updatesAvailable = !currentVersion || !manifest.commit.compare(0, strlen(OBS_COMMIT), OBS_COMMIT);
  74. }
  75. return true;
  76. } catch (string &text) {
  77. blog(LOG_WARNING, "%s: %s", __FUNCTION__, text.c_str());
  78. return false;
  79. }
  80. /* ------------------------------------------------------------------------ */
  81. bool GetBranchAndUrl(string &selectedBranch, string &manifestUrl)
  82. {
  83. const char *config_branch = config_get_string(App()->GetAppConfig(), "General", "UpdateBranch");
  84. if (!config_branch)
  85. return true;
  86. bool found = false;
  87. for (const UpdateBranch &branch : App()->GetBranches()) {
  88. if (branch.name != config_branch)
  89. continue;
  90. /* A branch that is found but disabled will just silently fall back to
  91. * the default. But if the branch was removed entirely, the user should
  92. * be warned, so leave this false *only* if the branch was removed. */
  93. found = true;
  94. if (branch.is_enabled) {
  95. selectedBranch = branch.name.toStdString();
  96. if (branch.name != WIN_DEFAULT_BRANCH) {
  97. manifestUrl = WIN_MANIFEST_BASE_URL;
  98. manifestUrl += "manifest_" + branch.name.toStdString() + ".json";
  99. }
  100. }
  101. break;
  102. }
  103. return found;
  104. }
  105. /* ------------------------------------------------------------------------ */
  106. void AutoUpdateThread::infoMsg(const QString &title, const QString &text)
  107. {
  108. OBSMessageBox::information(App()->GetMainWindow(), title, text);
  109. }
  110. void AutoUpdateThread::info(const QString &title, const QString &text)
  111. {
  112. QMetaObject::invokeMethod(this, "infoMsg", Qt::BlockingQueuedConnection, Q_ARG(QString, title),
  113. Q_ARG(QString, text));
  114. }
  115. int AutoUpdateThread::queryUpdateSlot(bool localManualUpdate, const QString &text)
  116. {
  117. OBSUpdate updateDlg(App()->GetMainWindow(), localManualUpdate, text);
  118. return updateDlg.exec();
  119. }
  120. int AutoUpdateThread::queryUpdate(bool localManualUpdate, const char *text_utf8)
  121. {
  122. int ret = OBSUpdate::No;
  123. QString text = text_utf8;
  124. QMetaObject::invokeMethod(this, "queryUpdateSlot", Qt::BlockingQueuedConnection, Q_RETURN_ARG(int, ret),
  125. Q_ARG(bool, localManualUpdate), Q_ARG(QString, text));
  126. return ret;
  127. }
  128. bool AutoUpdateThread::queryRepairSlot()
  129. {
  130. QMessageBox::StandardButton res =
  131. OBSMessageBox::question(App()->GetMainWindow(), QTStr("Updater.RepairConfirm.Title"),
  132. QTStr("Updater.RepairConfirm.Text"), QMessageBox::Yes | QMessageBox::Cancel);
  133. return res == QMessageBox::Yes;
  134. }
  135. bool AutoUpdateThread::queryRepair()
  136. {
  137. bool ret = false;
  138. QMetaObject::invokeMethod(this, "queryRepairSlot", Qt::BlockingQueuedConnection, Q_RETURN_ARG(bool, ret));
  139. return ret;
  140. }
  141. void AutoUpdateThread::run()
  142. try {
  143. string text;
  144. string branch = WIN_DEFAULT_BRANCH;
  145. string manifestUrl = WIN_MANIFEST_URL;
  146. vector<string> extraHeaders;
  147. bool updatesAvailable = false;
  148. struct FinishedTrigger {
  149. inline ~FinishedTrigger() { QMetaObject::invokeMethod(App()->GetMainWindow(), "updateCheckFinished"); }
  150. } finishedTrigger;
  151. /* ----------------------------------- *
  152. * get branches from server */
  153. if (FetchAndVerifyFile("branches", "obs-studio\\updates\\branches.json", WIN_BRANCHES_URL, &text))
  154. App()->SetBranchData(text);
  155. /* ----------------------------------- *
  156. * check branch and get manifest url */
  157. if (!GetBranchAndUrl(branch, manifestUrl)) {
  158. config_set_string(App()->GetAppConfig(), "General", "UpdateBranch", WIN_DEFAULT_BRANCH);
  159. info(QTStr("Updater.BranchNotFound.Title"), QTStr("Updater.BranchNotFound.Text"));
  160. }
  161. /* allow server to know if this was a manual update check in case
  162. * we want to allow people to bypass a configured rollout rate */
  163. if (manualUpdate)
  164. extraHeaders.emplace_back("X-OBS2-ManualUpdate: 1");
  165. /* ----------------------------------- *
  166. * get manifest from server */
  167. text.clear();
  168. if (!FetchAndVerifyFile("manifest", "obs-studio\\updates\\manifest.json", manifestUrl.c_str(), &text,
  169. extraHeaders))
  170. return;
  171. /* ----------------------------------- *
  172. * check manifest for update */
  173. string notes;
  174. string updateVer;
  175. if (!ParseUpdateManifest(text.c_str(), &updatesAvailable, notes, updateVer, branch))
  176. throw string("Failed to parse manifest");
  177. if (!updatesAvailable && !repairMode) {
  178. if (manualUpdate)
  179. info(QTStr("Updater.NoUpdatesAvailable.Title"), QTStr("Updater.NoUpdatesAvailable.Text"));
  180. return;
  181. } else if (updatesAvailable && repairMode) {
  182. info(QTStr("Updater.RepairButUpdatesAvailable.Title"), QTStr("Updater.RepairButUpdatesAvailable.Text"));
  183. return;
  184. }
  185. /* ----------------------------------- *
  186. * skip this version if set to skip */
  187. const char *skipUpdateVer = config_get_string(App()->GetAppConfig(), "General", "SkipUpdateVersion");
  188. if (!manualUpdate && !repairMode && skipUpdateVer && updateVer == skipUpdateVer)
  189. return;
  190. /* ----------------------------------- *
  191. * fetch updater module */
  192. if (!FetchAndVerifyFile("updater", "obs-studio\\updates\\updater.exe", WIN_UPDATER_URL, nullptr))
  193. return;
  194. /* ----------------------------------- *
  195. * query user for update */
  196. if (repairMode) {
  197. if (!queryRepair())
  198. return;
  199. } else {
  200. int queryResult = queryUpdate(manualUpdate, notes.c_str());
  201. if (queryResult == OBSUpdate::No) {
  202. if (!manualUpdate) {
  203. long long t = (long long)time(nullptr);
  204. config_set_int(App()->GetAppConfig(), "General", "LastUpdateCheck", t);
  205. }
  206. return;
  207. } else if (queryResult == OBSUpdate::Skip) {
  208. config_set_string(App()->GetAppConfig(), "General", "SkipUpdateVersion", updateVer.c_str());
  209. return;
  210. }
  211. }
  212. /* ----------------------------------- *
  213. * get working dir */
  214. wchar_t cwd[MAX_PATH];
  215. GetModuleFileNameW(nullptr, cwd, _countof(cwd) - 1);
  216. wchar_t *p = wcsrchr(cwd, '\\');
  217. if (p)
  218. *p = 0;
  219. /* ----------------------------------- *
  220. * execute updater */
  221. BPtr<char> updateFilePath = GetAppConfigPathPtr("obs-studio\\updates\\updater.exe");
  222. BPtr<wchar_t> wUpdateFilePath;
  223. size_t size = os_utf8_to_wcs_ptr(updateFilePath, 0, &wUpdateFilePath);
  224. if (!size)
  225. throw string("Could not convert updateFilePath to wide");
  226. /* note, can't use CreateProcess to launch as admin. */
  227. SHELLEXECUTEINFO execInfo = {};
  228. execInfo.cbSize = sizeof(execInfo);
  229. execInfo.lpFile = wUpdateFilePath;
  230. string parameters;
  231. if (branch != WIN_DEFAULT_BRANCH)
  232. parameters += "--branch=" + branch;
  233. obs_cmdline_args obs_args = obs_get_cmdline_args();
  234. for (int idx = 1; idx < obs_args.argc; idx++) {
  235. if (!parameters.empty())
  236. parameters += " ";
  237. parameters += obs_args.argv[idx];
  238. }
  239. /* Portable mode can be enabled via sentinel files, so copying the
  240. * command line doesn't guarantee the flag to be there. */
  241. if (App()->IsPortableMode() && parameters.find("--portable") == string::npos) {
  242. if (!parameters.empty())
  243. parameters += " ";
  244. parameters += "--portable";
  245. }
  246. BPtr<wchar_t> lpParameters;
  247. size = os_utf8_to_wcs_ptr(parameters.c_str(), 0, &lpParameters);
  248. if (!size && !parameters.empty())
  249. throw string("Could not convert parameters to wide");
  250. execInfo.lpParameters = lpParameters;
  251. execInfo.lpDirectory = cwd;
  252. execInfo.nShow = SW_SHOWNORMAL;
  253. if (!ShellExecuteEx(&execInfo)) {
  254. QString msg = QTStr("Updater.FailedToLaunch");
  255. info(msg, msg);
  256. throw strprintf("Can't launch updater '%s': %d", updateFilePath.Get(), GetLastError());
  257. }
  258. /* force OBS to perform another update check immediately after updating
  259. * in case of issues with the new version */
  260. config_set_int(App()->GetAppConfig(), "General", "LastUpdateCheck", 0);
  261. config_set_string(App()->GetAppConfig(), "General", "SkipUpdateVersion", "0");
  262. QMetaObject::invokeMethod(App()->GetMainWindow(), "close");
  263. } catch (string &text) {
  264. blog(LOG_WARNING, "%s: %s", __FUNCTION__, text.c_str());
  265. }