win-update.cpp 10 KB

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