Add logs for UpdateController

This commit is contained in:
aiamnezia 2024-12-24 18:53:35 +04:00
parent 44376847e2
commit 89df1df886
2 changed files with 57 additions and 36 deletions

View file

@ -8,10 +8,13 @@
#include "amnezia_application.h" #include "amnezia_application.h"
#include "core/errorstrings.h" #include "core/errorstrings.h"
#include "core/scripts_registry.h" #include "core/scripts_registry.h"
#include "logger.h"
#include "version.h" #include "version.h"
namespace namespace
{ {
Logger logger("UpdateController");
#ifdef Q_OS_MACOS #ifdef Q_OS_MACOS
const QString installerPath = QStandardPaths::writableLocation(QStandardPaths::TempLocation) + "/AmneziaVPN.dmg"; const QString installerPath = QStandardPaths::writableLocation(QStandardPaths::TempLocation) + "/AmneziaVPN.dmg";
#elif defined Q_OS_WINDOWS #elif defined Q_OS_WINDOWS
@ -53,7 +56,6 @@ void UpdateController::checkForUpdates()
m_version = data.value("tag_name").toString(); m_version = data.value("tag_name").toString();
auto currentVersion = QVersionNumber::fromString(QString(APP_VERSION)); auto currentVersion = QVersionNumber::fromString(QString(APP_VERSION));
qDebug() << currentVersion;
auto newVersion = QVersionNumber::fromString(m_version); auto newVersion = QVersionNumber::fromString(m_version);
if (newVersion > currentVersion) { if (newVersion > currentVersion) {
m_changelogText = data.value("body").toString(); m_changelogText = data.value("body").toString();
@ -86,30 +88,40 @@ void UpdateController::checkForUpdates()
} else { } else {
if (reply->error() == QNetworkReply::NetworkError::OperationCanceledError if (reply->error() == QNetworkReply::NetworkError::OperationCanceledError
|| reply->error() == QNetworkReply::NetworkError::TimeoutError) { || reply->error() == QNetworkReply::NetworkError::TimeoutError) {
qDebug() << errorString(ErrorCode::ApiConfigTimeoutError); logger.error() << errorString(ErrorCode::ApiConfigTimeoutError);
} else { } else {
QString err = reply->errorString(); QString err = reply->errorString();
qDebug() << QString::fromUtf8(reply->readAll()); logger.error() << QString::fromUtf8(reply->readAll());
qDebug() << reply->error(); logger.error() << "Network error code:" << QString::number(static_cast<int>(reply->error()));
qDebug() << err; logger.error() << "Error message:" << err;
qDebug() << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute); logger.error() << "HTTP status:" << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
qDebug() << errorString(ErrorCode::ApiConfigDownloadError); logger.error() << errorString(ErrorCode::ApiConfigDownloadError);
} }
} }
reply->deleteLater(); reply->deleteLater();
}); });
QObject::connect(reply, &QNetworkReply::errorOccurred, QObject::connect(reply, &QNetworkReply::errorOccurred, [this, reply](QNetworkReply::NetworkError error) {
[this, reply](QNetworkReply::NetworkError error) { qDebug() << reply->errorString() << error; }); logger.error() << "Network error occurred:" << reply->errorString() << error;
});
connect(reply, &QNetworkReply::sslErrors, [this, reply](const QList<QSslError> &errors) { connect(reply, &QNetworkReply::sslErrors, [this, reply](const QList<QSslError> &errors) {
qDebug().noquote() << errors; QStringList errorStrings;
qDebug() << errorString(ErrorCode::ApiConfigSslError); for (const QSslError &error : errors) {
errorStrings << error.errorString();
}
logger.error() << "SSL errors:" << errorStrings;
logger.error() << errorString(ErrorCode::ApiConfigSslError);
}); });
} }
void UpdateController::runInstaller() void UpdateController::runInstaller()
{ {
if (m_downloadUrl.isEmpty()) {
logger.error() << "Download URL is empty";
return;
}
QNetworkRequest request; QNetworkRequest request;
request.setTransferTimeout(7000); request.setTransferTimeout(7000);
request.setUrl(m_downloadUrl); request.setUrl(m_downloadUrl);
@ -119,31 +131,42 @@ void UpdateController::runInstaller()
QObject::connect(reply, &QNetworkReply::finished, [this, reply]() { QObject::connect(reply, &QNetworkReply::finished, [this, reply]() {
if (reply->error() == QNetworkReply::NoError) { if (reply->error() == QNetworkReply::NoError) {
QFile file(installerPath); QFile file(installerPath);
if (file.open(QIODevice::WriteOnly)) { if (!file.open(QIODevice::WriteOnly)) {
file.write(reply->readAll()); logger.error() << "Failed to open installer file for writing:" << installerPath
<< "Error:" << file.errorString();
reply->deleteLater();
return;
}
if (file.write(reply->readAll()) == -1) {
logger.error() << "Failed to write installer data to file:" << installerPath
<< "Error:" << file.errorString();
file.close(); file.close();
QString t = installerPath; reply->deleteLater();
return;
}
file.close();
QString t = installerPath;
#if defined(Q_OS_WINDOWS) #if defined(Q_OS_WINDOWS)
runWindowsInstaller(t); runWindowsInstaller(t);
#elif defined(Q_OS_MACOS) #elif defined(Q_OS_MACOS)
runMacInstaller(t); runMacInstaller(t);
#elif defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID) #elif defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID)
runLinuxInstaller(t); runLinuxInstaller(t);
#endif #endif
// emit errorOccured("");
}
} else { } else {
if (reply->error() == QNetworkReply::NetworkError::OperationCanceledError if (reply->error() == QNetworkReply::NetworkError::OperationCanceledError
|| reply->error() == QNetworkReply::NetworkError::TimeoutError) { || reply->error() == QNetworkReply::NetworkError::TimeoutError) {
qDebug() << errorString(ErrorCode::ApiConfigTimeoutError); logger.error() << errorString(ErrorCode::ApiConfigTimeoutError);
} else { } else {
QString err = reply->errorString(); QString err = reply->errorString();
qDebug() << QString::fromUtf8(reply->readAll()); logger.error() << QString::fromUtf8(reply->readAll());
qDebug() << reply->error(); logger.error() << "Network error code:" << QString::number(static_cast<int>(reply->error()));
qDebug() << err; logger.error() << "Error message:" << err;
qDebug() << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute); logger.error() << "HTTP status:" << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
qDebug() << errorString(ErrorCode::ApiConfigDownloadError); logger.error() << errorString(ErrorCode::ApiConfigDownloadError);
} }
} }
reply->deleteLater(); reply->deleteLater();
@ -153,14 +176,13 @@ void UpdateController::runInstaller()
#if defined(Q_OS_WINDOWS) #if defined(Q_OS_WINDOWS)
int UpdateController::runWindowsInstaller(const QString &installerPath) int UpdateController::runWindowsInstaller(const QString &installerPath)
{ {
// Start the installer process
qint64 pid; qint64 pid;
bool success = QProcess::startDetached(installerPath, QStringList(), QString(), &pid); bool success = QProcess::startDetached(installerPath, QStringList(), QString(), &pid);
if (success) { if (success) {
qDebug() << "Installation process started with PID:" << pid; logger.info() << "Installation process started with PID:" << pid;
} else { } else {
qDebug() << "Failed to start installation process"; logger.error() << "Failed to start installation process";
return -1; return -1;
} }
@ -171,7 +193,7 @@ int UpdateController::runWindowsInstaller(const QString &installerPath)
#if defined(Q_OS_MACOS) #if defined(Q_OS_MACOS)
int UpdateController::runMacInstaller(const QString &installerPath) int UpdateController::runMacInstaller(const QString &installerPath)
{ {
qDebug() << "macOS installer path:" << installerPath; logger.info() << "macOS installer path:" << installerPath;
// TODO: Implement macOS installation logic // TODO: Implement macOS installation logic
return -1; return -1;
} }
@ -184,16 +206,16 @@ int UpdateController::runLinuxInstaller(const QString &installerPath)
QTemporaryDir extractDir; QTemporaryDir extractDir;
extractDir.setAutoRemove(false); extractDir.setAutoRemove(false);
if (!extractDir.isValid()) { if (!extractDir.isValid()) {
qDebug() << "Failed to create temporary directory"; logger.error() << "Failed to create temporary directory";
return -1; return -1;
} }
qDebug() << "Temporary directory created:" << extractDir.path(); logger.info() << "Temporary directory created:" << extractDir.path();
// Create script file in the temporary directory // Create script file in the temporary directory
QString scriptPath = extractDir.path() + "/installer.sh"; QString scriptPath = extractDir.path() + "/installer.sh";
QFile scriptFile(scriptPath); QFile scriptFile(scriptPath);
if (!scriptFile.open(QIODevice::WriteOnly)) { if (!scriptFile.open(QIODevice::WriteOnly)) {
qDebug() << "Failed to create script file"; logger.error() << "Failed to create script file";
return -1; return -1;
} }
@ -201,7 +223,7 @@ int UpdateController::runLinuxInstaller(const QString &installerPath)
QString scriptContent = amnezia::scriptData(amnezia::ClientScriptType::linux_installer); QString scriptContent = amnezia::scriptData(amnezia::ClientScriptType::linux_installer);
scriptFile.write(scriptContent.toUtf8()); scriptFile.write(scriptContent.toUtf8());
scriptFile.close(); scriptFile.close();
qDebug() << "Script file created:" << scriptPath; logger.info() << "Script file created:" << scriptPath;
// Make script executable // Make script executable
QFile::setPermissions(scriptPath, QFile::permissions(scriptPath) | QFile::ExeUser); QFile::setPermissions(scriptPath, QFile::permissions(scriptPath) | QFile::ExeUser);
@ -212,9 +234,9 @@ int UpdateController::runLinuxInstaller(const QString &installerPath)
"/bin/bash", QStringList() << scriptPath << extractDir.path() << installerPath, extractDir.path(), &pid); "/bin/bash", QStringList() << scriptPath << extractDir.path() << installerPath, extractDir.path(), &pid);
if (success) { if (success) {
qDebug() << "Installation process started with PID:" << pid; logger.info() << "Installation process started with PID:" << pid;
} else { } else {
qDebug() << "Failed to start installation process"; logger.error() << "Failed to start installation process";
return -1; return -1;
} }

View file

@ -21,7 +21,6 @@ public slots:
void runInstaller(); void runInstaller();
signals: signals:
void updateFound(); void updateFound();
void errorOccured(const QString &errorMessage);
private: private:
std::shared_ptr<Settings> m_settings; std::shared_ptr<Settings> m_settings;