Merge pull request #1085 from amnezia-vpn/bugfix/win_check_ps

Refactoring wmic to winapi
This commit is contained in:
pokamest 2024-10-18 15:45:32 +01:00 committed by GitHub
commit 628e22869d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 128 additions and 60 deletions

View file

@ -34,13 +34,13 @@ ConnectionController::ConnectionController(const QSharedPointer<ServersModel> &s
void ConnectionController::openConnection() void ConnectionController::openConnection()
{ {
// #if !defined(Q_OS_ANDROID) && !defined(Q_OS_IOS) #if !defined(Q_OS_ANDROID) && !defined(Q_OS_IOS)
// if (!Utils::processIsRunning(Utils::executable(SERVICE_NAME, false), true)) if (!Utils::processIsRunning(Utils::executable(SERVICE_NAME, false), true))
// { {
// emit connectionErrorOccurred(ErrorCode::AmneziaServiceNotRunning); emit connectionErrorOccurred(ErrorCode::AmneziaServiceNotRunning);
// return; return;
// } }
// #endif #endif
int serverIndex = m_serversModel->getDefaultServerIndex(); int serverIndex = m_serversModel->getDefaultServerIndex();
QJsonObject serverConfig = m_serversModel->getServerConfig(serverIndex); QJsonObject serverConfig = m_serversModel->getServerConfig(serverIndex);

27
client/ui/controllers/installController.cpp Normal file → Executable file
View file

@ -34,31 +34,6 @@ namespace
constexpr char apiConfig[] = "api_config"; constexpr char apiConfig[] = "api_config";
constexpr char authData[] = "auth_data"; constexpr char authData[] = "auth_data";
} }
#ifdef Q_OS_WINDOWS
QString getNextDriverLetter()
{
QProcess drivesProc;
drivesProc.start("wmic logicaldisk get caption");
drivesProc.waitForFinished();
QString drives = drivesProc.readAll();
qDebug() << drives;
QString letters = "CFGHIJKLMNOPQRSTUVWXYZ";
QString letter;
for (int i = letters.size() - 1; i > 0; i--) {
letter = letters.at(i);
if (!drives.contains(letter + ":"))
break;
}
if (letter == "C:") {
// set err info
qDebug() << "Can't find free drive letter";
return "";
}
return letter;
}
#endif
} }
InstallController::InstallController(const QSharedPointer<ServersModel> &serversModel, const QSharedPointer<ContainersModel> &containersModel, InstallController::InstallController(const QSharedPointer<ServersModel> &serversModel, const QSharedPointer<ContainersModel> &containersModel,
@ -668,7 +643,7 @@ void InstallController::mountSftpDrive(const QString &port, const QString &passw
QString hostname = serverCredentials.hostName; QString hostname = serverCredentials.hostName;
#ifdef Q_OS_WINDOWS #ifdef Q_OS_WINDOWS
mountPath = getNextDriverLetter() + ":"; mountPath = Utils::getNextDriverLetter() + ":";
// QString cmd = QString("net use \\\\sshfs\\%1@x.x.x.x!%2 /USER:%1 %3") // QString cmd = QString("net use \\\\sshfs\\%1@x.x.x.x!%2 /USER:%1 %3")
// .arg(labelTftpUserNameText()) // .arg(labelTftpUserNameText())
// .arg(labelTftpPortText()) // .arg(labelTftpPortText())

141
client/utilities.cpp Normal file → Executable file
View file

@ -10,7 +10,62 @@
#include <QJsonObject> #include <QJsonObject>
#include "utilities.h" #include "utilities.h"
#include "version.h"
#ifdef Q_OS_WINDOWS
QString printErrorMessage(DWORD errorCode) {
LPVOID lpMsgBuf;
DWORD dwFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS;
DWORD dwLanguageId = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US);
FormatMessageW(
dwFlags,
NULL,
errorCode,
dwLanguageId,
(LPWSTR)&lpMsgBuf,
0,
NULL
);
QString errorMsg = QString::fromWCharArray((LPCWSTR)lpMsgBuf);
LocalFree(lpMsgBuf);
return errorMsg.trimmed();
}
QString Utils::getNextDriverLetter()
{
DWORD drivesBitmask = GetLogicalDrives();
if (drivesBitmask == 0) {
DWORD error = GetLastError();
qDebug() << "GetLogicalDrives failed. Error code:" << error;
return "";
}
QString letters = "FGHIJKLMNOPQRSTUVWXYZ";
QString availableLetter;
for (int i = letters.size() - 1; i >= 0; --i) {
QChar letterChar = letters.at(i);
int driveIndex = letterChar.toLatin1() - 'A';
if ((drivesBitmask & (1 << driveIndex)) == 0) {
availableLetter = letterChar;
break;
}
}
if (availableLetter.isEmpty()) {
qDebug() << "Can't find free drive letter";
return "";
}
return availableLetter;
}
#endif
QString Utils::getRandomString(int len) QString Utils::getRandomString(int len)
{ {
@ -108,30 +163,34 @@ QString Utils::usrExecutable(const QString &baseName)
bool Utils::processIsRunning(const QString &fileName, const bool fullFlag) bool Utils::processIsRunning(const QString &fileName, const bool fullFlag)
{ {
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
QProcess process; HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
process.setReadChannel(QProcess::StandardOutput); if (hSnapshot == INVALID_HANDLE_VALUE) {
process.setProcessChannelMode(QProcess::MergedChannels); qWarning() << "Utils::processIsRunning error CreateToolhelp32Snapshot";
process.start("wmic.exe", return false;
QStringList() << "/OUTPUT:STDOUT" }
<< "PROCESS"
<< "get"
<< "Caption");
process.waitForStarted();
process.waitForFinished();
QString processData(process.readAll());
QStringList processList = processData.split(QRegularExpression("[\r\n]"), Qt::SkipEmptyParts);
foreach (const QString &rawLine, processList) {
const QString line = rawLine.simplified();
if (line.isEmpty()) {
continue;
}
if (line == fileName) { PROCESSENTRY32W pe32;
pe32.dwSize = sizeof(PROCESSENTRY32W);
if (!Process32FirstW(hSnapshot, &pe32)) {
CloseHandle(hSnapshot);
qWarning() << "Utils::processIsRunning error Process32FirstW";
return false;
}
do {
QString exeFile = QString::fromWCharArray(pe32.szExeFile);
if (exeFile.compare(fileName, Qt::CaseInsensitive) == 0) {
CloseHandle(hSnapshot);
return true; return true;
} }
} } while (Process32NextW(hSnapshot, &pe32));
CloseHandle(hSnapshot);
return false; return false;
#elif defined(Q_OS_IOS)
#elif defined(Q_OS_IOS) || defined(Q_OS_ANDROID)
return false; return false;
#else #else
QProcess process; QProcess process;
@ -149,13 +208,45 @@ bool Utils::processIsRunning(const QString &fileName, const bool fullFlag)
#endif #endif
} }
void Utils::killProcessByName(const QString &name) bool Utils::killProcessByName(const QString &name)
{ {
qDebug().noquote() << "Kill process" << name; qDebug().noquote() << "Kill process" << name;
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
QProcess::execute("taskkill", QStringList() << "/IM" << name << "/F"); HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
#elif defined Q_OS_IOS if (hSnapshot == INVALID_HANDLE_VALUE)
return; return false;
PROCESSENTRY32W pe32;
pe32.dwSize = sizeof(PROCESSENTRY32W);
bool success = false;
if (Process32FirstW(hSnapshot, &pe32)) {
do {
QString exeFile = QString::fromWCharArray(pe32.szExeFile);
if (exeFile.compare(name, Qt::CaseInsensitive) == 0) {
HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, pe32.th32ProcessID);
if (hProcess != NULL) {
if (TerminateProcess(hProcess, 0)) {
success = true;
} else {
DWORD error = GetLastError();
qCritical() << "Can't terminate process" << exeFile << "(PID:" << pe32.th32ProcessID << "). Error:" << printErrorMessage(error);
}
CloseHandle(hProcess);
} else {
DWORD error = GetLastError();
qCritical() << "Can't open process for termination" << exeFile << "(PID:" << pe32.th32ProcessID << "). Error:" << printErrorMessage(error);
}
}
} while (Process32NextW(hSnapshot, &pe32));
}
CloseHandle(hSnapshot);
return success;
#elif defined Q_OS_IOS || defined(Q_OS_ANDROID)
return false;
#else #else
QProcess::execute(QString("pkill %1").arg(name)); QProcess::execute(QString("pkill %1").arg(name));
#endif #endif

6
client/utilities.h Normal file → Executable file
View file

@ -7,7 +7,8 @@
#include <QJsonDocument> #include <QJsonDocument>
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
#include "Windows.h" #include <windows.h>
#include <tlhelp32.h>
#endif #endif
class Utils : public QObject class Utils : public QObject
@ -27,7 +28,7 @@ public:
static bool initializePath(const QString &path); static bool initializePath(const QString &path);
static bool processIsRunning(const QString &fileName, const bool fullFlag = false); static bool processIsRunning(const QString &fileName, const bool fullFlag = false);
static void killProcessByName(const QString &name); static bool killProcessByName(const QString &name);
static QString openVpnExecPath(); static QString openVpnExecPath();
static QString wireguardExecPath(); static QString wireguardExecPath();
@ -39,6 +40,7 @@ public:
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
static bool signalCtrl(DWORD dwProcessId, DWORD dwCtrlEvent); static bool signalCtrl(DWORD dwProcessId, DWORD dwCtrlEvent);
static QString getNextDriverLetter();
#endif #endif
}; };