mirror of
https://github.com/amnezia-vpn/amnezia-client.git
synced 2026-05-08 14:33:23 +00:00
refactor: refactor the application to the mvvm architecture (#2009)
* refactor: move business logic from servers model * refactor: move containersModel initialization * refactor: added protocol ui controller and removed settings class from protocols model * refactor: moved cli management to separate controller * refactor: moved app split to separate controller * refactor: moved site split to separate controller * refactor: moved allowed dns to separate controller * refactor: moved language logic to separate ui controller * refactor: removed Settings from devices model * refactor: moved configs and services api logit to separate core controller * refactor: added a layer with a repository between the storage and controllers * refactor: use child parent system instead of smart pointers for controllers and models initialization * refactor: moved install functions from server controller to install controller * refactor: install controller refactoring * chore: renamed exportController to exportUiController * refactor: separate export controller * refactor: removed VpnConfigurationsController * chore: renamed ServerController to SshSession * refactor: replaced ServerController to SshSession * chore: moved qml controllers to separate folder * chore: include fixes * chore: moved utils from core root to core/utils * chore: include fixes * chore: rename core/utils files to camelCase foramt * chore: include fixes * chore: moved some utils to api and selfhosted folders * chore: include fixes * chore: remove unused file * chore: moved serialization folder to core/utils * chore: include fixes * chore: moved some files from client root to core/utils * chore: include fixes * chore: moved ui utils to ui/utils folder * chore: include fixes * chore: move utils from root to ui/utils * chore: include fixes * chore: moved configurators to core/configurators * chore: include fixes * refactor: moved iap logic from ui controller to core * refactor: moved remaining core logic from ApiConfigsController to SubscriptionController * chore: rename apiNewsController to apiNewsUiController * refactor: moved core logic from news ui controller to core * chore: renamed apiConfigsController to subscriptionUiController * chore: include fixes * refactor: merge ApiSettingsController with SubscriptionUiController * chore: moved ui selfhosted controllers to separate folder * chore: include fixes * chore: rename connectionController to connectiomUiController * refactor: moved core logic from connectionUiController * chore: rename settingsController to settingsUiController * refactor: move core logic from settingsUiController * refactor: moved core controller signal/slot connections to separate class * fix: newsController fixes after refactoring * chore: rename model to camelCase * chore: include fixes * chore: remove unused code * chore: move selfhosted core to separate folder * chore: include fixes * chore: rename importController to importUiController * refactor: move core logic from importUiController * chore: minor fixes * chore: remove prem v1 migration * refactor: remove openvpn over cloak and openvpn over shadowsocks * refactor: removed protocolsForContainer function * refactor: add core models * refactor: replace json with c++ structs for server config * refactor: move getDnsPair to ServerConfigUtils * feat: add admin selfhosted config export test * feat: add multi import test * refactor: use coreController for tests * feat: add few simple tests * chore: qrepos in all core controllers * feat: add test for settings * refactor: remove repo dependency from configurators * chore: moved protocols to core folder * chore: include fixes * refactor: moved containersDefs, defs, apiDefs, protocolsDefs to different places * chore: include fixes * chore: build fixes * chore: build fixes * refactor: remove q repo and interface repo * feat: add test for ui servers model and controller * chore: renamed to camelCase * chore: include fixes * refactor: moved core logic from sites ui controller * fix: fixed api config processing * fix: fixed processed server index processing * refactor: protocol models now use c++ structs instead of json configs * refactor: servers model now use c++ struct instead of json config * fix: fixed default server index processing * fix: fix logs init * fix: fix secure settings load keys * chore: build fixes * fix: fixed clear settings * fix: fixed restore backup * fix: sshSession usage * fix: fixed export functions signatures * fix: return missing part from buildContainerWorker * fix: fixed server description on page home * refactor: add container config helpers functions * refactor: c++ structs instead of json * chore: add dns protocol config struct * refactor: move config utils functions to config structs * feat: add test for selfhosted server setup * refactor: separate resources.qrc * fix: fixed server rename * chore: return nameOverriddenByUser * fix: build fixes * fix: fixed models init * refactor: cleanup models usage * fix: fixed models init * chore: cleanup connections and functions signatures * chore: cleanup updateModel calls * feat: added cache to servers repo * chore: cleanup unused functions * chore: ssxray processing * chore: remove transportProtoWithDefault and portWithDefault functions * chore: removed proto types any and l2tp * refactor: moved some constants * fix: fixed native configs export * refactor: remove json from processConfigWith functions * fix: fixed processed server index usage * fix: qml warning fixes * chore: merge fixes * chore: update tests * fix: fixed xray config processing * fix: fixed split tunneling processing * chore: rename sites controllers and model * chore: rename fixes * chore: minor fixes * chore: remove ability to load backup from "file with connection settings" button * fix: fixed api device revoke * fix: remove full model update when renaming a user * fix: fixed premium/free server rename * fix: fixed selfhosted new server install * fix: fixed updateContainer function * fix: fixed revoke for external premium configs * feat: add native configs qr processing * chore: codestyle fixes * fix: fixed admin config create * chore: again remove ability to load backup from "file with connection settings" button * chore: minor fixes * fix: fixed variables initialization * fix: fixed qml imports * fix: minor fixes * fix: fix vpnConnection function calls * feat: add buckup error handling * fix: fixed admin config revok * fix: fixed selfhosted awg installation * fix: ad visability * feat: add empty check for primary dns * chore: minor fixes
This commit is contained in:
360
client/core/utils/utilities.cpp
Executable file
360
client/core/utils/utilities.cpp
Executable file
@@ -0,0 +1,360 @@
|
||||
#include <QCoreApplication>
|
||||
#include <QDebug>
|
||||
#include <QDir>
|
||||
#include <QProcess>
|
||||
#include <QRandomGenerator>
|
||||
#include <QRegularExpression>
|
||||
#include <QStandardPaths>
|
||||
#include <QUrl>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
|
||||
#include "core/utils/utilities.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)
|
||||
{
|
||||
const QString possibleCharacters = QStringLiteral("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
|
||||
QString randomString;
|
||||
|
||||
for (int i = 0; i < len; ++i) {
|
||||
randomString.append(possibleCharacters.at(QRandomGenerator::system()->bounded(possibleCharacters.length())));
|
||||
}
|
||||
|
||||
return randomString;
|
||||
}
|
||||
|
||||
QString Utils::VerifyJsonString(const QString &source)
|
||||
{
|
||||
QJsonParseError error;
|
||||
QJsonDocument doc = QJsonDocument::fromJson(source.toUtf8(), &error);
|
||||
Q_UNUSED(doc)
|
||||
|
||||
if (error.error == QJsonParseError::NoError) {
|
||||
return "";
|
||||
} else {
|
||||
qDebug() << "WARNING: Json parse returns: " + error.errorString();
|
||||
return error.errorString();
|
||||
}
|
||||
}
|
||||
|
||||
QJsonObject Utils::JsonFromString(const QString &string)
|
||||
{
|
||||
auto removeComment = string.trimmed();
|
||||
if (removeComment != string.trimmed()) {
|
||||
qDebug() << "Some comments have been removed from the json.";
|
||||
}
|
||||
QJsonDocument doc = QJsonDocument::fromJson(removeComment.toUtf8());
|
||||
return doc.object();
|
||||
}
|
||||
|
||||
QString Utils::SafeBase64Decode(QString string)
|
||||
{
|
||||
QByteArray ba = string.replace(QChar('-'), QChar('+')).replace(QChar('_'), QChar('/')).toUtf8();
|
||||
return QByteArray::fromBase64(ba, QByteArray::Base64Option::OmitTrailingEquals);
|
||||
}
|
||||
|
||||
QString Utils::JsonToString(const QJsonObject &json, QJsonDocument::JsonFormat format)
|
||||
{
|
||||
QJsonDocument doc;
|
||||
doc.setObject(json);
|
||||
return doc.toJson(format);
|
||||
}
|
||||
|
||||
QString Utils::JsonToString(const QJsonArray &array, QJsonDocument::JsonFormat format)
|
||||
{
|
||||
QJsonDocument doc;
|
||||
doc.setArray(array);
|
||||
return doc.toJson(format);
|
||||
}
|
||||
|
||||
bool Utils::initializePath(const QString &path)
|
||||
{
|
||||
QDir dir;
|
||||
if (!dir.mkpath(path)) {
|
||||
qWarning().noquote() << QString("Cannot initialize path: '%1'").arg(path);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Utils::createEmptyFile(const QString &path)
|
||||
{
|
||||
QFile f(path);
|
||||
return f.open(QIODevice::WriteOnly | QIODevice::Truncate);
|
||||
}
|
||||
|
||||
QString Utils::executable(const QString &baseName, bool absPath)
|
||||
{
|
||||
QString ext;
|
||||
#ifdef Q_OS_WIN
|
||||
ext = ".exe";
|
||||
#endif
|
||||
const QString fileName = baseName + ext;
|
||||
if (!absPath) {
|
||||
return fileName;
|
||||
}
|
||||
return QCoreApplication::applicationDirPath() + "/" + fileName;
|
||||
}
|
||||
|
||||
QString Utils::usrExecutable(const QString &baseName)
|
||||
{
|
||||
if (QFileInfo::exists("/usr/sbin/" + baseName))
|
||||
return ("/usr/sbin/" + baseName);
|
||||
else
|
||||
return ("/usr/bin/" + baseName);
|
||||
}
|
||||
|
||||
bool Utils::processIsRunning(const QString &fileName, const bool fullFlag)
|
||||
{
|
||||
#ifdef Q_OS_WIN
|
||||
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
|
||||
if (hSnapshot == INVALID_HANDLE_VALUE) {
|
||||
qWarning() << "Utils::processIsRunning error CreateToolhelp32Snapshot";
|
||||
return false;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
} while (Process32NextW(hSnapshot, &pe32));
|
||||
|
||||
CloseHandle(hSnapshot);
|
||||
return false;
|
||||
|
||||
#elif defined(Q_OS_IOS) || defined(Q_OS_ANDROID) || defined(MACOS_NE)
|
||||
return false;
|
||||
#else
|
||||
QProcess process;
|
||||
QStringList arguments;
|
||||
if (fullFlag) {
|
||||
arguments << "-f";
|
||||
}
|
||||
arguments << fileName;
|
||||
process.setProcessChannelMode(QProcess::MergedChannels);
|
||||
process.start("pgrep", arguments);
|
||||
process.waitForFinished();
|
||||
if (process.exitStatus() == QProcess::NormalExit) {
|
||||
if (fullFlag) {
|
||||
return (process.readLine().toUInt() > 0);
|
||||
} else {
|
||||
return (process.readAll().toUInt() > 0);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool Utils::killProcessByName(const QString &name)
|
||||
{
|
||||
qDebug().noquote() << "Kill process" << name;
|
||||
#ifdef Q_OS_WIN
|
||||
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
|
||||
if (hSnapshot == INVALID_HANDLE_VALUE)
|
||||
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
|
||||
return QProcess::execute("pkill", { name }) == 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
QString Utils::openVpnExecPath()
|
||||
{
|
||||
#ifdef Q_OS_WIN
|
||||
return Utils::executable("openvpn/openvpn", true);
|
||||
#elif defined Q_OS_LINUX
|
||||
// We have service that runs OpenVPN on Linux. We need to make same
|
||||
// path for client and service.
|
||||
return Utils::executable("../../client/bin/openvpn", true);
|
||||
#else
|
||||
return Utils::executable("/openvpn", true);
|
||||
#endif
|
||||
}
|
||||
|
||||
QString Utils::wireguardExecPath()
|
||||
{
|
||||
#ifdef Q_OS_WIN
|
||||
return Utils::executable("wireguard/wireguard-service", true);
|
||||
#elif defined Q_OS_LINUX
|
||||
return Utils::usrExecutable("wg-quick");
|
||||
#elif defined Q_OS_MAC
|
||||
return Utils::executable("/wireguard", true);
|
||||
#else
|
||||
return {};
|
||||
#endif
|
||||
}
|
||||
|
||||
QString Utils::certUtilPath()
|
||||
{
|
||||
#ifdef Q_OS_WIN
|
||||
QString winPath = QString::fromUtf8(qgetenv("windir"));
|
||||
return winPath + "\\system32\\certutil.exe";
|
||||
#else
|
||||
return "";
|
||||
#endif
|
||||
}
|
||||
|
||||
QString Utils::tun2socksPath()
|
||||
{
|
||||
#ifdef Q_OS_WIN
|
||||
return Utils::executable("xray/tun2socks", true);
|
||||
#elif defined Q_OS_LINUX
|
||||
// We have service that runs OpenVPN on Linux. We need to make same
|
||||
// path for client and service.
|
||||
return Utils::executable("../../client/bin/tun2socks", true);
|
||||
#else
|
||||
return Utils::executable("/tun2socks", true);
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef Q_OS_WIN
|
||||
// Inspired from http://stackoverflow.com/a/15281070/1529139
|
||||
// and http://stackoverflow.com/q/40059902/1529139
|
||||
bool Utils::signalCtrl(DWORD dwProcessId, DWORD dwCtrlEvent)
|
||||
{
|
||||
bool success = false;
|
||||
DWORD thisConsoleId = GetCurrentProcessId();
|
||||
// Leave current console if it exists
|
||||
// (otherwise AttachConsole will return ERROR_ACCESS_DENIED)
|
||||
bool consoleDetached = (FreeConsole() != FALSE);
|
||||
|
||||
if (AttachConsole(dwProcessId) != FALSE) {
|
||||
// Add a fake Ctrl-C handler for avoid instant kill is this console
|
||||
// WARNING: do not revert it or current program will be also killed
|
||||
SetConsoleCtrlHandler(nullptr, true);
|
||||
success = (GenerateConsoleCtrlEvent(dwCtrlEvent, 0) != FALSE);
|
||||
FreeConsole();
|
||||
}
|
||||
|
||||
if (consoleDetached) {
|
||||
// Create a new console if previous was deleted by OS
|
||||
if (AttachConsole(thisConsoleId) == FALSE) {
|
||||
int errorCode = GetLastError();
|
||||
if (errorCode == 31) // 31=ERROR_GEN_FAILURE
|
||||
{
|
||||
AllocConsole();
|
||||
}
|
||||
}
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
void Utils::logException(const std::exception &e)
|
||||
{
|
||||
qCritical() << e.what();
|
||||
try {
|
||||
std::rethrow_if_nested(e);
|
||||
} catch (const std::exception &nested) {
|
||||
logException(nested);
|
||||
} catch (...) {}
|
||||
}
|
||||
|
||||
void Utils::logException(const std::exception_ptr &eptr)
|
||||
{
|
||||
try {
|
||||
if (eptr) std::rethrow_exception(eptr);
|
||||
} catch (const std::exception &e) {
|
||||
logException(e);
|
||||
} catch (...) {}
|
||||
}
|
||||
Reference in New Issue
Block a user