Refactoring

This commit is contained in:
pokamest 2021-01-06 17:12:24 +03:00
parent 13f9764853
commit 5eede71667
21 changed files with 566 additions and 220 deletions

56
client/core/defs.h Normal file
View file

@ -0,0 +1,56 @@
#ifndef DEFS_H
#define DEFS_H
#include <QObject>
namespace amnezia {
enum class Protocol {
Any,
OpenVpn,
ShadowSocks,
WireGuard
};
struct ServerCredentials
{
QString hostName;
QString userName;
QString password;
int port = 22;
};
enum ErrorCode
{
// General error codes
NoError = 0,
UnknownError,
InternalError,
NotImplementedError,
// Server errorz
ServerCheckFailed,
// Ssh connection errors
SshSocketError, SshTimeoutError, SshProtocolError,
SshHostKeyError, SshKeyFileError, SshAuthenticationError,
SshClosedByServerError, SshInternalError,
// Ssh remote process errors
SshRemoteProcessCreationError,
FailedToStartRemoteProcessError, RemoteProcessCrashError,
// Local errors
FailedToSaveConfigData,
OpenVpnConfigMissing,
OpenVpnManagementServerError,
// Distro errors
AmneziaServiceConnectionFailed,
OpenVpnExecutableMissing,
EasyRsaExecutableMissing
};
} // namespace amnezia
#endif // DEFS_H

View file

@ -0,0 +1,47 @@
#ifndef ERRORSTRINGS_H
#define ERRORSTRINGS_H
#include "defs.h"
using namespace amnezia;
QString errorString(ErrorCode code){
switch (code) {
// General error codes
case(NoError): return QObject::tr("No error");
case(UnknownError): return QObject::tr("Unknown Error");
case(NotImplementedError): return QObject::tr("Function not implemented");
case(ServerCheckFailed): return QObject::tr("Server check failed");
// Ssh connection errors
case(SshSocketError): return QObject::tr("Ssh connection error");
case(SshTimeoutError): return QObject::tr("Ssh connection timeout");
case(SshProtocolError): return QObject::tr("Ssh protocol error");
case(SshHostKeyError): return QObject::tr("Ssh server ket check failed");
case(SshKeyFileError): return QObject::tr("Ssh key file error");
case(SshAuthenticationError): return QObject::tr("Ssh authentication error");
case(SshClosedByServerError): return QObject::tr("Ssh session closed");
case(SshInternalError): return QObject::tr("Ssh internal error");
// Ssh remote process errors
case(SshRemoteProcessCreationError): return QObject::tr("Failed to create remote process on server");
case(FailedToStartRemoteProcessError): return QObject::tr("Failed to start remote process on server");
case(RemoteProcessCrashError): return QObject::tr("Remote process on server crashed");
// Local errors
case (FailedToSaveConfigData): return QObject::tr("Failed to save config to disk");
case (OpenVpnConfigMissing): return QObject::tr("OpenVPN config missing");
case (OpenVpnManagementServerError): return QObject::tr("OpenVpn management server error");
case (OpenVpnExecutableMissing): return QObject::tr("OpenVPN executable missing");
case (EasyRsaExecutableMissing): return QObject::tr("EasyRsa executable missing");
case (AmneziaServiceConnectionFailed): return QObject::tr("Amnezia helper service error");
case(InternalError):
default:
return QObject::tr("Internal error");
}
}
#endif // ERRORSTRINGS_H

View file

