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,
InternalError = 101,
NotImplementedError = 102,
AmneziaServiceNotRunning = 103,
// Server errors
ServerCheckFailed = 200,

View file

@ -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;

View file

@ -7,9 +7,11 @@
#endif
#include <QtConcurrent>
#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> &serversModel,
const QSharedPointer<ContainersModel> &containersModel,
@ -32,6 +34,14 @@ ConnectionController::ConnectionController(const QSharedPointer<ServersModel> &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);

View file

@ -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,11 +107,15 @@ 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) {
if (fullFlag) {
return (process.readLine().toUInt() > 0);
} else {
return (process.readAll().toUInt() > 0);
}
}
return false;
#endif
}

View file

@ -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();