Add checking background service before connect (#716)

checking if the service is running for all platforms
This commit is contained in:
Vladyslav Miachkov 2024-05-10 13:06:04 +03:00 committed by GitHub
parent d67c378bff
commit ff348a348c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 20 additions and 4 deletions

View file

@ -42,6 +42,7 @@ namespace amnezia
UnknownError = 100, UnknownError = 100,
InternalError = 101, InternalError = 101,
NotImplementedError = 102, NotImplementedError = 102,
AmneziaServiceNotRunning = 103,
// Server errors // Server errors
ServerCheckFailed = 200, ServerCheckFailed = 200,

View file

@ -11,6 +11,7 @@ QString errorString(ErrorCode code) {
case(NoError): errorMessage = QObject::tr("No error"); break; case(NoError): errorMessage = QObject::tr("No error"); break;
case(UnknownError): errorMessage = QObject::tr("Unknown Error"); break; case(UnknownError): errorMessage = QObject::tr("Unknown Error"); break;
case(NotImplementedError): errorMessage = QObject::tr("Function not implemented"); break; case(NotImplementedError): errorMessage = QObject::tr("Function not implemented"); break;
case(AmneziaServiceNotRunning): errorMessage = QObject::tr("Background service is not running"); break;
// Server errors // Server errors
case(ServerCheckFailed): errorMessage = QObject::tr("Server check failed"); break; case(ServerCheckFailed): errorMessage = QObject::tr("Server check failed"); break;

View file

@ -7,9 +7,11 @@
#endif #endif
#include <QtConcurrent> #include <QtConcurrent>
#include "utilities.h"
#include "core/controllers/apiController.h" #include "core/controllers/apiController.h"
#include "core/controllers/vpnConfigurationController.h" #include "core/controllers/vpnConfigurationController.h"
#include "core/errorstrings.h" #include "core/errorstrings.h"
#include "version.h"
ConnectionController::ConnectionController(const QSharedPointer<ServersModel> &serversModel, ConnectionController::ConnectionController(const QSharedPointer<ServersModel> &serversModel,
const QSharedPointer<ContainersModel> &containersModel, const QSharedPointer<ContainersModel> &containersModel,
@ -32,6 +34,14 @@ ConnectionController::ConnectionController(const QSharedPointer<ServersModel> &s
void ConnectionController::openConnection() 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(); int serverIndex = m_serversModel->getDefaultServerIndex();
auto serverConfig = m_serversModel->getServerConfig(serverIndex); auto serverConfig = m_serversModel->getServerConfig(serverIndex);

View file

@ -76,7 +76,7 @@ QString Utils::usrExecutable(const QString &baseName)
return ("/usr/bin/" + baseName); return ("/usr/bin/" + baseName);
} }
bool Utils::processIsRunning(const QString &fileName) bool Utils::processIsRunning(const QString &fileName, const bool fullFlag)
{ {
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
QProcess process; QProcess process;
@ -107,10 +107,14 @@ bool Utils::processIsRunning(const QString &fileName)
#else #else
QProcess process; QProcess process;
process.setProcessChannelMode(QProcess::MergedChannels); process.setProcessChannelMode(QProcess::MergedChannels);
process.start("pgrep", QStringList({ fileName })); process.start("pgrep", QStringList({ fullFlag ? "-f" : "", fileName }));
process.waitForFinished(); process.waitForFinished();
if (process.exitStatus() == QProcess::NormalExit) { 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; return false;
#endif #endif

View file

@ -22,7 +22,7 @@ public:
static bool createEmptyFile(const QString &path); static bool createEmptyFile(const QString &path);
static bool initializePath(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 void killProcessByName(const QString &name);
static QString openVpnExecPath(); static QString openVpnExecPath();