2026-04-30 14:53:03 +08:00
|
|
|
#include "wireguardConfigurator.h"
|
2023-08-31 16:00:41 +05:00
|
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QJsonDocument>
|
2021-06-12 11:59:36 +03:00
|
|
|
#include <QProcess>
|
2025-03-06 15:43:47 +01:00
|
|
|
#include <QRegularExpression>
|
2021-06-12 11:59:36 +03:00
|
|
|
#include <QString>
|
|
|
|
|
#include <QTemporaryDir>
|
|
|
|
|
#include <QTemporaryFile>
|
|
|
|
|
|
2023-08-31 16:00:41 +05:00
|
|
|
#include <openssl/pem.h>
|
2021-10-27 00:42:25 +03:00
|
|
|
#include <openssl/rand.h>
|
|
|
|
|
#include <openssl/rsa.h>
|
|
|
|
|
#include <openssl/x509.h>
|
2021-06-12 11:59:36 +03:00
|
|
|
|
2026-04-30 14:53:03 +08:00
|
|
|
#include "core/utils/containerEnum.h"
|
|
|
|
|
#include "core/utils/containers/containerUtils.h"
|
|
|
|
|
#include "core/utils/protocolEnum.h"
|
|
|
|
|
#include "core/utils/selfhosted/sshSession.h"
|
|
|
|
|
#include "core/utils/selfhosted/scriptsRegistry.h"
|
|
|
|
|
#include "core/utils/protocolEnum.h"
|
|
|
|
|
#include "core/protocols/protocolUtils.h"
|
|
|
|
|
#include "core/utils/constants/configKeys.h"
|
|
|
|
|
#include "core/utils/constants/protocolConstants.h"
|
|
|
|
|
#include "core/utils/utilities.h"
|
|
|
|
|
#include "core/models/containerConfig.h"
|
|
|
|
|
#include "core/models/protocols/wireGuardProtocolConfig.h"
|
|
|
|
|
#include "core/models/protocols/awgProtocolConfig.h"
|
|
|
|
|
#include <QJsonArray>
|
|
|
|
|
|
|
|
|
|
using namespace amnezia;
|
|
|
|
|
|
|
|
|
|
WireguardConfigurator::WireguardConfigurator(SshSession* sshSession, bool isAwg,
|
2025-03-06 15:43:47 +01:00
|
|
|
QObject *parent)
|
2026-04-30 14:53:03 +08:00
|
|
|
: ConfiguratorBase(sshSession, parent), m_isAwg(isAwg)
|
2022-08-25 17:35:28 +03:00
|
|
|
{
|
2025-03-06 15:43:47 +01:00
|
|
|
m_serverConfigPath =
|
|
|
|
|
m_isAwg ? amnezia::protocols::awg::serverConfigPath : amnezia::protocols::wireguard::serverConfigPath;
|
|
|
|
|
m_serverPublicKeyPath =
|
|
|
|
|
m_isAwg ? amnezia::protocols::awg::serverPublicKeyPath : amnezia::protocols::wireguard::serverPublicKeyPath;
|
|
|
|
|
m_serverPskKeyPath =
|
|
|
|
|
m_isAwg ? amnezia::protocols::awg::serverPskKeyPath : amnezia::protocols::wireguard::serverPskKeyPath;
|
2024-04-01 20:20:02 +07:00
|
|
|
m_configTemplate = m_isAwg ? ProtocolScriptType::awg_template : ProtocolScriptType::wireguard_template;
|
2023-10-06 16:43:52 +05:00
|
|
|
|
2026-04-30 14:53:03 +08:00
|
|
|
m_protocolName = m_isAwg ? configKey::awg : configKey::wireguard;
|
|
|
|
|
m_defaultPort = m_isAwg ? protocols::awg::defaultPort : protocols::wireguard::defaultPort;
|
2022-08-25 17:35:28 +03:00
|
|
|
}
|
2021-06-12 11:59:36 +03:00
|
|
|
|
|
|
|
|
WireguardConfigurator::ConnectionData WireguardConfigurator::genClientKeys()
|
|
|
|
|
{
|
2021-10-27 00:42:25 +03:00
|
|
|
// TODO review
|
|
|
|
|
constexpr size_t EDDSA_KEY_LENGTH = 32;
|
2021-06-12 11:59:36 +03:00
|
|
|
|
2021-10-27 00:42:25 +03:00
|
|
|
ConnectionData connData;
|
2021-09-15 08:03:28 -07:00
|
|
|
|
2021-10-27 00:42:25 +03:00
|
|
|
unsigned char buff[EDDSA_KEY_LENGTH];
|
|
|
|
|
int ret = RAND_priv_bytes(buff, EDDSA_KEY_LENGTH);
|
2023-08-31 16:00:41 +05:00
|
|
|
if (ret <= 0)
|
|
|
|
|
return connData;
|
2021-06-12 11:59:36 +03:00
|
|
|
|
2023-08-31 16:00:41 +05:00
|
|
|
EVP_PKEY *pKey = EVP_PKEY_new();
|
2021-10-27 00:42:25 +03:00
|
|
|
q_check_ptr(pKey);
|
2021-11-02 21:50:28 +03:00
|
|
|
pKey = EVP_PKEY_new_raw_private_key(EVP_PKEY_X25519, NULL, &buff[0], EDDSA_KEY_LENGTH);
|
2021-06-12 11:59:36 +03:00
|
|
|
|
2021-10-27 00:42:25 +03:00
|
|
|
size_t keySize = EDDSA_KEY_LENGTH;
|
2021-06-12 11:59:36 +03:00
|
|
|
|
2021-10-27 00:42:25 +03:00
|
|
|
// save private key
|
|
|
|
|
unsigned char priv[EDDSA_KEY_LENGTH];
|
|
|
|
|
EVP_PKEY_get_raw_private_key(pKey, priv, &keySize);
|
2023-08-31 16:00:41 +05:00
|
|
|
connData.clientPrivKey = QByteArray::fromRawData((char *)priv, keySize).toBase64();
|
2021-06-12 11:59:36 +03:00
|
|
|
|
2021-10-27 00:42:25 +03:00
|
|
|
// save public key
|
|
|
|
|
unsigned char pub[EDDSA_KEY_LENGTH];
|
|
|
|
|
EVP_PKEY_get_raw_public_key(pKey, pub, &keySize);
|
2023-08-31 16:00:41 +05:00
|
|
|
connData.clientPubKey = QByteArray::fromRawData((char *)pub, keySize).toBase64();
|
2021-06-12 11:59:36 +03:00
|
|
|
|
|
|
|
|
return connData;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-06 15:43:47 +01:00
|
|
|
QList<QHostAddress> WireguardConfigurator::getIpsFromConf(const QString &input)
|
|
|
|
|
{
|
|
|
|
|
QRegularExpression regex("AllowedIPs = (\\d+\\.\\d+\\.\\d+\\.\\d+)");
|
|
|
|
|
QRegularExpressionMatchIterator matchIterator = regex.globalMatch(input);
|
|
|
|
|
|
|
|
|
|
QList<QHostAddress> ips;
|
|
|
|
|
|
|
|
|
|
while (matchIterator.hasNext()) {
|
|
|
|
|
QRegularExpressionMatch match = matchIterator.next();
|
|
|
|
|
const QString address_string { match.captured(1) };
|
|
|
|
|
const QHostAddress address { address_string };
|
|
|
|
|
if (address.isNull()) {
|
|
|
|
|
qWarning() << "Couldn't recognize the ip address: " << address_string;
|
|
|
|
|
} else {
|
|
|
|
|
ips << address;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ips;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-12 11:59:36 +03:00
|
|
|
WireguardConfigurator::ConnectionData WireguardConfigurator::prepareWireguardConfig(const ServerCredentials &credentials,
|
2023-08-31 16:00:41 +05:00
|
|
|
DockerContainer container,
|
2026-04-30 14:53:03 +08:00
|
|
|
const WireGuardServerConfig* serverConfig,
|
|
|
|
|
const AwgServerConfig* awgServerConfig,
|
|
|
|
|
const DnsSettings &dnsSettings,
|
2025-03-06 15:43:47 +01:00
|
|
|
ErrorCode &errorCode)
|
2021-06-12 11:59:36 +03:00
|
|
|
{
|
|
|
|
|
WireguardConfigurator::ConnectionData connData = WireguardConfigurator::genClientKeys();
|
|
|
|
|
connData.host = credentials.hostName;
|
2026-04-30 14:53:03 +08:00
|
|
|
|
|
|
|
|
QString portStr = m_defaultPort;
|
|
|
|
|
if (serverConfig && !serverConfig->port.isEmpty()) {
|
|
|
|
|
portStr = serverConfig->port;
|
|
|
|
|
} else if (awgServerConfig && !awgServerConfig->port.isEmpty()) {
|
|
|
|
|
portStr = awgServerConfig->port;
|
|
|
|
|
}
|
|
|
|
|
connData.port = portStr;
|
2021-06-12 11:59:36 +03:00
|
|
|
|
|
|
|
|
if (connData.clientPrivKey.isEmpty() || connData.clientPubKey.isEmpty()) {
|
2024-04-01 20:20:02 +07:00
|
|
|
errorCode = ErrorCode::InternalError;
|
2021-06-12 11:59:36 +03:00
|
|
|
return connData;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-11 15:18:36 +08:00
|
|
|
QString configPath = m_serverConfigPath;
|
|
|
|
|
if (container == DockerContainer::Awg) {
|
|
|
|
|
configPath = amnezia::protocols::awg::serverLegacyConfigPath;
|
|
|
|
|
}
|
|
|
|
|
QString getIpsScript = QString("cat %1 | grep AllowedIPs").arg(configPath);
|
2025-03-06 15:43:47 +01:00
|
|
|
QString stdOut;
|
|
|
|
|
auto cbReadStdOut = [&](const QString &data, libssh::Client &) {
|
|
|
|
|
stdOut += data + "\n";
|
|
|
|
|
return ErrorCode::NoError;
|
|
|
|
|
};
|
2022-09-19 00:44:00 +03:00
|
|
|
|
2026-04-30 14:53:03 +08:00
|
|
|
errorCode = m_sshSession->runContainerScript(credentials, container, getIpsScript, cbReadStdOut);
|
2025-03-06 15:43:47 +01:00
|
|
|
if (errorCode != ErrorCode::NoError) {
|
|
|
|
|
return connData;
|
|
|
|
|
}
|
|
|
|
|
auto ips = getIpsFromConf(stdOut);
|
|
|
|
|
|
|
|
|
|
QHostAddress nextIp = [&] {
|
|
|
|
|
QHostAddress result;
|
|
|
|
|
QHostAddress lastIp;
|
2026-04-30 14:53:03 +08:00
|
|
|
QString subnetAddress = protocols::wireguard::defaultSubnetAddress;
|
|
|
|
|
if (serverConfig && !serverConfig->subnetAddress.isEmpty()) {
|
|
|
|
|
subnetAddress = serverConfig->subnetAddress;
|
|
|
|
|
} else if (awgServerConfig && !awgServerConfig->subnetAddress.isEmpty()) {
|
|
|
|
|
subnetAddress = awgServerConfig->subnetAddress;
|
|
|
|
|
}
|
2025-03-06 15:43:47 +01:00
|
|
|
if (ips.empty()) {
|
2026-04-30 14:53:03 +08:00
|
|
|
lastIp.setAddress(subnetAddress);
|
2023-08-31 16:00:41 +05:00
|
|
|
} else {
|
2025-03-06 15:43:47 +01:00
|
|
|
lastIp = ips.last();
|
2021-12-25 21:14:55 +03:00
|
|
|
}
|
2025-03-06 15:43:47 +01:00
|
|
|
quint8 lastOctet = static_cast<quint8>(lastIp.toIPv4Address());
|
|
|
|
|
switch (lastOctet) {
|
|
|
|
|
case 254: result.setAddress(lastIp.toIPv4Address() + 3); break;
|
|
|
|
|
case 255: result.setAddress(lastIp.toIPv4Address() + 2); break;
|
|
|
|
|
default: result.setAddress(lastIp.toIPv4Address() + 1); break;
|
2021-12-25 21:14:55 +03:00
|
|
|
}
|
|
|
|
|
|
2025-03-06 15:43:47 +01:00
|
|
|
return result;
|
|
|
|
|
}();
|
|
|
|
|
|
|
|
|
|
connData.clientIP = nextIp.toString();
|
2021-12-25 21:14:55 +03:00
|
|
|
|
|
|
|
|
// Get keys
|
2025-03-06 15:43:47 +01:00
|
|
|
connData.serverPubKey =
|
2026-04-30 14:53:03 +08:00
|
|
|
m_sshSession->getTextFileFromContainer(container, credentials, m_serverPublicKeyPath, errorCode);
|
2021-06-12 11:59:36 +03:00
|
|
|
connData.serverPubKey.replace("\n", "");
|
2024-04-01 20:20:02 +07:00
|
|
|
if (errorCode != ErrorCode::NoError) {
|
2021-06-12 11:59:36 +03:00
|
|
|
return connData;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-30 14:53:03 +08:00
|
|
|
connData.pskKey = m_sshSession->getTextFileFromContainer(container, credentials, m_serverPskKeyPath, errorCode);
|
2021-06-12 11:59:36 +03:00
|
|
|
connData.pskKey.replace("\n", "");
|
|
|
|
|
|
2024-04-01 20:20:02 +07:00
|
|
|
if (errorCode != ErrorCode::NoError) {
|
2021-06-12 11:59:36 +03:00
|
|
|
return connData;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-25 21:14:55 +03:00
|
|
|
// Add client to config
|
2023-08-31 16:00:41 +05:00
|
|
|
QString configPart = QString("[Peer]\n"
|
|
|
|
|
"PublicKey = %1\n"
|
|
|
|
|
"PresharedKey = %2\n"
|
|
|
|
|
"AllowedIPs = %3/32\n\n")
|
2023-09-27 00:40:01 +05:00
|
|
|
.arg(connData.clientPubKey, connData.pskKey, connData.clientIP);
|
2021-06-12 11:59:36 +03:00
|
|
|
|
2026-04-30 14:53:03 +08:00
|
|
|
errorCode = m_sshSession->uploadTextFileToContainer(container, credentials, configPart, configPath,
|
2024-04-12 20:00:21 +05:00
|
|
|
libssh::ScpOverwriteMode::ScpAppendToExisting);
|
2021-06-12 11:59:36 +03:00
|
|
|
|
2024-04-01 20:20:02 +07:00
|
|
|
if (errorCode != ErrorCode::NoError) {
|
2021-06-12 11:59:36 +03:00
|
|
|
return connData;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-11 15:18:36 +08:00
|
|
|
bool isAwg = (container == DockerContainer::Awg2);
|
|
|
|
|
QString bin = isAwg ? QStringLiteral("awg") : QStringLiteral("wg");
|
|
|
|
|
QString iface = isAwg ? QStringLiteral("awg0") : QStringLiteral("wg0");
|
|
|
|
|
QString script = QString(
|
|
|
|
|
"sudo docker exec -i $CONTAINER_NAME bash -c '%1 syncconf %2 <(%1-quick strip %3)'").arg(bin, iface, configPath);
|
2023-09-27 00:40:01 +05:00
|
|
|
|
2026-04-30 14:53:03 +08:00
|
|
|
errorCode = m_sshSession->runScript(
|
2025-03-06 15:43:47 +01:00
|
|
|
credentials,
|
2026-04-30 14:53:03 +08:00
|
|
|
m_sshSession->replaceVars(script, amnezia::genBaseVars(credentials, container, dnsSettings.primaryDns, dnsSettings.secondaryDns)));
|
2021-06-12 11:59:36 +03:00
|
|
|
|
|
|
|
|
return connData;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-30 14:53:03 +08:00
|
|
|
ProtocolConfig WireguardConfigurator::createConfig(const ServerCredentials &credentials, DockerContainer container,
|
|
|
|
|
const ContainerConfig &containerConfig,
|
|
|
|
|
const DnsSettings &dnsSettings,
|
|
|
|
|
ErrorCode &errorCode)
|
2021-06-12 11:59:36 +03:00
|
|
|
{
|
2026-04-30 14:53:03 +08:00
|
|
|
const WireGuardServerConfig* wireguardServerConfig = nullptr;
|
|
|
|
|
const WireGuardClientConfig* wireguardClientConfig = nullptr;
|
|
|
|
|
const AwgServerConfig* awgServerConfig = nullptr;
|
|
|
|
|
const AwgClientConfig* awgClientConfig = nullptr;
|
|
|
|
|
|
|
|
|
|
if (auto* wireGuardProtocolConfig = containerConfig.getWireGuardProtocolConfig()) {
|
|
|
|
|
wireguardServerConfig = &wireGuardProtocolConfig->serverConfig;
|
|
|
|
|
if (wireGuardProtocolConfig->clientConfig.has_value()) {
|
|
|
|
|
wireguardClientConfig = &wireGuardProtocolConfig->clientConfig.value();
|
|
|
|
|
}
|
|
|
|
|
} else if (auto* awgProtocolConfig = containerConfig.getAwgProtocolConfig()) {
|
|
|
|
|
awgServerConfig = &awgProtocolConfig->serverConfig;
|
|
|
|
|
if (awgProtocolConfig->clientConfig.has_value()) {
|
|
|
|
|
awgClientConfig = &awgProtocolConfig->clientConfig.value();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
amnezia::ScriptVars vars = amnezia::genBaseVars(credentials, container, dnsSettings.primaryDns, dnsSettings.secondaryDns);
|
|
|
|
|
vars.append(amnezia::genProtocolVarsForContainer(container, containerConfig));
|
2023-09-27 00:40:01 +05:00
|
|
|
QString scriptData = amnezia::scriptData(m_configTemplate, container);
|
2026-04-30 14:53:03 +08:00
|
|
|
QString config = m_sshSession->replaceVars(scriptData, vars);
|
2021-06-12 11:59:36 +03:00
|
|
|
|
2026-04-30 14:53:03 +08:00
|
|
|
ConnectionData connData = prepareWireguardConfig(credentials, container, wireguardServerConfig, awgServerConfig, dnsSettings, errorCode);
|
2024-04-01 20:20:02 +07:00
|
|
|
if (errorCode != ErrorCode::NoError) {
|
2026-04-30 14:53:03 +08:00
|
|
|
return WireGuardProtocolConfig{};
|
2021-06-12 11:59:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
config.replace("$WIREGUARD_CLIENT_PRIVATE_KEY", connData.clientPrivKey);
|
2021-12-25 21:14:55 +03:00
|
|
|
config.replace("$WIREGUARD_CLIENT_IP", connData.clientIP);
|
2021-06-12 11:59:36 +03:00
|
|
|
config.replace("$WIREGUARD_SERVER_PUBLIC_KEY", connData.serverPubKey);
|
|
|
|
|
config.replace("$WIREGUARD_PSK", connData.pskKey);
|
|
|
|
|
|
2026-04-30 14:53:03 +08:00
|
|
|
QString mtu = protocols::wireguard::defaultMtu;
|
|
|
|
|
if (wireguardClientConfig && !wireguardClientConfig->mtu.isEmpty()) {
|
|
|
|
|
mtu = wireguardClientConfig->mtu;
|
|
|
|
|
} else if (awgClientConfig && !awgClientConfig->mtu.isEmpty()) {
|
|
|
|
|
mtu = awgClientConfig->mtu;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WireGuardProtocolConfig protocolConfig;
|
|
|
|
|
if (wireguardServerConfig) {
|
|
|
|
|
protocolConfig.serverConfig = *wireguardServerConfig;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WireGuardClientConfig clientConfig;
|
|
|
|
|
clientConfig.nativeConfig = config;
|
|
|
|
|
clientConfig.hostName = connData.host;
|
|
|
|
|
clientConfig.port = connData.port.toInt();
|
|
|
|
|
clientConfig.clientIp = connData.clientIP;
|
|
|
|
|
clientConfig.clientPrivateKey = connData.clientPrivKey;
|
|
|
|
|
clientConfig.clientPublicKey = connData.clientPubKey;
|
|
|
|
|
clientConfig.serverPublicKey = connData.serverPubKey;
|
|
|
|
|
clientConfig.presharedKey = connData.pskKey;
|
|
|
|
|
clientConfig.clientId = connData.clientPubKey;
|
|
|
|
|
clientConfig.allowedIps = QStringList { "0.0.0.0/0", "::/0" };
|
|
|
|
|
clientConfig.persistentKeepAlive = "25";
|
|
|
|
|
clientConfig.mtu = mtu;
|
|
|
|
|
clientConfig.isObfuscationEnabled = false;
|
|
|
|
|
|
|
|
|
|
protocolConfig.setClientConfig(clientConfig);
|
|
|
|
|
|
|
|
|
|
return protocolConfig;
|
2021-06-12 11:59:36 +03:00
|
|
|
}
|
|
|
|
|
|
2026-04-30 14:53:03 +08:00
|
|
|
ProtocolConfig WireguardConfigurator::processConfigWithLocalSettings(const ConnectionSettings &settings,
|
|
|
|
|
ProtocolConfig protocolConfig)
|
2021-06-12 11:59:36 +03:00
|
|
|
{
|
2026-04-30 14:53:03 +08:00
|
|
|
return ConfiguratorBase::processConfigWithLocalSettings(settings, protocolConfig);
|
2021-06-12 11:59:36 +03:00
|
|
|
}
|
|
|
|
|
|
2026-04-30 14:53:03 +08:00
|
|
|
ProtocolConfig WireguardConfigurator::processConfigWithExportSettings(const ExportSettings &settings,
|
|
|
|
|
ProtocolConfig protocolConfig)
|
2021-06-12 11:59:36 +03:00
|
|
|
{
|
2026-04-30 14:53:03 +08:00
|
|
|
return ConfiguratorBase::processConfigWithExportSettings(settings, protocolConfig);
|
2021-06-12 11:59:36 +03:00
|
|
|
}
|