From 8262d743d87865715abbfde508ae8d8ff7352d1f Mon Sep 17 00:00:00 2001 From: Fedotov Anton Date: Wed, 24 Nov 2021 13:35:37 +0300 Subject: [PATCH] WMI dependency removed, all available protocols checked --- client/3rd/AdpInfo/netadpinfo.h | 46 +------------------------ client/3rd/AdpInfo/win_netadpinfo.cc | 18 ++++++++-- client/protocols/ikev2_vpn_protocol.cpp | 44 ++++++++--------------- client/protocols/openvpnprotocol.cpp | 27 ++++++++++++--- client/protocols/vpnprotocol.h | 3 ++ client/protocols/wireguardprotocol.cpp | 44 ++++++++++++++++------- 6 files changed, 87 insertions(+), 95 deletions(-) diff --git a/client/3rd/AdpInfo/netadpinfo.h b/client/3rd/AdpInfo/netadpinfo.h index dfc8ab3b..90c98a90 100644 --- a/client/3rd/AdpInfo/netadpinfo.h +++ b/client/3rd/AdpInfo/netadpinfo.h @@ -8,54 +8,12 @@ namespace adpinfo{ //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -//static bool is_string_equal(const std::string &lhs, const std::string &rhs){ -// if (lhs.find(rhs) != std::string::npos) -// return true; -// return false; -//} -//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // {false,""} - no error // {true,"descr"} - error with description using RET_TYPE = std::tuple; //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -//class Adapter{ -//private: -// std::string name{}; -// std::string descr{}; -// std::string current_ip_address_v4{}; -// std::string maskv4{}; -// std::vector dns_address{}; - -//public: -// explicit Adapter() = default; -// ~Adapter() = default; - -// void set_name(std::string_view); -// std::string_view get_name()const; - -// void set_description(std::string_view); -// std::string_view get_description()const; - -// void set_mac(std::string_view); -// std::string_view get_mac()const; - -//// bool operator==(const adapter& rhs) { -//// if (!is_string_equal(name, rhs.name)) -//// return false; -//// if (!is_string_equal(mac, rhs.mac)) -//// return false; -//// if (dns_address != rhs.dns_address) -//// return false; -//// return true; -//// } -//}adapter; -//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /* * The object uses for collect the information about active network adapters/interfaces - * QString m_routeGateway; - QString m_vpnLocalAddress; - QString m_vpnGateway; */ class NetAdpInfo final{ //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -90,12 +48,10 @@ public: explicit NetAdpInfo() = default; ~NetAdpInfo() = default; - RET_TYPE get_adapter_infor(std::string_view ); + RET_TYPE get_adapter_info(std::string_view ); std::string_view get_adapter_route_gateway()const; std::string_view get_adapter_local_address()const; std::string_view get_adapter_local_gateway()const; - - //static std::string get_system_route(); }; //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/client/3rd/AdpInfo/win_netadpinfo.cc b/client/3rd/AdpInfo/win_netadpinfo.cc index cf631c85..d36777b4 100644 --- a/client/3rd/AdpInfo/win_netadpinfo.cc +++ b/client/3rd/AdpInfo/win_netadpinfo.cc @@ -122,7 +122,7 @@ RET_TYPE NetAdpInfo::collect_adapters_data(){ std::vector buffer{}; IP_ADAPTER_INFO *adapter_info{nullptr}; DWORD result{ERROR_BUFFER_OVERFLOW}; - ULONG buffer_len = sizeof(IP_ADAPTER_INFO) * 3; + ULONG buffer_len = sizeof(IP_ADAPTER_INFO) * 10; while (result == ERROR_BUFFER_OVERFLOW){ buffer.resize(buffer_len); adapter_info = reinterpret_cast(&buffer[0]); @@ -141,7 +141,16 @@ RET_TYPE NetAdpInfo::collect_adapters_data(){ _tmp->set_name(adapter_iterator->AdapterName); _tmp->set_description(adapter_iterator->Description); _tmp->set_local_address(adapter_iterator->IpAddressList.IpAddress.String); - _tmp->set_local_gateway(adapter_iterator->GatewayList.IpAddress.String); + std::string lgw = adapter_iterator->GatewayList.IpAddress.String; + if (lgw.length() == 0 || lgw.find("0.0.0.0") != std::string::npos) + { + if (adapter_iterator->DhcpEnabled == 1) + { + lgw = adapter_iterator->DhcpServer.IpAddress.String; + } + } + _tmp->set_local_gateway(lgw); + //_tmp->set_local_gateway(adapter_iterator->GatewayList.IpAddress.String); _tmp->set_route_gateway(get_route_gateway()); _adapters.emplace_back(_tmp); adapter_iterator = adapter_iterator->Next; @@ -149,7 +158,7 @@ RET_TYPE NetAdpInfo::collect_adapters_data(){ return {false, ""}; } -RET_TYPE NetAdpInfo::get_adapter_infor(std::string_view _adapter_name){ +RET_TYPE NetAdpInfo::get_adapter_info(std::string_view _adapter_name){ _index_of_adapter = -1; const auto result{collect_adapters_data()}; @@ -161,6 +170,9 @@ RET_TYPE NetAdpInfo::get_adapter_infor(std::string_view _adapter_name){ for (auto i = 0; i< len; ++i){ auto adap_name = _adapters[i]->get_name(); auto adap_desc = _adapters[i]->get_description(); + qDebug()<<"adap name : "< -#endif - static Ikev2Protocol* self = nullptr; static std::mutex rasDialFuncMutex; @@ -25,14 +21,6 @@ static void WINAPI RasDialFuncCallback(UINT unMsg, DWORD dwError ); } //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -//QString m_routeGateway; -//QString m_vpnLocalAddress; -//QString m_vpnGateway; -//static void get_connecting_status(std::string_view _vpn_name, -// std::string &_m_route_gateway, -// std::string & _m_vpn_local_address, -// std::string &_m_vpn_gateway); -//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Ikev2Protocol::Ikev2Protocol(const QJsonObject &configuration, QObject* parent) : VpnProtocol(configuration, parent) @@ -184,15 +172,11 @@ void Ikev2Protocol::newConnectionStateEventReceived(UINT unMsg, tagRASCONNSTATE { //get the network settings of adapters std::string p1,p2,p3; - const auto ret = adpInfo.get_adapter_infor(tunnelName().toStdString()); + const auto ret = adpInfo.get_adapter_info(tunnelName().toStdString()); if (std::get<0>(ret) == false){ p1 = adpInfo.get_adapter_route_gateway(); p2 = adpInfo.get_adapter_local_address(); p3 = adpInfo.get_adapter_local_gateway(); - // get_connecting_status(tunnelName().toStdString().c_str(), - // p1, - // p2, - // p3); m_routeGateway = QString::fromStdString(p1); m_vpnLocalAddress = QString::fromStdString(p2); m_vpnGateway = QString::fromStdString(p3); @@ -248,21 +232,21 @@ ErrorCode Ikev2Protocol::start() "-importpfx", certFile.fileName(), "NoExport" }); certInstallProcess->setArguments(arguments); - qDebug()<readAll(); - req.waitForFinished(); - qDebug() << "PrivilegedProcess readyRead" << req.returnValue(); - }); +// connect(certInstallProcess.data(), &PrivilegedProcess::stateChanged, [certInstallProcess](QProcess::ProcessState newState) { +// qDebug() << "PrivilegedProcess stateChanged" << newState; +// }); + +// connect(certInstallProcess.data(), &PrivilegedProcess::readyRead, [certInstallProcess]() { +// auto req = certInstallProcess->readAll(); +// req.waitForFinished(); +// qDebug() << "PrivilegedProcess readyRead" << req.returnValue(); +// }); certInstallProcess->start(); diff --git a/client/protocols/openvpnprotocol.cpp b/client/protocols/openvpnprotocol.cpp index df57e365..beabe704 100644 --- a/client/protocols/openvpnprotocol.cpp +++ b/client/protocols/openvpnprotocol.cpp @@ -185,10 +185,10 @@ ErrorCode OpenVpnProtocol::start() } m_openVpnProcess->setProgram(openVpnExecPath()); QStringList arguments({"--config" , configPath(), - "--management", m_managementHost, QString::number(m_managementPort), - "--management-client", - "--log", vpnLogFileNamePath - }); + "--management", m_managementHost, QString::number(m_managementPort), + "--management-client", + "--log", vpnLogFileNamePath + }); m_openVpnProcess->setArguments(arguments); qDebug() << arguments.join(" "); @@ -234,7 +234,6 @@ void OpenVpnProtocol::onReadyReadDataFromManagementServer() { for (;;) { QString line = m_managementServer.readLine().simplified(); - if (line.isEmpty()) { return; } @@ -247,6 +246,24 @@ void OpenVpnProtocol::onReadyReadDataFromManagementServer() sendInitialData(); } else if (line.startsWith(">STATE")) { if (line.contains("CONNECTED,SUCCESS")) { + { + std::string p1,p2,p3; + const auto &ret = adpInfo.get_adapter_info("TAP-Windows Adapter V9"); + if (std::get<0>(ret) == false){ + p1 = adpInfo.get_adapter_route_gateway(); + p2 = adpInfo.get_adapter_local_address(); + p3 = adpInfo.get_adapter_local_gateway(); + m_routeGateway = QString::fromStdString(p1); + m_vpnLocalAddress = QString::fromStdString(p2); + m_vpnGateway = QString::fromStdString(p3); + qDebug()<<"My openvpn m_routeGateway "<(ret)); + } + } sendByteCount(); stopTimeoutTimer(); setConnectionState(VpnProtocol::Connected); diff --git a/client/protocols/vpnprotocol.h b/client/protocols/vpnprotocol.h index c6bfc3b2..cf9acb25 100644 --- a/client/protocols/vpnprotocol.h +++ b/client/protocols/vpnprotocol.h @@ -8,6 +8,8 @@ #include "core/defs.h" #include "containers/containers_defs.h" +#include "3rd/AdpInfo/netadpinfo.h" + using namespace amnezia; class QTimer; @@ -69,6 +71,7 @@ protected: QString m_routeGateway; QString m_vpnLocalAddress; QString m_vpnGateway; + adpinfo::NetAdpInfo adpInfo; QJsonObject m_rawConfig; diff --git a/client/protocols/wireguardprotocol.cpp b/client/protocols/wireguardprotocol.cpp index d08843e7..11aee85c 100644 --- a/client/protocols/wireguardprotocol.cpp +++ b/client/protocols/wireguardprotocol.cpp @@ -185,8 +185,28 @@ ErrorCode WireguardProtocol::start() qDebug() << "WireguardProtocol::WireguardProtocol stateChanged" << newState; }); - connect(m_wireguardStartProcess.data(), &PrivilegedProcess::finished, this, [this]() { + connect(m_wireguardStartProcess.data(), &PrivilegedProcess::finished, this, [&]() { setConnectionState(ConnectionState::Connected); + { + //TODO:FIXME: without some ugly sleep we have't get a adapter parametrs + std::this_thread::sleep_for(std::chrono::seconds(2)); + std::string p1,p2,p3; + const auto &ret = adpInfo.get_adapter_info("WireGuard Tunnel");//serviceName().toStdString());//("AmneziaVPN IKEv2"); + if (std::get<0>(ret) == false){ + p1 = adpInfo.get_adapter_route_gateway(); + p2 = adpInfo.get_adapter_local_address(); + p3 = adpInfo.get_adapter_local_gateway(); + m_routeGateway = QString::fromStdString(p1); + m_vpnLocalAddress = QString::fromStdString(p2); + m_vpnGateway = QString::fromStdString(p3); + qDebug()<<"My wireguard m_routeGateway "<(ret)); + } + } }); connect(m_wireguardStartProcess.data(), &PrivilegedProcess::readyRead, this, [this]() { @@ -220,19 +240,19 @@ ErrorCode WireguardProtocol::start() void WireguardProtocol::updateVpnGateway(const QString &line) { -// // line looks like -// // PUSH: Received control message: 'PUSH_REPLY,route 10.8.0.1,topology net30,ping 10,ping-restart 120,ifconfig 10.8.0.6 10.8.0.5,peer-id 0,cipher AES-256-GCM' + // // line looks like + // // PUSH: Received control message: 'PUSH_REPLY,route 10.8.0.1,topology net30,ping 10,ping-restart 120,ifconfig 10.8.0.6 10.8.0.5,peer-id 0,cipher AES-256-GCM' -// QStringList params = line.split(","); -// for (const QString &l : params) { -// if (l.contains("ifconfig")) { -// if (l.split(" ").size() == 3) { -// m_vpnLocalAddress = l.split(" ").at(1); -// m_vpnGateway = l.split(" ").at(2); + // QStringList params = line.split(","); + // for (const QString &l : params) { + // if (l.contains("ifconfig")) { + // if (l.split(" ").size() == 3) { + // m_vpnLocalAddress = l.split(" ").at(1); + // m_vpnGateway = l.split(" ").at(2); -// qDebug() << QString("Set vpn local address %1, gw %2").arg(m_vpnLocalAddress).arg(vpnGateway()); -// } -// } + // qDebug() << QString("Set vpn local address %1, gw %2").arg(m_vpnLocalAddress).arg(vpnGateway()); + // } + // } // } }