moved getClientsList and setClientsList from serverController

This commit is contained in:
vladimir.kuznetsov 2023-01-17 18:41:36 +03:00
parent 3a210c5bab
commit f6e8346841
6 changed files with 118 additions and 109 deletions

View file

@ -796,104 +796,6 @@ SshConnection *ServerController::connectToHost(const SshConnectionParameters &ss
return client; 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<QSsh::SshRemoteProcess> 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) void ServerController::disconnectFromHost(const ServerCredentials &credentials)
{ {
SshConnection *client = acquireConnection(sshParams(credentials)); SshConnection *client = acquireConnection(sshParams(credentials));

View file

@ -73,9 +73,6 @@ public:
QString checkSshConnection(const ServerCredentials &credentials, ErrorCode *errorCode = nullptr); QString checkSshConnection(const ServerCredentials &credentials, ErrorCode *errorCode = nullptr);
QSsh::SshConnection *connectToHost(const QSsh::SshConnectionParameters &sshParams); 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()); ErrorCode startupContainerWorker(const ServerCredentials &credentials, DockerContainer container, const QJsonObject &config = QJsonObject());
private: private:

View file

@ -69,7 +69,7 @@ void ClientInfoLogic::onLineEditNameAliasEditingFinished()
if (!protocols.empty()) { if (!protocols.empty()) {
const Proto currentMainProtocol = protocols.front(); const Proto currentMainProtocol = protocols.front();
const QJsonObject clientsTable = model->getContent(currentMainProtocol); const QJsonObject clientsTable = model->getContent(currentMainProtocol);
ErrorCode error = m_serverController->setClientsList(credentials, ErrorCode error = setClientsList(credentials,
selectedContainer, selectedContainer,
currentMainProtocol, currentMainProtocol,
clientsTable); clientsTable);
@ -105,7 +105,7 @@ void ClientInfoLogic::onRevokeOpenVpnCertificateClicked()
model->removeRows(m_currentClientIndex); model->removeRows(m_currentClientIndex);
const QJsonObject clientsTable = model->getContent(Proto::OpenVpn); 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)) { if (isErrorOccured(error)) {
set_busyIndicatorIsRunning(false); set_busyIndicatorIsRunning(false);
return; return;
@ -164,7 +164,7 @@ void ClientInfoLogic::onRevokeWireGuardKeyClicked()
model->removeRows(m_currentClientIndex); model->removeRows(m_currentClientIndex);
const QJsonObject clientsTable = model->getContent(Proto::WireGuard); 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)) { if (isErrorOccured(error)) {
set_busyIndicatorIsRunning(false); set_busyIndicatorIsRunning(false);
return; return;
@ -180,3 +180,11 @@ void ClientInfoLogic::onRevokeWireGuardKeyClicked()
m_serverController->disconnectFromHost(credentials); m_serverController->disconnectFromHost(credentials);
set_busyIndicatorIsRunning(false); 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;
}

View file

@ -3,6 +3,10 @@
#include "PageLogicBase.h" #include "PageLogicBase.h"
#include "core/defs.h"
#include "containers/containers_defs.h"
#include "protocols/protocols_defs.h"
class UiLogic; class UiLogic;
class ClientInfoLogic : public PageLogicBase class ClientInfoLogic : public PageLogicBase
@ -29,6 +33,8 @@ public slots:
void onRevokeWireGuardKeyClicked(); void onRevokeWireGuardKeyClicked();
private: private:
ErrorCode setClientsList(const ServerCredentials &credentials, DockerContainer container, Proto mainProtocol, const QJsonObject &clietns);
int m_currentClientIndex; int m_currentClientIndex;
}; };

View file

@ -30,7 +30,7 @@ void ClientManagementLogic::onUpdatePage()
if (!protocols.empty()) { if (!protocols.empty()) {
m_currentMainProtocol = protocols.front(); m_currentMainProtocol = protocols.front();
ErrorCode error = m_serverController->getClientsList(m_settings->serverCredentials(uiLogic()->selectedServerIndex), ErrorCode error = getClientsList(m_settings->serverCredentials(uiLogic()->selectedServerIndex),
selectedContainer, m_currentMainProtocol, clients); selectedContainer, m_currentMainProtocol, clients);
if (error != ErrorCode::NoError) { if (error != ErrorCode::NoError) {
QMessageBox::warning(nullptr, APPLICATION_NAME, QMessageBox::warning(nullptr, APPLICATION_NAME,
@ -53,3 +53,95 @@ void ClientManagementLogic::onClientItemClicked(int index)
uiLogic()->pageLogic<ClientInfoLogic>()->setCurrentClientId(index); uiLogic()->pageLogic<ClientInfoLogic>()->setCurrentClientId(index);
emit uiLogic()->goToClientInfoPage(m_currentMainProtocol); 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<QSsh::SshRemoteProcess> 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;
}

View file

@ -3,6 +3,8 @@
#include "PageLogicBase.h" #include "PageLogicBase.h"
#include "core/defs.h"
#include "containers/containers_defs.h"
#include "protocols/protocols_defs.h" #include "protocols/protocols_defs.h"
class UiLogic; class UiLogic;
@ -23,6 +25,8 @@ public slots:
void onClientItemClicked(int index); void onClientItemClicked(int index);
private: private:
ErrorCode getClientsList(const ServerCredentials &credentials, DockerContainer container, Proto mainProtocol, QJsonObject &clietns);
amnezia::Proto m_currentMainProtocol; amnezia::Proto m_currentMainProtocol;
}; };