Skip to content

Commit

Permalink
added passing new wireguard config parameters over uapi and configuri…
Browse files Browse the repository at this point in the history
…ng the amneziawireguard container
  • Loading branch information
Nethius committed Sep 26, 2023
1 parent 7284bb5 commit af53c45
Show file tree
Hide file tree
Showing 19 changed files with 342 additions and 30 deletions.
51 changes: 48 additions & 3 deletions client/configurators/amneziaWireGuardConfigurator.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,60 @@
#include "amneziaWireGuardConfigurator.h"

#include <QJsonDocument>
#include <QJsonObject>

AmneziaWireGuardConfigurator::AmneziaWireGuardConfigurator(std::shared_ptr<Settings> settings, QObject *parent)
: WireguardConfigurator(settings, parent)
: WireguardConfigurator(settings, true, parent)
{
}

QString AmneziaWireGuardConfigurator::genAmneziaWireGuardConfig(const ServerCredentials &credentials,
DockerContainer container,
const QJsonObject &containerConfig, ErrorCode *errorCode)
{
auto config = WireguardConfigurator::genWireguardConfig(credentials, container, containerConfig, errorCode);
QString config = WireguardConfigurator::genWireguardConfig(credentials, container, containerConfig, errorCode);

QJsonObject jsonConfig = QJsonDocument::fromJson(config.toUtf8()).object();
QJsonObject awgConfig = containerConfig.value(config_key::amneziaWireguard).toObject();

auto junkPacketCount =
awgConfig.value(config_key::junkPacketCount).toString(protocols::amneziawireguard::defaultJunkPacketCount);
auto junkPacketMinSize =
awgConfig.value(config_key::junkPacketMinSize).toString(protocols::amneziawireguard::defaultJunkPacketMinSize);
auto junkPacketMaxSize =
awgConfig.value(config_key::junkPacketMaxSize).toString(protocols::amneziawireguard::defaultJunkPacketMaxSize);
auto initPacketJunkSize =
awgConfig.value(config_key::initPacketJunkSize).toString(protocols::amneziawireguard::defaultInitPacketJunkSize);
auto responsePacketJunkSize =
awgConfig.value(config_key::responsePacketJunkSize).toString(protocols::amneziawireguard::defaultResponsePacketJunkSize);
auto initPacketMagicHeader =
awgConfig.value(config_key::initPacketMagicHeader).toString(protocols::amneziawireguard::defaultInitPacketMagicHeader);
auto responsePacketMagicHeader =
awgConfig.value(config_key::responsePacketMagicHeader).toString(protocols::amneziawireguard::defaultResponsePacketMagicHeader);
auto underloadPacketMagicHeader =
awgConfig.value(config_key::underloadPacketMagicHeader).toString(protocols::amneziawireguard::defaultUnderloadPacketMagicHeader);
auto transportPacketMagicHeader =
awgConfig.value(config_key::transportPacketMagicHeader).toString(protocols::amneziawireguard::defaultTransportPacketMagicHeader);

config.replace("$JUNK_PACKET_COUNT", junkPacketCount);
config.replace("$JUNK_PACKET_MIN_SIZE", junkPacketMinSize);
config.replace("$JUNK_PACKET_MAX_SIZE", junkPacketMaxSize);
config.replace("$INIT_PACKET_JUNK_SIZE", initPacketJunkSize);
config.replace("$RESPONSE_PACKET_JUNK_SIZE", responsePacketJunkSize);
config.replace("$INIT_PACKET_MAGIC_HEADER", initPacketMagicHeader);
config.replace("$RESPONSE_PACKET_MAGIC_HEADER", responsePacketMagicHeader);
config.replace("$UNDERLOAD_PACKET_MAGIC_HEADER", underloadPacketMagicHeader);
config.replace("$TRANSPORT_PACKET_MAGIC_HEADER", transportPacketMagicHeader);

jsonConfig[config_key::junkPacketCount] = junkPacketCount;
jsonConfig[config_key::junkPacketMinSize] = junkPacketMinSize;
jsonConfig[config_key::junkPacketMaxSize] = junkPacketMaxSize;
jsonConfig[config_key::initPacketJunkSize] = initPacketJunkSize;
jsonConfig[config_key::responsePacketJunkSize] = responsePacketJunkSize;
jsonConfig[config_key::initPacketMagicHeader] = initPacketMagicHeader;
jsonConfig[config_key::responsePacketMagicHeader] = responsePacketMagicHeader;
jsonConfig[config_key::underloadPacketMagicHeader] = underloadPacketMagicHeader;
jsonConfig[config_key::transportPacketMagicHeader] = transportPacketMagicHeader;

return config;
return QJsonDocument(jsonConfig).toJson();
}
6 changes: 4 additions & 2 deletions client/configurators/vpn_configurator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "shadowsocks_configurator.h"
#include "ssh_configurator.h"
#include "wireguard_configurator.h"
#include "amneziaWireGuardConfigurator.h"

