diff --git a/client/core/servercontroller.cpp b/client/core/servercontroller.cpp index e14032c7..c7336b62 100644 --- a/client/core/servercontroller.cpp +++ b/client/core/servercontroller.cpp @@ -796,104 +796,6 @@ SshConnection *ServerController::connectToHost(const SshConnectionParameters &ss return client; } -ErrorCode ServerController::getClientsList(const ServerCredentials &credentials, DockerContainer container, Proto mainProtocol, QJsonObject &clietns) -{ - ErrorCode error = ErrorCode::NoError; - QString stdOut; - auto cbReadStdOut = [&](const QString &data, QSharedPointer proc) { - stdOut += data + "\n"; - }; - - const QString mainProtocolString = ProtocolProps::protoToString(mainProtocol); - - const QString clientsTableFile = QString("/opt/amnezia/%1/clientsTable").arg(mainProtocolString); - const QByteArray clientsTableString = getTextFileFromContainer(container, credentials, clientsTableFile, &error); - if (error != ErrorCode::NoError) { - return error; - } - QJsonObject clientsTable = QJsonDocument::fromJson(clientsTableString).object(); - int count = 0; - - if (mainProtocol == Proto::OpenVpn) { - const QString getOpenVpnClientsList = "sudo docker exec -i $CONTAINER_NAME bash -c 'ls /opt/amnezia/openvpn/pki/issued'"; - error = runScript(credentials, replaceVars(getOpenVpnClientsList, genVarsForScript(credentials, container)), cbReadStdOut); - if (error != ErrorCode::NoError) { - return error; - } - - if (!stdOut.isEmpty()) { - QStringList certsIds = stdOut.split("\n", Qt::SkipEmptyParts); - certsIds.removeAll("AmneziaReq.crt"); - - for (auto &openvpnCertId : certsIds) { - openvpnCertId.replace(".crt", ""); - if (!clientsTable.contains(openvpnCertId)) { - stdOut.clear(); - const QString getOpenVpnCertData = QString("sudo docker exec -i $CONTAINER_NAME bash -c 'cat /opt/amnezia/openvpn/pki/issued/%1.crt'") - .arg(openvpnCertId); - error = runScript(credentials, replaceVars(getOpenVpnCertData, genVarsForScript(credentials, container)), cbReadStdOut); - if (error != ErrorCode::NoError) { - return error; - } - - QJsonObject client; - client["openvpnCertId"] = openvpnCertId; - client["clientName"] = QString("Client %1").arg(count); - client["openvpnCertData"] = stdOut; - clientsTable[openvpnCertId] = client; - count++; - } - } - } - } else if (mainProtocol == Proto::WireGuard) { - const QString wireGuardConfigFile = "opt/amnezia/wireguard/wg0.conf"; - const QString wireguardConfigString = getTextFileFromContainer(container, credentials, wireGuardConfigFile, &error); - if (error != ErrorCode::NoError) { - return error; - } - - auto configLines = wireguardConfigString.split("\n", Qt::SkipEmptyParts); - QStringList wireguardKeys; - for (const auto &line : configLines) { - auto configPair = line.split(" = ", Qt::SkipEmptyParts); - if (configPair.front() == "PublicKey") { - wireguardKeys.push_back(configPair.back()); - } - } - - for (auto &wireguardKey : wireguardKeys) { - if (!clientsTable.contains(wireguardKey)) { - QJsonObject client; - client["clientName"] = QString("Client %1").arg(count); - client["wireguardPublicKey"] = wireguardKey; - clientsTable[wireguardKey] = client; - count++; - } - } - } - - const QByteArray newClientsTableString = QJsonDocument(clientsTable).toJson(); - if (clientsTableString != newClientsTableString) { - error = uploadTextFileToContainer(container, credentials, newClientsTableString, clientsTableFile); - } - - if (error != ErrorCode::NoError) { - return error; - } - - clietns = clientsTable; - - return error; -} - -ErrorCode ServerController::setClientsList(const ServerCredentials &credentials, DockerContainer container, Proto mainProtocol, const QJsonObject &clietns) -{ - const QString mainProtocolString = ProtocolProps::protoToString(mainProtocol); - const QString clientsTableFile = QString("opt/amnezia/%1/clientsTable").arg(mainProtocolString); - ErrorCode error = uploadTextFileToContainer(container, credentials, QJsonDocument(clietns).toJson(), clientsTableFile); - return error; -} - void ServerController::disconnectFromHost(const ServerCredentials &credentials) { SshConnection *client = acquireConnection(sshParams(credentials)); diff --git a/client/core/servercontroller.h b/client/core/servercontroller.h index d85520d0..c7abaff9 100644 --- a/client/core/servercontroller.h +++ b/client/core/servercontroller.h @@ -73,9 +73,6 @@ public: QString checkSshConnection(const ServerCredentials &credentials, ErrorCode *errorCode = nullptr); QSsh::SshConnection *connectToHost(const QSsh::SshConnectionParameters &sshParams); - ErrorCode getClientsList(const ServerCredentials &credentials, DockerContainer container, Proto mainProtocol, QJsonObject &clietns); - ErrorCode setClientsList(const ServerCredentials &credentials, DockerContainer container, Proto mainProtocol, const QJsonObject &clietns); - ErrorCode startupContainerWorker(const ServerCredentials &credentials, DockerContainer container, const QJsonObject &config = QJsonObject()); private: diff --git a/client/ui/pages_logic/ClientInfoLogic.cpp b/client/ui/pages_logic/ClientInfoLogic.cpp index 7183e239..fb521eec 100644 --- a/client/ui/pages_logic/ClientInfoLogic.cpp +++ b/client/ui/pages_logic/ClientInfoLogic.cpp @@ -69,10 +69,10 @@ void ClientInfoLogic::onLineEditNameAliasEditingFinished() if (!protocols.empty()) { const Proto currentMainProtocol = protocols.front(); const QJsonObject clientsTable = model->getContent(currentMainProtocol); - ErrorCode error = m_serverController->setClientsList(credentials, - selectedContainer, - currentMainProtocol, - clientsTable); + ErrorCode error = setClientsList(credentials, + selectedContainer, + currentMainProtocol, + clientsTable); isErrorOccured(error); } @@ -105,7 +105,7 @@ void ClientInfoLogic::onRevokeOpenVpnCertificateClicked() model->removeRows(m_currentClientIndex); const QJsonObject clientsTable = model->getContent(Proto::OpenVpn); - error = m_serverController->setClientsList(credentials, container, Proto::OpenVpn, clientsTable); + error = setClientsList(credentials, container, Proto::OpenVpn, clientsTable); if (isErrorOccured(error)) { set_busyIndicatorIsRunning(false); return; @@ -164,7 +164,7 @@ void ClientInfoLogic::onRevokeWireGuardKeyClicked() model->removeRows(m_currentClientIndex); const QJsonObject clientsTable = model->getContent(Proto::WireGuard); - error = m_serverController->setClientsList(credentials, container, Proto::WireGuard, clientsTable); + error = setClientsList(credentials, container, Proto::WireGuard, clientsTable); if (isErrorOccured(error)) { set_busyIndicatorIsRunning(false); return; @@ -180,3 +180,11 @@ void ClientInfoLogic::onRevokeWireGuardKeyClicked() m_serverController->disconnectFromHost(credentials); set_busyIndicatorIsRunning(false); } + +ErrorCode ClientInfoLogic::setClientsList(const ServerCredentials &credentials, DockerContainer container, Proto mainProtocol, const QJsonObject &clietns) +{ + const QString mainProtocolString = ProtocolProps::protoToString(mainProtocol); + const QString clientsTableFile = QString("opt/amnezia/%1/clientsTable").arg(mainProtocolString); + ErrorCode error = m_serverController->uploadTextFileToContainer(container, credentials, QJsonDocument(clietns).toJson(), clientsTableFile); + return error; +} diff --git a/client/ui/pages_logic/ClientInfoLogic.h b/client/ui/pages_logic/ClientInfoLogic.h index 0e6b537f..177000f2 100644 --- a/client/ui/pages_logic/ClientInfoLogic.h +++ b/client/ui/pages_logic/ClientInfoLogic.h @@ -3,6 +3,10 @@ #include "PageLogicBase.h" +#include "core/defs.h" +#include "containers/containers_defs.h" +#include "protocols/protocols_defs.h" + class UiLogic; class ClientInfoLogic : public PageLogicBase @@ -29,6 +33,8 @@ public slots: void onRevokeWireGuardKeyClicked(); private: + ErrorCode setClientsList(const ServerCredentials &credentials, DockerContainer container, Proto mainProtocol, const QJsonObject &clietns); + int m_currentClientIndex; }; diff --git a/client/ui/pages_logic/ClientManagementLogic.cpp b/client/ui/pages_logic/ClientManagementLogic.cpp index 5bd5948d..ad4dedb1 100644 --- a/client/ui/pages_logic/ClientManagementLogic.cpp +++ b/client/ui/pages_logic/ClientManagementLogic.cpp @@ -30,8 +30,8 @@ void ClientManagementLogic::onUpdatePage() if (!protocols.empty()) { m_currentMainProtocol = protocols.front(); - ErrorCode error = m_serverController->getClientsList(m_settings->serverCredentials(uiLogic()->selectedServerIndex), - selectedContainer, m_currentMainProtocol, clients); + ErrorCode error = getClientsList(m_settings->serverCredentials(uiLogic()->selectedServerIndex), + selectedContainer, m_currentMainProtocol, clients); if (error != ErrorCode::NoError) { QMessageBox::warning(nullptr, APPLICATION_NAME, tr("An error occurred while getting the list of clients.") + "\n" + errorString(error)); @@ -53,3 +53,95 @@ void ClientManagementLogic::onClientItemClicked(int index) uiLogic()->pageLogic()->setCurrentClientId(index); emit uiLogic()->goToClientInfoPage(m_currentMainProtocol); } + +ErrorCode ClientManagementLogic::getClientsList(const ServerCredentials &credentials, DockerContainer container, Proto mainProtocol, QJsonObject &clietns) +{ + ErrorCode error = ErrorCode::NoError; + QString stdOut; + auto cbReadStdOut = [&](const QString &data, QSharedPointer proc) { + stdOut += data + "\n"; + }; + + const QString mainProtocolString = ProtocolProps::protoToString(mainProtocol); + + const QString clientsTableFile = QString("/opt/amnezia/%1/clientsTable").arg(mainProtocolString); + const QByteArray clientsTableString = m_serverController->getTextFileFromContainer(container, credentials, clientsTableFile, &error); + if (error != ErrorCode::NoError) { + return error; + } + QJsonObject clientsTable = QJsonDocument::fromJson(clientsTableString).object(); + int count = 0; + + if (mainProtocol == Proto::OpenVpn) { + const QString getOpenVpnClientsList = "sudo docker exec -i $CONTAINER_NAME bash -c 'ls /opt/amnezia/openvpn/pki/issued'"; + QString script = m_serverController->replaceVars(getOpenVpnClientsList, m_serverController->genVarsForScript(credentials, container)); + error = m_serverController->runScript(credentials, script, cbReadStdOut); + if (error != ErrorCode::NoError) { + return error; + } + + if (!stdOut.isEmpty()) { + QStringList certsIds = stdOut.split("\n", Qt::SkipEmptyParts); + certsIds.removeAll("AmneziaReq.crt"); + + for (auto &openvpnCertId : certsIds) { + openvpnCertId.replace(".crt", ""); + if (!clientsTable.contains(openvpnCertId)) { + stdOut.clear(); + const QString getOpenVpnCertData = QString("sudo docker exec -i $CONTAINER_NAME bash -c 'cat /opt/amnezia/openvpn/pki/issued/%1.crt'") + .arg(openvpnCertId); + script = m_serverController->replaceVars(getOpenVpnCertData, m_serverController->genVarsForScript(credentials, container)); + error = m_serverController->runScript(credentials, script, cbReadStdOut); + if (error != ErrorCode::NoError) { + return error; + } + + QJsonObject client; + client["openvpnCertId"] = openvpnCertId; + client["clientName"] = QString("Client %1").arg(count); + client["openvpnCertData"] = stdOut; + clientsTable[openvpnCertId] = client; + count++; + } + } + } + } else if (mainProtocol == Proto::WireGuard) { + const QString wireGuardConfigFile = "opt/amnezia/wireguard/wg0.conf"; + const QString wireguardConfigString = m_serverController->getTextFileFromContainer(container, credentials, wireGuardConfigFile, &error); + if (error != ErrorCode::NoError) { + return error; + } + + auto configLines = wireguardConfigString.split("\n", Qt::SkipEmptyParts); + QStringList wireguardKeys; + for (const auto &line : configLines) { + auto configPair = line.split(" = ", Qt::SkipEmptyParts); + if (configPair.front() == "PublicKey") { + wireguardKeys.push_back(configPair.back()); + } + } + + for (auto &wireguardKey : wireguardKeys) { + if (!clientsTable.contains(wireguardKey)) { + QJsonObject client; + client["clientName"] = QString("Client %1").arg(count); + client["wireguardPublicKey"] = wireguardKey; + clientsTable[wireguardKey] = client; + count++; + } + } + } + + const QByteArray newClientsTableString = QJsonDocument(clientsTable).toJson(); + if (clientsTableString != newClientsTableString) { + error = m_serverController->uploadTextFileToContainer(container, credentials, newClientsTableString, clientsTableFile); + } + + if (error != ErrorCode::NoError) { + return error; + } + + clietns = clientsTable; + + return error; +} diff --git a/client/ui/pages_logic/ClientManagementLogic.h b/client/ui/pages_logic/ClientManagementLogic.h index 82a323ea..9c181716 100644 --- a/client/ui/pages_logic/ClientManagementLogic.h +++ b/client/ui/pages_logic/ClientManagementLogic.h @@ -3,6 +3,8 @@ #include "PageLogicBase.h" +#include "core/defs.h" +#include "containers/containers_defs.h" #include "protocols/protocols_defs.h" class UiLogic; @@ -23,6 +25,8 @@ public slots: void onClientItemClicked(int index); private: + ErrorCode getClientsList(const ServerCredentials &credentials, DockerContainer container, Proto mainProtocol, QJsonObject &clietns); + amnezia::Proto m_currentMainProtocol; };