@ -121,31 +121,39 @@ OpenVpnConfigurator::ConnectionData OpenVpnConfigurator::createCertRequest()
return connData;
}
OpenVpnConfigurator::ConnectionData OpenVpnConfigurator::prepareOpenVpnConfig(const QSsh::SshConnectionParameters &sshParams)
OpenVpnConfigurator::ConnectionData OpenVpnConfigurator::prepareOpenVpnConfig(const ServerCredentials &credentials, ErrorCode *errorCode)
{
OpenVpnConfigurator::ConnectionData connData = OpenVpnConfigurator::createCertRequest();
connData.host = sshParams.host;
connData.host = credentials.hostName;
QString reqFileName = QString("/opt/amneziavpn_data/clients/%1.req").arg(connData.clientId);
ServerController::uploadTextFileToContainer(sshParams, connData.request, reqFileName);
ErrorCode e = ServerController::uploadTextFileToContainer(credentials, connData.request, reqFileName);
if (e) {
*errorCode = e;
return connData;
}
ServerController::signCert(sshParams, connData.clientId);
ServerController::signCert(credentials, connData.clientId);
connData.caCert = ServerController::getTextFileFromContainer(sshParams, QString("/opt/amneziavpn_data/pki/ca.crt"));
connData.clientCert = ServerController::getTextFileFromContainer(sshParams, QString("/opt/amneziavpn_data/pki/issued/%1.crt").arg(connData.clientId));
connData.taKey = ServerController::getTextFileFromContainer(sshParams, QString("/opt/amneziavpn_data/ta.key"));
connData.caCert = ServerController::getTextFileFromContainer(credentials, ServerController::caCertPath(), &e);
connData.clientCert = ServerController::getTextFileFromContainer(credentials, ServerController::clientCertPath() + QString("%1.crt").arg(connData.clientId), &e);
if (e) {
*errorCode = e;
return connData;
}
connData.taKey = ServerController::getTextFileFromContainer(credentials, ServerController::taKeyPath(), &e);
return connData;
}
QString OpenVpnConfigurator::genOpenVpnConfig(const QSsh::SshConnectionParameters &sshParams)
QString OpenVpnConfigurator::genOpenVpnConfig(const ServerCredentials &credentials, ErrorCode *errorCode)
{
QFile configTemplFile(":/server_scripts/template.ovpn");
configTemplFile.open(QIODevice::ReadOnly);
QString config = configTemplFile.readAll();
ConnectionData connData = prepareOpenVpnConfig(sshParams);
ConnectionData connData = prepareOpenVpnConfig(credentials, errorCode);
config.replace("$PROTO", "udp");
config.replace("$REMOTE_HOST", connData.host);

View file

@ -3,6 +3,8 @@
#include <QObject>
#include <QProcessEnvironment>
#include "defs.h"
#include "servercontroller.h"
@ -20,7 +22,7 @@ public:
QString host; // host ip
};
static QString genOpenVpnConfig(const QSsh::SshConnectionParameters &sshParams);
static QString genOpenVpnConfig(const ServerCredentials &credentials, ErrorCode *errorCode = nullptr);
private:
static QString getRandomString(int len);
@ -32,7 +34,7 @@ private:
static ConnectionData createCertRequest();
static ConnectionData prepareOpenVpnConfig(const QSsh::SshConnectionParameters &sshParams);
static ConnectionData prepareOpenVpnConfig(const ServerCredentials &credentials, ErrorCode *errorCode = nullptr);
};

View file

