Add a method to calculate instance hash to Launcher

This commit is contained in:
Ilya Fedin 2025-03-11 05:26:12 +00:00 committed by John Preston
parent ff0f7f49da
commit 0fb67c78a9
4 changed files with 24 additions and 27 deletions

View file

@ -504,6 +504,22 @@ uint64 Launcher::installationTag() const {
return InstallationTag; 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() { void Launcher::processArguments() {
enum class KeyFormat { enum class KeyFormat {
NoValues, NoValues,

View file

@ -33,6 +33,7 @@ public:
bool customWorkingDir() const; bool customWorkingDir() const;
uint64 installationTag() const; uint64 installationTag() const;
QByteArray instanceHash() const;
bool checkPortableVersionFolder(); bool checkPortableVersionFolder();
bool validateCustomWorkingDir(); bool validateCustomWorkingDir();

View file

@ -497,10 +497,11 @@ void InstallLauncher() {
}); });
} }
[[nodiscard]] QByteArray HashForSocketPath(const QByteArray &data) { [[nodiscard]] QByteArray HashForSocketPath() {
constexpr auto kHashForSocketPathLength = 24; 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( const auto base64 = QByteArray(
reinterpret_cast<const char*>(binary.data()), reinterpret_cast<const char*>(binary.data()),
binary.size()).toBase64(QByteArray::Base64UrlEncoding); binary.size()).toBase64(QByteArray::Base64UrlEncoding);
@ -656,10 +657,6 @@ int psFixPrevious() {
namespace Platform { namespace Platform {
void start() { void start() {
const auto d = QFile::encodeName(QDir(cWorkingDir()).absolutePath());
char h[33] = { 0 };
hashMd5Hex(d.constData(), d.size(), h);
QGuiApplication::setDesktopFileName([&] { QGuiApplication::setDesktopFileName([&] {
if (KSandbox::isFlatpak()) { if (KSandbox::isFlatpak()) {
return qEnvironmentVariable("FLATPAK_ID"); return qEnvironmentVariable("FLATPAK_ID");
@ -672,18 +669,8 @@ void start() {
} }
if (!Core::UpdaterDisabled()) { if (!Core::UpdaterDisabled()) {
QByteArray md5Hash(h); return u"org.telegram.desktop._%1"_q.arg(
if (!Core::Launcher::Instance().customWorkingDir()) { Core::Launcher::Instance().instanceHash().constData());
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"_q; return u"org.telegram.desktop"_q;
@ -704,7 +691,7 @@ void start() {
Webview::WebKitGTK::SetSocketPath(u"%1/%2-%3-webview-%4"_q.arg( Webview::WebKitGTK::SetSocketPath(u"%1/%2-%3-webview-%4"_q.arg(
QDir::tempPath(), QDir::tempPath(),
HashForSocketPath(d), HashForSocketPath(),
u"TD"_q,//QCoreApplication::applicationName(), - make path smaller. u"TD"_q,//QCoreApplication::applicationName(), - make path smaller.
u"%1"_q).toStdString()); u"%1"_q).toStdString());

View file

@ -493,14 +493,7 @@ const std::wstring &Id() {
return BaseId; return BaseId;
} }
static const auto PortableId = [] { static const auto PortableId = [] {
std::string h(32, 0); const auto h = Core::Launcher::Instance().instanceHash();
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());
}
return BaseId + L'.' + std::wstring(h.begin(), h.end()); return BaseId + L'.' + std::wstring(h.begin(), h.end());
}(); }();
return PortableId; return PortableId;