diff --git a/client/protocols/ikev2_vpn_protocol_linux.cpp b/client/protocols/ikev2_vpn_protocol_linux.cpp index 57b0953f..2fc85150 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 f29425e0..c180cb87 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 13418d3a..f2a2da4f 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 63b195d3..67c6f777 100644 --- a/ipc/ipcserver.h +++ b/ipc/ipcserver.h @@ -42,6 +42,7 @@ public: 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;