@ -6,19 +6,19 @@
#include <QPointer>
#include <QTimer>
//#include "sshclient.h"
//#include "sshprocess.h"
#include "sshconnectionmanager.h"
#include "sshremoteprocess.h"
using namespace QSsh;
bool ServerController::runScript(const SshConnectionParameters &sshParams, QString script)
ErrorCode ServerController::runScript(const SshConnectionParameters &sshParams, QString script)
{
QLoggingCategory::setFilterRules(QStringLiteral("qtc.ssh=false"));
SshConnection *client = connectToHost(sshParams);
if (client->state() != SshConnection::State::Connected) {
return fromSshConnectionErrorCode(client->errorState());
}
script.replace("\r", "");
@ -36,49 +36,52 @@ bool ServerController::runScript(const SshConnectionParameters &sshParams, QStri
if (!proc) {
qCritical() << "Failed to create SshRemoteProcess, breaking.";
return false;
return ErrorCode::SshRemoteProcessCreationError;
}
QEventLoop wait;
int exitStatus;
QObject::connect(proc.data(), &SshRemoteProcess::started, &wait, [](){
qDebug() << "Command started";
});
// QObject::connect(proc.data(), &SshRemoteProcess::started, &wait, [](){
// qDebug() << "Command started";
// });
QObject::connect(proc.data(), &SshRemoteProcess::closed, &wait, [&](int status){
qDebug() << "Remote process exited with status" << status;
exitStatus = status;
//qDebug() << "Remote process exited with status" << status;
wait.quit();
});
QObject::connect(proc.data(), &SshRemoteProcess::readyReadStandardOutput, [proc](){
QString s = proc->readAllStandardOutput();
if (s != "." && !s.isEmpty()) {
qDebug().noquote() << s;
}
});
// QObject::connect(proc.data(), &SshRemoteProcess::readyReadStandardOutput, [proc](){
// QString s = proc->readAllStandardOutput();
// if (s != "." && !s.isEmpty()) {
// qDebug().noquote() << s << s.size();
// }
// });
QObject::connect(proc.data(), &SshRemoteProcess::readyReadStandardError, [proc](){
QString s = proc->readAllStandardError();
if (s != "." && !s.isEmpty()) {
qDebug().noquote() << s;
}
});
// QObject::connect(proc.data(), &SshRemoteProcess::readyReadStandardError, [proc](){
// QString s = proc->readAllStandardError();
// if (s != "." && !s.isEmpty()) {
// qDebug().noquote() << s;
// }
// });
proc->start();
if (i < lines.count() - 1) {
wait.exec();
}
if (SshRemoteProcess::ExitStatus(exitStatus) != QSsh::SshRemoteProcess::ExitStatus::NormalExit) {
return fromSshProcessExitStatus(exitStatus);
}
}
qDebug() << "ServerController::runScript finished\n";
// client->disconnectFromHost();
// client->deleteLater();
return true;
return ErrorCode::NoError;
}
void ServerController::uploadTextFileToContainer(const SshConnectionParameters &sshParams,
ErrorCode ServerController::uploadTextFileToContainer(const ServerCredentials &credentials,
QString &file, const QString &path)
{
QLoggingCategory::setFilterRules(QStringLiteral("qtc.ssh=false"));
@ -88,60 +91,85 @@ void ServerController::uploadTextFileToContainer(const SshConnectionParameters &
qDebug().noquote() << script;
SshConnection *client = connectToHost(sshParams);
SshConnection *client = connectToHost(sshParams(credentials));
if (client->state() != SshConnection::State::Connected) {
return fromSshConnectionErrorCode(client->errorState());
}
QSharedPointer<SshRemoteProcess> proc = client->createRemoteProcess(script.toUtf8());
if (!proc) {
qCritical() << "Failed to create SshRemoteProcess, breaking.";
return;
return ErrorCode::SshRemoteProcessCreationError;
}
QEventLoop wait;
int exitStatus = 0;
QObject::connect(proc.data(), &SshRemoteProcess::started, &wait, [](){
qDebug() << "Command started";
});
// QObject::connect(proc.data(), &SshRemoteProcess::started, &wait, [](){
// qDebug() << "Command started";
// });
QObject::connect(proc.data(), &SshRemoteProcess::closed, &wait, [&](int status){
qDebug() << "Remote process exited with status" << status;
//qDebug() << "Remote process exited with status" << status;
exitStatus = status;
wait.quit();
});
QObject::connect(proc.data(), &SshRemoteProcess::readyReadStandardOutput, [proc](){
qDebug().noquote() << proc->readAllStandardOutput();
});
// QObject::connect(proc.data(), &SshRemoteProcess::readyReadStandardOutput, [proc](){
// qDebug().noquote() << proc->readAllStandardOutput();
// });
QObject::connect(proc.data(), &SshRemoteProcess::readyReadStandardError, [proc](){
qDebug().noquote() << proc->readAllStandardError();
});
// QObject::connect(proc.data(), &SshRemoteProcess::readyReadStandardError, [proc](){
// qDebug().noquote() << proc->readAllStandardError();
// });
proc->start();
wait.exec();
return fromSshProcessExitStatus(exitStatus);
}
QString ServerController::getTextFileFromContainer(const SshConnectionParameters &sshParams, const QString &path)
QString ServerController::getTextFileFromContainer(const ServerCredentials &credentials, const QString &path,
ErrorCode *errorCode)
{
QString script = QString("docker exec -i amneziavpn sh -c \"cat \'%1\'\"").
arg(path);
qDebug().noquote() << "Copy file from container\n" << script;
SshConnection *client = connectToHost(sshParams);
SshConnection *client = connectToHost(sshParams(credentials));
if (client->state() != SshConnection::State::Connected) {
if (errorCode) *errorCode = fromSshConnectionErrorCode(client->errorState());
return QString();
}
QSharedPointer<SshRemoteProcess> proc = client->createRemoteProcess(script.toUtf8());
if (!proc) {
qCritical() << "Failed to create SshRemoteProcess, breaking.";
if (errorCode) *errorCode = ErrorCode::SshRemoteProcessCreationError;
return QString();
}
QEventLoop wait;
int exitStatus = 0;
QObject::connect(proc.data(), &SshRemoteProcess::closed, &wait, [&](int ){
QObject::connect(proc.data(), &SshRemoteProcess::closed, &wait, [&](int status){
exitStatus = status;
wait.quit();
});
proc->start();
wait.exec();
if (SshRemoteProcess::ExitStatus(exitStatus) != QSsh::SshRemoteProcess::ExitStatus::NormalExit) {
if (errorCode) *errorCode = fromSshProcessExitStatus(exitStatus);
}
return proc->readAllStandardOutput();
}
bool ServerController::signCert(const SshConnectionParameters &sshParams, QString clientId)
ErrorCode ServerController::signCert(const ServerCredentials &credentials, QString clientId)
{
QString script_import = QString("docker exec -i amneziavpn bash -c \"cd /opt/amneziavpn_data && "
"easyrsa import-req /opt/amneziavpn_data/clients/%1.req %1 &>/dev/null\"")
@ -153,55 +181,121 @@ bool ServerController::signCert(const SshConnectionParameters &sshParams, QStrin
QStringList script {script_import, script_sign};
return runScript(sshParams, script.join("\n"));
return runScript(sshParams(credentials), script.join("\n"));
}
bool ServerController::removeServer(const SshConnectionParameters &sshParams, ServerController::ServerType sType)
ErrorCode ServerController::checkOpenVpnServer(const ServerCredentials &credentials)
{
QString caCert = ServerController::getTextFileFromContainer(credentials, ServerController::caCertPath());
QString taKey = ServerController::getTextFileFromContainer(credentials, ServerController::taKeyPath());
if (!caCert.isEmpty() && !taKey.isEmpty()) {
return ErrorCode::NoError;
}
else {
return ErrorCode::ServerCheckFailed;
}
}
ErrorCode ServerController::fromSshConnectionErrorCode(SshError error)
{
switch (error) {
case(SshNoError): return ErrorCode::NoError;
case(QSsh::SshSocketError): return ErrorCode::SshSocketError;
case(QSsh::SshTimeoutError): return ErrorCode::SshTimeoutError;
case(QSsh::SshProtocolError): return ErrorCode::SshProtocolError;
case(QSsh::SshHostKeyError): return ErrorCode::SshHostKeyError;
case(QSsh::SshKeyFileError): return ErrorCode::SshKeyFileError;
case(QSsh::SshAuthenticationError): return ErrorCode::SshAuthenticationError;
case(QSsh::SshClosedByServerError): return ErrorCode::SshClosedByServerError;
case(QSsh::SshInternalError): return ErrorCode::SshInternalError;
}
}
ErrorCode ServerController::fromSshProcessExitStatus(int exitStatus)
{
switch (SshRemoteProcess::ExitStatus(exitStatus)) {
case(SshRemoteProcess::ExitStatus::NormalExit): return ErrorCode::NoError;
case(SshRemoteProcess::ExitStatus::FailedToStart): return ErrorCode::FailedToStartRemoteProcessError;
case(SshRemoteProcess::ExitStatus::CrashExit): return ErrorCode::RemoteProcessCrashError;
}
}
SshConnectionParameters ServerController::sshParams(const ServerCredentials &credentials)
{
QSsh::SshConnectionParameters sshParams;
sshParams.authenticationType = QSsh::SshConnectionParameters::AuthenticationTypePassword;
sshParams.host = credentials.hostName;
sshParams.userName = credentials.userName;
sshParams.password = credentials.password;
sshParams.timeout = 10;
sshParams.port = credentials.port;
sshParams.hostKeyCheckingMode = QSsh::SshHostKeyCheckingMode::SshHostKeyCheckingNone;
return sshParams;
}
ErrorCode ServerController::removeServer(const ServerCredentials &credentials, Protocol proto)
{
QString scriptFileName;
if (sType == OpenVPN) {
if (proto == Protocol::OpenVpn) {
scriptFileName = ":/server_scripts/remove_openvpn_server.sh";
}
QString scriptData;
QFile file(scriptFileName);
if (! file.open(QIODevice::ReadOnly)) {
return false;
}
if (! file.open(QIODevice::ReadOnly)) return ErrorCode::InternalError;
scriptData = file.readAll();
if (scriptData.isEmpty()) return false;
if (scriptData.isEmpty()) return ErrorCode::InternalError;
return runScript(sshParams, scriptData);
return runScript(sshParams(credentials), scriptData);
}
bool ServerController::setupServer(const SshConnectionParameters &sshParams, ServerController::ServerType sType)
ErrorCode ServerController::setupServer(const ServerCredentials &credentials, Protocol proto)
{
QString scriptFileName;
if (sType == OpenVPN) {
scriptFileName = ":/server_scripts/setup_openvpn_server.sh";
if (proto == Protocol::OpenVpn) {
return setupOpenVpnServer(credentials);
}
else if (proto == Protocol::ShadowSocks) {
return setupShadowSocksServer(credentials);
}
else if (proto == Protocol::Any) {
// TODO: run concurently
return setupOpenVpnServer(credentials);
//setupShadowSocksServer(credentials);
}
return ErrorCode::NotImplementedError;
}
ErrorCode ServerController::setupOpenVpnServer(const ServerCredentials &credentials)
{
QString scriptData;
QString scriptFileName = ":/server_scripts/setup_openvpn_server.sh";
QFile file(scriptFileName);
if (! file.open(QIODevice::ReadOnly)) {
return false;
}
if (! file.open(QIODevice::ReadOnly)) return ErrorCode::InternalError;
scriptData = file.readAll();
if (scriptData.isEmpty()) return false;
if (scriptData.isEmpty()) return ErrorCode::InternalError;
return runScript(sshParams, scriptData);
ErrorCode e = runScript(sshParams(credentials), scriptData);
if (e) return e;
//return ok;
return checkOpenVpnServer(credentials);
}
ErrorCode ServerController::setupShadowSocksServer(const ServerCredentials &credentials)
{
return ErrorCode::NotImplementedError;
}
SshConnection *ServerController::connectToHost(const SshConnectionParameters &sshParams)
{
SshConnection *client = acquireConnection(sshParams);
//QPointer<SshConnection> client = new SshConnection(serverInfo);
QEventLoop waitssh;
QObject::connect(client, &SshConnection::connected, &waitssh, [&]() {

View file

@ -3,29 +3,43 @@
#include <QObject>
#include "sshconnection.h"
#include "sshremoteprocess.h"
#include "defs.h"
using namespace amnezia;
class ServerController : public QObject
{
Q_OBJECT
public:
enum ServerType {
OpenVPN,
ShadowSocks,
WireGuard
};
static bool removeServer(const QSsh::SshConnectionParameters &sshParams, ServerType sType);
static bool setupServer(const QSsh::SshConnectionParameters &sshParams, ServerType sType);
static ErrorCode fromSshConnectionErrorCode(QSsh::SshError error);
// QSsh exitCode and exitStatus are different things
static ErrorCode fromSshProcessExitStatus(int exitStatus);
static QString caCertPath() { return "/opt/amneziavpn_data/pki/ca.crt"; }
static QString clientCertPath() { return "/opt/amneziavpn_data/pki/issued/"; }
static QString taKeyPath() { return "/opt/amneziavpn_data/ta.key"; }
static QSsh::SshConnectionParameters sshParams(const ServerCredentials &credentials);
static ErrorCode removeServer(const ServerCredentials &credentials, Protocol proto);
static ErrorCode setupServer(const ServerCredentials &credentials, Protocol proto);
static ErrorCode checkOpenVpnServer(const ServerCredentials &credentials);
static ErrorCode uploadTextFileToContainer(const ServerCredentials &credentials, QString &file, const QString &path);
static QString getTextFileFromContainer(const ServerCredentials &credentials, const QString &path, ErrorCode *errorCode = nullptr);
static ErrorCode signCert(const ServerCredentials &credentials, QString clientId);
private:
static QSsh::SshConnection *connectToHost(const QSsh::SshConnectionParameters &sshParams);
static bool runScript(const QSsh::SshConnectionParameters &sshParams, QString script);
static ErrorCode runScript(const QSsh::SshConnectionParameters &sshParams, QString script);
static void uploadTextFileToContainer(const QSsh::SshConnectionParameters &sshParams, QString &file, const QString &path);
static QString getTextFileFromContainer(const QSsh::SshConnectionParameters &sshParams, const QString &path);
static bool signCert(const QSsh::SshConnectionParameters &sshParams, QString clientId);
signals:
static ErrorCode setupOpenVpnServer(const ServerCredentials &credentials);
static ErrorCode setupShadowSocksServer(const ServerCredentials &credentials);
};