From 0fb67c78a93536656342de458aa328031a09dbb3 Mon Sep 17 00:00:00 2001 From: Ilya Fedin Date: Tue, 11 Mar 2025 05:26:12 +0000 Subject: [PATCH] Add a method to calculate instance hash to Launcher --- Telegram/SourceFiles/core/launcher.cpp | 16 ++++++++++++ Telegram/SourceFiles/core/launcher.h | 1 + .../platform/linux/specific_linux.cpp | 25 +++++-------------- .../win/windows_app_user_model_id.cpp | 9 +------ 4 files changed, 24 insertions(+), 27 deletions(-) diff --git a/Telegram/SourceFiles/core/launcher.cpp b/Telegram/SourceFiles/core/launcher.cpp index 312ce96673..e844acf9ee 100644 --- a/Telegram/SourceFiles/core/launcher.cpp +++ b/Telegram/SourceFiles/core/launcher.cpp @@ -504,6 +504,22 @@ uint64 Launcher::installationTag() const { return InstallationTag; } +QByteArray Launcher::instanceHash() const { + static const auto Result = [&] { + QByteArray h(32, 0); + if (customWorkingDir()) { + const auto d = QFile::encodeName( + QDir(cWorkingDir()).absolutePath()); + hashMd5Hex(d.constData(), d.size(), h.data()); + } else { + const auto f = QFile::encodeName(cExeDir() + cExeName()); + hashMd5Hex(f.constData(), f.size(), h.data()); + } + return h; + }(); + return Result; +} + void Launcher::processArguments() { enum class KeyFormat { NoValues, diff --git a/Telegram/SourceFiles/core/launcher.h b/Telegram/SourceFiles/core/launcher.h index ea5b1c97f3..e4de3c31d2 100644 --- a/Telegram/SourceFiles/core/launcher.h +++ b/Telegram/SourceFiles/core/launcher.h @@ -33,6 +33,7 @@ public: bool customWorkingDir() const; uint64 installationTag() const; + QByteArray instanceHash() const; bool checkPortableVersionFolder(); bool validateCustomWorkingDir(); diff --git a/Telegram/SourceFiles/platform/linux/specific_linux.cpp b/Telegram/SourceFiles/platform/linux/specific_linux.cpp index 8eece7d4d7..97255154b1 100644 --- a/Telegram/SourceFiles/platform/linux/specific_linux.cpp +++ b/Telegram/SourceFiles/platform/linux/specific_linux.cpp @@ -497,10 +497,11 @@ void InstallLauncher() { }); } -[[nodiscard]] QByteArray HashForSocketPath(const QByteArray &data) { +[[nodiscard]] QByteArray HashForSocketPath() { constexpr auto kHashForSocketPathLength = 24; - const auto binary = openssl::Sha256(bytes::make_span(data)); + const auto binary = openssl::Sha256( + bytes::make_span(Core::Launcher::Instance().instanceHash())); const auto base64 = QByteArray( reinterpret_cast(binary.data()), binary.size()).toBase64(QByteArray::Base64UrlEncoding); @@ -656,10 +657,6 @@ int psFixPrevious() { namespace Platform { void start() { - const auto d = QFile::encodeName(QDir(cWorkingDir()).absolutePath()); - char h[33] = { 0 }; - hashMd5Hex(d.constData(), d.size(), h); - QGuiApplication::setDesktopFileName([&] { if (KSandbox::isFlatpak()) { return qEnvironmentVariable("FLATPAK_ID"); @@ -672,18 +669,8 @@ void start() { } if (!Core::UpdaterDisabled()) { - QByteArray md5Hash(h); - if (!Core::Launcher::Instance().customWorkingDir()) { - const auto exePath = QFile::encodeName( - cExeDir() + cExeName()); - - hashMd5Hex( - exePath.constData(), - exePath.size(), - md5Hash.data()); - } - - return u"org.telegram.desktop._%1"_q.arg(md5Hash.constData()); + return u"org.telegram.desktop._%1"_q.arg( + Core::Launcher::Instance().instanceHash().constData()); } return u"org.telegram.desktop"_q; @@ -704,7 +691,7 @@ void start() { Webview::WebKitGTK::SetSocketPath(u"%1/%2-%3-webview-%4"_q.arg( QDir::tempPath(), - HashForSocketPath(d), + HashForSocketPath(), u"TD"_q,//QCoreApplication::applicationName(), - make path smaller. u"%1"_q).toStdString()); diff --git a/Telegram/SourceFiles/platform/win/windows_app_user_model_id.cpp b/Telegram/SourceFiles/platform/win/windows_app_user_model_id.cpp index d18e3f9904..3d051f4ce0 100644 --- a/Telegram/SourceFiles/platform/win/windows_app_user_model_id.cpp +++ b/Telegram/SourceFiles/platform/win/windows_app_user_model_id.cpp @@ -493,14 +493,7 @@ const std::wstring &Id() { return BaseId; } static const auto PortableId = [] { - std::string h(32, 0); - if (Core::Launcher::Instance().customWorkingDir()) { - const auto d = QFile::encodeName(QDir(cWorkingDir()).absolutePath()); - hashMd5Hex(d.constData(), d.size(), h.data()); - } else { - const auto exePath = QFile::encodeName(cExeDir() + cExeName()); - hashMd5Hex(exePath.constData(), exePath.size(), h.data()); - } + const auto h = Core::Launcher::Instance().instanceHash(); return BaseId + L'.' + std::wstring(h.begin(), h.end()); }(); return PortableId;