win-update.cpp 11 KB

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