From 90912f9231ec558708daa29671fd8e0e4d864e40 Mon Sep 17 00:00:00 2001 From: Mykola Baibuz Date: Fri, 26 Jul 2024 00:55:13 +0300 Subject: [PATCH 01/14] Fix Windows IPsec --- .../protocols/ikev2_vpn_protocol_windows.cpp | 26 ++++++++++--------- .../PageSetupWizardProtocolSettings.qml | 3 ++- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/client/protocols/ikev2_vpn_protocol_windows.cpp b/client/protocols/ikev2_vpn_protocol_windows.cpp index 5c471e220..ac5966cbb 100644 --- a/client/protocols/ikev2_vpn_protocol_windows.cpp +++ b/client/protocols/ikev2_vpn_protocol_windows.cpp @@ -10,6 +10,7 @@ #include "ikev2_vpn_protocol_windows.h" #include "utilities.h" + static Ikev2Protocol* self = nullptr; static std::mutex rasDialFuncMutex; @@ -80,10 +81,10 @@ void Ikev2Protocol::newConnectionStateEventReceived(UINT unMsg, tagRASCONNSTATE case RASCS_AuthNotify: //qDebug()<<__FUNCTION__ << __LINE__; if (dwError != 0) { - //qDebug() << "have error" << dwError; + qDebug() << "have error" << dwError; setConnectionState(Vpn::ConnectionState::Disconnected); } else { - //qDebug() << "RASCS_AuthNotify but no error" << dwError; + qDebug() << "RASCS_AuthNotify but no error" << dwError; } break; case RASCS_AuthRetry: @@ -193,16 +194,16 @@ ErrorCode Ikev2Protocol::start() return ErrorCode::AmneziaServiceConnectionFailed; } - certInstallProcess->waitForSource(1000); + certInstallProcess->waitForSource(); if (!certInstallProcess->isInitialized()) { qWarning() << "IpcProcess replica is not connected!"; setLastError(ErrorCode::AmneziaServiceConnectionFailed); return ErrorCode::AmneziaServiceConnectionFailed; } certInstallProcess->setProgram(PermittedProcess::CertUtil); - QStringList arguments({"-f" , "-importpfx", - "-p", m_config[config_key::password].toString(), - certFile.fileName(), "NoExport" + QString password = QString("-p %1").arg(m_config[config_key::password].toString()); + QStringList arguments({"-f", "-importpfx", password, + QDir::toNativeSeparators(certFile.fileName()), "NoExport" }); certInstallProcess->setArguments(arguments); @@ -227,9 +228,8 @@ ErrorCode Ikev2Protocol::start() } { - auto adapterConfigProcess = new QProcess; - - adapterConfigProcess->setProgram("powershell"); + QProcess adapterConfigProcess; + adapterConfigProcess.setProgram("powershell"); QString arguments = QString("-command \"Set-VpnConnectionIPsecConfiguration\" " "-ConnectionName '%1' " "-AuthenticationTransformConstants GCMAES128 " @@ -240,10 +240,11 @@ ErrorCode Ikev2Protocol::start() "-DHGroup Group14 " "-PassThru -Force\"") .arg(tunnelName()); - adapterConfigProcess->setNativeArguments(arguments); - adapterConfigProcess->start(); - adapterConfigProcess->waitForFinished(5000); + adapterConfigProcess.setNativeArguments(arguments); + + adapterConfigProcess.start(); + adapterConfigProcess.waitForFinished(5000); } //*/ { @@ -299,6 +300,7 @@ bool Ikev2Protocol::connect_to_vpn(const QString & vpn_name){ auto ret = RasDial(NULL, NULL, &RasDialParams, 0, &RasDialFuncCallback, &hRasConn); + if (ret == ERROR_SUCCESS){ return true; } diff --git a/client/ui/qml/Pages2/PageSetupWizardProtocolSettings.qml b/client/ui/qml/Pages2/PageSetupWizardProtocolSettings.qml index d3e4aa4a6..d0841f67b 100644 --- a/client/ui/qml/Pages2/PageSetupWizardProtocolSettings.qml +++ b/client/ui/qml/Pages2/PageSetupWizardProtocolSettings.qml @@ -263,7 +263,8 @@ PageType { clickedFunc: function() { if (!port.textField.acceptableInput && - ContainerProps.containerTypeToString(dockerContainer) !== "torwebsite") { + ContainerProps.containerTypeToString(dockerContainer) !== "torwebsite" && + ContainerProps.containerTypeToString(dockerContainer) !== "ikev2") { port.errorText = qsTr("The port must be in the range of 1 to 65535") return } From b0b185027e377ecf3fd13e8ad5ba60b6a9a482b3 Mon Sep 17 00:00:00 2001 From: Mykola Baibuz Date: Thu, 1 Aug 2024 21:37:56 +0300 Subject: [PATCH 02/14] Linux IPSec initial --- client/CMakeLists.txt | 9 ++ client/configurators/ikev2_configurator.cpp | 40 +++++++ client/configurators/ikev2_configurator.h | 4 + client/containers/containers_defs.cpp | 9 +- client/core/scripts_registry.cpp | 1 + client/core/scripts_registry.h | 3 +- client/protocols/ikev2_vpn_protocol_linux.cpp | 101 ++++++++++++++++++ client/protocols/ikev2_vpn_protocol_linux.h | 49 +++++++++ client/protocols/protocols_defs.h | 1 + client/protocols/vpnprotocol.cpp | 6 +- client/resources.qrc | 1 + client/server_scripts/ipsec/template.conf | 30 ++++++ ipc/ipc_interface.rep | 6 ++ ipc/ipcserver.cpp | 57 ++++++++++ ipc/ipcserver.h | 6 ++ 15 files changed, 313 insertions(+), 10 deletions(-) create mode 100644 client/protocols/ikev2_vpn_protocol_linux.cpp create mode 100644 client/protocols/ikev2_vpn_protocol_linux.h create mode 100644 client/server_scripts/ipsec/template.conf diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index 1fc28b82f..baad1b9a1 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -306,6 +306,15 @@ endif() if(LINUX AND NOT ANDROID) set(LIBS ${LIBS} -static-libstdc++ -static-libgcc -ldl) link_directories(${CMAKE_CURRENT_LIST_DIR}/platforms/linux) + + set(HEADERS ${HEADERS} + ${CMAKE_CURRENT_LIST_DIR}/protocols/ikev2_vpn_protocol_linux.h + ) + + set(SOURCES ${SOURCES} + ${CMAKE_CURRENT_LIST_DIR}/protocols/ikev2_vpn_protocol_linux.cpp + ) + endif() if(WIN32 OR (APPLE AND NOT IOS) OR (LINUX AND NOT ANDROID)) diff --git a/client/configurators/ikev2_configurator.cpp b/client/configurators/ikev2_configurator.cpp index 894a0e3df..fea17f499 100644 --- a/client/configurators/ikev2_configurator.cpp +++ b/client/configurators/ikev2_configurator.cpp @@ -64,6 +64,26 @@ QString Ikev2Configurator::createConfig(const ServerCredentials &credentials, Do return ""; } +#if defined(Q_OS_LINUX) + QString config = m_serverController->replaceVars(amnezia::scriptData(ProtocolScriptType::ipsec_template, container), + m_serverController->genVarsForScript(credentials, container, containerConfig)); + + config.replace("$CLIENT_NAME", connData.clientId); + config.replace("$UUID1", QUuid::createUuid().toString()); + config.replace("$SERVER_ADDR", connData.host); + + QJsonObject jConfig; + jConfig[config_key::config] = config; + + jConfig[config_key::hostName] = connData.host; + jConfig[config_key::userName] = connData.clientId; + jConfig[config_key::cert] = QString(connData.clientCert.toBase64()); + jConfig[config_key::cacert] = QString(connData.caCert); + jConfig[config_key::password] = connData.password; + + return QJsonDocument(jConfig).toJson(); +#endif + return genIkev2Config(connData); } @@ -73,6 +93,7 @@ QString Ikev2Configurator::genIkev2Config(const ConnectionData &connData) config[config_key::hostName] = connData.host; config[config_key::userName] = connData.clientId; config[config_key::cert] = QString(connData.clientCert.toBase64()); + config[config_key::cacert] = QString(connData.caCert); config[config_key::password] = connData.password; return QJsonDocument(config).toJson(); @@ -115,3 +136,22 @@ QString Ikev2Configurator::genStrongSwanConfig(const ConnectionData &connData) return config; } + +QString Ikev2Configurator::processConfigWithLocalSettings(const QPair &dns, const bool isApiConfig, + QString &protocolConfigString) +{ + processConfigWithDnsSettings(dns, protocolConfigString); + + QJsonObject json; + json[config_key::config] = protocolConfigString; + return QJsonDocument(json).toJson(); +} + +QString Ikev2Configurator::processConfigWithExportSettings(const QPair &dns, const bool isApiConfig, + QString &protocolConfigString) +{ + processConfigWithDnsSettings(dns, protocolConfigString); + QJsonObject json; + json[config_key::config] = protocolConfigString; + return QJsonDocument(json).toJson(); +} diff --git a/client/configurators/ikev2_configurator.h b/client/configurators/ikev2_configurator.h index e3a852163..673e51394 100644 --- a/client/configurators/ikev2_configurator.h +++ b/client/configurators/ikev2_configurator.h @@ -27,6 +27,10 @@ class Ikev2Configurator : public ConfiguratorBase QString genIkev2Config(const ConnectionData &connData); QString genMobileConfig(const ConnectionData &connData); QString genStrongSwanConfig(const ConnectionData &connData); + QString genIPSecConfig(const ConnectionData &connData); + + QString processConfigWithLocalSettings(const QPair &dns, const bool isApiConfig, QString &protocolConfigString); + QString processConfigWithExportSettings(const QPair &dns, const bool isApiConfig, QString &protocolConfigString); ConnectionData prepareIkev2Config(const ServerCredentials &credentials, DockerContainer container, ErrorCode &errorCode); diff --git a/client/containers/containers_defs.cpp b/client/containers/containers_defs.cpp index 91d4b0671..cdf14db8c 100644 --- a/client/containers/containers_defs.cpp +++ b/client/containers/containers_defs.cpp @@ -277,7 +277,7 @@ Proto ContainerProps::defaultProtocol(DockerContainer c) bool ContainerProps::isSupportedByCurrentPlatform(DockerContainer c) { -#ifdef Q_OS_WINDOWS +#if defined(Q_OS_WINDOWS) || defined(Q_OS_LINUX) return true; #elif defined(Q_OS_IOS) @@ -309,13 +309,6 @@ bool ContainerProps::isSupportedByCurrentPlatform(DockerContainer c) case DockerContainer::SSXray: return true; default: return false; } - -#elif defined(Q_OS_LINUX) - switch (c) { - case DockerContainer::Ipsec: return false; - default: return true; - } - #else return false; #endif diff --git a/client/core/scripts_registry.cpp b/client/core/scripts_registry.cpp index 95b5df4ad..150899921 100644 --- a/client/core/scripts_registry.cpp +++ b/client/core/scripts_registry.cpp @@ -50,6 +50,7 @@ QString amnezia::scriptName(ProtocolScriptType type) case ProtocolScriptType::wireguard_template: return QLatin1String("template.conf"); case ProtocolScriptType::awg_template: return QLatin1String("template.conf"); case ProtocolScriptType::xray_template: return QLatin1String("template.json"); + case ProtocolScriptType::ipsec_template: return QLatin1String("template.conf"); default: return QString(); } } diff --git a/client/core/scripts_registry.h b/client/core/scripts_registry.h index d952dafb0..ced7eb005 100644 --- a/client/core/scripts_registry.h +++ b/client/core/scripts_registry.h @@ -28,7 +28,8 @@ enum ProtocolScriptType { openvpn_template, wireguard_template, awg_template, - xray_template + xray_template, + ipsec_template }; diff --git a/client/protocols/ikev2_vpn_protocol_linux.cpp b/client/protocols/ikev2_vpn_protocol_linux.cpp new file mode 100644 index 000000000..9465036b0 --- /dev/null +++ b/client/protocols/ikev2_vpn_protocol_linux.cpp @@ -0,0 +1,101 @@ +#include +#include +#include + +#include + +#include + +#include "logger.h" +#include "ikev2_vpn_protocol_linux.h" +#include "utilities.h" +#include "core/ipcclient.h" +#include +#include +#include + + +static Ikev2Protocol* self = nullptr; + + +Ikev2Protocol::Ikev2Protocol(const QJsonObject &configuration, QObject* parent) : + VpnProtocol(configuration, parent) +{ + self = this; + readIkev2Configuration(configuration); +} + +Ikev2Protocol::~Ikev2Protocol() +{ + qDebug() << "IpsecProtocol::~IpsecProtocol()"; + disconnect_vpn(); + Ikev2Protocol::stop(); +} + +void Ikev2Protocol::stop() +{ + setConnectionState(Vpn::ConnectionState::Disconnected); + qDebug() << "IpsecProtocol::stop()"; +} + + +void Ikev2Protocol::readIkev2Configuration(const QJsonObject &configuration) +{ + QJsonObject ikev2_data = configuration.value(ProtocolProps::key_proto_config_data(Proto::Ikev2)).toObject(); + m_config = QJsonDocument::fromJson(ikev2_data.value(config_key::config).toString().toUtf8()).object(); + +} + +ErrorCode Ikev2Protocol::start() +{ + STACK_OF(X509) *certstack = sk_X509_new_null(); + BIO *p12 = BIO_new(BIO_s_mem()); + + EVP_PKEY *pkey; + X509 *cert; + + BIO_write(p12, QByteArray::fromBase64(m_config[config_key::cert].toString().toUtf8()), + QByteArray::fromBase64(m_config[config_key::cert].toString().toUtf8()).size()); + + PKCS12 *pkcs12 = d2i_PKCS12_bio(p12, NULL); + PKCS12_parse(pkcs12, m_config[config_key::password].toString().toStdString().c_str(), &pkey, &cert, &certstack); + BIO *bio = BIO_new(BIO_s_mem()); + PEM_write_bio_X509(bio, cert); + + BUF_MEM *mem = NULL; + BIO_get_mem_ptr(bio, &mem); + + std::string pem(mem->data, mem->length); + qDebug() << pem; + + QString alias(pem.c_str()); + + IpcClient::Interface()->writeIPsecUserCert(alias, m_config[config_key::userName].toString()); + IpcClient::Interface()->writeIPsecConfig(m_config[config_key::config].toString()); + IpcClient::Interface()->writeIPsecCaCert(m_config[config_key::cacert].toString(), m_config[config_key::userName].toString()); + IpcClient::Interface()->writeIPsecPrivate(m_config[config_key::cert].toString(), m_config[config_key::userName].toString()); + IpcClient::Interface()->writeIPsecPrivatePass(m_config[config_key::password].toString(), m_config[config_key::userName].toString()); + + + setConnectionState(Vpn::ConnectionState::Connected); + return ErrorCode::NoError; +} +//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +bool Ikev2Protocol::create_new_vpn(const QString & vpn_name, + const QString & serv_addr){ + qDebug() << "Ikev2Protocol::create_new_vpn()"; + return true; +} +//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +bool Ikev2Protocol::delete_vpn_connection(const QString &vpn_name){ + + return false; +} +//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +bool Ikev2Protocol::connect_to_vpn(const QString & vpn_name){ + return false; +} +//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +bool Ikev2Protocol::disconnect_vpn(){ + return true; +} diff --git a/client/protocols/ikev2_vpn_protocol_linux.h b/client/protocols/ikev2_vpn_protocol_linux.h new file mode 100644 index 000000000..11ca2140f --- /dev/null +++ b/client/protocols/ikev2_vpn_protocol_linux.h @@ -0,0 +1,49 @@ +#ifndef IKEV2_VPN_PROTOCOL_LINUX_H +#define IKEV2_VPN_PROTOCOL_LINUX_H + +#include +#include +#include +#include +#include + +#include "vpnprotocol.h" + +#include +#include +#include +#include +#include +#include + +class Ikev2Protocol : public VpnProtocol +{ + Q_OBJECT + +public: + explicit Ikev2Protocol(const QJsonObject& configuration, QObject* parent = nullptr); + virtual ~Ikev2Protocol() override; + + ErrorCode start() override; + void stop() override; + + static QString tunnelName() { return "AmneziaVPN IKEv2"; } + + +private: + void readIkev2Configuration(const QJsonObject &configuration); + +private: + QJsonObject m_config; + + + bool create_new_vpn(const QString & vpn_name, + const QString & serv_addr); + bool delete_vpn_connection(const QString &vpn_name); + + bool connect_to_vpn(const QString & vpn_name); + bool disconnect_vpn(); +}; + + +#endif // IKEV2_VPN_PROTOCOL_LINUX_H diff --git a/client/protocols/protocols_defs.h b/client/protocols/protocols_defs.h index 56be0d7d3..39d0b3aa6 100644 --- a/client/protocols/protocols_defs.h +++ b/client/protocols/protocols_defs.h @@ -24,6 +24,7 @@ namespace amnezia constexpr char description[] = "description"; constexpr char name[] = "name"; constexpr char cert[] = "cert"; + constexpr char cacert[] = "cacert"; constexpr char config[] = "config"; constexpr char containers[] = "containers"; diff --git a/client/protocols/vpnprotocol.cpp b/client/protocols/vpnprotocol.cpp index 056089b8f..765e86ab3 100644 --- a/client/protocols/vpnprotocol.cpp +++ b/client/protocols/vpnprotocol.cpp @@ -16,6 +16,10 @@ #include "ikev2_vpn_protocol_windows.h" #endif +#ifdef Q_OS_LINUX +#include "ikev2_vpn_protocol_linux.h" +#endif + VpnProtocol::VpnProtocol(const QJsonObject &configuration, QObject *parent) : QObject(parent), m_connectionState(Vpn::ConnectionState::Unknown), @@ -106,7 +110,7 @@ QString VpnProtocol::vpnGateway() const VpnProtocol *VpnProtocol::factory(DockerContainer container, const QJsonObject &configuration) { switch (container) { -#if defined(Q_OS_WINDOWS) +#if defined(Q_OS_WINDOWS) || defined(Q_OS_LINUX) case DockerContainer::Ipsec: return new Ikev2Protocol(configuration); #endif #if defined(Q_OS_WINDOWS) || defined(Q_OS_MACX) || (defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID)) diff --git a/client/resources.qrc b/client/resources.qrc index 98f048022..a12b08050 100644 --- a/client/resources.qrc +++ b/client/resources.qrc @@ -236,5 +236,6 @@ server_scripts/socks5_proxy/Dockerfile server_scripts/socks5_proxy/configure_container.sh server_scripts/socks5_proxy/start.sh + server_scripts/ipsec/template.conf diff --git a/client/server_scripts/ipsec/template.conf b/client/server_scripts/ipsec/template.conf new file mode 100644 index 000000000..53fa44bdb --- /dev/null +++ b/client/server_scripts/ipsec/template.conf @@ -0,0 +1,30 @@ +config setup + charondebug="ike 1, knl 1, cfg 0" + uniqueids=no + +conn ikev2-vpn + auto=add + compress=no + type=tunnel + keyexchange=ikev2 + fragmentation=yes + forceencaps=yes + dpdaction=clear + dpddelay=300s + rekey=no + left=%any + leftid=$CLIENT_NAME + leftcert=$CLIENT_NAME.crt + leftsendcert=always + leftsubnet=0.0.0.0/0 + right=%any + rightid=%any + rightauth=rsa + rightsourceip=$IPSEC_VPN_L2TP_NET + rightdns=$PRIMARY_DNS,$SECONDARY_DNS + rightsendcert=never + eap_identity=%identity + ike=aes256-sha1-modp1024,aes128-sha1-modp1024 + esp=aes256-sha1,aes256-sha2_512 + + diff --git a/ipc/ipc_interface.rep b/ipc/ipc_interface.rep index 79f2d0422..02e8c5248 100644 --- a/ipc/ipc_interface.rep +++ b/ipc/ipc_interface.rep @@ -32,5 +32,11 @@ class IpcInterface SLOT( bool enablePeerTraffic( const QJsonObject &configStr) ); SLOT( bool enableKillSwitch( const QJsonObject &excludeAddr, int vpnAdapterIndex) ); SLOT( bool updateResolvers(const QString& ifname, const QList& resolvers) ); + + SLOT( bool writeIPsecCaCert(QString cacert, QString uuid) ); + SLOT( bool writeIPsecPrivate(QString privKey, QString uuid) ); + SLOT( bool writeIPsecConfig(QString config) ); + SLOT( bool writeIPsecUserCert(QString usercert, QString uuid) ); + SLOT( bool writeIPsecPrivatePass(QString pass, QString uuid) ); }; diff --git a/ipc/ipcserver.cpp b/ipc/ipcserver.cpp index c734912b1..501384584 100644 --- a/ipc/ipcserver.cpp +++ b/ipc/ipcserver.cpp @@ -5,6 +5,7 @@ #include #include +#include "qjsonarray.h" #include "router.h" #include "logger.h" @@ -308,6 +309,62 @@ bool IpcServer::disableKillSwitch() return true; } +bool IpcServer::writeIPsecConfig(QString config) +{ + qDebug() << "IPSEC: IPSec config file"; + QString configFile = QString("/etc/ipsec.conf"); + QFile ipSecConfFile(configFile); + if (ipSecConfFile.open(QIODevice::WriteOnly)) { + ipSecConfFile.write(config.toUtf8()); + ipSecConfFile.close(); + } +} + +bool IpcServer::writeIPsecUserCert(QString usercert, QString uuid) +{ + qDebug() << "IPSEC: Write user cert " << uuid; + QString certName = QString("/etc/ipsec.d/certs/%1.crt").arg(uuid); + QFile userCertFile(certName); + if (userCertFile.open(QIODevice::WriteOnly)) { + userCertFile.write(usercert.toUtf8()); + userCertFile.close(); + } +} + +bool IpcServer::writeIPsecCaCert(QString cacert, QString uuid) +{ + qDebug() << "IPSEC: Write CA cert user " << uuid; + QString certName = QString("/etc/ipsec.d/cacerts/%1.crt").arg(uuid); + QFile caCertFile(certName); + if (caCertFile.open(QIODevice::WriteOnly)) { + caCertFile.write(cacert.toUtf8()); + caCertFile.close(); + } +} + +bool IpcServer::writeIPsecPrivate(QString privKey, QString uuid) +{ + qDebug() << "IPSEC: User private key " << uuid; + QString privateKey = QString("/etc/ipsec.d/private/%1.p12").arg(uuid); + QFile pKeyFile(privateKey); + if (pKeyFile.open(QIODevice::WriteOnly)) { + pKeyFile.write(QByteArray::fromBase64(privKey.toUtf8())); + pKeyFile.close(); + } +} + + +bool IpcServer::writeIPsecPrivatePass(QString pass, QString uuid) +{ + qDebug() << "IPSEC: User private key " << uuid; + QFile secretsFile("/etc/ipsec.secrets"); + QString P12 = QString(": P12 %1.p12 \"%2\" \n").arg(uuid, pass); + if (secretsFile.open(QIODevice::WriteOnly | QIODevice::Append)) { + secretsFile.write(P12.toUtf8()); + secretsFile.close(); + } +} + bool IpcServer::enablePeerTraffic(const QJsonObject &configStr) { #ifdef Q_OS_WIN diff --git a/ipc/ipcserver.h b/ipc/ipcserver.h index bd474481c..43ab32102 100644 --- a/ipc/ipcserver.h +++ b/ipc/ipcserver.h @@ -35,6 +35,12 @@ class IpcServer : public IpcInterfaceSource virtual bool enableKillSwitch(const QJsonObject &excludeAddr, int vpnAdapterIndex) override; virtual bool disableKillSwitch() override; virtual bool updateResolvers(const QString& ifname, const QList& resolvers) override; + virtual bool writeIPsecCaCert(QString cacert, QString uuid) override; + virtual bool writeIPsecPrivate(QString privKey, QString uuid) override; + virtual bool writeIPsecConfig(QString config) override; + virtual bool writeIPsecUserCert(QString usercert, QString uuid) override; + virtual bool writeIPsecPrivatePass(QString pass, QString uuid) override; + private: int m_localpid = 0; From 89d4c18e87a5713aec597a14c6f1ae4529b97601 Mon Sep 17 00:00:00 2001 From: Mykola Baibuz Date: Sun, 18 Aug 2024 01:46:06 +0300 Subject: [PATCH 03/14] Update IPSec configs templates --- .../ipsec/configure_container.sh | 1 + client/server_scripts/ipsec/template.conf | 21 ++++++++----------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/client/server_scripts/ipsec/configure_container.sh b/client/server_scripts/ipsec/configure_container.sh index 76c4dfafc..0a103198e 100644 --- a/client/server_scripts/ipsec/configure_container.sh +++ b/client/server_scripts/ipsec/configure_container.sh @@ -242,6 +242,7 @@ conn ikev2-cp dpdtimeout=120 dpdaction=clear auto=add + authby=rsa-sha1 ikev2=insist rekey=no pfs=no diff --git a/client/server_scripts/ipsec/template.conf b/client/server_scripts/ipsec/template.conf index 53fa44bdb..d8cf6b1f4 100644 --- a/client/server_scripts/ipsec/template.conf +++ b/client/server_scripts/ipsec/template.conf @@ -4,7 +4,6 @@ config setup conn ikev2-vpn auto=add - compress=no type=tunnel keyexchange=ikev2 fragmentation=yes @@ -12,19 +11,17 @@ conn ikev2-vpn dpdaction=clear dpddelay=300s rekey=no - left=%any leftid=$CLIENT_NAME leftcert=$CLIENT_NAME.crt + leftdns=$PRIMARY_DNS,$SECONDARY_DNS leftsendcert=always - leftsubnet=0.0.0.0/0 - right=%any - rightid=%any - rightauth=rsa - rightsourceip=$IPSEC_VPN_L2TP_NET - rightdns=$PRIMARY_DNS,$SECONDARY_DNS - rightsendcert=never - eap_identity=%identity - ike=aes256-sha1-modp1024,aes128-sha1-modp1024 - esp=aes256-sha1,aes256-sha2_512 + leftsourceip=%config + right=$SERVER_IP_ADDRESS + rightsubnet=0.0.0.0/0 + rightsendcert=never + eap_identity=%identity + encapsulation=yes + ike=aes256-sha256-modp2048,aes256-sha1-modp1024,3des-sha1-modp1024 + esp=aes256-sha256,aes256-sha1,3des-sha1 From 654d219e7eede94069d43cc8889fb519d44f2a16 Mon Sep 17 00:00:00 2001 From: Mykola Baibuz Date: Sun, 18 Aug 2024 13:53:38 +0300 Subject: [PATCH 04/14] Fix Win Build for IPSec protocol --- ipc/ipcserver.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/ipc/ipcserver.cpp b/ipc/ipcserver.cpp index 501384584..cd0bcf575 100644 --- a/ipc/ipcserver.cpp +++ b/ipc/ipcserver.cpp @@ -311,6 +311,7 @@ bool IpcServer::disableKillSwitch() bool IpcServer::writeIPsecConfig(QString config) { +#ifdef Q_OS_LINUX qDebug() << "IPSEC: IPSec config file"; QString configFile = QString("/etc/ipsec.conf"); QFile ipSecConfFile(configFile); @@ -318,10 +319,13 @@ bool IpcServer::writeIPsecConfig(QString config) ipSecConfFile.write(config.toUtf8()); ipSecConfFile.close(); } +#endif + return true; } bool IpcServer::writeIPsecUserCert(QString usercert, QString uuid) { +#ifdef Q_OS_LINUX qDebug() << "IPSEC: Write user cert " << uuid; QString certName = QString("/etc/ipsec.d/certs/%1.crt").arg(uuid); QFile userCertFile(certName); @@ -329,10 +333,13 @@ bool IpcServer::writeIPsecUserCert(QString usercert, QString uuid) userCertFile.write(usercert.toUtf8()); userCertFile.close(); } +#endif + return true; } bool IpcServer::writeIPsecCaCert(QString cacert, QString uuid) { +#ifdef Q_OS_LINUX qDebug() << "IPSEC: Write CA cert user " << uuid; QString certName = QString("/etc/ipsec.d/cacerts/%1.crt").arg(uuid); QFile caCertFile(certName); @@ -340,10 +347,13 @@ bool IpcServer::writeIPsecCaCert(QString cacert, QString uuid) caCertFile.write(cacert.toUtf8()); caCertFile.close(); } +#endif + return true; } bool IpcServer::writeIPsecPrivate(QString privKey, QString uuid) { +#ifdef Q_OS_LINUX qDebug() << "IPSEC: User private key " << uuid; QString privateKey = QString("/etc/ipsec.d/private/%1.p12").arg(uuid); QFile pKeyFile(privateKey); @@ -351,11 +361,14 @@ bool IpcServer::writeIPsecPrivate(QString privKey, QString uuid) pKeyFile.write(QByteArray::fromBase64(privKey.toUtf8())); pKeyFile.close(); } +#endif + return true; } bool IpcServer::writeIPsecPrivatePass(QString pass, QString uuid) { +#ifdef Q_OS_LINUX qDebug() << "IPSEC: User private key " << uuid; QFile secretsFile("/etc/ipsec.secrets"); QString P12 = QString(": P12 %1.p12 \"%2\" \n").arg(uuid, pass); @@ -363,6 +376,8 @@ bool IpcServer::writeIPsecPrivatePass(QString pass, QString uuid) secretsFile.write(P12.toUtf8()); secretsFile.close(); } +#endif + return true; } bool IpcServer::enablePeerTraffic(const QJsonObject &configStr) From 09c58cb39ef995976a45a3946e709c81f76d799d Mon Sep 17 00:00:00 2001 From: Mykola Baibuz Date: Mon, 19 Aug 2024 18:46:53 +0300 Subject: [PATCH 05/14] Fix certwrite for Win IPSec --- client/protocols/ikev2_vpn_protocol_windows.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/client/protocols/ikev2_vpn_protocol_windows.cpp b/client/protocols/ikev2_vpn_protocol_windows.cpp index ac5966cbb..56cf5f6d2 100644 --- a/client/protocols/ikev2_vpn_protocol_windows.cpp +++ b/client/protocols/ikev2_vpn_protocol_windows.cpp @@ -172,7 +172,8 @@ void Ikev2Protocol::newConnectionStateEventReceived(UINT unMsg, tagRASCONNSTATE void Ikev2Protocol::readIkev2Configuration(const QJsonObject &configuration) { - m_config = configuration.value(ProtocolProps::key_proto_config_data(Proto::Ikev2)).toObject(); + QJsonObject ikev2_data = configuration.value(ProtocolProps::key_proto_config_data(Proto::Ikev2)).toObject(); + m_config = QJsonDocument::fromJson(ikev2_data.value(config_key::config).toString().toUtf8()).object(); } ErrorCode Ikev2Protocol::start() @@ -201,6 +202,7 @@ ErrorCode Ikev2Protocol::start() return ErrorCode::AmneziaServiceConnectionFailed; } certInstallProcess->setProgram(PermittedProcess::CertUtil); + QString password = QString("-p %1").arg(m_config[config_key::password].toString()); QStringList arguments({"-f", "-importpfx", password, QDir::toNativeSeparators(certFile.fileName()), "NoExport" From 2d3b9c2752b1e250307237e0cac44addf96f4c09 Mon Sep 17 00:00:00 2001 From: Mykola Baibuz Date: Tue, 20 Aug 2024 13:44:33 +0300 Subject: [PATCH 06/14] Windows import PFX changes --- client/protocols/ikev2_vpn_protocol_windows.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/client/protocols/ikev2_vpn_protocol_windows.cpp b/client/protocols/ikev2_vpn_protocol_windows.cpp index 56cf5f6d2..10bd4e97b 100644 --- a/client/protocols/ikev2_vpn_protocol_windows.cpp +++ b/client/protocols/ikev2_vpn_protocol_windows.cpp @@ -203,10 +203,12 @@ ErrorCode Ikev2Protocol::start() } certInstallProcess->setProgram(PermittedProcess::CertUtil); - QString password = QString("-p %1").arg(m_config[config_key::password].toString()); + QString password = QString("-p \"%1\"").arg(m_config[config_key::password].toString()); + QStringList arguments({"-f", "-importpfx", password, QDir::toNativeSeparators(certFile.fileName()), "NoExport" }); + certInstallProcess->setArguments(arguments); certInstallProcess->start(); From fb63cdf7e9e91aa78ba37618b353e7de158fa947 Mon Sep 17 00:00:00 2001 From: Mykola Baibuz Date: Tue, 20 Aug 2024 22:45:06 +0300 Subject: [PATCH 07/14] Fix work with PKCS12 TempFile --- .../protocols/ikev2_vpn_protocol_windows.cpp | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/client/protocols/ikev2_vpn_protocol_windows.cpp b/client/protocols/ikev2_vpn_protocol_windows.cpp index 10bd4e97b..ed6fb174a 100644 --- a/client/protocols/ikev2_vpn_protocol_windows.cpp +++ b/client/protocols/ikev2_vpn_protocol_windows.cpp @@ -181,11 +181,13 @@ ErrorCode Ikev2Protocol::start() QByteArray cert = QByteArray::fromBase64(m_config[config_key::cert].toString().toUtf8()); setConnectionState(Vpn::ConnectionState::Connecting); - QTemporaryFile certFile; - certFile.setAutoRemove(false); - certFile.open(); - certFile.write(cert); - certFile.close(); + QTemporaryFile * certFile = new QTemporaryFile; + certFile->setAutoRemove(false); + certFile->open(); + QString m_filename = certFile->fileName(); + certFile->write(cert); + certFile->close(); + delete certFile; { auto certInstallProcess = IpcClient::CreatePrivilegedProcess(); @@ -203,14 +205,11 @@ ErrorCode Ikev2Protocol::start() } certInstallProcess->setProgram(PermittedProcess::CertUtil); - QString password = QString("-p \"%1\"").arg(m_config[config_key::password].toString()); - - QStringList arguments({"-f", "-importpfx", password, - QDir::toNativeSeparators(certFile.fileName()), "NoExport" + QStringList arguments({"-f", "-importpfx", "-p", m_config[config_key::password].toString(), + QDir::toNativeSeparators(m_filename), "NoExport" }); certInstallProcess->setArguments(arguments); - certInstallProcess->start(); } // /* From a96f9dc18af6e98b79ac9e190ca8d8a71725c964 Mon Sep 17 00:00:00 2001 From: Mykola Baibuz Date: Sat, 24 Aug 2024 00:57:47 +0300 Subject: [PATCH 08/14] Start and Stop for Linux tunnel --- client/protocols/ikev2_vpn_protocol_linux.cpp | 15 ++++--- ipc/ipc_interface.rep | 6 ++- ipc/ipcserver.cpp | 45 +++++++++++++++++-- ipc/ipcserver.h | 5 ++- 4 files changed, 58 insertions(+), 13 deletions(-) diff --git a/client/protocols/ikev2_vpn_protocol_linux.cpp b/client/protocols/ikev2_vpn_protocol_linux.cpp index 9465036b0..6bf3205c8 100644 --- a/client/protocols/ikev2_vpn_protocol_linux.cpp +++ b/client/protocols/ikev2_vpn_protocol_linux.cpp @@ -28,13 +28,13 @@ Ikev2Protocol::Ikev2Protocol(const QJsonObject &configuration, QObject* parent) Ikev2Protocol::~Ikev2Protocol() { qDebug() << "IpsecProtocol::~IpsecProtocol()"; - disconnect_vpn(); Ikev2Protocol::stop(); } void Ikev2Protocol::stop() { setConnectionState(Vpn::ConnectionState::Disconnected); + Ikev2Protocol::disconnect_vpn(); qDebug() << "IpsecProtocol::stop()"; } @@ -74,9 +74,10 @@ ErrorCode Ikev2Protocol::start() IpcClient::Interface()->writeIPsecConfig(m_config[config_key::config].toString()); IpcClient::Interface()->writeIPsecCaCert(m_config[config_key::cacert].toString(), m_config[config_key::userName].toString()); IpcClient::Interface()->writeIPsecPrivate(m_config[config_key::cert].toString(), m_config[config_key::userName].toString()); - IpcClient::Interface()->writeIPsecPrivatePass(m_config[config_key::password].toString(), m_config[config_key::userName].toString()); - + IpcClient::Interface()->writeIPsecPrivatePass(m_config[config_key::password].toString(), m_config[config_key::hostName].toString(), + m_config[config_key::userName].toString()); + connect_to_vpn("ikev2-vpn"); setConnectionState(Vpn::ConnectionState::Connected); return ErrorCode::NoError; } @@ -92,10 +93,12 @@ bool Ikev2Protocol::delete_vpn_connection(const QString &vpn_name){ return false; } //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -bool Ikev2Protocol::connect_to_vpn(const QString & vpn_name){ - return false; +bool Ikev2Protocol::connect_to_vpn(const QString &vpn_name) { + IpcClient::Interface()->startIPsec(vpn_name); + return true; } //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -bool Ikev2Protocol::disconnect_vpn(){ +bool Ikev2Protocol::disconnect_vpn() { + IpcClient::Interface()->stopIPsec("ikev2-vpn"); return true; } diff --git a/ipc/ipc_interface.rep b/ipc/ipc_interface.rep index 02e8c5248..f29425e06 100644 --- a/ipc/ipc_interface.rep +++ b/ipc/ipc_interface.rep @@ -37,6 +37,10 @@ class IpcInterface SLOT( bool writeIPsecPrivate(QString privKey, QString uuid) ); SLOT( bool writeIPsecConfig(QString config) ); SLOT( bool writeIPsecUserCert(QString usercert, QString uuid) ); - SLOT( bool writeIPsecPrivatePass(QString pass, QString uuid) ); + SLOT( bool writeIPsecPrivatePass(QString pass, QString host, QString uuid) ); + + SLOT( bool stopIPsec(QString tunnelName) ); + SLOT( bool startIPsec(QString tunnelName) ); + }; diff --git a/ipc/ipcserver.cpp b/ipc/ipcserver.cpp index cd0bcf575..7034465c6 100644 --- a/ipc/ipcserver.cpp +++ b/ipc/ipcserver.cpp @@ -182,6 +182,7 @@ void IpcServer::StartRoutingIpv6() { Router::StartRoutingIpv6(); } + void IpcServer::StopRoutingIpv6() { Router::StopRoutingIpv6(); @@ -201,7 +202,6 @@ void IpcServer::setLogsEnabled(bool enabled) } } - bool IpcServer::enableKillSwitch(const QJsonObject &configStr, int vpnAdapterIndex) { #ifdef Q_OS_WIN @@ -288,7 +288,6 @@ bool IpcServer::enableKillSwitch(const QJsonObject &configStr, int vpnAdapterInd MacOSFirewall::setAnchorEnabled(QStringLiteral("310.blockDNS"), true); MacOSFirewall::setAnchorTable(QStringLiteral("310.blockDNS"), true, QStringLiteral("dnsaddr"), dnsServers); #endif - return true; } @@ -309,6 +308,44 @@ bool IpcServer::disableKillSwitch() return true; } +bool IpcServer::startIPsec(QString tunnelName) +{ + QProcess process; + QStringList commands; + commands << "ipsec" << "up" << QString("%1").arg(tunnelName); + process.start("sudo", commands); + if (!process.waitForStarted(1000)) + { + qDebug().noquote() << "Could not start ipsec tunnel!\n"; + return false; + } + else if (!process.waitForFinished(2000)) + { + qDebug().noquote() << "Could not start ipsec tunnel\n"; + return false; + } + commands.clear(); +} + +bool IpcServer::stopIPsec(QString tunnelName) +{ + QProcess process; + QStringList commands; + commands << "ipsec" << "down" << QString("%1").arg(tunnelName); + process.start("sudo", commands); + if (!process.waitForStarted(1000)) + { + qDebug().noquote() << "Could not stop ipsec tunnel\n"; + return false; + } + else if (!process.waitForFinished(2000)) + { + qDebug().noquote() << "Could not stop ipsec tunnel\n"; + return false; + } + commands.clear(); +} + bool IpcServer::writeIPsecConfig(QString config) { #ifdef Q_OS_LINUX @@ -366,12 +403,12 @@ bool IpcServer::writeIPsecPrivate(QString privKey, QString uuid) } -bool IpcServer::writeIPsecPrivatePass(QString pass, QString uuid) +bool IpcServer::writeIPsecPrivatePass(QString pass, QString host, QString uuid) { #ifdef Q_OS_LINUX qDebug() << "IPSEC: User private key " << uuid; QFile secretsFile("/etc/ipsec.secrets"); - QString P12 = QString(": P12 %1.p12 \"%2\" \n").arg(uuid, pass); + QString P12 = QString("%any %1 : P12 %2.p12 \"%3\" \n").arg(host, uuid, pass); if (secretsFile.open(QIODevice::WriteOnly | QIODevice::Append)) { secretsFile.write(P12.toUtf8()); secretsFile.close(); diff --git a/ipc/ipcserver.h b/ipc/ipcserver.h index 43ab32102..63b195d3f 100644 --- a/ipc/ipcserver.h +++ b/ipc/ipcserver.h @@ -39,8 +39,9 @@ class IpcServer : public IpcInterfaceSource virtual bool writeIPsecPrivate(QString privKey, QString uuid) override; virtual bool writeIPsecConfig(QString config) override; virtual bool writeIPsecUserCert(QString usercert, QString uuid) override; - virtual bool writeIPsecPrivatePass(QString pass, QString uuid) override; - + virtual bool writeIPsecPrivatePass(QString pass, QString host, QString uuid) override; + virtual bool stopIPsec(QString tunnelName) override; + virtual bool startIPsec(QString tunnelName) override; private: int m_localpid = 0; From 63c569c3d27cd85927c7ae87f6af93f1c951329f Mon Sep 17 00:00:00 2001 From: Mykola Baibuz Date: Sun, 25 Aug 2024 00:26:32 +0300 Subject: [PATCH 09/14] Setup routing for Linux IPSec --- client/protocols/ikev2_vpn_protocol_linux.cpp | 25 ++++++++++++++++++- client/protocols/ikev2_vpn_protocol_linux.h | 2 ++ ipc/ipcserver.cpp | 24 ++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/client/protocols/ikev2_vpn_protocol_linux.cpp b/client/protocols/ikev2_vpn_protocol_linux.cpp index 6bf3205c8..692531db5 100644 --- a/client/protocols/ikev2_vpn_protocol_linux.cpp +++ b/client/protocols/ikev2_vpn_protocol_linux.cpp @@ -6,6 +6,8 @@ #include +#include "core/networkUtilities.h" + #include "logger.h" #include "ikev2_vpn_protocol_linux.h" #include "utilities.h" @@ -23,6 +25,11 @@ Ikev2Protocol::Ikev2Protocol(const QJsonObject &configuration, QObject* parent) { self = this; readIkev2Configuration(configuration); + m_routeGateway = NetworkUtilities::getGatewayAndIface(); + m_vpnGateway = "192.168.43.10"; + m_vpnLocalAddress = "192.168.43.10"; + m_remoteAddress = configuration.value(amnezia::config_key::hostName).toString(); + m_routeMode = configuration.value(amnezia::config_key::splitTunnelType).toInt(); } Ikev2Protocol::~Ikev2Protocol() @@ -43,7 +50,6 @@ void Ikev2Protocol::readIkev2Configuration(const QJsonObject &configuration) { QJsonObject ikev2_data = configuration.value(ProtocolProps::key_proto_config_data(Proto::Ikev2)).toObject(); m_config = QJsonDocument::fromJson(ikev2_data.value(config_key::config).toString().toUtf8()).object(); - } ErrorCode Ikev2Protocol::start() @@ -95,10 +101,27 @@ bool Ikev2Protocol::delete_vpn_connection(const QString &vpn_name){ //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ bool Ikev2Protocol::connect_to_vpn(const QString &vpn_name) { IpcClient::Interface()->startIPsec(vpn_name); + + QThread::msleep(3000); + +#if defined(Q_OS_LINUX) || defined(Q_OS_MACOS) + // killSwitch toggle + if (QVariant(m_config.value(config_key::killSwitchOption).toString()).toBool()) { + IpcClient::Interface()->enableKillSwitch(m_config, 0); + } +#endif + if (m_routeMode == 0) { + IpcClient::Interface()->routeAddList(m_vpnGateway, QStringList() << "0.0.0.0/1"); + IpcClient::Interface()->routeAddList(m_vpnGateway, QStringList() << "128.0.0.0/1"); + IpcClient::Interface()->routeAddList(m_routeGateway, QStringList() << m_remoteAddress); + } + IpcClient::Interface()->StopRoutingIpv6(); return true; } //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ bool Ikev2Protocol::disconnect_vpn() { IpcClient::Interface()->stopIPsec("ikev2-vpn"); + IpcClient::Interface()->disableKillSwitch(); + IpcClient::Interface()->StartRoutingIpv6(); return true; } diff --git a/client/protocols/ikev2_vpn_protocol_linux.h b/client/protocols/ikev2_vpn_protocol_linux.h index 11ca2140f..b4e2039da 100644 --- a/client/protocols/ikev2_vpn_protocol_linux.h +++ b/client/protocols/ikev2_vpn_protocol_linux.h @@ -35,6 +35,8 @@ class Ikev2Protocol : public VpnProtocol private: QJsonObject m_config; + QString m_remoteAddress; + int m_routeMode; bool create_new_vpn(const QString & vpn_name, diff --git a/ipc/ipcserver.cpp b/ipc/ipcserver.cpp index 7034465c6..6500af694 100644 --- a/ipc/ipcserver.cpp +++ b/ipc/ipcserver.cpp @@ -310,6 +310,25 @@ bool IpcServer::disableKillSwitch() bool IpcServer::startIPsec(QString tunnelName) { +#ifdef Q_OS_LINUX +/* QProcess processSystemd; + QStringList commandsSystemd; + commandsSystemd << "systemctl" << "restart" << "ipsec"; + processSystemd.start("sudo", commandsSystemd); + if (!processSystemd.waitForStarted(1000)) + { + qDebug().noquote() << "Could not start ipsec tunnel!\n"; + return false; + } + else if (!processSystemd.waitForFinished(2000)) + { + qDebug().noquote() << "Could not start ipsec tunnel\n"; + return false; + } + commandsSystemd.clear(); + + QThread::msleep(2000); +*/ QProcess process; QStringList commands; commands << "ipsec" << "up" << QString("%1").arg(tunnelName); @@ -325,10 +344,13 @@ bool IpcServer::startIPsec(QString tunnelName) return false; } commands.clear(); +#endif + return true; } bool IpcServer::stopIPsec(QString tunnelName) { +#ifdef Q_OS_LINUX QProcess process; QStringList commands; commands << "ipsec" << "down" << QString("%1").arg(tunnelName); @@ -344,6 +366,8 @@ bool IpcServer::stopIPsec(QString tunnelName) return false; } commands.clear(); +#endif + return true; } bool IpcServer::writeIPsecConfig(QString config) From ad61ef0b2232606a1fd95f30d880e511de491996 Mon Sep 17 00:00:00 2001 From: Mykola Baibuz Date: Thu, 29 Aug 2024 00:23:53 +0300 Subject: [PATCH 10/14] Cleanup ipsec.secrets from duplicates --- ipc/ipcserver.cpp | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/ipc/ipcserver.cpp b/ipc/ipcserver.cpp index 6500af694..bcf782165 100644 --- a/ipc/ipcserver.cpp +++ b/ipc/ipcserver.cpp @@ -431,12 +431,43 @@ bool IpcServer::writeIPsecPrivatePass(QString pass, QString host, QString uuid) { #ifdef Q_OS_LINUX qDebug() << "IPSEC: User private key " << uuid; - QFile secretsFile("/etc/ipsec.secrets"); - QString P12 = QString("%any %1 : P12 %2.p12 \"%3\" \n").arg(host, uuid, pass); - if (secretsFile.open(QIODevice::WriteOnly | QIODevice::Append)) { + const QString secretsFilename = "/etc/ipsec.secrets"; + QStringList lines; + + { + QFile secretsFile(secretsFilename); + if (secretsFile.open(QIODevice::ReadOnly | QIODevice::Text)) + { + QTextStream edit(&secretsFile); + while (!edit.atEnd()) lines.push_back(edit.readLine()); + } + secretsFile.close(); + } + + for (auto iter = lines.begin(); iter!=lines.end();) + { + if (iter->contains(host)) + { + iter = lines.erase(iter); + } + else + { + ++iter; + } + } + + { + QFile secretsFile(secretsFilename); + if (secretsFile.open(QIODevice::WriteOnly | QIODevice::Text)) + { + QTextStream edit(&secretsFile); + for (int i=0; i Date: Thu, 29 Aug 2024 23:18:21 +0300 Subject: [PATCH 11/14] Restart IPSec service before VPN connect --- client/protocols/ikev2_vpn_protocol_linux.cpp | 5 ++--- ipc/ipcserver.cpp | 6 +++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/client/protocols/ikev2_vpn_protocol_linux.cpp b/client/protocols/ikev2_vpn_protocol_linux.cpp index 692531db5..57b0953f0 100644 --- a/client/protocols/ikev2_vpn_protocol_linux.cpp +++ b/client/protocols/ikev2_vpn_protocol_linux.cpp @@ -45,7 +45,6 @@ void Ikev2Protocol::stop() qDebug() << "IpsecProtocol::stop()"; } - void Ikev2Protocol::readIkev2Configuration(const QJsonObject &configuration) { QJsonObject ikev2_data = configuration.value(ProtocolProps::key_proto_config_data(Proto::Ikev2)).toObject(); @@ -89,12 +88,12 @@ ErrorCode Ikev2Protocol::start() } //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ bool Ikev2Protocol::create_new_vpn(const QString & vpn_name, - const QString & serv_addr){ + const QString & serv_addr) { qDebug() << "Ikev2Protocol::create_new_vpn()"; return true; } //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -bool Ikev2Protocol::delete_vpn_connection(const QString &vpn_name){ +bool Ikev2Protocol::delete_vpn_connection(const QString &vpn_name) { return false; } diff --git a/ipc/ipcserver.cpp b/ipc/ipcserver.cpp index bcf782165..13418d3a7 100644 --- a/ipc/ipcserver.cpp +++ b/ipc/ipcserver.cpp @@ -311,7 +311,7 @@ bool IpcServer::disableKillSwitch() bool IpcServer::startIPsec(QString tunnelName) { #ifdef Q_OS_LINUX -/* QProcess processSystemd; + QProcess processSystemd; QStringList commandsSystemd; commandsSystemd << "systemctl" << "restart" << "ipsec"; processSystemd.start("sudo", commandsSystemd); @@ -327,8 +327,8 @@ bool IpcServer::startIPsec(QString tunnelName) } commandsSystemd.clear(); - QThread::msleep(2000); -*/ + QThread::msleep(5000); + QProcess process; QStringList commands; commands << "ipsec" << "up" << QString("%1").arg(tunnelName); From 052261c2b4a3e58642679cf3b726c801660cbea7 Mon Sep 17 00:00:00 2001 From: Mykola Baibuz Date: Fri, 30 Aug 2024 21:44:47 +0300 Subject: [PATCH 12/14] Get Linux IPSec tunnel status --- client/protocols/ikev2_vpn_protocol_linux.cpp | 64 ++++++++++++++----- ipc/ipc_interface.rep | 2 + ipc/ipcserver.cpp | 28 +++++++- ipc/ipcserver.h | 1 + 4 files changed, 78 insertions(+), 17 deletions(-) diff --git a/client/protocols/ikev2_vpn_protocol_linux.cpp b/client/protocols/ikev2_vpn_protocol_linux.cpp index 57b0953f0..2fc851504 100644 --- a/client/protocols/ikev2_vpn_protocol_linux.cpp +++ b/client/protocols/ikev2_vpn_protocol_linux.cpp @@ -71,8 +71,6 @@ ErrorCode Ikev2Protocol::start() BIO_get_mem_ptr(bio, &mem); std::string pem(mem->data, mem->length); - qDebug() << pem; - QString alias(pem.c_str()); IpcClient::Interface()->writeIPsecUserCert(alias, m_config[config_key::userName].toString()); @@ -83,7 +81,54 @@ ErrorCode Ikev2Protocol::start() m_config[config_key::userName].toString()); connect_to_vpn("ikev2-vpn"); - setConnectionState(Vpn::ConnectionState::Connected); + + if (!IpcClient::Interface()) { + return ErrorCode::AmneziaServiceConnectionFailed; + } + + QString connectionStatus; + + auto futureResult = IpcClient::Interface()->getTunnelStatus("ikev2-vpn"); + futureResult.waitForFinished(); + + if (futureResult.returnValue().isEmpty()) { + auto futureResult = IpcClient::Interface()->getTunnelStatus("ikev2-vpn"); + futureResult.waitForFinished(); + } + + connectionStatus = futureResult.returnValue(); + + if (connectionStatus.contains("ESTABLISHED")) { + QStringList lines = connectionStatus.split('\n'); + for (auto iter = lines.begin(); iter!=lines.end(); iter++) + { + if (iter->contains("0.0.0.0/0")) { + + m_routeGateway = iter->split("===", Qt::SkipEmptyParts).first(); + m_routeGateway = m_routeGateway.split(" ").at(2); + m_routeGateway = m_routeGateway.split("/").first(); + qDebug() << "m_routeGateway " << m_routeGateway; + + // killSwitch toggle + if (QVariant(m_config.value(config_key::killSwitchOption).toString()).toBool()) { + IpcClient::Interface()->enableKillSwitch(m_config, 0); + } + + if (m_routeMode == 0) { + IpcClient::Interface()->routeAddList(m_vpnGateway, QStringList() << "0.0.0.0/1"); + IpcClient::Interface()->routeAddList(m_vpnGateway, QStringList() << "128.0.0.0/1"); + IpcClient::Interface()->routeAddList(m_routeGateway, QStringList() << m_remoteAddress); + } + + IpcClient::Interface()->StopRoutingIpv6(); + + } + } + setConnectionState(Vpn::ConnectionState::Connected); + } else { + setConnectionState(Vpn::ConnectionState::Disconnected); + } + return ErrorCode::NoError; } //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -102,19 +147,6 @@ bool Ikev2Protocol::connect_to_vpn(const QString &vpn_name) { IpcClient::Interface()->startIPsec(vpn_name); QThread::msleep(3000); - -#if defined(Q_OS_LINUX) || defined(Q_OS_MACOS) - // killSwitch toggle - if (QVariant(m_config.value(config_key::killSwitchOption).toString()).toBool()) { - IpcClient::Interface()->enableKillSwitch(m_config, 0); - } -#endif - if (m_routeMode == 0) { - IpcClient::Interface()->routeAddList(m_vpnGateway, QStringList() << "0.0.0.0/1"); - IpcClient::Interface()->routeAddList(m_vpnGateway, QStringList() << "128.0.0.0/1"); - IpcClient::Interface()->routeAddList(m_routeGateway, QStringList() << m_remoteAddress); - } - IpcClient::Interface()->StopRoutingIpv6(); return true; } //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/ipc/ipc_interface.rep b/ipc/ipc_interface.rep index f29425e06..c180cb879 100644 --- a/ipc/ipc_interface.rep +++ b/ipc/ipc_interface.rep @@ -42,5 +42,7 @@ class IpcInterface SLOT( bool stopIPsec(QString tunnelName) ); SLOT( bool startIPsec(QString tunnelName) ); + SLOT( QString getTunnelStatus(QString tunnelName) ); + }; diff --git a/ipc/ipcserver.cpp b/ipc/ipcserver.cpp index 13418d3a7..f2a2da4fa 100644 --- a/ipc/ipcserver.cpp +++ b/ipc/ipcserver.cpp @@ -467,11 +467,37 @@ bool IpcServer::writeIPsecPrivatePass(QString pass, QString host, QString uuid) secretsFile.write(P12.toUtf8()); secretsFile.close(); } - #endif return true; } +QString IpcServer::getTunnelStatus(QString tunnelName) +{ +#ifdef Q_OS_LINUX + QProcess process; + QStringList commands; + commands << "ipsec" << "status" << QString("%1").arg(tunnelName); + process.start("sudo", commands); + if (!process.waitForStarted(1000)) + { + qDebug().noquote() << "Could not stop ipsec tunnel\n"; + return ""; + } + else if (!process.waitForFinished(2000)) + { + qDebug().noquote() << "Could not stop ipsec tunnel\n"; + return ""; + } + commands.clear(); + + + QString status = process.readAll(); + return status; +#endif + return QString(); + +} + bool IpcServer::enablePeerTraffic(const QJsonObject &configStr) { #ifdef Q_OS_WIN diff --git a/ipc/ipcserver.h b/ipc/ipcserver.h index 63b195d3f..67c6f777e 100644 --- a/ipc/ipcserver.h +++ b/ipc/ipcserver.h @@ -42,6 +42,7 @@ class IpcServer : public IpcInterfaceSource virtual bool writeIPsecPrivatePass(QString pass, QString host, QString uuid) override; virtual bool stopIPsec(QString tunnelName) override; virtual bool startIPsec(QString tunnelName) override; + virtual QString getTunnelStatus(QString tunnelName) override; private: int m_localpid = 0; From 948ab4cf718b25a66732a7fbdf9e31a4ad895e61 Mon Sep 17 00:00:00 2001 From: Mykola Baibuz Date: Fri, 30 Aug 2024 22:10:39 +0300 Subject: [PATCH 13/14] Set local IPSec VPN address --- client/protocols/ikev2_vpn_protocol_linux.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/client/protocols/ikev2_vpn_protocol_linux.cpp b/client/protocols/ikev2_vpn_protocol_linux.cpp index 2fc851504..7c6904c2a 100644 --- a/client/protocols/ikev2_vpn_protocol_linux.cpp +++ b/client/protocols/ikev2_vpn_protocol_linux.cpp @@ -107,8 +107,10 @@ ErrorCode Ikev2Protocol::start() m_routeGateway = iter->split("===", Qt::SkipEmptyParts).first(); m_routeGateway = m_routeGateway.split(" ").at(2); m_routeGateway = m_routeGateway.split("/").first(); + m_vpnLocalAddress = m_routeGateway; qDebug() << "m_routeGateway " << m_routeGateway; + // killSwitch toggle if (QVariant(m_config.value(config_key::killSwitchOption).toString()).toBool()) { IpcClient::Interface()->enableKillSwitch(m_config, 0); @@ -145,7 +147,6 @@ bool Ikev2Protocol::delete_vpn_connection(const QString &vpn_name) { //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ bool Ikev2Protocol::connect_to_vpn(const QString &vpn_name) { IpcClient::Interface()->startIPsec(vpn_name); - QThread::msleep(3000); return true; } From 4147632a62867180793ec221d734dd48ed902eb5 Mon Sep 17 00:00:00 2001 From: Mykola Baibuz Date: Fri, 30 Aug 2024 22:15:51 +0300 Subject: [PATCH 14/14] Fix Android build --- client/protocols/vpnprotocol.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/protocols/vpnprotocol.cpp b/client/protocols/vpnprotocol.cpp index 765e86ab3..40b22dca0 100644 --- a/client/protocols/vpnprotocol.cpp +++ b/client/protocols/vpnprotocol.cpp @@ -110,7 +110,7 @@ QString VpnProtocol::vpnGateway() const VpnProtocol *VpnProtocol::factory(DockerContainer container, const QJsonObject &configuration) { switch (container) { -#if defined(Q_OS_WINDOWS) || defined(Q_OS_LINUX) +#if defined(Q_OS_WINDOWS) || defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID) case DockerContainer::Ipsec: return new Ikev2Protocol(configuration); #endif #if defined(Q_OS_WINDOWS) || defined(Q_OS_MACX) || (defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID))