From ff348a348c7d2a45d1c06b474a8e8f9be8541999 Mon Sep 17 00:00:00 2001 From: Vladyslav Miachkov <34045014+fameowner99@users.noreply.github.com> Date: Fri, 10 May 2024 13:06:04 +0300 Subject: [PATCH] Add checking background service before connect (#716) checking if the service is running for all platforms --- client/core/defs.h | 1 + client/core/errorstrings.cpp | 1 + client/ui/controllers/connectionController.cpp | 10 ++++++++++ client/utilities.cpp | 10 +++++++--- client/utilities.h | 2 +- 5 files changed, 20 insertions(+), 4 deletions(-) diff --git a/client/core/defs.h b/client/core/defs.h index 7d179ea6..e35c2f38 100644 --- a/client/core/defs.h +++ b/client/core/defs.h @@ -42,6 +42,7 @@ namespace amnezia UnknownError = 100, InternalError = 101, NotImplementedError = 102, + AmneziaServiceNotRunning = 103, // Server errors ServerCheckFailed = 200, diff --git a/client/core/errorstrings.cpp b/client/core/errorstrings.cpp index 99b55fcd..2a14b3cd 100644 --- a/client/core/errorstrings.cpp +++ b/client/core/errorstrings.cpp @@ -11,6 +11,7 @@ QString errorString(ErrorCode code) { case(NoError): errorMessage = QObject::tr("No error"); break; case(UnknownError): errorMessage = QObject::tr("Unknown Error"); break; case(NotImplementedError): errorMessage = QObject::tr("Function not implemented"); break; + case(AmneziaServiceNotRunning): errorMessage = QObject::tr("Background service is not running"); break; // Server errors case(ServerCheckFailed): errorMessage = QObject::tr("Server check failed"); break; diff --git a/client/ui/controllers/connectionController.cpp b/client/ui/controllers/connectionController.cpp index 97c4aa62..a72ff06b 100644 --- a/client/ui/controllers/connectionController.cpp +++ b/client/ui/controllers/connectionController.cpp @@ -7,9 +7,11 @@ #endif #include +#include "utilities.h" #include "core/controllers/apiController.h" #include "core/controllers/vpnConfigurationController.h" #include "core/errorstrings.h" +#include "version.h" ConnectionController::ConnectionController(const QSharedPointer &serversModel, const QSharedPointer &containersModel, @@ -32,6 +34,14 @@ ConnectionController::ConnectionController(const QSharedPointer &s void ConnectionController::openConnection() { +#if !defined(Q_OS_ANDROID) && !defined(Q_OS_IOS) + if (!Utils::processIsRunning(Utils::executable(SERVICE_NAME, false), true)) + { + emit connectionErrorOccurred(errorString(ErrorCode::AmneziaServiceNotRunning)); + return; + } +#endif + int serverIndex = m_serversModel->getDefaultServerIndex(); auto serverConfig = m_serversModel->getServerConfig(serverIndex); diff --git a/client/utilities.cpp b/client/utilities.cpp index 46df1dda..bc026330 100644 --- a/client/utilities.cpp +++ b/client/utilities.cpp @@ -76,7 +76,7 @@ QString Utils::usrExecutable(const QString &baseName) return ("/usr/bin/" + baseName); } -bool Utils::processIsRunning(const QString &fileName) +bool Utils::processIsRunning(const QString &fileName, const bool fullFlag) { #ifdef Q_OS_WIN QProcess process; @@ -107,10 +107,14 @@ bool Utils::processIsRunning(const QString &fileName) #else QProcess process; process.setProcessChannelMode(QProcess::MergedChannels); - process.start("pgrep", QStringList({ fileName })); + process.start("pgrep", QStringList({ fullFlag ? "-f" : "", fileName })); process.waitForFinished(); if (process.exitStatus() == QProcess::NormalExit) { - return (process.readAll().toUInt() > 0); + if (fullFlag) { + return (process.readLine().toUInt() > 0); + } else { + return (process.readAll().toUInt() > 0); + } } return false; #endif diff --git a/client/utilities.h b/client/utilities.h index 8a4cdc7a..3fd919f5 100644 --- a/client/utilities.h +++ b/client/utilities.h @@ -22,7 +22,7 @@ public: static bool createEmptyFile(const QString &path); static bool initializePath(const QString &path); - static bool processIsRunning(const QString &fileName); + static bool processIsRunning(const QString &fileName, const bool fullFlag = false); static void killProcessByName(const QString &name); static QString openVpnExecPath();