2014-05-30 08:53:19 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2014-05-30 08:53:19 +00:00
|
|
|
*/
|
|
|
|
#include "updater.h"
|
|
|
|
|
2021-06-30 21:15:56 +00:00
|
|
|
#include "base/platform/win/base_windows_safe_library.h"
|
2021-06-30 15:20:49 +00:00
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
bool _debug = false;
|
|
|
|
|
2017-12-12 07:52:53 +00:00
|
|
|
wstring updaterName, updaterDir, updateTo, exeName, customWorkingDir, customKeyFile;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
bool equal(const wstring &a, const wstring &b) {
|
|
|
|
return !_wcsicmp(a.c_str(), b.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateError(const WCHAR *msg, DWORD errorCode) {
|
|
|
|
WCHAR errMsg[2048];
|
2017-11-11 17:53:23 +00:00
|
|
|
LPWSTR errorTextFormatted = nullptr;
|
|
|
|
auto formatFlags = FORMAT_MESSAGE_FROM_SYSTEM
|
|
|
|
| FORMAT_MESSAGE_ALLOCATE_BUFFER
|
|
|
|
| FORMAT_MESSAGE_IGNORE_INSERTS;
|
|
|
|
FormatMessage(
|
|
|
|
formatFlags,
|
|
|
|
NULL,
|
|
|
|
errorCode,
|
|
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
|
|
(LPWSTR)&errorTextFormatted,
|
|
|
|
0,
|
|
|
|
0);
|
|
|
|
auto errorText = errorTextFormatted
|
|
|
|
? errorTextFormatted
|
|
|
|
: L"(Unknown error)";
|
2014-05-30 08:53:19 +00:00
|
|
|
wsprintf(errMsg, L"%s, error code: %d\nError message: %s", msg, errorCode, errorText);
|
|
|
|
|
|
|
|
MessageBox(0, errMsg, L"Update error!", MB_ICONERROR);
|
|
|
|
|
2017-11-11 17:53:23 +00:00
|
|
|
LocalFree(errorTextFormatted);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE _logFile = 0;
|
|
|
|
void openLog() {
|
|
|
|
if (!_debug || _logFile) return;
|
|
|
|
wstring logPath = L"DebugLogs";
|
|
|
|
if (!CreateDirectory(logPath.c_str(), NULL)) {
|
|
|
|
DWORD errorCode = GetLastError();
|
|
|
|
if (errorCode && errorCode != ERROR_ALREADY_EXISTS) {
|
|
|
|
updateError(L"Failed to create log directory", errorCode);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SYSTEMTIME stLocalTime;
|
|
|
|
|
|
|
|
GetLocalTime(&stLocalTime);
|
|
|
|
|
|
|
|
static const int maxFileLen = MAX_PATH * 10;
|
|
|
|
WCHAR logName[maxFileLen];
|
|
|
|
wsprintf(logName, L"DebugLogs\\%04d%02d%02d_%02d%02d%02d_upd.txt",
|
|
|
|
stLocalTime.wYear, stLocalTime.wMonth, stLocalTime.wDay,
|
|
|
|
stLocalTime.wHour, stLocalTime.wMinute, stLocalTime.wSecond);
|
|
|
|
_logFile = CreateFile(logName, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
|
|
|
|
if (_logFile == INVALID_HANDLE_VALUE) { // :(
|
|
|
|
updateError(L"Failed to create log file", GetLastError());
|
|
|
|
_logFile = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void closeLog() {
|
|
|
|
if (!_logFile) return;
|
|
|
|
|
|
|
|
CloseHandle(_logFile);
|
|
|
|
_logFile = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeLog(const wstring &msg) {
|
|
|
|
if (!_logFile) return;
|
|
|
|
|
|
|
|
wstring full = msg + L'\n';
|
|
|
|
DWORD written = 0;
|
|
|
|
BOOL result = WriteFile(_logFile, full.c_str(), full.size() * sizeof(wchar_t), &written, 0);
|
|
|
|
if (!result) {
|
|
|
|
updateError((L"Failed to write log entry '" + msg + L"'").c_str(), GetLastError());
|
|
|
|
closeLog();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
BOOL flushr = FlushFileBuffers(_logFile);
|
|
|
|
if (!flushr) {
|
|
|
|
updateError((L"Failed to flush log on entry '" + msg + L"'").c_str(), GetLastError());
|
|
|
|
closeLog();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-03 18:13:01 +00:00
|
|
|
void fullClearPath(const wstring &dir) {
|
2014-05-30 08:53:19 +00:00
|
|
|
WCHAR path[4096];
|
2015-06-03 18:13:01 +00:00
|
|
|
memcpy(path, dir.c_str(), (dir.size() + 1) * sizeof(WCHAR));
|
|
|
|
path[dir.size() + 1] = 0;
|
|
|
|
writeLog(L"Fully clearing path '" + dir + L"'..");
|
2014-05-30 08:53:19 +00:00
|
|
|
SHFILEOPSTRUCT file_op = {
|
|
|
|
NULL,
|
|
|
|
FO_DELETE,
|
|
|
|
path,
|
|
|
|
L"",
|
|
|
|
FOF_NOCONFIRMATION |
|
|
|
|
FOF_NOERRORUI |
|
|
|
|
FOF_SILENT,
|
|
|
|
false,
|
|
|
|
0,
|
|
|
|
L""
|
|
|
|
};
|
|
|
|
int res = SHFileOperation(&file_op);
|
|
|
|
if (res) writeLog(L"Error: failed to clear path! :(");
|
2015-06-03 18:13:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void delFolder() {
|
|
|
|
wstring delPathOld = L"tupdates\\ready", delPath = L"tupdates\\temp", delFolder = L"tupdates";
|
|
|
|
fullClearPath(delPathOld);
|
|
|
|
fullClearPath(delPath);
|
2014-05-30 08:53:19 +00:00
|
|
|
RemoveDirectory(delFolder.c_str());
|
|
|
|
}
|
|
|
|
|
2015-06-04 11:59:30 +00:00
|
|
|
DWORD versionNum = 0, versionLen = 0, readLen = 0;
|
|
|
|
WCHAR versionStr[32] = { 0 };
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
bool update() {
|
|
|
|
writeLog(L"Update started..");
|
|
|
|
|
2015-06-04 11:59:30 +00:00
|
|
|
wstring updDir = L"tupdates\\temp", readyFilePath = L"tupdates\\temp\\ready", tdataDir = L"tupdates\\temp\\tdata";
|
2015-06-03 18:13:01 +00:00
|
|
|
{
|
|
|
|
HANDLE readyFile = CreateFile(readyFilePath.c_str(), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
|
|
|
|
if (readyFile != INVALID_HANDLE_VALUE) {
|
|
|
|
CloseHandle(readyFile);
|
|
|
|
} else {
|
|
|
|
updDir = L"tupdates\\ready"; // old
|
2015-06-04 11:59:30 +00:00
|
|
|
tdataDir = L"tupdates\\ready\\tdata";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE versionFile = CreateFile((tdataDir + L"\\version").c_str(), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
|
|
|
|
if (versionFile != INVALID_HANDLE_VALUE) {
|
2015-10-04 08:16:16 +00:00
|
|
|
if (!ReadFile(versionFile, &versionNum, sizeof(DWORD), &readLen, NULL) || readLen != sizeof(DWORD)) {
|
2015-06-04 11:59:30 +00:00
|
|
|
versionNum = 0;
|
2015-12-03 18:16:34 +00:00
|
|
|
} else {
|
2018-09-26 14:58:09 +00:00
|
|
|
if (versionNum == 0x7FFFFFFF) { // alpha version
|
2015-12-03 18:16:34 +00:00
|
|
|
|
|
|
|
} else if (!ReadFile(versionFile, &versionLen, sizeof(DWORD), &readLen, NULL) || readLen != sizeof(DWORD) || versionLen > 63) {
|
|
|
|
versionNum = 0;
|
|
|
|
} else if (!ReadFile(versionFile, versionStr, versionLen, &readLen, NULL) || readLen != versionLen) {
|
|
|
|
versionNum = 0;
|
|
|
|
}
|
2015-06-03 18:13:01 +00:00
|
|
|
}
|
2015-06-04 11:59:30 +00:00
|
|
|
CloseHandle(versionFile);
|
|
|
|
writeLog(L"Version file read.");
|
|
|
|
} else {
|
|
|
|
writeLog(L"Could not open version file to update registry :(");
|
2015-06-03 18:13:01 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
deque<wstring> dirs;
|
|
|
|
dirs.push_back(updDir);
|
|
|
|
|
|
|
|
deque<wstring> from, to, forcedirs;
|
|
|
|
|
|
|
|
do {
|
|
|
|
wstring dir = dirs.front();
|
|
|
|
dirs.pop_front();
|
|
|
|
|
2014-12-02 16:25:17 +00:00
|
|
|
wstring toDir = updateTo;
|
2014-05-30 08:53:19 +00:00
|
|
|
if (dir.size() > updDir.size() + 1) {
|
|
|
|
toDir += (dir.substr(updDir.size() + 1) + L"\\");
|
|
|
|
forcedirs.push_back(toDir);
|
|
|
|
writeLog(L"Parsing dir '" + toDir + L"' in update tree..");
|
|
|
|
}
|
|
|
|
|
|
|
|
WIN32_FIND_DATA findData;
|
|
|
|
HANDLE findHandle = FindFirstFileEx((dir + L"\\*").c_str(), FindExInfoStandard, &findData, FindExSearchNameMatch, 0, 0);
|
|
|
|
if (findHandle == INVALID_HANDLE_VALUE) {
|
|
|
|
DWORD errorCode = GetLastError();
|
|
|
|
if (errorCode == ERROR_PATH_NOT_FOUND) { // no update is ready
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
writeLog(L"Error: failed to find update files :(");
|
|
|
|
updateError(L"Failed to find update files", errorCode);
|
|
|
|
delFolder();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2015-06-04 11:59:30 +00:00
|
|
|
wstring fname = dir + L"\\" + findData.cFileName;
|
|
|
|
if (fname.substr(0, tdataDir.size()) == tdataDir && (fname.size() <= tdataDir.size() || fname.at(tdataDir.size()) == '/')) {
|
|
|
|
writeLog(L"Skipped 'tdata' path '" + fname + L"'");
|
|
|
|
} else if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
|
2014-05-30 08:53:19 +00:00
|
|
|
if (findData.cFileName != wstring(L".") && findData.cFileName != wstring(L"..")) {
|
2015-06-04 11:59:30 +00:00
|
|
|
dirs.push_back(fname);
|
|
|
|
writeLog(L"Added dir '" + fname + L"' in update tree..");
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
} else {
|
2014-12-02 16:25:17 +00:00
|
|
|
wstring tofname = updateTo + fname.substr(updDir.size() + 1);
|
2017-05-30 18:26:53 +00:00
|
|
|
if (equal(tofname, updaterName)) { // bad update - has Updater.exe - delete all dir
|
|
|
|
writeLog(L"Error: bad update, has Updater.exe! '" + tofname + L"' equal '" + updaterName + L"'");
|
2014-05-30 08:53:19 +00:00
|
|
|
delFolder();
|
|
|
|
return false;
|
2017-05-30 18:26:53 +00:00
|
|
|
} else if (equal(tofname, updateTo + L"Telegram.exe") && exeName != L"Telegram.exe") {
|
|
|
|
wstring fullBinaryPath = updateTo + exeName;
|
|
|
|
writeLog(L"Target binary found: '" + tofname + L"', changing to '" + fullBinaryPath + L"'");
|
|
|
|
tofname = fullBinaryPath;
|
|
|
|
}
|
|
|
|
if (equal(fname, readyFilePath)) {
|
2015-06-03 18:13:01 +00:00
|
|
|
writeLog(L"Skipped ready file '" + fname + L"'");
|
|
|
|
} else {
|
|
|
|
from.push_back(fname);
|
|
|
|
to.push_back(tofname);
|
|
|
|
writeLog(L"Added file '" + fname + L"' to be copied to '" + tofname + L"'");
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (FindNextFile(findHandle, &findData));
|
|
|
|
DWORD errorCode = GetLastError();
|
|
|
|
if (errorCode && errorCode != ERROR_NO_MORE_FILES) { // everything is found
|
|
|
|
writeLog(L"Error: failed to find next update file :(");
|
|
|
|
updateError(L"Failed to find next update file", errorCode);
|
|
|
|
delFolder();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
FindClose(findHandle);
|
|
|
|
} while (!dirs.empty());
|
|
|
|
|
|
|
|
for (size_t i = 0; i < forcedirs.size(); ++i) {
|
|
|
|
wstring forcedir = forcedirs[i];
|
|
|
|
writeLog(L"Forcing dir '" + forcedir + L"'..");
|
|
|
|
if (!forcedir.empty() && !CreateDirectory(forcedir.c_str(), NULL)) {
|
|
|
|
DWORD errorCode = GetLastError();
|
|
|
|
if (errorCode && errorCode != ERROR_ALREADY_EXISTS) {
|
|
|
|
writeLog(L"Error: failed to create dir '" + forcedir + L"'..");
|
|
|
|
updateError(L"Failed to create directory", errorCode);
|
|
|
|
delFolder();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
writeLog(L"Already exists!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < from.size(); ++i) {
|
|
|
|
wstring fname = from[i], tofname = to[i];
|
|
|
|
BOOL copyResult;
|
|
|
|
do {
|
|
|
|
writeLog(L"Copying file '" + fname + L"' to '" + tofname + L"'..");
|
|
|
|
int copyTries = 0;
|
|
|
|
do {
|
|
|
|
copyResult = CopyFile(fname.c_str(), tofname.c_str(), FALSE);
|
2015-10-04 08:16:16 +00:00
|
|
|
if (!copyResult) {
|
2014-05-30 08:53:19 +00:00
|
|
|
++copyTries;
|
|
|
|
Sleep(100);
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2016-02-29 16:53:26 +00:00
|
|
|
} while (copyTries < 100);
|
2015-10-04 08:16:16 +00:00
|
|
|
if (!copyResult) {
|
2014-05-30 08:53:19 +00:00
|
|
|
writeLog(L"Error: failed to copy, asking to retry..");
|
|
|
|
WCHAR errMsg[2048];
|
2015-09-23 17:43:08 +00:00
|
|
|
wsprintf(errMsg, L"Failed to update Telegram :(\n%s is not accessible.", tofname.c_str());
|
2014-05-30 08:53:19 +00:00
|
|
|
if (MessageBox(0, errMsg, L"Update error!", MB_ICONERROR | MB_RETRYCANCEL) != IDRETRY) {
|
|
|
|
delFolder();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2015-10-04 08:16:16 +00:00
|
|
|
} while (!copyResult);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
writeLog(L"Update succeed! Clearing folder..");
|
|
|
|
delFolder();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateRegistry() {
|
2015-12-03 18:16:34 +00:00
|
|
|
if (versionNum && versionNum != 0x7FFFFFFF) {
|
2014-12-02 16:25:17 +00:00
|
|
|
writeLog(L"Updating registry..");
|
2015-06-04 11:59:30 +00:00
|
|
|
versionStr[versionLen / 2] = 0;
|
|
|
|
HKEY rkey;
|
|
|
|
LSTATUS status = RegOpenKeyEx(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\{53F49750-6209-4FBF-9CA8-7A333C87D1ED}_is1", 0, KEY_QUERY_VALUE | KEY_SET_VALUE, &rkey);
|
|
|
|
if (status == ERROR_SUCCESS) {
|
|
|
|
writeLog(L"Checking registry install location..");
|
|
|
|
static const int bufSize = 4096;
|
|
|
|
DWORD locationType, locationSize = bufSize * 2;
|
|
|
|
WCHAR locationStr[bufSize], exp[bufSize];
|
|
|
|
if (RegQueryValueEx(rkey, L"InstallLocation", 0, &locationType, (BYTE*)locationStr, &locationSize) == ERROR_SUCCESS) {
|
|
|
|
locationSize /= 2;
|
|
|
|
if (locationStr[locationSize - 1]) {
|
|
|
|
locationStr[locationSize++] = 0;
|
|
|
|
}
|
|
|
|
if (locationType == REG_EXPAND_SZ) {
|
|
|
|
DWORD copy = ExpandEnvironmentStrings(locationStr, exp, bufSize);
|
|
|
|
if (copy <= bufSize) {
|
|
|
|
memcpy(locationStr, exp, copy * sizeof(WCHAR));
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2015-06-04 11:59:30 +00:00
|
|
|
}
|
|
|
|
if (locationType == REG_EXPAND_SZ || locationType == REG_SZ) {
|
2015-10-04 08:16:16 +00:00
|
|
|
if (PathCanonicalize(exp, locationStr)) {
|
2015-06-04 11:59:30 +00:00
|
|
|
memcpy(locationStr, exp, bufSize * sizeof(WCHAR));
|
|
|
|
if (GetFullPathName(L".", bufSize, exp, 0) < bufSize) {
|
|
|
|
wstring installpath = locationStr, mypath = exp;
|
|
|
|
if (installpath == mypath + L"\\" || true) { // always update reg info, if we found it
|
|
|
|
WCHAR nameStr[bufSize], dateStr[bufSize], publisherStr[bufSize], icongroupStr[bufSize];
|
|
|
|
SYSTEMTIME stLocalTime;
|
|
|
|
GetLocalTime(&stLocalTime);
|
2017-11-11 17:53:23 +00:00
|
|
|
RegSetValueEx(rkey, L"DisplayVersion", 0, REG_SZ, (const BYTE*)versionStr, ((versionLen / 2) + 1) * sizeof(WCHAR));
|
2015-06-04 11:59:30 +00:00
|
|
|
wsprintf(nameStr, L"Telegram Desktop version %s", versionStr);
|
2017-11-11 17:53:23 +00:00
|
|
|
RegSetValueEx(rkey, L"DisplayName", 0, REG_SZ, (const BYTE*)nameStr, (wcslen(nameStr) + 1) * sizeof(WCHAR));
|
2019-08-08 17:31:44 +00:00
|
|
|
wsprintf(publisherStr, L"Telegram FZ-LLC");
|
2017-11-11 17:53:23 +00:00
|
|
|
RegSetValueEx(rkey, L"Publisher", 0, REG_SZ, (const BYTE*)publisherStr, (wcslen(publisherStr) + 1) * sizeof(WCHAR));
|
2015-06-04 11:59:30 +00:00
|
|
|
wsprintf(icongroupStr, L"Telegram Desktop");
|
2017-11-11 17:53:23 +00:00
|
|
|
RegSetValueEx(rkey, L"Inno Setup: Icon Group", 0, REG_SZ, (const BYTE*)icongroupStr, (wcslen(icongroupStr) + 1) * sizeof(WCHAR));
|
2015-06-04 11:59:30 +00:00
|
|
|
wsprintf(dateStr, L"%04d%02d%02d", stLocalTime.wYear, stLocalTime.wMonth, stLocalTime.wDay);
|
2017-11-11 17:53:23 +00:00
|
|
|
RegSetValueEx(rkey, L"InstallDate", 0, REG_SZ, (const BYTE*)dateStr, (wcslen(dateStr) + 1) * sizeof(WCHAR));
|
2015-06-04 11:59:30 +00:00
|
|
|
|
2017-11-11 17:53:23 +00:00
|
|
|
const WCHAR *appURL = L"https://desktop.telegram.org";
|
|
|
|
RegSetValueEx(rkey, L"HelpLink", 0, REG_SZ, (const BYTE*)appURL, (wcslen(appURL) + 1) * sizeof(WCHAR));
|
|
|
|
RegSetValueEx(rkey, L"URLInfoAbout", 0, REG_SZ, (const BYTE*)appURL, (wcslen(appURL) + 1) * sizeof(WCHAR));
|
|
|
|
RegSetValueEx(rkey, L"URLUpdateInfo", 0, REG_SZ, (const BYTE*)appURL, (wcslen(appURL) + 1) * sizeof(WCHAR));
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-04 11:59:30 +00:00
|
|
|
RegCloseKey(rkey);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-04 08:16:16 +00:00
|
|
|
int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE prevInstance, LPWSTR cmdParamarg, int cmdShow) {
|
2021-06-30 21:15:56 +00:00
|
|
|
base::Platform::InitDynamicLibraries();
|
2021-06-30 15:20:49 +00:00
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
openLog();
|
|
|
|
|
|
|
|
_oldWndExceptionFilter = SetUnhandledExceptionFilter(_exceptionFilter);
|
2016-01-10 06:05:23 +00:00
|
|
|
// CAPIHook apiHook("kernel32.dll", "SetUnhandledExceptionFilter", (PROC)RedirectedSetUnhandledExceptionFilter);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
writeLog(L"Updaters started..");
|
|
|
|
|
|
|
|
LPWSTR *args;
|
|
|
|
int argsCount;
|
|
|
|
|
2021-08-11 01:17:57 +00:00
|
|
|
bool needupdate = false, autostart = false, debug = false, writeprotected = false, startintray = false, freetype = false;
|
2014-05-30 08:53:19 +00:00
|
|
|
args = CommandLineToArgvW(GetCommandLine(), &argsCount);
|
|
|
|
if (args) {
|
|
|
|
for (int i = 1; i < argsCount; ++i) {
|
2017-12-12 07:52:53 +00:00
|
|
|
writeLog(std::wstring(L"Argument: ") + args[i]);
|
2014-05-30 08:53:19 +00:00
|
|
|
if (equal(args[i], L"-update")) {
|
|
|
|
needupdate = true;
|
|
|
|
} else if (equal(args[i], L"-autostart")) {
|
|
|
|
autostart = true;
|
|
|
|
} else if (equal(args[i], L"-debug")) {
|
|
|
|
debug = _debug = true;
|
|
|
|
openLog();
|
2015-01-27 16:58:58 +00:00
|
|
|
} else if (equal(args[i], L"-startintray")) {
|
|
|
|
startintray = true;
|
2020-05-20 00:10:21 +00:00
|
|
|
} else if (equal(args[i], L"-freetype")) {
|
|
|
|
freetype = true;
|
2014-12-02 16:25:17 +00:00
|
|
|
} else if (equal(args[i], L"-writeprotected") && ++i < argsCount) {
|
2017-12-12 07:52:53 +00:00
|
|
|
writeLog(std::wstring(L"Argument: ") + args[i]);
|
2014-12-02 16:25:17 +00:00
|
|
|
writeprotected = true;
|
|
|
|
updateTo = args[i];
|
2017-12-12 07:52:53 +00:00
|
|
|
for (int j = 0, l = updateTo.size(); j < l; ++j) {
|
|
|
|
if (updateTo[j] == L'/') {
|
|
|
|
updateTo[j] = L'\\';
|
2014-12-02 16:25:17 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-12 07:52:53 +00:00
|
|
|
} else if (equal(args[i], L"-workdir") && ++i < argsCount) {
|
|
|
|
writeLog(std::wstring(L"Argument: ") + args[i]);
|
|
|
|
customWorkingDir = args[i];
|
|
|
|
} else if (equal(args[i], L"-key") && ++i < argsCount) {
|
|
|
|
writeLog(std::wstring(L"Argument: ") + args[i]);
|
|
|
|
customKeyFile = args[i];
|
2017-05-30 18:26:53 +00:00
|
|
|
} else if (equal(args[i], L"-exename") && ++i < argsCount) {
|
2017-12-12 07:52:53 +00:00
|
|
|
writeLog(std::wstring(L"Argument: ") + args[i]);
|
2017-05-30 18:26:53 +00:00
|
|
|
exeName = args[i];
|
2017-12-12 07:52:53 +00:00
|
|
|
for (int j = 0, l = exeName.size(); j < l; ++j) {
|
|
|
|
if (exeName[j] == L'/' || exeName[j] == L'\\') {
|
2017-05-30 18:26:53 +00:00
|
|
|
exeName = L"Telegram.exe";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-30 18:26:53 +00:00
|
|
|
if (exeName.empty()) {
|
|
|
|
exeName = L"Telegram.exe";
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
if (needupdate) writeLog(L"Need to update!");
|
|
|
|
if (autostart) writeLog(L"From autostart!");
|
2014-12-02 16:25:17 +00:00
|
|
|
if (writeprotected) writeLog(L"Write Protected folder!");
|
2017-12-12 07:52:53 +00:00
|
|
|
if (!customWorkingDir.empty()) writeLog(L"Will pass custom working dir: " + customWorkingDir);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2017-05-30 18:26:53 +00:00
|
|
|
updaterName = args[0];
|
|
|
|
writeLog(L"Updater name is: " + updaterName);
|
|
|
|
if (updaterName.size() > 11) {
|
|
|
|
if (equal(updaterName.substr(updaterName.size() - 11), L"Updater.exe")) {
|
|
|
|
updaterDir = updaterName.substr(0, updaterName.size() - 11);
|
|
|
|
writeLog(L"Updater dir is: " + updaterDir);
|
2014-12-02 16:25:17 +00:00
|
|
|
if (!writeprotected) {
|
2017-05-30 18:26:53 +00:00
|
|
|
updateTo = updaterDir;
|
2014-12-02 16:25:17 +00:00
|
|
|
}
|
|
|
|
writeLog(L"Update to: " + updateTo);
|
2014-05-30 08:53:19 +00:00
|
|
|
if (needupdate && update()) {
|
|
|
|
updateRegistry();
|
|
|
|
}
|
2014-12-02 16:25:17 +00:00
|
|
|
if (writeprotected) { // if we can't clear all tupdates\ready (Updater.exe is there) - clear only version
|
2015-06-03 18:13:01 +00:00
|
|
|
if (DeleteFile(L"tupdates\\temp\\tdata\\version") || DeleteFile(L"tupdates\\ready\\tdata\\version")) {
|
2014-12-02 16:25:17 +00:00
|
|
|
writeLog(L"Version file deleted!");
|
|
|
|
} else {
|
|
|
|
writeLog(L"Error: could not delete version file");
|
|
|
|
}
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
} else {
|
|
|
|
writeLog(L"Error: bad exe name!");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
writeLog(L"Error: short exe name!");
|
|
|
|
}
|
|
|
|
LocalFree(args);
|
|
|
|
} else {
|
|
|
|
writeLog(L"Error: No command line arguments!");
|
|
|
|
}
|
|
|
|
|
2014-12-02 16:25:17 +00:00
|
|
|
wstring targs;
|
2014-05-30 08:53:19 +00:00
|
|
|
if (autostart) targs += L" -autostart";
|
|
|
|
if (debug) targs += L" -debug";
|
2015-01-27 16:58:58 +00:00
|
|
|
if (startintray) targs += L" -startintray";
|
2020-05-20 00:10:21 +00:00
|
|
|
if (freetype) targs += L" -freetype";
|
2017-12-12 07:52:53 +00:00
|
|
|
if (!customWorkingDir.empty()) {
|
|
|
|
targs += L" -workdir \"" + customWorkingDir + L"\"";
|
|
|
|
}
|
|
|
|
if (!customKeyFile.empty()) {
|
|
|
|
targs += L" -key \"" + customKeyFile + L"\"";
|
|
|
|
}
|
|
|
|
writeLog(L"Result arguments: " + targs);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2014-12-02 16:25:17 +00:00
|
|
|
bool executed = false;
|
|
|
|
if (writeprotected) { // run un-elevated
|
|
|
|
writeLog(L"Trying to run un-elevated by temp.lnk");
|
|
|
|
|
|
|
|
HRESULT hres = CoInitialize(0);
|
|
|
|
if (SUCCEEDED(hres)) {
|
|
|
|
IShellLink* psl;
|
|
|
|
HRESULT hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl);
|
|
|
|
if (SUCCEEDED(hres)) {
|
|
|
|
IPersistFile* ppf;
|
|
|
|
|
2017-05-30 18:26:53 +00:00
|
|
|
wstring exe = updateTo + exeName, dir = updateTo;
|
2014-12-02 16:25:17 +00:00
|
|
|
psl->SetArguments((targs.size() ? targs.substr(1) : targs).c_str());
|
|
|
|
psl->SetPath(exe.c_str());
|
|
|
|
psl->SetWorkingDirectory(dir.c_str());
|
|
|
|
psl->SetDescription(L"");
|
|
|
|
|
|
|
|
hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf);
|
|
|
|
|
|
|
|
if (SUCCEEDED(hres)) {
|
2015-06-03 18:13:01 +00:00
|
|
|
wstring lnk = L"tupdates\\temp\\temp.lnk";
|
2014-12-02 16:25:17 +00:00
|
|
|
hres = ppf->Save(lnk.c_str(), TRUE);
|
2015-06-03 18:13:01 +00:00
|
|
|
if (!SUCCEEDED(hres)) {
|
|
|
|
lnk = L"tupdates\\ready\\temp.lnk"; // old
|
|
|
|
hres = ppf->Save(lnk.c_str(), TRUE);
|
|
|
|
}
|
2014-12-02 16:25:17 +00:00
|
|
|
ppf->Release();
|
|
|
|
|
|
|
|
if (SUCCEEDED(hres)) {
|
|
|
|
writeLog(L"Executing un-elevated through link..");
|
|
|
|
ShellExecute(0, 0, L"explorer.exe", lnk.c_str(), 0, SW_SHOWNORMAL);
|
|
|
|
executed = true;
|
|
|
|
} else {
|
|
|
|
writeLog(L"Error: ppf->Save failed");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
writeLog(L"Error: Could not create interface IID_IPersistFile");
|
|
|
|
}
|
|
|
|
psl->Release();
|
|
|
|
} else {
|
|
|
|
writeLog(L"Error: could not create instance of IID_IShellLink");
|
|
|
|
}
|
|
|
|
CoUninitialize();
|
|
|
|
} else {
|
|
|
|
writeLog(L"Error: Could not initialize COM");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!executed) {
|
2017-05-30 18:26:53 +00:00
|
|
|
ShellExecute(0, 0, (updateTo + exeName).c_str(), (L"-noupdate" + targs).c_str(), 0, SW_SHOWNORMAL);
|
2014-12-02 16:25:17 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2017-05-30 18:26:53 +00:00
|
|
|
writeLog(L"Executed '" + exeName + L"', closing log and quitting..");
|
2014-05-30 08:53:19 +00:00
|
|
|
closeLog();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-30 14:11:09 +00:00
|
|
|
static const WCHAR *_programName = L"Telegram Desktop"; // folder in APPDATA, if current path is unavailable for writing
|
2014-05-30 08:53:19 +00:00
|
|
|
static const WCHAR *_exeName = L"Updater.exe";
|
|
|
|
|
|
|
|
LPTOP_LEVEL_EXCEPTION_FILTER _oldWndExceptionFilter = 0;
|
|
|
|
|
|
|
|
typedef BOOL (FAR STDAPICALLTYPE *t_miniDumpWriteDump)(
|
2016-11-07 15:24:28 +00:00
|
|
|
_In_ HANDLE hProcess,
|
|
|
|
_In_ DWORD ProcessId,
|
|
|
|
_In_ HANDLE hFile,
|
|
|
|
_In_ MINIDUMP_TYPE DumpType,
|
|
|
|
_In_opt_ PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam,
|
|
|
|
_In_opt_ PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,
|
|
|
|
_In_opt_ PMINIDUMP_CALLBACK_INFORMATION CallbackParam
|
2014-05-30 08:53:19 +00:00
|
|
|
);
|
|
|
|
t_miniDumpWriteDump miniDumpWriteDump = 0;
|
|
|
|
|
|
|
|
HANDLE _generateDumpFileAtPath(const WCHAR *path) {
|
|
|
|
static const int maxFileLen = MAX_PATH * 10;
|
|
|
|
|
|
|
|
WCHAR szPath[maxFileLen];
|
2016-02-14 18:29:17 +00:00
|
|
|
wsprintf(szPath, L"%stdata\\", path);
|
2016-11-07 15:24:28 +00:00
|
|
|
if (!CreateDirectory(szPath, NULL)) {
|
2014-05-30 08:53:19 +00:00
|
|
|
if (GetLastError() != ERROR_ALREADY_EXISTS) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2016-02-14 18:29:17 +00:00
|
|
|
wsprintf(szPath, L"%sdumps\\", path);
|
|
|
|
if (!CreateDirectory(szPath, NULL)) {
|
|
|
|
if (GetLastError() != ERROR_ALREADY_EXISTS) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-11-07 15:24:28 +00:00
|
|
|
WCHAR szFileName[maxFileLen];
|
2014-05-30 08:53:19 +00:00
|
|
|
WCHAR szExeName[maxFileLen];
|
|
|
|
|
|
|
|
wcscpy_s(szExeName, _exeName);
|
|
|
|
WCHAR *dotFrom = wcschr(szExeName, WCHAR(L'.'));
|
|
|
|
if (dotFrom) {
|
|
|
|
wsprintf(dotFrom, L"");
|
|
|
|
}
|
|
|
|
|
2016-11-07 15:24:28 +00:00
|
|
|
SYSTEMTIME stLocalTime;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-11-07 15:24:28 +00:00
|
|
|
GetLocalTime(&stLocalTime);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-11-07 15:24:28 +00:00
|
|
|
wsprintf(szFileName, L"%s%s-%s-%04d%02d%02d-%02d%02d%02d-%ld-%ld.dmp",
|
|
|
|
szPath, szExeName, updaterVersionStr,
|
|
|
|
stLocalTime.wYear, stLocalTime.wMonth, stLocalTime.wDay,
|
|
|
|
stLocalTime.wHour, stLocalTime.wMinute, stLocalTime.wSecond,
|
|
|
|
GetCurrentProcessId(), GetCurrentThreadId());
|
|
|
|
return CreateFile(szFileName, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_WRITE|FILE_SHARE_READ, 0, CREATE_ALWAYS, 0, 0);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void _generateDump(EXCEPTION_POINTERS* pExceptionPointers) {
|
|
|
|
static const int maxFileLen = MAX_PATH * 10;
|
|
|
|
|
|
|
|
closeLog();
|
|
|
|
|
|
|
|
HMODULE hDll = LoadLibrary(L"DBGHELP.DLL");
|
|
|
|
if (!hDll) return;
|
|
|
|
|
|
|
|
miniDumpWriteDump = (t_miniDumpWriteDump)GetProcAddress(hDll, "MiniDumpWriteDump");
|
|
|
|
if (!miniDumpWriteDump) return;
|
|
|
|
|
|
|
|
HANDLE hDumpFile = 0;
|
|
|
|
|
|
|
|
WCHAR szPath[maxFileLen];
|
|
|
|
DWORD len = GetModuleFileName(GetModuleHandle(0), szPath, maxFileLen);
|
|
|
|
if (!len) return;
|
|
|
|
|
|
|
|
WCHAR *pathEnd = szPath + len;
|
|
|
|
|
|
|
|
if (!_wcsicmp(pathEnd - wcslen(_exeName), _exeName)) {
|
|
|
|
wsprintf(pathEnd - wcslen(_exeName), L"");
|
|
|
|
hDumpFile = _generateDumpFileAtPath(szPath);
|
|
|
|
}
|
|
|
|
if (!hDumpFile || hDumpFile == INVALID_HANDLE_VALUE) {
|
|
|
|
WCHAR wstrPath[maxFileLen];
|
|
|
|
DWORD wstrPathLen;
|
|
|
|
if (wstrPathLen = GetEnvironmentVariable(L"APPDATA", wstrPath, maxFileLen)) {
|
|
|
|
wsprintf(wstrPath + wstrPathLen, L"\\%s\\", _programName);
|
|
|
|
hDumpFile = _generateDumpFileAtPath(wstrPath);
|
|
|
|
}
|
|
|
|
}
|
2016-01-10 06:05:23 +00:00
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
if (!hDumpFile || hDumpFile == INVALID_HANDLE_VALUE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MINIDUMP_EXCEPTION_INFORMATION ExpParam = {0};
|
2016-11-07 15:24:28 +00:00
|
|
|
ExpParam.ThreadId = GetCurrentThreadId();
|
|
|
|
ExpParam.ExceptionPointers = pExceptionPointers;
|
|
|
|
ExpParam.ClientPointers = TRUE;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-11-07 15:24:28 +00:00
|
|
|
miniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hDumpFile, MiniDumpWithDataSegs, &ExpParam, NULL, NULL);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LONG CALLBACK _exceptionFilter(EXCEPTION_POINTERS* pExceptionPointers) {
|
|
|
|
_generateDump(pExceptionPointers);
|
2016-11-07 15:24:28 +00:00
|
|
|
return _oldWndExceptionFilter ? (*_oldWndExceptionFilter)(pExceptionPointers) : EXCEPTION_CONTINUE_SEARCH;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-01-10 06:05:23 +00:00
|
|
|
// see http://www.codeproject.com/Articles/154686/SetUnhandledExceptionFilter-and-the-C-C-Runtime-Li
|
|
|
|
LPTOP_LEVEL_EXCEPTION_FILTER WINAPI RedirectedSetUnhandledExceptionFilter(_In_opt_ LPTOP_LEVEL_EXCEPTION_FILTER lpTopLevelExceptionFilter) {
|
|
|
|
// When the CRT calls SetUnhandledExceptionFilter with NULL parameter
|
|
|
|
// our handler will not get removed.
|
|
|
|
_oldWndExceptionFilter = lpTopLevelExceptionFilter;
|
|
|
|
return 0;
|
|
|
|
}
|