mirror of
https://github.com/amnezia-vpn/amnezia-client.git
synced 2026-06-23 02:00:20 +07:00
fix: adapt ServersBackupController to new codebase after rebase
- Remove deleted ServerController dependency; implement runHostScript, downloadFileFromHost, uploadFileToHostPublic directly using SshSession and libssh::Client which are the current SSH abstractions - Change constructor from std::shared_ptr<Settings> to QPointer<SecureQSettings> to match CoreController's settings type - Register PageSettingsServerBackup, PageSettingsServerBackupRestored, PageSettingsServerRestoreMode in qml.qrc (were in resources.qrc which was removed from the codebase)
This commit is contained in:
@@ -1,4 +1,6 @@
|
|||||||
#include "serversBackupController.h"
|
#include "serversBackupController.h"
|
||||||
|
#include "secureQSettings.h"
|
||||||
|
#include "core/utils/utilities.h"
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QDir>
|
#include <QDir>
|
||||||
#include <QRegularExpression>
|
#include <QRegularExpression>
|
||||||
@@ -24,11 +26,10 @@
|
|||||||
#include "ui/models/servers_model.h"
|
#include "ui/models/servers_model.h"
|
||||||
#include "ui/models/containers_model.h"
|
#include "ui/models/containers_model.h"
|
||||||
|
|
||||||
ServersBackupController::ServersBackupController(std::shared_ptr<Settings> settings, ServersModel *serversModel, QObject *parent)
|
ServersBackupController::ServersBackupController(SecureQSettings *settings, ServersModel *serversModel, QObject *parent)
|
||||||
: QObject(parent)
|
: QObject(parent)
|
||||||
, m_settings(settings)
|
, m_settings(settings)
|
||||||
, m_serversModel(serversModel)
|
, m_serversModel(serversModel)
|
||||||
, m_serverController(new ServerController(settings, this))
|
|
||||||
, m_status(Idle)
|
, m_status(Idle)
|
||||||
, m_backupDir("/var/backups/amnezia")
|
, m_backupDir("/var/backups/amnezia")
|
||||||
, m_restoreReplaceMode(false)
|
, m_restoreReplaceMode(false)
|
||||||
@@ -87,7 +88,7 @@ void ServersBackupController::createBackup(const ServerCredentials &credentials)
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Run script on server
|
// Run script on server
|
||||||
ErrorCode error = m_serverController->runHostScript(credentials, script, cbStdOut, cbStdErr);
|
ErrorCode error = runHostScript(credentials, script, cbStdOut, cbStdErr);
|
||||||
|
|
||||||
if (error == ErrorCode::NoError) {
|
if (error == ErrorCode::NoError) {
|
||||||
// Parse created backup name from output: format "IP_ADDRESS - DD-MM-YYYY_HH-MM-SS.tgz"
|
// Parse created backup name from output: format "IP_ADDRESS - DD-MM-YYYY_HH-MM-SS.tgz"
|
||||||
@@ -165,7 +166,7 @@ void ServersBackupController::createContainerBackup(const ServerCredentials &cre
|
|||||||
return handleStdErr(data, m_currentError);
|
return handleStdErr(data, m_currentError);
|
||||||
};
|
};
|
||||||
|
|
||||||
ErrorCode error = m_serverController->runHostScript(credentials, script, cbStdOut, cbStdErr);
|
ErrorCode error = runHostScript(credentials, script, cbStdOut, cbStdErr);
|
||||||
|
|
||||||
if (error == ErrorCode::NoError) {
|
if (error == ErrorCode::NoError) {
|
||||||
// Parse created backup name from output: format "IP_ADDRESS - DD-MM-YYYY_HH-MM-SS.tgz"
|
// Parse created backup name from output: format "IP_ADDRESS - DD-MM-YYYY_HH-MM-SS.tgz"
|
||||||
@@ -221,7 +222,7 @@ void ServersBackupController::createContainersBackup(const ServerCredentials &cr
|
|||||||
return handleStdErr(data, m_currentError);
|
return handleStdErr(data, m_currentError);
|
||||||
};
|
};
|
||||||
|
|
||||||
ErrorCode error = m_serverController->runHostScript(credentials, script, cbStdOut, cbStdErr);
|
ErrorCode error = runHostScript(credentials, script, cbStdOut, cbStdErr);
|
||||||
|
|
||||||
if (error == ErrorCode::NoError) {
|
if (error == ErrorCode::NoError) {
|
||||||
// Parse created backup name from output: format "IP_ADDRESS - DD-MM-YYYY_HH-MM-SS.tgz"
|
// Parse created backup name from output: format "IP_ADDRESS - DD-MM-YYYY_HH-MM-SS.tgz"
|
||||||
@@ -268,7 +269,7 @@ void ServersBackupController::fetchBackupList(const ServerCredentials &credentia
|
|||||||
return handleStdErr(data, m_currentError);
|
return handleStdErr(data, m_currentError);
|
||||||
};
|
};
|
||||||
|
|
||||||
ErrorCode error = m_serverController->runHostScript(credentials, script, cbStdOut, cbStdErr);
|
ErrorCode error = runHostScript(credentials, script, cbStdOut, cbStdErr);
|
||||||
|
|
||||||
if (error == ErrorCode::NoError) {
|
if (error == ErrorCode::NoError) {
|
||||||
QList<BackupInfo> backups = parseBackupList(m_currentOutput);
|
QList<BackupInfo> backups = parseBackupList(m_currentOutput);
|
||||||
@@ -310,7 +311,7 @@ void ServersBackupController::restoreBackup(const ServerCredentials &credentials
|
|||||||
return handleStdErr(data, m_currentError);
|
return handleStdErr(data, m_currentError);
|
||||||
};
|
};
|
||||||
|
|
||||||
ErrorCode error = m_serverController->runHostScript(credentials, script, cbStdOut, cbStdErr);
|
ErrorCode error = runHostScript(credentials, script, cbStdOut, cbStdErr);
|
||||||
|
|
||||||
// Check output for errors, even if script exited with code 0
|
// Check output for errors, even if script exited with code 0
|
||||||
bool hasError = m_currentOutput.contains("[ERROR]") ||
|
bool hasError = m_currentOutput.contains("[ERROR]") ||
|
||||||
@@ -362,7 +363,7 @@ void ServersBackupController::checkBackupStatus(const ServerCredentials &credent
|
|||||||
return handleStdErr(data, m_currentError);
|
return handleStdErr(data, m_currentError);
|
||||||
};
|
};
|
||||||
|
|
||||||
ErrorCode error = m_serverController->runHostScript(credentials, script, cbStdOut, cbStdErr);
|
ErrorCode error = runHostScript(credentials, script, cbStdOut, cbStdErr);
|
||||||
|
|
||||||
if (error == ErrorCode::NoError) {
|
if (error == ErrorCode::NoError) {
|
||||||
QJsonObject status = parseBackupStatus(m_currentOutput);
|
QJsonObject status = parseBackupStatus(m_currentOutput);
|
||||||
@@ -438,7 +439,7 @@ void ServersBackupController::downloadBackup(const ServerCredentials &credential
|
|||||||
|
|
||||||
setProgress(25, tr("Starting file transfer..."));
|
setProgress(25, tr("Starting file transfer..."));
|
||||||
|
|
||||||
ErrorCode error = m_serverController->downloadFileFromHost(credentials, remotePath, actualLocalPath);
|
ErrorCode error = downloadFileFromHost(credentials, remotePath, actualLocalPath);
|
||||||
|
|
||||||
if (error == ErrorCode::NoError) {
|
if (error == ErrorCode::NoError) {
|
||||||
// qDebug() << "Backup downloaded to:" << actualLocalPath;
|
// qDebug() << "Backup downloaded to:" << actualLocalPath;
|
||||||
@@ -582,7 +583,7 @@ void ServersBackupController::uploadBackup(const ServerCredentials &credentials,
|
|||||||
|
|
||||||
setProgress(25, tr("Starting file transfer..."));
|
setProgress(25, tr("Starting file transfer..."));
|
||||||
|
|
||||||
ErrorCode error = m_serverController->uploadFileToHostPublic(credentials, actualLocalPath, remotePath,
|
ErrorCode error = uploadFileToHostPublic(credentials, actualLocalPath, remotePath,
|
||||||
libssh::ScpOverwriteMode::ScpOverwriteExisting);
|
libssh::ScpOverwriteMode::ScpOverwriteExisting);
|
||||||
|
|
||||||
qDebug() << "Upload result, error code:" << static_cast<int>(error);
|
qDebug() << "Upload result, error code:" << static_cast<int>(error);
|
||||||
@@ -763,7 +764,7 @@ void ServersBackupController::deleteBackup(const ServerCredentials &credentials,
|
|||||||
return handleStdErr(data, m_currentError);
|
return handleStdErr(data, m_currentError);
|
||||||
};
|
};
|
||||||
|
|
||||||
ErrorCode error = m_serverController->runHostScript(credentials, script, cbStdOut, cbStdErr);
|
ErrorCode error = runHostScript(credentials, script, cbStdOut, cbStdErr);
|
||||||
|
|
||||||
if (error == ErrorCode::NoError) {
|
if (error == ErrorCode::NoError) {
|
||||||
setStatus(Success);
|
setStatus(Success);
|
||||||
@@ -1482,3 +1483,36 @@ void ServersBackupController::trySetDefaultContainer()
|
|||||||
emit defaultServerAndContainerSet();
|
emit defaultServerAndContainerSet();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ErrorCode ServersBackupController::runHostScript(const ServerCredentials &credentials, const QString &script,
|
||||||
|
const std::function<ErrorCode(const QString &, libssh::Client &)> &cbStdOut,
|
||||||
|
const std::function<ErrorCode(const QString &, libssh::Client &)> &cbStdErr)
|
||||||
|
{
|
||||||
|
const QString fileName = "/tmp/amnezia_" + Utils::getRandomString(16) + ".sh";
|
||||||
|
ErrorCode e = m_sshSession.uploadFileToHost(credentials, script.toUtf8(), fileName);
|
||||||
|
if (e != ErrorCode::NoError)
|
||||||
|
return e;
|
||||||
|
|
||||||
|
e = m_sshSession.runScript(credentials, QString("sudo bash %1").arg(fileName), cbStdOut, cbStdErr);
|
||||||
|
m_sshSession.runScript(credentials, QString("sudo rm -f %1").arg(fileName));
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
|
||||||
|
ErrorCode ServersBackupController::downloadFileFromHost(const ServerCredentials &credentials, const QString &remotePath, const QString &localPath)
|
||||||
|
{
|
||||||
|
libssh::Client sshClient;
|
||||||
|
ErrorCode e = sshClient.connectToHost(credentials);
|
||||||
|
if (e != ErrorCode::NoError)
|
||||||
|
return e;
|
||||||
|
return sshClient.scpFileDownload(remotePath, localPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
ErrorCode ServersBackupController::uploadFileToHostPublic(const ServerCredentials &credentials, const QString &localPath, const QString &remotePath,
|
||||||
|
libssh::ScpOverwriteMode overwriteMode)
|
||||||
|
{
|
||||||
|
libssh::Client sshClient;
|
||||||
|
ErrorCode e = sshClient.connectToHost(credentials);
|
||||||
|
if (e != ErrorCode::NoError)
|
||||||
|
return e;
|
||||||
|
return sshClient.scpFileCopy(overwriteMode, localPath, remotePath, "backup_file");
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,10 +10,13 @@
|
|||||||
|
|
||||||
class QTemporaryFile;
|
class QTemporaryFile;
|
||||||
class ServersModel;
|
class ServersModel;
|
||||||
|
class SecureQSettings;
|
||||||
|
|
||||||
|
#include <QPointer>
|
||||||
|
|
||||||
#include "core/controllers/serverController.h"
|
|
||||||
#include "core/defs.h"
|
#include "core/defs.h"
|
||||||
#include "containers/containers_defs.h"
|
#include "containers/containers_defs.h"
|
||||||
|
#include "core/utils/selfhosted/sshSession.h"
|
||||||
|
|
||||||
using namespace amnezia;
|
using namespace amnezia;
|
||||||
|
|
||||||
@@ -31,7 +34,7 @@ class ServersBackupController : public QObject
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit ServersBackupController(std::shared_ptr<Settings> settings, ServersModel *serversModel, QObject *parent = nullptr);
|
explicit ServersBackupController(SecureQSettings *settings, ServersModel *serversModel, QObject *parent = nullptr);
|
||||||
~ServersBackupController();
|
~ServersBackupController();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -360,10 +363,17 @@ private:
|
|||||||
*/
|
*/
|
||||||
void trySetDefaultContainer();
|
void trySetDefaultContainer();
|
||||||
|
|
||||||
|
ErrorCode runHostScript(const ServerCredentials &credentials, const QString &script,
|
||||||
|
const std::function<ErrorCode(const QString &, libssh::Client &)> &cbStdOut = nullptr,
|
||||||
|
const std::function<ErrorCode(const QString &, libssh::Client &)> &cbStdErr = nullptr);
|
||||||
|
ErrorCode downloadFileFromHost(const ServerCredentials &credentials, const QString &remotePath, const QString &localPath);
|
||||||
|
ErrorCode uploadFileToHostPublic(const ServerCredentials &credentials, const QString &localPath, const QString &remotePath,
|
||||||
|
libssh::ScpOverwriteMode overwriteMode = libssh::ScpOverwriteMode::ScpOverwriteExisting);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<Settings> m_settings;
|
QPointer<SecureQSettings> m_settings;
|
||||||
ServersModel *m_serversModel;
|
ServersModel *m_serversModel;
|
||||||
ServerController *m_serverController;
|
SshSession m_sshSession;
|
||||||
BackupStatus m_status;
|
BackupStatus m_status;
|
||||||
QString m_backupDir;
|
QString m_backupDir;
|
||||||
QString m_currentOutput;
|
QString m_currentOutput;
|
||||||
|
|||||||
@@ -106,8 +106,11 @@
|
|||||||
<file>Pages2/PageSettingsKillSwitch.qml</file>
|
<file>Pages2/PageSettingsKillSwitch.qml</file>
|
||||||
<file>Pages2/PageSettingsKillSwitchExceptions.qml</file>
|
<file>Pages2/PageSettingsKillSwitchExceptions.qml</file>
|
||||||
<file>Pages2/PageSettingsLogging.qml</file>
|
<file>Pages2/PageSettingsLogging.qml</file>
|
||||||
|
<file>Pages2/PageSettingsServerBackup.qml</file>
|
||||||
|
<file>Pages2/PageSettingsServerBackupRestored.qml</file>
|
||||||
<file>Pages2/PageSettingsServerData.qml</file>
|
<file>Pages2/PageSettingsServerData.qml</file>
|
||||||
<file>Pages2/PageSettingsServerInfo.qml</file>
|
<file>Pages2/PageSettingsServerInfo.qml</file>
|
||||||
|
<file>Pages2/PageSettingsServerRestoreMode.qml</file>
|
||||||
<file>Pages2/PageSettingsServerProtocol.qml</file>
|
<file>Pages2/PageSettingsServerProtocol.qml</file>
|
||||||
<file>Pages2/PageSettingsServerProtocols.qml</file>
|
<file>Pages2/PageSettingsServerProtocols.qml</file>
|
||||||
<file>Pages2/PageSettingsServerServices.qml</file>
|
<file>Pages2/PageSettingsServerServices.qml</file>
|
||||||
|
|||||||
Reference in New Issue
Block a user