diff --git a/client/3rd/AdpInfo/adpinfo.pri b/client/3rd/AdpInfo/adpinfo.pri new file mode 100644 index 00000000..64128f84 --- /dev/null +++ b/client/3rd/AdpInfo/adpinfo.pri @@ -0,0 +1,7 @@ +HEADERS += \ + 3rd/AdpInfo/netadpinfo.h \ + +win32: { +SOURCES += \ + 3rd/AdpInfo/win_netadpinfo.cc \ +} diff --git a/client/3rd/AdpInfo/netadpinfo.h b/client/3rd/AdpInfo/netadpinfo.h new file mode 100644 index 00000000..9b6a8507 --- /dev/null +++ b/client/3rd/AdpInfo/netadpinfo.h @@ -0,0 +1,71 @@ +#pragma once + +#include +#include +#include +#include + + +namespace adpinfo{ +//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +// {false,""} - no error +// {true,"descr"} - error with description +using RET_TYPE = std::tuple; +//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +/* + * Getting the route table +*/ +typedef struct route_table{ + std::string szDestIp{}; + std::string szMaskIp{}; + std::string szGatewayIp{}; + std::string szInterfaceIp{}; + unsigned long ulIfIndex{}; +}route_table; +std::vector*/route_table>get_route_table(std::string_view); +//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +/* + * The object uses for collect the information about active network adapters/interfaces +*/ +class NetAdpInfo final{ + //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + class Adapter{ + std::string name{}; + std::string descr{}; + std::string route{}; + std::string address{}; + std::string gateway{}; + 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_route_gateway(std::string_view); + std::string_view get_route_gateway()const; + void set_local_address(std::string_view); + std::string_view get_local_address()const; + void set_local_gateway(std::string_view); + std::string_view get_local_gateway()const; + }; + //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + int16_t _index_of_adapter{}; + std::vector>_adapters{}; + + RET_TYPE collect_adapters_data(); + +public: + explicit NetAdpInfo() = default; + ~NetAdpInfo() = default; + + 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; + +}; +//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +} //end namespace diff --git a/client/3rd/AdpInfo/win_netadpinfo.cc b/client/3rd/AdpInfo/win_netadpinfo.cc new file mode 100644 index 00000000..79487315 --- /dev/null +++ b/client/3rd/AdpInfo/win_netadpinfo.cc @@ -0,0 +1,321 @@ +#include "netadpinfo.h" + +#include + +#include +#include +#include + +#include +#include + +#pragma comment(lib, "iphlpapi.lib") + + +//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +//static std::string convert_wide_to_ansi(const std::wstring& widestring) { +// auto nchars = WideCharToMultiByte( +// CP_ACP, +// 0, +// widestring.c_str(), +// static_cast(widestring.length() + 1), +// nullptr, +// 0, +// nullptr, +// nullptr); +// std::string converted_string{}; +// converted_string.resize(nchars); +// WideCharToMultiByte(CP_ACP, +// 0, +// widestring.c_str(), +// -1, +// &converted_string[0], +// static_cast(widestring.length()), +// nullptr, +// nullptr); +// return converted_string; +//} +//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +//static std::string get_founded_route(std::string_view ip_address){ + +// MIB_IPFORWARDROW br{0}; +// ZeroMemory(&br, sizeof(MIB_IPFORWARDROW)); +// struct in_addr ia; +// std::string sTmp{}; +// DWORD dwRes = GetBestRoute(inet_addr(ip_address.data()), 0, &br); +// if( dwRes == NO_ERROR ){ +// ia.S_un.S_addr = (u_long) br.dwForwardDest; +// sTmp = inet_ntoa(ia); +// qDebug()<<"Best Route:"<< sTmp.data(); +// } +// return sTmp; +//} +//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +static std::string get_route_gateway() +{ + std::string route_gateway{}; + PMIB_IPFORWARDTABLE pIpForwardTable{nullptr}; + DWORD dwSize = 0; + BOOL bOrder = FALSE; + + struct in_addr IpAddr; + char szGatewayIp[128]{'\0'}; + + DWORD dwStatus = GetIpForwardTable(pIpForwardTable, &dwSize, bOrder); + if (dwStatus == ERROR_INSUFFICIENT_BUFFER) { + if (!(pIpForwardTable = (PMIB_IPFORWARDTABLE) malloc(dwSize))) { + return {"Out of memory"}; + } + dwStatus = GetIpForwardTable(pIpForwardTable, &dwSize, bOrder); + } + if (dwStatus != ERROR_SUCCESS) { + if (pIpForwardTable) + free(pIpForwardTable); + return {"getIpForwardTable failed"}; + } + const DWORD end = pIpForwardTable->dwNumEntries; + for (DWORD i = 0; i < end; i++) { + if (pIpForwardTable->table[i].dwForwardDest == 0) { + IpAddr.S_un.S_addr = + (u_long) pIpForwardTable->table[i].dwForwardNextHop; + strcpy_s(szGatewayIp, sizeof (szGatewayIp), inet_ntoa(IpAddr)); + route_gateway = std::string(szGatewayIp); + break; + } + } + if (pIpForwardTable) + free(pIpForwardTable); + + return route_gateway; +} +//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +static std::string get_interface_ip(DWORD index){ + std::string _ipaddr{'\0'}; + std::vector buffer{}; + IP_ADAPTER_INFO *adapter_info{nullptr}; + DWORD result{ERROR_BUFFER_OVERFLOW}; + ULONG buffer_len = sizeof(IP_ADAPTER_INFO) * 10; + while (result == ERROR_BUFFER_OVERFLOW){ + buffer.resize(buffer_len); + adapter_info = reinterpret_cast(&buffer[0]); + result = GetAdaptersInfo(adapter_info, &buffer_len); + if (result == ERROR_NO_DATA){ + return _ipaddr; + } + }//end while + if (result != NO_ERROR){ + return _ipaddr; + } + IP_ADAPTER_INFO *adapter_iterator = adapter_info; + while(adapter_iterator){ + if (adapter_iterator->Index == index) + break; + adapter_iterator = adapter_iterator->Next; + }//end while + if ( adapter_iterator != nullptr || adapter_iterator != 0x0 || adapter_iterator != NULL ) + _ipaddr = std::string(adapter_iterator->IpAddressList.IpAddress.String, 16); + else + _ipaddr = "127.0.0.1"; + return _ipaddr; +} +//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +namespace adpinfo { + +std::vectorget_route_table(std::string_view ipifaddrs){ + + /*std::tuple*/ + std::vectorret_table{}; + PMIB_IPFORWARDTABLE p_table{nullptr}; + struct in_addr ip_addr{}; + DWORD size{}; + p_table = (MIB_IPFORWARDTABLE *) HeapAlloc(GetProcessHeap(), 0, (sizeof (MIB_IPFORWARDTABLE))); + if (p_table == nullptr) + { + return ret_table; + } + if (GetIpForwardTable(p_table, &size, 0) == ERROR_INSUFFICIENT_BUFFER) { + HeapFree(GetProcessHeap(), 0, p_table); + p_table = (MIB_IPFORWARDTABLE *) HeapAlloc(GetProcessHeap(), 0, size); + if (p_table == nullptr) { + return ret_table; + } + } + DWORD ret = GetIpForwardTable(p_table, &size, 0); + if (ret == NO_ERROR) { + const int &numEntries = static_cast(p_table->dwNumEntries); + for (int i = 0; i table[i].dwForwardDest; + strcpy_s(szDestIp, sizeof (szDestIp), inet_ntoa(ip_addr)); + ip_addr.S_un.S_addr = (u_long) p_table->table[i].dwForwardMask; + strcpy_s(szMaskIp, sizeof (szMaskIp), inet_ntoa(ip_addr)); + ip_addr.S_un.S_addr = (u_long) p_table->table[i].dwForwardNextHop; + strcpy_s(szGatewayIp, sizeof (szGatewayIp), inet_ntoa(ip_addr)); + const auto &ifname = get_interface_ip(p_table->table[i].dwForwardIfIndex); + const auto &ifnameSize = ifname.length() + 1; + strcpy_s(szInterfaceIp, ifnameSize, ifname.data()); + if (ipifaddrs.length() == 0){ + //std::make_tuple(std::string(szDestIp), std::string(szMaskIp), std::string(szGatewayIp), std::string(szInterfaceIp)); + const route_table &mt = { + std::string(szDestIp), + std::string(szMaskIp), + std::string(szGatewayIp), + std::string(szInterfaceIp), + p_table->table[i].dwForwardIfIndex + }; + ret_table.emplace_back(mt); + }else{ + bool in_not_empty = (ifname.find(ipifaddrs) != std::string::npos); + //bool in_as_destIp = ( std::string(szDestIp).find(ipifaddrs) != std::string::npos); + bool destIp_as_zero = (std::string(szDestIp).find("0.0.0.0") != std::string::npos); + bool mask_as_zero = (std::string(szMaskIp).find("0.0.0.0") != std::string::npos); +// bool ip_the_same = ( +// std::string(szDestIp).find(ipifaddrs) != std::string::npos && +// std::string(szDestIp).find(szGatewayIp) != std::string::npos && +// std::string(szDestIp).find(szInterfaceIp) != std::string::npos +// ); +// bool not_default = (std::string(szDestIp).find("127.0.0.1") == std::string::npos); + + if ( in_not_empty && + // in_as_destIp && + destIp_as_zero && + mask_as_zero//) || ( ip_the_same && not_default ) + ) + { + // finded + const route_table &mt = { + std::string(szDestIp), + std::string(szMaskIp), + std::string(szGatewayIp), + std::string(szInterfaceIp), + p_table->table[i].dwForwardIfIndex + };//std::make_tuple(std::string(szDestIp), std::string(szMaskIp), std::string(szGatewayIp), std::string(szInterfaceIp)); + ret_table.emplace_back(mt); + } + } + }//end for + }//end if + return ret_table; +} +}//end namespace adpinfo +//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +namespace adpinfo { + +void NetAdpInfo::Adapter::set_name(std::string_view value){ + name = value; +} +std::string_view NetAdpInfo::Adapter::get_name()const{ + + return name; +} +void NetAdpInfo::Adapter::set_description(std::string_view value){ + descr = value; +} +std::string_view NetAdpInfo::Adapter::get_description()const{ + return descr; +} +void NetAdpInfo::Adapter::set_route_gateway(std::string_view value){ + route = value; +} +std::string_view NetAdpInfo::Adapter::get_route_gateway()const{ + return route; + +} +void NetAdpInfo::Adapter::set_local_address(std::string_view value){ + address = value; +} +std::string_view NetAdpInfo::Adapter::get_local_address()const{ + return address; +} +void NetAdpInfo::Adapter::set_local_gateway(std::string_view value){ + gateway = value; +} +std::string_view NetAdpInfo::Adapter::get_local_gateway()const{ + return gateway; +} + +RET_TYPE NetAdpInfo::collect_adapters_data(){ + _adapters.clear(); + std::vector buffer{}; + IP_ADAPTER_INFO *adapter_info{nullptr}; + DWORD result{ERROR_BUFFER_OVERFLOW}; + ULONG buffer_len = sizeof(IP_ADAPTER_INFO) * 10; + while (result == ERROR_BUFFER_OVERFLOW){ + buffer.resize(buffer_len); + adapter_info = reinterpret_cast(&buffer[0]); + result = GetAdaptersInfo(adapter_info, &buffer_len); + if (result == ERROR_NO_DATA){ + return {true, "GetAdaptersInfo return ERROR_NO_DATA"}; + } + }//end while + if (result != NO_ERROR){ + const std::string &error = "GetAdaptersInfo failed :" + std::to_string(result); + return {true, error}; + } + IP_ADAPTER_INFO *adapter_iterator = adapter_info; + while(adapter_iterator){ + std::shared_ptr_tmp{std::make_shared()}; + _tmp->set_name(adapter_iterator->AdapterName); + _tmp->set_description(adapter_iterator->Description); + _tmp->set_local_address(adapter_iterator->IpAddressList.IpAddress.String); + std::string lgw = adapter_iterator->GatewayList.IpAddress.String; + // if (lgw.length() == 0 || lgw.find("0.0.0.0") != std::string::npos) + // { + // //lgw = get_founded_route("8.8.8.8"); + // if (adapter_iterator->DhcpEnabled == 1) + // { + // lgw = adapter_iterator->DhcpServer.IpAddress.String; + // } + // } + _tmp->set_local_gateway(lgw); + _tmp->set_route_gateway(get_route_gateway()); + _adapters.emplace_back(_tmp); + adapter_iterator = adapter_iterator->Next; + } + return {false, ""}; +} + +RET_TYPE NetAdpInfo::get_adapter_info(std::string_view _adapter_name){ + + _index_of_adapter = -1; + const auto result{collect_adapters_data()}; + if (std::get<0>(result) == true){ + _index_of_adapter = -1; + return result; + } + const int16_t &len = static_cast(_adapters.size()); + for (auto i = 0; i< len; ++i){ + auto adap_name = _adapters[i]->get_name(); + auto adap_desc = _adapters[i]->get_description(); + if ( adap_name.find(_adapter_name) != std::string::npos || adap_desc.find(_adapter_name) != std::string::npos ){ + _index_of_adapter = i; + return {false, ""}; + } + } + return {true, "adapters no founded"}; +} + +std::string_view NetAdpInfo::get_adapter_route_gateway()const{ + if (_index_of_adapter < 0) + return "error adapter index"; + return _adapters.at(_index_of_adapter)->get_route_gateway(); +} +std::string_view NetAdpInfo::get_adapter_local_address()const{ + if (_index_of_adapter < 0) + return "error adapter index"; + return _adapters.at(_index_of_adapter)->get_local_address(); +} +std::string_view NetAdpInfo::get_adapter_local_gateway()const{ + if (_index_of_adapter < 0) + return "error adapter index"; + return _adapters.at(_index_of_adapter)->get_local_gateway(); +} +//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +//std::string NetAdpInfo::get_system_route(){ +// return get_route_gateway(); +//} +//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +}// end namespace diff --git a/client/client.pro b/client/client.pro index 489edd4f..9cec9572 100644 --- a/client/client.pro +++ b/client/client.pro @@ -13,6 +13,7 @@ include("3rd/QtSsh/src/botan/botan.pri") !android:!ios:include("3rd/SingleApplication/singleapplication.pri") include("3rd/QRCodeGenerator/QRCodeGenerator.pri") include ("3rd/SortFilterProxyModel/SortFilterProxyModel.pri") +include("3rd/AdpInfo/adpinfo.pri") INCLUDEPATH += $$PWD/3rd/OpenSSL/include DEPENDPATH += $$PWD/3rd/OpenSSL/include diff --git a/client/configurators/ikev2_configurator.cpp b/client/configurators/ikev2_configurator.cpp index de9f0e82..79ff90b3 100644 --- a/client/configurators/ikev2_configurator.cpp +++ b/client/configurators/ikev2_configurator.cpp @@ -22,7 +22,7 @@ Ikev2Configurator::ConnectionData Ikev2Configurator::prepareIkev2Config(const Se connData.host = credentials.hostName; connData.clientId = Utils::getRandomString(16); connData.password = Utils::getRandomString(16); - connData.password = ""; + //connData.password = ""; QString certFileName = "/opt/amnezia/ikev2/clients/" + connData.clientId + ".p12"; diff --git a/client/protocols/ikev2_vpn_protocol.cpp b/client/protocols/ikev2_vpn_protocol.cpp index 8f5d4cd3..7256e6bc 100644 --- a/client/protocols/ikev2_vpn_protocol.cpp +++ b/client/protocols/ikev2_vpn_protocol.cpp @@ -1,11 +1,10 @@ #include #include #include -//#include -//#include #include #include +#include #include "debug.h" #include "ikev2_vpn_protocol.h" @@ -14,11 +13,13 @@ static Ikev2Protocol* self = nullptr; static std::mutex rasDialFuncMutex; +//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ extern "C" { static void WINAPI RasDialFuncCallback(UINT unMsg, RASCONNSTATE rasconnstate, DWORD dwError ); } +//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Ikev2Protocol::Ikev2Protocol(const QJsonObject &configuration, QObject* parent) : VpnProtocol(configuration, parent) @@ -62,38 +63,26 @@ void Ikev2Protocol::newConnectionStateEventReceived(UINT unMsg, tagRASCONNSTATE case RASCS_OpenPort: qDebug()<<__FUNCTION__ << __LINE__; setConnectionState(Preparing); - //printf ("RASCS_OpenPort = %d\n", _connection_state); - //printf ("Opening port...\n"); break; case RASCS_PortOpened: qDebug()<<__FUNCTION__ << __LINE__; setConnectionState(Preparing); - //printf ("RASCS_PortOpened = %d\n", _connection_state); - //printf ("Port opened.\n"); break; case RASCS_ConnectDevice: qDebug()<<__FUNCTION__ << __LINE__; setConnectionState(Preparing); - //printf ("RASCS_ConnectDevice = %d\n", _connection_state); - //printf ("Connecting device...\n"); break; case RASCS_DeviceConnected: qDebug()<<__FUNCTION__ << __LINE__; setConnectionState(Preparing); - //printf ("RASCS_DeviceConnected = %d\n", _connection_state); - //printf ("Device connected.\n"); break; case RASCS_AllDevicesConnected: qDebug()<<__FUNCTION__ << __LINE__; setConnectionState(Preparing); - //printf ("RASCS_AllDevicesConnected = %d\n", _connection_state); - //printf ("All devices connected.\n"); break; case RASCS_Authenticate: qDebug()<<__FUNCTION__ << __LINE__; setConnectionState(Preparing); - //printf ("RASCS_Authenticate = %d\n", _connection_state); - // printf ("Authenticating...\n"); break; case RASCS_AuthNotify: qDebug()<<__FUNCTION__ << __LINE__; @@ -103,119 +92,74 @@ void Ikev2Protocol::newConnectionStateEventReceived(UINT unMsg, tagRASCONNSTATE } else { qDebug() << "RASCS_AuthNotify but no error" << dwError; } - //printf ("RASCS_AuthNotify = %d\n", _connection_state); - // printf ("Authentication notify.\n"); break; case RASCS_AuthRetry: qDebug()<<__FUNCTION__ << __LINE__; setConnectionState(Preparing); - //printf ("RASCS_AuthRetry = %d\n", _connection_state); - //printf ("Retrying authentication...\n"); break; case RASCS_AuthCallback: qDebug()<<__FUNCTION__ << __LINE__; - //printf ("RASCS_AuthCallback = %d\n", _connection_state); - //printf ("Authentication callback...\n"); break; case RASCS_AuthChangePassword: qDebug()<<__FUNCTION__ << __LINE__; - // printf ("RASCS_AuthChangePassword = %d\n", _connection_state); - //printf ("Change password...\n"); break; case RASCS_AuthProject: qDebug()<<__FUNCTION__ << __LINE__; - //printf ("RASCS_AuthProject = %d\n", _connection_state); - //printf ("Projection phase started...\n"); break; case RASCS_AuthLinkSpeed: qDebug()<<__FUNCTION__ << __LINE__; - //printf ("RASCS_AuthLinkSpeed = %d\n", _connection_state); - //printf ("Negoting speed...\n"); break; case RASCS_AuthAck: qDebug()<<__FUNCTION__ << __LINE__; - //printf ("RASCS_AuthAck = %d\n", _connection_state); - //printf ("Authentication acknowledge...\n"); break; case RASCS_ReAuthenticate: qDebug()<<__FUNCTION__ << __LINE__; - //printf ("RASCS_ReAuthenticate = %d\n", _connection_state); - //printf ("Retrying Authentication...\n"); break; case RASCS_Authenticated: qDebug()<<__FUNCTION__ << __LINE__; - //printf ("RASCS_Authenticated = %d\n", _connection_state); - //printf ("Authentication complete.\n"); break; case RASCS_PrepareForCallback: qDebug()<<__FUNCTION__ << __LINE__; - //printf ("RASCS_PrepareForCallback = %d\n", _connection_state); - //printf ("Preparing for callback...\n"); break; case RASCS_WaitForModemReset: qDebug()<<__FUNCTION__ << __LINE__; - //printf ("RASCS_WaitForModemReset = %d\n", _connection_state); - // printf ("Waiting for modem reset...\n"); break; case RASCS_WaitForCallback: qDebug()<<__FUNCTION__ << __LINE__; - //printf ("RASCS_WaitForCallback = %d\n", _connection_state); - //printf ("Waiting for callback...\n"); break; case RASCS_Projected: qDebug()<<__FUNCTION__ << __LINE__; - //printf ("RASCS_Projected = %d\n", _connection_state); - //printf ("Projection completed.\n"); break; #if (WINVER >= 0x400) case RASCS_StartAuthentication: // Windows 95 only qDebug()<<__FUNCTION__ << __LINE__; - //printf ("RASCS_StartAuthentication = %d\n", _connection_state); - //printf ("Starting authentication...\n"); - break; case RASCS_CallbackComplete: // Windows 95 only qDebug()<<__FUNCTION__ << __LINE__; - //printf ("RASCS_CallbackComplete = %d\n", rasconnstate); - //printf ("Callback complete.\n"); break; case RASCS_LogonNetwork: // Windows 95 only qDebug()<<__FUNCTION__ << __LINE__; - //printf ("RASCS_LogonNetwork = %d\n", _connection_state); - //printf ("Login to the network.\n"); break; #endif case RASCS_SubEntryConnected: qDebug()<<__FUNCTION__ << __LINE__; - //printf ("RASCS_SubEntryConnected = %d\n", _connection_state); - //printf ("Subentry connected.\n"); break; case RASCS_SubEntryDisconnected: qDebug()<<__FUNCTION__ << __LINE__; - //printf ("RASCS_SubEntryDisconnected = %d\n", _connection_state); - //printf ("Subentry disconnected.\n"); break; //PAUSED STATES: case RASCS_Interactive: qDebug()<<__FUNCTION__ << __LINE__; - //printf ("RASCS_Interactive = %d\n", _connection_state); - //printf ("In Paused state: Interactive mode.\n"); break; case RASCS_RetryAuthentication: qDebug()<<__FUNCTION__ << __LINE__; - //printf ("RASCS_RetryAuthentication = %d\n", _connection_state); - //printf ("In Paused state: Retry Authentication...\n"); break; case RASCS_CallbackSetByCaller: qDebug()<<__FUNCTION__ << __LINE__; - //printf ("RASCS_CallbackSetByCaller = %d\n", _connection_state); - //printf ("In Paused state: Callback set by Caller.\n"); break; case RASCS_PasswordExpired: setConnectionState(Error); qDebug()<<__FUNCTION__ << __LINE__; - //printf ("RASCS_PasswordExpired = %d\n", _connection_state); - //printf ("In Paused state: Password has expired...\n"); break; case RASCS_Connected: // = RASCS_DONE: @@ -224,17 +168,47 @@ void Ikev2Protocol::newConnectionStateEventReceived(UINT unMsg, tagRASCONNSTATE //printf ("RASCS_Connected = %d\n", _connection_state); //printf ("Connection completed.\n"); //SetEvent(gEvent_handle); + { + //get the network settings of adapters + std::this_thread::sleep_for(std::chrono::seconds(4)); + std::string p1,p2,p3; + 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(); + m_routeGateway = QString::fromStdString(p1); + m_vpnLocalAddress = QString::fromStdString(p2); + m_vpnGateway = QString::fromStdString(p3); + qDebug()<<"My ikev2 m_routeGateway "<(itret); + const auto msk = itret.szMaskIp;//std::get<1>(itret); + const auto gw = itret.szGatewayIp;//std::get<2>(itret); + const auto itf = itret.szInterfaceIp;//std::get<3>(itret); + const auto itfInd = itret.ulIfIndex; + qDebug()<<"IP["<setProgram("certutil"); - QStringList arguments({"-f" , "-importpfx", - "-p", m_config[config_key::password].toString(), - certFile.fileName(), "NoExport" + QStringList arguments({"-f" , "-p", m_config[config_key::password].toString(), + "-importpfx", certFile.fileName(), "NoExport" }); certInstallProcess->setArguments(arguments); - // qDebug() << arguments.join(" "); - // connect(certInstallProcess.data(), &PrivilegedProcess::errorOccurred, [certInstallProcess](QProcess::ProcessError error) { - // qDebug() << "PrivilegedProcess errorOccurred" << error; - // }); - - // 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(); - // }); - + connect(certInstallProcess.data(), &PrivilegedProcess::errorOccurred, [certInstallProcess](QProcess::ProcessError error) { + qDebug() << "PrivilegedProcess errorOccurred" << error; + }); certInstallProcess->start(); } - // /* + ///* { - // auto adapterRemoveProcess = new QProcess; - - // adapterRemoveProcess->setProgram("powershell"); - // QString arguments = QString("-command \"Remove-VpnConnection -Name '%1' -Force\"").arg(tunnelName()); - // adapterRemoveProcess->setNativeArguments(arguments); - - // adapterRemoveProcess->start(); - // adapterRemoveProcess->waitForFinished(5000); if ( disconnect_vpn()){ qDebug()<<"VPN was disconnected"; } @@ -314,26 +267,9 @@ ErrorCode Ikev2Protocol::start() } { - { - if ( !create_new_vpn(tunnelName(), m_config[config_key::hostName].toString())){ - qDebug() <<"Can't create the VPN connect"; - } + if ( !create_new_vpn(tunnelName(), m_config[config_key::hostName].toString())){ + qDebug() <<"Can't create the VPN connect"; } - // auto adapterInstallProcess = new QProcess; - - // adapterInstallProcess->setProgram("powershell"); - // QString arguments = QString("-command \"Add-VpnConnection " - // "-ServerAddress '%1' " - // "-Name '%2' " - // "-TunnelType IKEv2 " - // "-AuthenticationMethod MachineCertificate " - // "-EncryptionLevel Required " - // "-PassThru\"") - // .arg(m_config[config_key::hostName].toString()) - // .arg(tunnelName()); - // adapterInstallProcess->setNativeArguments(arguments); - // adapterInstallProcess->start(); - // adapterInstallProcess->waitForFinished(5000); } { @@ -352,10 +288,6 @@ ErrorCode Ikev2Protocol::start() .arg(tunnelName()); adapterConfigProcess->setNativeArguments(arguments); - // connect(adapterConfigProcess, &QProcess::readyRead, [adapterConfigProcess]() { - // qDebug().noquote() << "adapterConfigProcess readyRead" << adapterConfigProcess->readAll(); - // }); - adapterConfigProcess->start(); adapterConfigProcess->waitForFinished(5000); } @@ -365,7 +297,6 @@ ErrorCode Ikev2Protocol::start() qDebug()<<"We can't connect to VPN"; } } - //setConnectionState(Connecting); return ErrorCode::NoError; #else return ErrorCode::NoError; @@ -433,7 +364,7 @@ bool Ikev2Protocol::disconnect_vpn(){ return true; } - +//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ void WINAPI RasDialFuncCallback(UINT unMsg, RASCONNSTATE rasconnstate, DWORD dwError ){ @@ -442,5 +373,6 @@ void WINAPI RasDialFuncCallback(UINT unMsg, self->newConnectionStateEventReceived(unMsg, rasconnstate, dwError); } } +//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #endif diff --git a/client/protocols/ikev2_vpn_protocol.h b/client/protocols/ikev2_vpn_protocol.h index 05a23643..f6fe65ee 100644 --- a/client/protocols/ikev2_vpn_protocol.h +++ b/client/protocols/ikev2_vpn_protocol.h @@ -74,8 +74,8 @@ private: }; -#ifdef Q_OS_WIN -DWORD CALLBACK rasCallback(UINT msg, RASCONNSTATE rascs, DWORD err); -#endif +//#ifdef Q_OS_WIN +//DWORD CALLBACK rasCallback(UINT msg, RASCONNSTATE rascs, DWORD err); +//#endif #endif // IPSEC_PROTOCOL_H diff --git a/client/protocols/openvpnprotocol.cpp b/client/protocols/openvpnprotocol.cpp index 75e56674..d86c0095 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; } @@ -250,6 +249,42 @@ void OpenVpnProtocol::onReadyReadDataFromManagementServer() sendByteCount(); stopTimeoutTimer(); setConnectionState(VpnProtocol::Connected); + { + std::this_thread::sleep_for(std::chrono::seconds(4)); + 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 "<(itret); + const auto msk = itret.szMaskIp;//std::get<1>(itret); + const auto gw = itret.szGatewayIp;//std::get<2>(itret); + const auto itf = itret.szInterfaceIp;//std::get<3>(itret); + const auto itfInd = itret.ulIfIndex; + qDebug()<<"IP["<(ret)); + } + } continue; } else if (line.contains("EXITING,SIGTER")) { //openVpnStateSigTermHandler(); diff --git a/client/protocols/vpnprotocol.h b/client/protocols/vpnprotocol.h index 4b6876d5..0e3b98de 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; @@ -48,6 +50,8 @@ signals: void timeoutTimerEvent(); void protocolError(amnezia::ErrorCode e); + void route_avaible(QString ip, QString mask, QString gateway, QString interface_ip, unsigned long interface_index); + public slots: virtual void onTimeout(); // todo: remove? @@ -63,6 +67,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 d650cb3f..f563586c 100644 --- a/client/protocols/wireguardprotocol.cpp +++ b/client/protocols/wireguardprotocol.cpp @@ -97,26 +97,11 @@ void WireguardProtocol::readWireguardConfiguration(const QJsonObject &configurat } -//bool WireguardProtocol::openVpnProcessIsRunning() const -//{ -// return Utils::processIsRunning("openvpn"); -//} - QString WireguardProtocol::configPath() const { return m_configFileName; } -void WireguardProtocol::updateRouteGateway(QString line) -{ - // TODO: fix for macos - line = line.split("ROUTE_GATEWAY", QString::SkipEmptyParts).at(1); - if (!line.contains("/")) return; - m_routeGateway = line.split("/", QString::SkipEmptyParts).first(); - m_routeGateway.replace(" ", ""); - qDebug() << "Set VPN route gateway" << m_routeGateway; -} - QString WireguardProtocol::wireguardExecPath() const { #ifdef Q_OS_WIN @@ -185,8 +170,48 @@ ErrorCode WireguardProtocol::start() qDebug() << "WireguardProtocol::WireguardProtocol stateChanged" << newState; }); - connect(m_wireguardStartProcess.data(), &PrivilegedProcess::finished, this, [this]() { + connect(m_wireguardStartProcess.data(), &PrivilegedProcess::finished, this, [&]() { setConnectionState(VpnConnectionState::Connected); + { + //TODO:FIXME: without some ugly sleep we have't get a adapter parametrs + std::this_thread::sleep_for(std::chrono::seconds(4)); + 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 "<(itret); + const auto msk = itret.szMaskIp;//std::get<1>(itret); + const auto gw = itret.szGatewayIp;//std::get<2>(itret); + const auto itf = itret.szInterfaceIp;//std::get<3>(itret); + const auto itfInd = itret.ulIfIndex; + qDebug()<<"IP["<(ret)); + } + } }); connect(m_wireguardStartProcess.data(), &PrivilegedProcess::readyRead, this, [this]() { @@ -218,24 +243,6 @@ ErrorCode WireguardProtocol::start() #endif } -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' - -// 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()); -// } -// } - // } -} - QString WireguardProtocol::serviceName() const { return "AmneziaVPN.WireGuard0"; diff --git a/client/protocols/wireguardprotocol.h b/client/protocols/wireguardprotocol.h index 8ed26c7d..e2b272f9 100644 --- a/client/protocols/wireguardprotocol.h +++ b/client/protocols/wireguardprotocol.h @@ -24,11 +24,8 @@ public: private: QString configPath() const; QString wireguardExecPath() const; - //bool openVpnProcessIsRunning() const; void readWireguardConfiguration(const QJsonObject &configuration); - void updateRouteGateway(QString line); - void updateVpnGateway(const QString &line); QString serviceName() const; diff --git a/client/ui/pages_logic/VpnLogic.cpp b/client/ui/pages_logic/VpnLogic.cpp index 1d66547d..dfd1199e 100644 --- a/client/ui/pages_logic/VpnLogic.cpp +++ b/client/ui/pages_logic/VpnLogic.cpp @@ -13,7 +13,6 @@ VpnLogic::VpnLogic(UiLogic *logic, QObject *parent): m_radioButtonVpnModeAllSitesChecked{true}, m_radioButtonVpnModeForwardSitesChecked{false}, m_radioButtonVpnModeExceptSitesChecked{false}, - m_pushButtonVpnAddSiteEnabled{true}, m_labelSpeedReceivedText{tr("0 Mbps")}, m_labelSpeedSentText{tr("0 Mbps")}, @@ -42,7 +41,6 @@ void VpnLogic::onUpdatePage() set_radioButtonVpnModeAllSitesChecked(mode == Settings::VpnAllSites); set_radioButtonVpnModeForwardSitesChecked(mode == Settings::VpnOnlyForwardSites); set_radioButtonVpnModeExceptSitesChecked(mode == Settings::VpnAllExceptSites); - set_pushButtonVpnAddSiteEnabled(mode != Settings::VpnAllSites); const QJsonObject &server = uiLogic()->m_settings.defaultServer(); QString serverString = QString("%2 (%3)") @@ -59,16 +57,19 @@ void VpnLogic::onUpdatePage() void VpnLogic::onRadioButtonVpnModeAllSitesClicked() { m_settings.setRouteMode(Settings::VpnAllSites); + onUpdatePage(); } void VpnLogic::onRadioButtonVpnModeForwardSitesClicked() { m_settings.setRouteMode(Settings::VpnOnlyForwardSites); + onUpdatePage(); } void VpnLogic::onRadioButtonVpnModeExceptSitesClicked() { m_settings.setRouteMode(Settings::VpnAllExceptSites); + onUpdatePage(); } void VpnLogic::onBytesChanged(quint64 receivedData, quint64 sentData) diff --git a/client/ui/pages_logic/VpnLogic.h b/client/ui/pages_logic/VpnLogic.h index d077ba90..176f61c7 100644 --- a/client/ui/pages_logic/VpnLogic.h +++ b/client/ui/pages_logic/VpnLogic.h @@ -20,7 +20,6 @@ class VpnLogic : public PageLogicBase AUTO_PROPERTY(bool, pushButtonConnectVisible) AUTO_PROPERTY(bool, widgetVpnModeEnabled) AUTO_PROPERTY(QString, labelErrorText) - AUTO_PROPERTY(bool, pushButtonVpnAddSiteEnabled) AUTO_PROPERTY(bool, radioButtonVpnModeAllSitesChecked) AUTO_PROPERTY(bool, radioButtonVpnModeForwardSitesChecked) diff --git a/client/ui/qml/Pages/PageVPN.qml b/client/ui/qml/Pages/PageVPN.qml index 7dad8e25..6bd0420e 100644 --- a/client/ui/qml/Pages/PageVPN.qml +++ b/client/ui/qml/Pages/PageVPN.qml @@ -273,7 +273,7 @@ PageBase { width: parent.width - 40 height: GC.isMobile() ? 0: 40 text: qsTr("+ Add site") - enabled: VpnLogic.pushButtonVpnAddSiteEnabled + enabled: ! VpnLogic.radioButtonVpnModeAllSitesChecked background: Rectangle { anchors.fill: parent radius: 4