Skip to content

Commit

Permalink
Merge pull request #2 from amnezia-vpn/ss/refactoring
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
pokamest authored Jan 6, 2021
2 parents 13f9764 + 5eede71 commit ff55758
Show file tree
Hide file tree
Showing 21 changed files with 559 additions and 213 deletions.
10 changes: 6 additions & 4 deletions client/client.pro
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ include("3rd/QtSsh/src/botan/botan.pri")

HEADERS += \
communicator.h \
core/defs.h \
core/errorstrings.h \
core/openvpnconfigurator.h \
core/router.h \
core/servercontroller.h \
Expand All @@ -19,14 +21,14 @@ HEADERS += \
localclient.h \
managementserver.h \
message.h \
openvpnprotocol.h \
runguard.h \
settings.h \
ui/Controls/SlidingStackedWidget.h \
ui/mainwindow.h \
utils.h \
vpnconnection.h \
vpnprotocol.h \
protocols/vpnprotocol.h \
protocols/openvpnprotocol.h \

SOURCES += \
communicator.cpp \
Expand All @@ -38,14 +40,14 @@ SOURCES += \
main.cpp \
managementserver.cpp \
message.cpp \
openvpnprotocol.cpp \
runguard.cpp \
settings.cpp \
ui/Controls/SlidingStackedWidget.cpp \
ui/mainwindow.cpp \
utils.cpp \
vpnconnection.cpp \
vpnprotocol.cpp \
protocols/vpnprotocol.cpp \
protocols/openvpnprotocol.cpp \

FORMS += ui/mainwindow.ui

Expand Down
56 changes: 56 additions & 0 deletions client/core/defs.h
Original file line number Diff line number Diff line change
@@ -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
47 changes: 47 additions & 0 deletions client/core/errorstrings.h
Original file line number Diff line number Diff line change
@@ -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
26 changes: 17 additions & 9 deletions client/core/openvpnconfigurator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions client/core/openvpnconfigurator.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include <QObject>
#include <QProcessEnvironment>

#include "defs.h"
#include "servercontroller.h"


Expand All @@ -20,7 +22,7 @@ class OpenVpnConfigurator
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);
Expand All @@ -32,7 +34,7 @@ class OpenVpnConfigurator

static ConnectionData createCertRequest();

static ConnectionData prepareOpenVpnConfig(const QSsh::SshConnectionParameters &sshParams);
static ConnectionData prepareOpenVpnConfig(const ServerCredentials &credentials, ErrorCode *errorCode = nullptr);

};

Expand Down
Loading

0 comments on commit ff55758

Please sign in to comment.