mirror of
https://github.com/amnezia-vpn/amnezia-client.git
synced 2026-06-23 02:00:20 +07:00
fixed SshSession parent
This commit is contained in:
@@ -103,7 +103,7 @@ ErrorCode InstallController::setupContainer(const ServerCredentials &credentials
|
|||||||
bool isUpdate)
|
bool isUpdate)
|
||||||
{
|
{
|
||||||
qDebug().noquote() << "InstallController::setupContainer" << ContainerUtils::containerToString(container);
|
qDebug().noquote() << "InstallController::setupContainer" << ContainerUtils::containerToString(container);
|
||||||
SshSession sshSession(this);
|
SshSession sshSession;
|
||||||
ErrorCode e = ErrorCode::NoError;
|
ErrorCode e = ErrorCode::NoError;
|
||||||
|
|
||||||
e = isUserInSudo(credentials, sshSession);
|
e = isUserInSudo(credentials, sshSession);
|
||||||
@@ -168,11 +168,11 @@ ErrorCode InstallController::updateServerConfig(const QString &serverId, DockerC
|
|||||||
}
|
}
|
||||||
if (container == DockerContainer::MtProxy) {
|
if (container == DockerContainer::MtProxy) {
|
||||||
ServerCredentials credentials = adminConfig->credentials();
|
ServerCredentials credentials = adminConfig->credentials();
|
||||||
SshSession sshSession(this);
|
SshSession sshSession;
|
||||||
MtProxyInstaller::uploadClientSettingsSnapshot(sshSession, credentials, container, newConfig);
|
MtProxyInstaller::uploadClientSettingsSnapshot(sshSession, credentials, container, newConfig);
|
||||||
} else if (container == DockerContainer::Telemt) {
|
} else if (container == DockerContainer::Telemt) {
|
||||||
ServerCredentials credentials = adminConfig->credentials();
|
ServerCredentials credentials = adminConfig->credentials();
|
||||||
SshSession sshSession(this);
|
SshSession sshSession;
|
||||||
TelemtInstaller::uploadClientSettingsSnapshot(sshSession, credentials, container, newConfig);
|
TelemtInstaller::uploadClientSettingsSnapshot(sshSession, credentials, container, newConfig);
|
||||||
}
|
}
|
||||||
adminConfig->updateContainerConfig(container, newConfig);
|
adminConfig->updateContainerConfig(container, newConfig);
|
||||||
@@ -188,7 +188,7 @@ ErrorCode InstallController::updateServerConfig(const QString &serverId, DockerC
|
|||||||
if (!credentials.isValid()) {
|
if (!credentials.isValid()) {
|
||||||
return ErrorCode::InternalError;
|
return ErrorCode::InternalError;
|
||||||
}
|
}
|
||||||
SshSession sshSession(this);
|
SshSession sshSession;
|
||||||
|
|
||||||
bool reinstallRequired = isReinstallContainerRequired(container, oldConfig, newConfig);
|
bool reinstallRequired = isReinstallContainerRequired(container, oldConfig, newConfig);
|
||||||
qDebug() << "InstallController::updateServerConfig for container" << container << "reinstall required is" << reinstallRequired;
|
qDebug() << "InstallController::updateServerConfig for container" << container << "reinstall required is" << reinstallRequired;
|
||||||
@@ -984,7 +984,7 @@ ErrorCode InstallController::rebootServer(const QString &serverId)
|
|||||||
if (!credentials.isValid()) {
|
if (!credentials.isValid()) {
|
||||||
return ErrorCode::InternalError;
|
return ErrorCode::InternalError;
|
||||||
}
|
}
|
||||||
SshSession sshSession(this);
|
SshSession sshSession;
|
||||||
|
|
||||||
QString script = QString("sudo reboot");
|
QString script = QString("sudo reboot");
|
||||||
|
|
||||||
@@ -1012,7 +1012,7 @@ ErrorCode InstallController::removeAllContainers(const QString &serverId)
|
|||||||
if (!credentials.isValid()) {
|
if (!credentials.isValid()) {
|
||||||
return ErrorCode::InternalError;
|
return ErrorCode::InternalError;
|
||||||
}
|
}
|
||||||
SshSession sshSession(this);
|
SshSession sshSession;
|
||||||
ErrorCode errorCode = sshSession.runScript(credentials, amnezia::scriptData(SharedScriptType::remove_all_containers));
|
ErrorCode errorCode = sshSession.runScript(credentials, amnezia::scriptData(SharedScriptType::remove_all_containers));
|
||||||
|
|
||||||
if (errorCode == ErrorCode::NoError) {
|
if (errorCode == ErrorCode::NoError) {
|
||||||
@@ -1034,7 +1034,7 @@ ErrorCode InstallController::removeContainer(const QString &serverId, DockerCont
|
|||||||
if (!credentials.isValid()) {
|
if (!credentials.isValid()) {
|
||||||
return ErrorCode::InternalError;
|
return ErrorCode::InternalError;
|
||||||
}
|
}
|
||||||
SshSession sshSession(this);
|
SshSession sshSession;
|
||||||
const amnezia::ScriptVars removeContainerVars =
|
const amnezia::ScriptVars removeContainerVars =
|
||||||
amnezia::genBaseVars(credentials, container, QString(), QString());
|
amnezia::genBaseVars(credentials, container, QString(), QString());
|
||||||
const bool removeDataVolume = (container == DockerContainer::MtProxy || container == DockerContainer::Telemt);
|
const bool removeDataVolume = (container == DockerContainer::MtProxy || container == DockerContainer::Telemt);
|
||||||
@@ -1143,7 +1143,7 @@ ErrorCode InstallController::scanServerForInstalledContainers(const QString &ser
|
|||||||
if (!credentials.isValid()) {
|
if (!credentials.isValid()) {
|
||||||
return ErrorCode::InternalError;
|
return ErrorCode::InternalError;
|
||||||
}
|
}
|
||||||
SshSession sshSession(this);
|
SshSession sshSession;
|
||||||
|
|
||||||
QMap<DockerContainer, ContainerConfig> installedContainers;
|
QMap<DockerContainer, ContainerConfig> installedContainers;
|
||||||
ErrorCode errorCode = getAlreadyInstalledContainers(credentials, installedContainers, sshSession);
|
ErrorCode errorCode = getAlreadyInstalledContainers(credentials, installedContainers, sshSession);
|
||||||
@@ -1186,7 +1186,7 @@ ErrorCode InstallController::scanServerForInstalledContainers(const QString &ser
|
|||||||
ErrorCode InstallController::installServer(const ServerCredentials &credentials, DockerContainer container, int port,
|
ErrorCode InstallController::installServer(const ServerCredentials &credentials, DockerContainer container, int port,
|
||||||
TransportProto transportProto, bool &wasContainerInstalled)
|
TransportProto transportProto, bool &wasContainerInstalled)
|
||||||
{
|
{
|
||||||
SshSession sshSession(this);
|
SshSession sshSession;
|
||||||
QMap<DockerContainer, ContainerConfig> installedContainers;
|
QMap<DockerContainer, ContainerConfig> installedContainers;
|
||||||
ErrorCode errorCode = getAlreadyInstalledContainers(credentials, installedContainers, sshSession);
|
ErrorCode errorCode = getAlreadyInstalledContainers(credentials, installedContainers, sshSession);
|
||||||
if (errorCode) {
|
if (errorCode) {
|
||||||
@@ -1255,7 +1255,7 @@ ErrorCode InstallController::installContainer(const QString &serverId, DockerCon
|
|||||||
if (!credentials.isValid()) {
|
if (!credentials.isValid()) {
|
||||||
return ErrorCode::InternalError;
|
return ErrorCode::InternalError;
|
||||||
}
|
}
|
||||||
SshSession sshSession(this);
|
SshSession sshSession;
|
||||||
|
|
||||||
QMap<DockerContainer, ContainerConfig> installedContainers;
|
QMap<DockerContainer, ContainerConfig> installedContainers;
|
||||||
ErrorCode errorCode = getAlreadyInstalledContainers(credentials, installedContainers, sshSession);
|
ErrorCode errorCode = getAlreadyInstalledContainers(credentials, installedContainers, sshSession);
|
||||||
@@ -1297,7 +1297,7 @@ ErrorCode InstallController::installContainer(const QString &serverId, DockerCon
|
|||||||
ErrorCode InstallController::checkSshConnection(ServerCredentials &credentials, QString &output,
|
ErrorCode InstallController::checkSshConnection(ServerCredentials &credentials, QString &output,
|
||||||
std::function<QString()> passphraseCallback)
|
std::function<QString()> passphraseCallback)
|
||||||
{
|
{
|
||||||
SshSession sshSession(this);
|
SshSession sshSession;
|
||||||
ErrorCode errorCode = ErrorCode::NoError;
|
ErrorCode errorCode = ErrorCode::NoError;
|
||||||
|
|
||||||
if (credentials.secretData.contains("BEGIN") && credentials.secretData.contains("PRIVATE KEY")) {
|
if (credentials.secretData.contains("BEGIN") && credentials.secretData.contains("PRIVATE KEY")) {
|
||||||
@@ -1578,7 +1578,7 @@ ErrorCode InstallController::setDockerContainerEnabledState(const QString &serve
|
|||||||
return ErrorCode::InternalError;
|
return ErrorCode::InternalError;
|
||||||
}
|
}
|
||||||
const QString containerName = ContainerUtils::containerToString(container);
|
const QString containerName = ContainerUtils::containerToString(container);
|
||||||
SshSession sshSession(this);
|
SshSession sshSession;
|
||||||
const QString script = enabled ? QStringLiteral("sudo docker start %1").arg(containerName)
|
const QString script = enabled ? QStringLiteral("sudo docker start %1").arg(containerName)
|
||||||
: QStringLiteral("sudo docker stop %1").arg(containerName);
|
: QStringLiteral("sudo docker stop %1").arg(containerName);
|
||||||
const ErrorCode runError = sshSession.runScript(credentials, script);
|
const ErrorCode runError = sshSession.runScript(credentials, script);
|
||||||
@@ -1618,7 +1618,7 @@ ErrorCode InstallController::queryDockerContainerStatus(const QString &serverId,
|
|||||||
stdOut += data;
|
stdOut += data;
|
||||||
return ErrorCode::NoError;
|
return ErrorCode::NoError;
|
||||||
};
|
};
|
||||||
SshSession sshSession(this);
|
SshSession sshSession;
|
||||||
const QString script = QStringLiteral(
|
const QString script = QStringLiteral(
|
||||||
"sudo docker inspect --format '{{.State.Status}}' %1 2>/dev/null || echo 'not_found'")
|
"sudo docker inspect --format '{{.State.Status}}' %1 2>/dev/null || echo 'not_found'")
|
||||||
.arg(containerName);
|
.arg(containerName);
|
||||||
@@ -1652,7 +1652,7 @@ ErrorCode InstallController::queryMtProxyDiagnostics(const QString &serverId, Do
|
|||||||
if (!credentials.isValid()) {
|
if (!credentials.isValid()) {
|
||||||
return ErrorCode::InternalError;
|
return ErrorCode::InternalError;
|
||||||
}
|
}
|
||||||
SshSession sshSession(this);
|
SshSession sshSession;
|
||||||
return MtProxyInstaller::queryDiagnostics(sshSession, credentials, container, listenPort, out);
|
return MtProxyInstaller::queryDiagnostics(sshSession, credentials, container, listenPort, out);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1675,7 +1675,7 @@ QString InstallController::fetchDockerContainerSecret(const QString &serverId, D
|
|||||||
stdOut += data;
|
stdOut += data;
|
||||||
return ErrorCode::NoError;
|
return ErrorCode::NoError;
|
||||||
};
|
};
|
||||||
SshSession sshSession(this);
|
SshSession sshSession;
|
||||||
const QString path = QStringLiteral("/data/secret");
|
const QString path = QStringLiteral("/data/secret");
|
||||||
const QString cmd = QStringLiteral("sudo docker exec %1 cat %2").arg(containerName, path);
|
const QString cmd = QStringLiteral("sudo docker exec %1 cat %2").arg(containerName, path);
|
||||||
const ErrorCode errorCode = sshSession.runScript(credentials, cmd, cbReadStdOut);
|
const ErrorCode errorCode = sshSession.runScript(credentials, cmd, cbReadStdOut);
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
#include <QStandardPaths>
|
#include <QStandardPaths>
|
||||||
#include <QFutureWatcher>
|
#include <QFutureWatcher>
|
||||||
#include <QtConcurrent>
|
#include <QtConcurrent>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
#include "core/utils/api/apiUtils.h"
|
#include "core/utils/api/apiUtils.h"
|
||||||
#include "core/controllers/selfhosted/installController.h"
|
#include "core/controllers/selfhosted/installController.h"
|
||||||
@@ -359,17 +360,27 @@ void InstallUiController::setContainerEnabled(const QString &serverId, int conta
|
|||||||
}
|
}
|
||||||
|
|
||||||
emit serverIsBusy(true);
|
emit serverIsBusy(true);
|
||||||
const ErrorCode errorCode = m_installController->setDockerContainerEnabledState(serverId, container, enabled);
|
|
||||||
emit serverIsBusy(false);
|
|
||||||
|
|
||||||
if (errorCode == ErrorCode::NoError) {
|
InstallController *installController = m_installController;
|
||||||
const ContainerConfig currentConfig = m_serversController->getContainerConfig(serverId, container);
|
auto *watcher = new QFutureWatcher<ErrorCode>(this);
|
||||||
m_protocolModel->updateModel(currentConfig);
|
QObject::connect(watcher, &QFutureWatcher<ErrorCode>::finished, this,
|
||||||
emit setContainerEnabledFinished(enabled);
|
[this, watcher, serverId, container, enabled]() {
|
||||||
return;
|
const ErrorCode errorCode = watcher->result();
|
||||||
}
|
watcher->deleteLater();
|
||||||
|
emit serverIsBusy(false);
|
||||||
|
|
||||||
emit installationErrorOccurred(errorCode);
|
if (errorCode == ErrorCode::NoError) {
|
||||||
|
const ContainerConfig currentConfig = m_serversController->getContainerConfig(serverId, container);
|
||||||
|
m_protocolModel->updateModel(currentConfig);
|
||||||
|
emit setContainerEnabledFinished(enabled);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
emit installationErrorOccurred(errorCode);
|
||||||
|
});
|
||||||
|
QFuture<ErrorCode> future = QtConcurrent::run([installController, serverId, container, enabled]() -> ErrorCode {
|
||||||
|
return installController->setDockerContainerEnabledState(serverId, container, enabled);
|
||||||
|
});
|
||||||
|
watcher->setFuture(future);
|
||||||
}
|
}
|
||||||
|
|
||||||
void InstallUiController::refreshContainerStatus(const QString &serverId, int containerIndex)
|
void InstallUiController::refreshContainerStatus(const QString &serverId, int containerIndex)
|
||||||
@@ -379,13 +390,22 @@ void InstallUiController::refreshContainerStatus(const QString &serverId, int co
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int status = 3;
|
InstallController *installController = m_installController;
|
||||||
const ErrorCode errorCode = m_installController->queryDockerContainerStatus(serverId, container, status);
|
auto *watcher = new QFutureWatcher<int>(this);
|
||||||
if (errorCode != ErrorCode::NoError) {
|
QObject::connect(watcher, &QFutureWatcher<int>::finished, this, [this, watcher]() {
|
||||||
emit containerStatusRefreshed(3);
|
const int status = watcher->result();
|
||||||
return;
|
watcher->deleteLater();
|
||||||
}
|
emit containerStatusRefreshed(status);
|
||||||
emit containerStatusRefreshed(status);
|
});
|
||||||
|
QFuture<int> future = QtConcurrent::run([installController, serverId, container]() -> int {
|
||||||
|
int status = 3;
|
||||||
|
const ErrorCode errorCode = installController->queryDockerContainerStatus(serverId, container, status);
|
||||||
|
if (errorCode != ErrorCode::NoError) {
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
});
|
||||||
|
watcher->setFuture(future);
|
||||||
}
|
}
|
||||||
|
|
||||||
void InstallUiController::refreshContainerDiagnostics(const QString &serverId, int containerIndex, int port)
|
void InstallUiController::refreshContainerDiagnostics(const QString &serverId, int containerIndex, int port)
|
||||||
@@ -395,14 +415,27 @@ void InstallUiController::refreshContainerDiagnostics(const QString &serverId, i
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
MtProxyContainerDiagnostics diag;
|
using DiagResult = std::pair<bool, MtProxyContainerDiagnostics>;
|
||||||
const ErrorCode errorCode = m_installController->queryMtProxyDiagnostics(serverId, container, port, diag);
|
InstallController *installController = m_installController;
|
||||||
if (errorCode != ErrorCode::NoError) {
|
auto *watcher = new QFutureWatcher<DiagResult>(this);
|
||||||
emit containerDiagnosticsRefreshed(false, false, -1, QString(), QString());
|
QObject::connect(watcher, &QFutureWatcher<DiagResult>::finished, this, [this, watcher]() {
|
||||||
return;
|
const DiagResult result = watcher->result();
|
||||||
}
|
watcher->deleteLater();
|
||||||
emit containerDiagnosticsRefreshed(diag.portReachable, diag.upstreamReachable, diag.clientsConnected,
|
if (!result.first) {
|
||||||
diag.lastConfigRefresh, diag.statsEndpoint);
|
emit containerDiagnosticsRefreshed(false, false, -1, QString(), QString());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const MtProxyContainerDiagnostics &diag = result.second;
|
||||||
|
emit containerDiagnosticsRefreshed(diag.portReachable, diag.upstreamReachable, diag.clientsConnected,
|
||||||
|
diag.lastConfigRefresh, diag.statsEndpoint);
|
||||||
|
});
|
||||||
|
QFuture<DiagResult> future =
|
||||||
|
QtConcurrent::run([installController, serverId, container, port]() -> DiagResult {
|
||||||
|
MtProxyContainerDiagnostics diag;
|
||||||
|
const ErrorCode errorCode = installController->queryMtProxyDiagnostics(serverId, container, port, diag);
|
||||||
|
return { errorCode == ErrorCode::NoError, diag };
|
||||||
|
});
|
||||||
|
watcher->setFuture(future);
|
||||||
}
|
}
|
||||||
|
|
||||||
void InstallUiController::fetchContainerSecret(const QString &serverId, int containerIndex)
|
void InstallUiController::fetchContainerSecret(const QString &serverId, int containerIndex)
|
||||||
@@ -412,8 +445,17 @@ void InstallUiController::fetchContainerSecret(const QString &serverId, int cont
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const QString secret = m_installController->fetchDockerContainerSecret(serverId, container);
|
InstallController *installController = m_installController;
|
||||||
emit containerSecretFetched(secret);
|
auto *watcher = new QFutureWatcher<QString>(this);
|
||||||
|
QObject::connect(watcher, &QFutureWatcher<QString>::finished, this, [this, watcher]() {
|
||||||
|
const QString secret = watcher->result();
|
||||||
|
watcher->deleteLater();
|
||||||
|
emit containerSecretFetched(secret);
|
||||||
|
});
|
||||||
|
QFuture<QString> future = QtConcurrent::run([installController, serverId, container]() -> QString {
|
||||||
|
return installController->fetchDockerContainerSecret(serverId, container);
|
||||||
|
});
|
||||||
|
watcher->setFuture(future);
|
||||||
}
|
}
|
||||||
|
|
||||||
void InstallUiController::rebootServer(const QString &serverId)
|
void InstallUiController::rebootServer(const QString &serverId)
|
||||||
|
|||||||
Reference in New Issue
Block a user