#include <QFile>
#include <QJsonDocument>
Expand All @@ -20,9 +21,10 @@ VpnConfigurator::VpnConfigurator(std::shared_ptr<Settings> settings, QObject *pa
openVpnConfigurator = std::shared_ptr<OpenVpnConfigurator>(new OpenVpnConfigurator(settings, this));
shadowSocksConfigurator = std::shared_ptr<ShadowSocksConfigurator>(new ShadowSocksConfigurator(settings, this));
cloakConfigurator = std::shared_ptr<CloakConfigurator>(new CloakConfigurator(settings, this));
wireguardConfigurator = std::shared_ptr<WireguardConfigurator>(new WireguardConfigurator(settings, this));
wireguardConfigurator = std::shared_ptr<WireguardConfigurator>(new WireguardConfigurator(settings, false, this));
ikev2Configurator = std::shared_ptr<Ikev2Configurator>(new Ikev2Configurator(settings, this));
sshConfigurator = std::shared_ptr<SshConfigurator>(new SshConfigurator(settings, this));
amneziaWireGuardConfigurator = std::shared_ptr<AmneziaWireGuardConfigurator>(new AmneziaWireGuardConfigurator(settings, this));
}

QString VpnConfigurator::genVpnProtocolConfig(const ServerCredentials &credentials, DockerContainer container,
Expand All @@ -41,7 +43,7 @@ QString VpnConfigurator::genVpnProtocolConfig(const ServerCredentials &credentia
return wireguardConfigurator->genWireguardConfig(credentials, container, containerConfig, errorCode);

case Proto::AmneziaWireGuard:
return wireguardConfigurator->genWireguardConfig(credentials, container, containerConfig, errorCode);
return amneziaWireGuardConfigurator->genAmneziaWireGuardConfig(credentials, container, containerConfig, errorCode);

case Proto::Ikev2: return ikev2Configurator->genIkev2Config(credentials, container, containerConfig, errorCode);

Expand Down
4 changes: 3 additions & 1 deletion client/configurators/vpn_configurator.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ class CloakConfigurator;
class WireguardConfigurator;
class Ikev2Configurator;
class SshConfigurator;
class AmneziaWireGuardConfigurator;

// Retrieve connection settings from server
class VpnConfigurator : ConfiguratorBase
{
Q_OBJECT
public:
VpnConfigurator(std::shared_ptr<Settings> settings, QObject *parent = nullptr);
explicit VpnConfigurator(std::shared_ptr<Settings> settings, QObject *parent = nullptr);

QString genVpnProtocolConfig(const ServerCredentials &credentials, DockerContainer container,
const QJsonObject &containerConfig, Proto proto, ErrorCode *errorCode = nullptr);
Expand All @@ -40,6 +41,7 @@ class VpnConfigurator : ConfiguratorBase
std::shared_ptr<WireguardConfigurator> wireguardConfigurator;
std::shared_ptr<Ikev2Configurator> ikev2Configurator;
std::shared_ptr<SshConfigurator> sshConfigurator;
std::shared_ptr<AmneziaWireGuardConfigurator> amneziaWireGuardConfigurator;
};

#endif // VPN_CONFIGURATOR_H
43 changes: 23 additions & 20 deletions client/configurators/wireguard_configurator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,17 @@
#include "settings.h"
#include "utilities.h"

WireguardConfigurator::WireguardConfigurator(std::shared_ptr<Settings> settings, QObject *parent)
: ConfiguratorBase(settings, parent)
WireguardConfigurator::WireguardConfigurator(std::shared_ptr<Settings> settings, bool isAmneziaWireGuard, QObject *parent)
: ConfiguratorBase(settings, parent), m_isAmneziaWireGuard(isAmneziaWireGuard)
{
m_serverConfigPath = m_isAmneziaWireGuard ? amnezia::protocols::amneziawireguard::serverConfigPath
: amnezia::protocols::wireguard::serverConfigPath;
m_serverPublicKeyPath = m_isAmneziaWireGuard ? amnezia::protocols::amneziawireguard::serverPublicKeyPath
: amnezia::protocols::wireguard::serverPublicKeyPath;
m_serverPskKeyPath = m_isAmneziaWireGuard ? amnezia::protocols::amneziawireguard::serverPskKeyPath
: amnezia::protocols::wireguard::serverPskKeyPath;
m_configTemplate = m_isAmneziaWireGuard ? ProtocolScriptType::amnezia_wireguard_template
: ProtocolScriptType::wireguard_template;
}

WireguardConfigurator::ConnectionData WireguardConfigurator::genClientKeys()
Expand Down Expand Up @@ -62,7 +70,7 @@ WireguardConfigurator::ConnectionData WireguardConfigurator::prepareWireguardCon
{
WireguardConfigurator::ConnectionData connData = WireguardConfigurator::genClientKeys();
connData.host = credentials.hostName;
connData.port = containerConfig.value(config_key::wireguard)
connData.port = containerConfig.value(m_isAmneziaWireGuard ? config_key::amneziaWireguard : config_key::wireguard)
.toObject()
.value(config_key::port)
.toString(protocols::wireguard::defaultPort);
Expand All @@ -79,7 +87,7 @@ WireguardConfigurator::ConnectionData WireguardConfigurator::prepareWireguardCon
// Get list of already created clients (only IP addresses)
QString nextIpNumber;
{
QString script = QString("cat %1 | grep AllowedIPs").arg(amnezia::protocols::wireguard::serverConfigPath);
QString script = QString("cat %1 | grep AllowedIPs").arg(m_serverConfigPath);
QString stdOut;
auto cbReadStdOut = [&](const QString &data, libssh::Client &) {
stdOut += data + "\n";
Expand Down Expand Up @@ -126,17 +134,15 @@ WireguardConfigurator::ConnectionData WireguardConfigurator::prepareWireguardCon
}

// Get keys
connData.serverPubKey = serverController.getTextFileFromContainer(
container, credentials, amnezia::protocols::wireguard::serverPublicKeyPath, &e);
connData.serverPubKey = serverController.getTextFileFromContainer(container, credentials, m_serverPublicKeyPath, &e);
connData.serverPubKey.replace("\n", "");
if (e) {
if (errorCode)
*errorCode = e;
return connData;
}

connData.pskKey = serverController.getTextFileFromContainer(container, credentials,
amnezia::protocols::wireguard::serverPskKeyPath, &e);
connData.pskKey = serverController.getTextFileFromContainer(container, credentials, m_serverPskKeyPath, &e);
connData.pskKey.replace("\n", "");

if (e) {
Expand All @@ -150,12 +156,9 @@ WireguardConfigurator::ConnectionData WireguardConfigurator::prepareWireguardCon
"PublicKey = %1\n"
"PresharedKey = %2\n"
"AllowedIPs = %3/32\n\n")
.arg(connData.clientPubKey)
.arg(connData.pskKey)
.arg(connData.clientIP);
.arg(connData.clientPubKey, connData.pskKey, connData.clientIP);

e = serverController.uploadTextFileToContainer(container, credentials, configPart,
protocols::wireguard::serverConfigPath,
e = serverController.uploadTextFileToContainer(container, credentials, configPart, m_serverConfigPath,
libssh::SftpOverwriteMode::SftpAppendToExisting);

if (e) {
Expand All @@ -164,11 +167,11 @@ WireguardConfigurator::ConnectionData WireguardConfigurator::prepareWireguardCon
return connData;
}

QString script = QString("sudo docker exec -i $CONTAINER_NAME bash -c 'wg syncconf wg0 <(wg-quick strip %1)'")
.arg(m_serverConfigPath);

e = serverController.runScript(
credentials,
serverController.replaceVars("sudo docker exec -i $CONTAINER_NAME bash -c 'wg syncconf wg0 <(wg-quick "
"strip /opt/amnezia/wireguard/wg0.conf)'",
serverController.genVarsForScript(credentials, container)));
credentials, serverController.replaceVars(script, serverController.genVarsForScript(credentials, container)));

return connData;
}
Expand All @@ -177,9 +180,9 @@ QString WireguardConfigurator::genWireguardConfig(const ServerCredentials &crede
const QJsonObject &containerConfig, ErrorCode *errorCode)
{
ServerController serverController(m_settings);
QString config =
serverController.replaceVars(amnezia::scriptData(ProtocolScriptType::wireguard_template, container),
serverController.genVarsForScript(credentials, container, containerConfig));
QString scriptData = amnezia::scriptData(m_configTemplate, container);
QString config = serverController.replaceVars(
scriptData, serverController.genVarsForScript(credentials, container, containerConfig));

ConnectionData connData = prepareWireguardConfig(credentials, container, containerConfig, errorCode);
if (errorCode && *errorCode) {
Expand Down
9 changes: 8 additions & 1 deletion client/configurators/wireguard_configurator.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@

#include "configurator_base.h"
#include "core/defs.h"
#include "core/scripts_registry.h"

class WireguardConfigurator : public ConfiguratorBase
{
Q_OBJECT
public:
WireguardConfigurator(std::shared_ptr<Settings> settings, QObject *parent = nullptr);
WireguardConfigurator(std::shared_ptr<Settings> settings, bool isAmneziaWireGuard, QObject *parent = nullptr);

struct ConnectionData
{
Expand All @@ -35,6 +36,12 @@ class WireguardConfigurator : public ConfiguratorBase
const QJsonObject &containerConfig, ErrorCode *errorCode = nullptr);

ConnectionData genClientKeys();

bool m_isAmneziaWireGuard;
QString m_serverConfigPath;
QString m_serverPublicKeyPath;
QString m_serverPskKeyPath;
amnezia::ProtocolScriptType m_configTemplate;
};

#endif // WIREGUARD_CONFIGURATOR_H
3 changes: 2 additions & 1 deletion client/core/scripts_registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ QString amnezia::scriptFolder(amnezia::DockerContainer container)
case DockerContainer::Cloak: return QLatin1String("openvpn_cloak");
case DockerContainer::ShadowSocks: return QLatin1String("openvpn_shadowsocks");
case DockerContainer::WireGuard: return QLatin1String("wireguard");
case DockerContainer::AmneziaWireGuard: return QLatin1String("wireguard");
case DockerContainer::AmneziaWireGuard: return QLatin1String("amnezia_wireguard");
case DockerContainer::Ipsec: return QLatin1String("ipsec");

case DockerContainer::TorWebSite: return QLatin1String("website_tor");
Expand Down Expand Up @@ -46,6 +46,7 @@ QString amnezia::scriptName(ProtocolScriptType type)
case ProtocolScriptType::container_startup: return QLatin1String("start.sh");
case ProtocolScriptType::openvpn_template: return QLatin1String("template.ovpn");
case ProtocolScriptType::wireguard_template: return QLatin1String("template.conf");
case ProtocolScriptType::amnezia_wireguard_template: return QLatin1String("template.conf");
}
}

Expand Down
3 changes: 2 additions & 1 deletion client/core/scripts_registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ enum ProtocolScriptType {
configure_container,
container_startup,
openvpn_template,
wireguard_template
wireguard_template,
amnezia_wireguard_template
};


Expand Down
31 changes: 31 additions & 0 deletions client/core/servercontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,37 @@ ServerController::Vars ServerController::genVarsForScript(const ServerCredential
vars.append({ { "$SFTP_USER", sftpConfig.value(config_key::userName).toString() } });
vars.append({ { "$SFTP_PASSWORD", sftpConfig.value(config_key::password).toString() } });

// Amnezia wireguard vars
vars.append({ { "$AMNEZIAWIREGUARD_SERVER_PORT",
amneziaWireguarConfig.value(config_key::port).toString(protocols::amneziawireguard::defaultPort) } });
vars.append({ { "$JUNK_PACKET_COUNT",
amneziaWireguarConfig.value(config_key::junkPacketCount)
.toString(protocols::amneziawireguard::defaultJunkPacketCount) } });
vars.append({ { "$JUNK_PACKET_MIN_SIZE",
amneziaWireguarConfig.value(config_key::junkPacketMinSize)
.toString(protocols::amneziawireguard::defaultJunkPacketMinSize) } });
vars.append({ { "$JUNK_PACKET_MAX_SIZE",
amneziaWireguarConfig.value(config_key::junkPacketMaxSize)
.toString(protocols::amneziawireguard::defaultJunkPacketMaxSize) } });
vars.append({ { "$INIT_PACKET_JUNK_SIZE",
amneziaWireguarConfig.value(config_key::initPacketJunkSize)
.toString(protocols::amneziawireguard::defaultInitPacketJunkSize) } });
vars.append({ { "$RESPONSE_PACKET_JUNK_SIZE",
amneziaWireguarConfig.value(config_key::responsePacketJunkSize)
.toString(protocols::amneziawireguard::defaultResponsePacketJunkSize) } });
vars.append({ { "$INIT_PACKET_MAGIC_HEADER",
amneziaWireguarConfig.value(config_key::initPacketMagicHeader)
.toString(protocols::amneziawireguard::defaultInitPacketMagicHeader) } });
vars.append({ { "$RESPONSE_PACKET_MAGIC_HEADER",
amneziaWireguarConfig.value(config_key::responsePacketMagicHeader)
.toString(protocols::amneziawireguard::defaultResponsePacketMagicHeader) } });
vars.append({ { "$UNDERLOAD_PACKET_MAGIC_HEADER",
amneziaWireguarConfig.value(config_key::underloadPacketMagicHeader)
.toString(protocols::amneziawireguard::defaultUnderloadPacketMagicHeader) } });
vars.append({ { "$TRANSPORT_PACKET_MAGIC_HEADER",
amneziaWireguarConfig.value(config_key::transportPacketMagicHeader)
.toString(protocols::amneziawireguard::defaultTransportPacketMagicHeader) } });

QString serverIp = Utils::getIPAddress(credentials.hostName);
if (!serverIp.isEmpty()) {
vars.append({ { "$SERVER_IP_ADDRESS", serverIp } });
Expand Down
11 changes: 11 additions & 0 deletions client/daemon/daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,17 @@ bool Daemon::parseConfig(const QJsonObject& obj, InterfaceConfig& config) {
if (!parseStringList(obj, "vpnDisabledApps", config.m_vpnDisabledApps)) {
return false;
}

config.m_junkPacketCount = obj.value("Jc").toString();
config.m_junkPacketMinSize = obj.value("Jmin").toString();
config.m_junkPacketMaxSize = obj.value("Jmax").toString();
config.m_initPacketJunkSize = obj.value("S1").toString();
config.m_responsePacketJunkSize = obj.value("S2").toString();
config.m_initPacketMagicHeader = obj.value("H1").toString();
config.m_responsePacketMagicHeader = obj.value("H2").toString();
config.m_underloadPacketMagicHeader = obj.value("H3").toString();
config.m_transportPacketMagicHeader = obj.value("H4").toString();

return true;
}

Expand Down
10 changes: 10 additions & 0 deletions client/daemon/interfaceconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ class InterfaceConfig {
QString m_installationId;
#endif

QString m_junkPacketCount;
QString m_junkPacketMinSize;
QString m_junkPacketMaxSize;
QString m_initPacketJunkSize;
QString m_responsePacketJunkSize;
QString m_initPacketMagicHeader;
QString m_responsePacketMagicHeader;
QString m_underloadPacketMagicHeader;
QString m_transportPacketMagicHeader;

QJsonObject toJson() const;
QString toWgConf(
const QMap<QString, QString>& extra = QMap<QString, QString>()) const;
Expand Down
Loading

0 comments on commit af53c45

Please sign in to comment.