From d3935259d7408b045e7e2e7d6d97195324fdeae3 Mon Sep 17 00:00:00 2001 From: RadRussianRus Date: Thu, 23 Jul 2020 17:52:26 +0300 Subject: [PATCH] Option to set API ID and hash from start parameter --- Telegram/SourceFiles/_other/updater_linux.cpp | 17 +++++++++++++++++ Telegram/SourceFiles/_other/updater_osx.m | 17 ++++++++++++++++- Telegram/SourceFiles/_other/updater_win.cpp | 15 +++++++++++++++ Telegram/SourceFiles/core/launcher.cpp | 16 +++++++++++++++- Telegram/SourceFiles/kotato/settings.cpp | 3 +++ Telegram/SourceFiles/kotato/settings.h | 2 ++ .../platform/linux/launcher_linux.cpp | 11 +++++++++++ .../SourceFiles/platform/mac/launcher_mac.mm | 7 +++++++ .../SourceFiles/platform/win/launcher_win.cpp | 10 ++++++++++ .../SourceFiles/platform/win/specific_win.cpp | 13 +++++++++++-- 10 files changed, 107 insertions(+), 4 deletions(-) diff --git a/Telegram/SourceFiles/_other/updater_linux.cpp b/Telegram/SourceFiles/_other/updater_linux.cpp index 971c940c5..a61b42ae9 100644 --- a/Telegram/SourceFiles/_other/updater_linux.cpp +++ b/Telegram/SourceFiles/_other/updater_linux.cpp @@ -363,9 +363,12 @@ int main(int argc, char *argv[]) { bool testmode = false; bool externalupdater = false; bool customWorkingDir = false; + bool useEnvApi = true; char *key = 0; char *workdir = 0; + char *customApiId = 0; + char *customApiHash = 0; for (int i = 1; i < argc; ++i) { if (equal(argv[i], "-noupdate")) { needupdate = false; @@ -391,6 +394,12 @@ int main(int argc, char *argv[]) { exeName = argv[i]; } else if (equal(argv[i], "-exepath") && ++i < argc) { exePath = argv[i]; + } else if (equal(argv[i], "-no-env-api")) { + useEnvApi = false; + } else if (equal(argv[i], "-api-id") && ++i < argc) { + customApiId = argv[i]; + } else if (equal(argv[i], "-api-hash") && ++i < argc) { + customApiHash = argv[i]; } } if (exeName.empty() || exeName.find('/') != string::npos) { @@ -488,6 +497,14 @@ int main(int argc, char *argv[]) { push(workdir); } + if (!useEnvApi) push("-no-env-api"); + if (customApiId && customApiHash) { + push("-api-id"); + push(customApiId); + push("-api-hash"); + push(customApiHash); + } + auto args = vector(); for (auto &arg : values) { args.push_back(arg.data()); diff --git a/Telegram/SourceFiles/_other/updater_osx.m b/Telegram/SourceFiles/_other/updater_osx.m index 7fa9cd039..21a49c1d4 100644 --- a/Telegram/SourceFiles/_other/updater_osx.m +++ b/Telegram/SourceFiles/_other/updater_osx.m @@ -91,8 +91,10 @@ int main(int argc, const char * argv[]) { openLog(); pid_t procId = 0; BOOL update = YES, toSettings = NO, autoStart = NO, startInTray = NO, testMode = NO, freeType = NO, externalUpdater = NO; - BOOL customWorkingDir = NO; + BOOL customWorkingDir = NO, useEnvApi = YES; NSString *key = nil; + NSString *customApiId = nil; + NSString *customApiHash = nil; for (int i = 0; i < argc; ++i) { if ([@"-workpath" isEqualToString:[NSString stringWithUTF8String:argv[i]]]) { if (++i < argc) { @@ -124,6 +126,12 @@ int main(int argc, const char * argv[]) { customWorkingDir = YES; } else if ([@"-key" isEqualToString:[NSString stringWithUTF8String:argv[i]]]) { if (++i < argc) key = [NSString stringWithUTF8String:argv[i]]; + } else if ([@"-no-env-api" isEqualToString:[NSString stringWithUTF8String:argv[i]]]) { + useEnvApi = NO; + } else if ([@"-api-id" isEqualToString:[NSString stringWithUTF8String:argv[i]]]) { + if (++i < argc) customApiId = [NSString stringWithUTF8String:argv[i]]; + } else if ([@"-api-hash" isEqualToString:[NSString stringWithUTF8String:argv[i]]]) { + if (++i < argc) customApiHash = [NSString stringWithUTF8String:argv[i]]; } } if (!workDir) { @@ -268,6 +276,13 @@ int main(int argc, const char * argv[]) { [args addObject:@"-workdir"]; [args addObject:workDir]; } + if (!useEnvApi) [args addObject:@"-no-env-api"]; + if (customApiId && customApiHash) { + [args addObject:@"-api-id"]; + [args addObject:customApiId]; + [args addObject:@"-api-hash"]; + [args addObject:customApiHash]; + } writeLog([[NSArray arrayWithObjects:@"Running application '", appPath, @"' with args '", [args componentsJoinedByString:@"' '"], @"'..", nil] componentsJoinedByString:@""]); for (int i = 0; i < 5; ++i) { diff --git a/Telegram/SourceFiles/_other/updater_win.cpp b/Telegram/SourceFiles/_other/updater_win.cpp index 4c54e3029..6b4721265 100644 --- a/Telegram/SourceFiles/_other/updater_win.cpp +++ b/Telegram/SourceFiles/_other/updater_win.cpp @@ -10,6 +10,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL bool _debug = false; wstring updaterName, updaterDir, updateTo, exeName, customWorkingDir, customKeyFile; +wstring customApiId, customApiHash; bool equal(const wstring &a, const wstring &b) { return !_wcsicmp(a.c_str(), b.c_str()); @@ -340,6 +341,7 @@ int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE prevInstance, LPWSTR cmdPara int argsCount; bool needupdate = false, autostart = false, debug = false, writeprotected = false, startintray = false, testmode = false, freetype = false, externalupdater = false; + bool useEnvApi = true; args = CommandLineToArgvW(GetCommandLine(), &argsCount); if (args) { for (int i = 1; i < argsCount; ++i) { @@ -383,6 +385,14 @@ int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE prevInstance, LPWSTR cmdPara break; } } + } else if (equal(args[i], L"-no-env-api")) { + useEnvApi = false; + } else if (equal(args[i], L"-api-id") && ++i < argsCount) { + writeLog(std::wstring(L"Argument: ") + args[i]); + customApiId = args[i]; + } else if (equal(args[i], L"-api-hash") && ++i < argsCount) { + writeLog(std::wstring(L"Argument: ") + args[i]); + customApiHash = args[i]; } } if (exeName.empty()) { @@ -437,6 +447,11 @@ int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE prevInstance, LPWSTR cmdPara if (!customKeyFile.empty()) { targs += L" -key \"" + customKeyFile + L"\""; } + if (!useEnvApi) targs += L" -no-env-api"; + if (!customApiId.empty() && !customApiHash.empty()) { + targs += L" -api-id \"" + customApiId + L"\""; + targs += L" -api-hash \"" + customApiHash + L"\""; + } writeLog(L"Result arguments: " + targs); bool executed = false; diff --git a/Telegram/SourceFiles/core/launcher.cpp b/Telegram/SourceFiles/core/launcher.cpp index bbe6aff82..adcae6f58 100644 --- a/Telegram/SourceFiles/core/launcher.cpp +++ b/Telegram/SourceFiles/core/launcher.cpp @@ -335,10 +335,12 @@ int Launcher::exec() { Platform::start(); Ui::DisableCustomScaling(); - if (qEnvironmentVariableIsSet(kApiIdVarName.utf8()) + if (cUseEnvApi() + && qEnvironmentVariableIsSet(kApiIdVarName.utf8()) && qEnvironmentVariableIsSet(kApiHashVarName.utf8())) { cSetApiId(qgetenv(kApiIdVarName.utf8()).toInt()); cSetApiHash(QString::fromLatin1(qgetenv(kApiHashVarName.utf8()))); + cSetApiFromStartParams(false); } auto result = executeApplication(); @@ -463,6 +465,9 @@ void Launcher::processArguments() { { "-workdir" , KeyFormat::OneValue }, { "--" , KeyFormat::OneValue }, { "-scale" , KeyFormat::OneValue }, + { "-no-env-api" , KeyFormat::NoValues }, + { "-api-id" , KeyFormat::OneValue }, + { "-api-hash" , KeyFormat::OneValue }, }; auto parseResult = QMap(); auto parsingKey = QByteArray(); @@ -520,6 +525,15 @@ void Launcher::processArguments() { ? style::kScaleAuto : value; } + + gUseEnvApi = !parseResult.contains("-no-env-api"); + auto customApiId = parseResult.value("-api-id", {}).join(QString()).toInt(); + auto customApiHash = parseResult.value("-api-hash", {}).join(QString()); + if (customApiId > 0 && !customApiHash.isEmpty()) { + gApiId = customApiId; + gApiHash = customApiHash; + gApiFromStartParams = true; + } } int Launcher::executeApplication() { diff --git a/Telegram/SourceFiles/kotato/settings.cpp b/Telegram/SourceFiles/kotato/settings.cpp index 2dde00a75..cab06f977 100644 --- a/Telegram/SourceFiles/kotato/settings.cpp +++ b/Telegram/SourceFiles/kotato/settings.cpp @@ -216,3 +216,6 @@ int gApiId = 0; QString gApiHash; #endif // TDESKTOP_API_ID && TDESKTOP_API_HASH + +bool gUseEnvApi = true; +bool gApiFromStartParams = false; diff --git a/Telegram/SourceFiles/kotato/settings.h b/Telegram/SourceFiles/kotato/settings.h index 623aa8a2b..3b133ba50 100644 --- a/Telegram/SourceFiles/kotato/settings.h +++ b/Telegram/SourceFiles/kotato/settings.h @@ -127,3 +127,5 @@ DeclareSetting(bool, ForwardChatOnClick); DeclareSetting(int, ApiId); DeclareSetting(QString, ApiHash); +DeclareSetting(bool, UseEnvApi); +DeclareSetting(bool, ApiFromStartParams); diff --git a/Telegram/SourceFiles/platform/linux/launcher_linux.cpp b/Telegram/SourceFiles/platform/linux/launcher_linux.cpp index 1d948fd19..2ea231ea2 100644 --- a/Telegram/SourceFiles/platform/linux/launcher_linux.cpp +++ b/Telegram/SourceFiles/platform/linux/launcher_linux.cpp @@ -96,6 +96,17 @@ bool Launcher::launchUpdater(UpdaterLaunch action) { } } + if (!cUseEnvApi()) { + argumentsList.push("-no-env-api"); + } + + if (cApiFromStartParams()) { + argumentsList.push("-api-id"); + argumentsList.push(QFile::encodeName(QString::number(cApiId()))); + argumentsList.push("-api-hash"); + argumentsList.push(QFile::encodeName(cApiHash())); + } + Logs::closeMain(); CrashReports::Finish(); diff --git a/Telegram/SourceFiles/platform/mac/launcher_mac.mm b/Telegram/SourceFiles/platform/mac/launcher_mac.mm index a8d0cb26b..37a907810 100644 --- a/Telegram/SourceFiles/platform/mac/launcher_mac.mm +++ b/Telegram/SourceFiles/platform/mac/launcher_mac.mm @@ -74,6 +74,13 @@ bool Launcher::launchUpdater(UpdaterLaunch action) { if (customWorkingDir()) { [args addObject:@"-workdir_custom"]; } + if (!cUseEnvApi()) [args addObject:@"-no-env-api"]; + if (cApiFromStartParams()) { + [args addObject:@"-api-id"]; + [args addObject:Q2NSString(QString::number(cApiId()))]; + [args addObject:@"-api-hash"]; + [args addObject:Q2NSString(cApiHash())]; + } DEBUG_LOG(("Application Info: executing %1 %2").arg(NS2QString(path)).arg(NS2QString([args componentsJoinedByString:@" "]))); Logs::closeMain(); diff --git a/Telegram/SourceFiles/platform/win/launcher_win.cpp b/Telegram/SourceFiles/platform/win/launcher_win.cpp index 20d530249..588defd2e 100644 --- a/Telegram/SourceFiles/platform/win/launcher_win.cpp +++ b/Telegram/SourceFiles/platform/win/launcher_win.cpp @@ -99,6 +99,16 @@ bool Launcher::launchUpdater(UpdaterLaunch action) { pushArgument('"' + cExeDir() + '"'); } } + + if (!cUseEnvApi()) { + pushArgument(qsl("-no-env-api")); + } + if (cApiFromStartParams()) { + pushArgument(qsl("-api-id")); + pushArgument('"' + QString::number(cApiId()) + '"'); + pushArgument(qsl("-api-hash")); + pushArgument('"' + cApiHash() + '"'); + } return launch(operation, binaryPath, argumentsList); } diff --git a/Telegram/SourceFiles/platform/win/specific_win.cpp b/Telegram/SourceFiles/platform/win/specific_win.cpp index d2466a4ce..8cd0b936c 100644 --- a/Telegram/SourceFiles/platform/win/specific_win.cpp +++ b/Telegram/SourceFiles/platform/win/specific_win.cpp @@ -458,6 +458,15 @@ void RegisterCustomScheme(bool force) { HKEY rkey; QString exe = QDir::toNativeSeparators(cExeDir() + cExeName()); + QString possibleParams; + + if (!cUseEnvApi()) { + possibleParams += " -no-env-api"; + } + if (cApiFromStartParams()) { + possibleParams += " -api-id \"" + QString::number(cApiId()) + "\""; + possibleParams += " -api-hash \"" + cApiHash() + "\""; + } // Legacy URI scheme registration if (!_psOpenRegKey(L"Software\\Classes\\tg", &rkey)) return; @@ -470,7 +479,7 @@ void RegisterCustomScheme(bool force) { if (!_psOpenRegKey(L"Software\\Classes\\tg\\shell", &rkey)) return; if (!_psOpenRegKey(L"Software\\Classes\\tg\\shell\\open", &rkey)) return; if (!_psOpenRegKey(L"Software\\Classes\\tg\\shell\\open\\command", &rkey)) return; - if (!_psSetKeyValue(rkey, 0, '"' + exe + qsl("\" -workdir \"") + cWorkingDir() + qsl("\" -- \"%1\""))) return; + if (!_psSetKeyValue(rkey, 0, '"' + exe + qsl("\" -workdir \"") + cWorkingDir() + qsl("\"") + possibleParams + qsl(" -- \"%1\""))) return; // URI scheme registration as Default Program - Windows Vista and above if (!_psOpenRegKey(L"Software\\Classes\\ktgdesktop.tg", &rkey)) return; @@ -480,7 +489,7 @@ void RegisterCustomScheme(bool force) { if (!_psOpenRegKey(L"Software\\Classes\\ktgdesktop.tg\\shell", &rkey)) return; if (!_psOpenRegKey(L"Software\\Classes\\ktgdesktop.tg\\shell\\open", &rkey)) return; if (!_psOpenRegKey(L"Software\\Classes\\ktgdesktop.tg\\shell\\open\\command", &rkey)) return; - if (!_psSetKeyValue(rkey, 0, '"' + exe + qsl("\" -workdir \"") + cWorkingDir() + qsl("\" -- \"%1\""))) return; + if (!_psSetKeyValue(rkey, 0, '"' + exe + qsl("\" -workdir \"") + cWorkingDir() + qsl("\"") + possibleParams + qsl(" -- \"%1\""))) return; if (!_psOpenRegKey(L"Software\\KotatogramDesktop", &rkey)) return; if (!_psOpenRegKey(L"Software\\KotatogramDesktop\\Capabilities", &rkey)) return;