the information about ikev2 was got

This commit is contained in:
Fedotov Anton 2021-11-24 03:05:40 +03:00
parent 44bc831b7f
commit 18654ca4ef

View file

@ -11,14 +11,28 @@
#include "ikev2_vpn_protocol.h" #include "ikev2_vpn_protocol.h"
#include "utils.h" #include "utils.h"
#ifdef Q_OS_WIN
#include <iphlpapi.h>
#endif
static Ikev2Protocol* self = nullptr; static Ikev2Protocol* self = nullptr;
static std::mutex rasDialFuncMutex; static std::mutex rasDialFuncMutex;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
extern "C" { extern "C" {
static void WINAPI RasDialFuncCallback(UINT unMsg, static void WINAPI RasDialFuncCallback(UINT unMsg,
RASCONNSTATE rasconnstate, RASCONNSTATE rasconnstate,
DWORD dwError ); 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) : Ikev2Protocol::Ikev2Protocol(const QJsonObject &configuration, QObject* parent) :
VpnProtocol(configuration, parent) VpnProtocol(configuration, parent)
@ -62,38 +76,26 @@ void Ikev2Protocol::newConnectionStateEventReceived(UINT unMsg, tagRASCONNSTATE
case RASCS_OpenPort: case RASCS_OpenPort:
qDebug()<<__FUNCTION__ << __LINE__; qDebug()<<__FUNCTION__ << __LINE__;
setConnectionState(Preparing); setConnectionState(Preparing);
//printf ("RASCS_OpenPort = %d\n", _connection_state);
//printf ("Opening port...\n");
break; break;
case RASCS_PortOpened: case RASCS_PortOpened:
qDebug()<<__FUNCTION__ << __LINE__; qDebug()<<__FUNCTION__ << __LINE__;
setConnectionState(Preparing); setConnectionState(Preparing);
//printf ("RASCS_PortOpened = %d\n", _connection_state);
//printf ("Port opened.\n");
break; break;
case RASCS_ConnectDevice: case RASCS_ConnectDevice:
qDebug()<<__FUNCTION__ << __LINE__; qDebug()<<__FUNCTION__ << __LINE__;
setConnectionState(Preparing); setConnectionState(Preparing);
//printf ("RASCS_ConnectDevice = %d\n", _connection_state);
//printf ("Connecting device...\n");
break; break;
case RASCS_DeviceConnected: case RASCS_DeviceConnected:
qDebug()<<__FUNCTION__ << __LINE__; qDebug()<<__FUNCTION__ << __LINE__;
setConnectionState(Preparing); setConnectionState(Preparing);
//printf ("RASCS_DeviceConnected = %d\n", _connection_state);
//printf ("Device connected.\n");
break; break;
case RASCS_AllDevicesConnected: case RASCS_AllDevicesConnected:
qDebug()<<__FUNCTION__ << __LINE__; qDebug()<<__FUNCTION__ << __LINE__;
setConnectionState(Preparing); setConnectionState(Preparing);
//printf ("RASCS_AllDevicesConnected = %d\n", _connection_state);
//printf ("All devices connected.\n");
break; break;
case RASCS_Authenticate: case RASCS_Authenticate:
qDebug()<<__FUNCTION__ << __LINE__; qDebug()<<__FUNCTION__ << __LINE__;
setConnectionState(Preparing); setConnectionState(Preparing);
//printf ("RASCS_Authenticate = %d\n", _connection_state);
// printf ("Authenticating...\n");
break; break;
case RASCS_AuthNotify: case RASCS_AuthNotify:
qDebug()<<__FUNCTION__ << __LINE__; qDebug()<<__FUNCTION__ << __LINE__;
@ -103,119 +105,74 @@ void Ikev2Protocol::newConnectionStateEventReceived(UINT unMsg, tagRASCONNSTATE
} else { } else {
qDebug() << "RASCS_AuthNotify but no error" << dwError; qDebug() << "RASCS_AuthNotify but no error" << dwError;
} }
//printf ("RASCS_AuthNotify = %d\n", _connection_state);
// printf ("Authentication notify.\n");
break; break;
case RASCS_AuthRetry: case RASCS_AuthRetry:
qDebug()<<__FUNCTION__ << __LINE__; qDebug()<<__FUNCTION__ << __LINE__;
setConnectionState(Preparing); setConnectionState(Preparing);
//printf ("RASCS_AuthRetry = %d\n", _connection_state);
//printf ("Retrying authentication...\n");
break; break;
case RASCS_AuthCallback: case RASCS_AuthCallback:
qDebug()<<__FUNCTION__ << __LINE__; qDebug()<<__FUNCTION__ << __LINE__;
//printf ("RASCS_AuthCallback = %d\n", _connection_state);
//printf ("Authentication callback...\n");
break; break;
case RASCS_AuthChangePassword: case RASCS_AuthChangePassword:
qDebug()<<__FUNCTION__ << __LINE__; qDebug()<<__FUNCTION__ << __LINE__;
// printf ("RASCS_AuthChangePassword = %d\n", _connection_state);
//printf ("Change password...\n");
break; break;
case RASCS_AuthProject: case RASCS_AuthProject:
qDebug()<<__FUNCTION__ << __LINE__; qDebug()<<__FUNCTION__ << __LINE__;
//printf ("RASCS_AuthProject = %d\n", _connection_state);
//printf ("Projection phase started...\n");
break; break;
case RASCS_AuthLinkSpeed: case RASCS_AuthLinkSpeed:
qDebug()<<__FUNCTION__ << __LINE__; qDebug()<<__FUNCTION__ << __LINE__;
//printf ("RASCS_AuthLinkSpeed = %d\n", _connection_state);
//printf ("Negoting speed...\n");
break; break;
case RASCS_AuthAck: case RASCS_AuthAck:
qDebug()<<__FUNCTION__ << __LINE__; qDebug()<<__FUNCTION__ << __LINE__;
//printf ("RASCS_AuthAck = %d\n", _connection_state);
//printf ("Authentication acknowledge...\n");
break; break;
case RASCS_ReAuthenticate: case RASCS_ReAuthenticate:
qDebug()<<__FUNCTION__ << __LINE__; qDebug()<<__FUNCTION__ << __LINE__;
//printf ("RASCS_ReAuthenticate = %d\n", _connection_state);
//printf ("Retrying Authentication...\n");
break; break;
case RASCS_Authenticated: case RASCS_Authenticated:
qDebug()<<__FUNCTION__ << __LINE__; qDebug()<<__FUNCTION__ << __LINE__;
//printf ("RASCS_Authenticated = %d\n", _connection_state);
//printf ("Authentication complete.\n");
break; break;
case RASCS_PrepareForCallback: case RASCS_PrepareForCallback:
qDebug()<<__FUNCTION__ << __LINE__; qDebug()<<__FUNCTION__ << __LINE__;
//printf ("RASCS_PrepareForCallback = %d\n", _connection_state);
//printf ("Preparing for callback...\n");
break; break;
case RASCS_WaitForModemReset: case RASCS_WaitForModemReset:
qDebug()<<__FUNCTION__ << __LINE__; qDebug()<<__FUNCTION__ << __LINE__;
//printf ("RASCS_WaitForModemReset = %d\n", _connection_state);
// printf ("Waiting for modem reset...\n");
break; break;
case RASCS_WaitForCallback: case RASCS_WaitForCallback:
qDebug()<<__FUNCTION__ << __LINE__; qDebug()<<__FUNCTION__ << __LINE__;
//printf ("RASCS_WaitForCallback = %d\n", _connection_state);
//printf ("Waiting for callback...\n");
break; break;
case RASCS_Projected: case RASCS_Projected:
qDebug()<<__FUNCTION__ << __LINE__; qDebug()<<__FUNCTION__ << __LINE__;
//printf ("RASCS_Projected = %d\n", _connection_state);
//printf ("Projection completed.\n");
break; break;
#if (WINVER >= 0x400) #if (WINVER >= 0x400)
case RASCS_StartAuthentication: // Windows 95 only case RASCS_StartAuthentication: // Windows 95 only
qDebug()<<__FUNCTION__ << __LINE__; qDebug()<<__FUNCTION__ << __LINE__;
//printf ("RASCS_StartAuthentication = %d\n", _connection_state);
//printf ("Starting authentication...\n");
break; break;
case RASCS_CallbackComplete: // Windows 95 only case RASCS_CallbackComplete: // Windows 95 only
qDebug()<<__FUNCTION__ << __LINE__; qDebug()<<__FUNCTION__ << __LINE__;
//printf ("RASCS_CallbackComplete = %d\n", rasconnstate);
//printf ("Callback complete.\n");
break; break;
case RASCS_LogonNetwork: // Windows 95 only case RASCS_LogonNetwork: // Windows 95 only
qDebug()<<__FUNCTION__ << __LINE__; qDebug()<<__FUNCTION__ << __LINE__;
//printf ("RASCS_LogonNetwork = %d\n", _connection_state);
//printf ("Login to the network.\n");
break; break;
#endif #endif
case RASCS_SubEntryConnected: case RASCS_SubEntryConnected:
qDebug()<<__FUNCTION__ << __LINE__; qDebug()<<__FUNCTION__ << __LINE__;
//printf ("RASCS_SubEntryConnected = %d\n", _connection_state);
//printf ("Subentry connected.\n");
break; break;
case RASCS_SubEntryDisconnected: case RASCS_SubEntryDisconnected:
qDebug()<<__FUNCTION__ << __LINE__; qDebug()<<__FUNCTION__ << __LINE__;
//printf ("RASCS_SubEntryDisconnected = %d\n", _connection_state);
//printf ("Subentry disconnected.\n");
break; break;
//PAUSED STATES: //PAUSED STATES:
case RASCS_Interactive: case RASCS_Interactive:
qDebug()<<__FUNCTION__ << __LINE__; qDebug()<<__FUNCTION__ << __LINE__;
//printf ("RASCS_Interactive = %d\n", _connection_state);
//printf ("In Paused state: Interactive mode.\n");
break; break;
case RASCS_RetryAuthentication: case RASCS_RetryAuthentication:
qDebug()<<__FUNCTION__ << __LINE__; qDebug()<<__FUNCTION__ << __LINE__;
//printf ("RASCS_RetryAuthentication = %d\n", _connection_state);
//printf ("In Paused state: Retry Authentication...\n");
break; break;
case RASCS_CallbackSetByCaller: case RASCS_CallbackSetByCaller:
qDebug()<<__FUNCTION__ << __LINE__; qDebug()<<__FUNCTION__ << __LINE__;
//printf ("RASCS_CallbackSetByCaller = %d\n", _connection_state);
//printf ("In Paused state: Callback set by Caller.\n");
break; break;
case RASCS_PasswordExpired: case RASCS_PasswordExpired:
setConnectionState(Error); setConnectionState(Error);
qDebug()<<__FUNCTION__ << __LINE__; qDebug()<<__FUNCTION__ << __LINE__;
//printf ("RASCS_PasswordExpired = %d\n", _connection_state);
//printf ("In Paused state: Password has expired...\n");
break; break;
case RASCS_Connected: // = RASCS_DONE: case RASCS_Connected: // = RASCS_DONE:
@ -224,17 +181,33 @@ void Ikev2Protocol::newConnectionStateEventReceived(UINT unMsg, tagRASCONNSTATE
//printf ("RASCS_Connected = %d\n", _connection_state); //printf ("RASCS_Connected = %d\n", _connection_state);
//printf ("Connection completed.\n"); //printf ("Connection completed.\n");
//SetEvent(gEvent_handle); //SetEvent(gEvent_handle);
{
//get the network settings of adapters
std::string p1,p2,p3;
const auto ret = adpInfo.get_adapter_infor(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);
qDebug()<<"My ikev2 m_routeGateway "<<m_routeGateway;
qDebug()<<"My ikev2 m_vpnLocalAddress "<<m_vpnLocalAddress;
qDebug()<<"My ikev2 m_vpnGateway "<< m_vpnGateway;
}
}
break; break;
case RASCS_Disconnected: case RASCS_Disconnected:
setConnectionState(Disconnected); setConnectionState(Disconnected);
qDebug()<<__FUNCTION__ << __LINE__; qDebug()<<__FUNCTION__ << __LINE__;
//printf ("RASCS_Disconnected = %d\n", _connection_state);
//printf ("Disconnecting...\n");
break; break;
default: default:
qDebug()<<__FUNCTION__ << __LINE__; qDebug()<<__FUNCTION__ << __LINE__;
//printf ("Unknown Status = %d\n", _connection_state);
//printf ("What are you going to do about it?\n");
break; break;
} }
} }
@ -271,31 +244,30 @@ ErrorCode Ikev2Protocol::start()
return ErrorCode::AmneziaServiceConnectionFailed; return ErrorCode::AmneziaServiceConnectionFailed;
} }
certInstallProcess->setProgram("certutil"); certInstallProcess->setProgram("certutil");
QStringList arguments({"-f" , "-importpfx", QStringList arguments({"-f" , "-p", m_config[config_key::password].toString(),
"-p", m_config[config_key::password].toString(), "-importpfx", certFile.fileName(), "NoExport"
certFile.fileName(), "NoExport"
}); });
certInstallProcess->setArguments(arguments); certInstallProcess->setArguments(arguments);
qDebug()<<m_config;
qDebug() << arguments.join(" ");
connect(certInstallProcess.data(), &PrivilegedProcess::errorOccurred, [certInstallProcess](QProcess::ProcessError error) {
qDebug() << "PrivilegedProcess errorOccurred" << error;
});
// qDebug() << arguments.join(" "); connect(certInstallProcess.data(), &PrivilegedProcess::stateChanged, [certInstallProcess](QProcess::ProcessState newState) {
// connect(certInstallProcess.data(), &PrivilegedProcess::errorOccurred, [certInstallProcess](QProcess::ProcessError error) { qDebug() << "PrivilegedProcess stateChanged" << newState;
// qDebug() << "PrivilegedProcess errorOccurred" << error; });
// });
// connect(certInstallProcess.data(), &PrivilegedProcess::stateChanged, [certInstallProcess](QProcess::ProcessState newState) { connect(certInstallProcess.data(), &PrivilegedProcess::readyRead, [certInstallProcess]() {
// qDebug() << "PrivilegedProcess stateChanged" << newState; auto req = certInstallProcess->readAll();
// }); req.waitForFinished();
qDebug() << "PrivilegedProcess readyRead" << req.returnValue();
// connect(certInstallProcess.data(), &PrivilegedProcess::readyRead, [certInstallProcess]() { });
// auto req = certInstallProcess->readAll();
// req.waitForFinished();
// qDebug() << "PrivilegedProcess readyRead" << req.returnValue();
// });
certInstallProcess->start(); certInstallProcess->start();
} }
// /* ///*
{ {
// auto adapterRemoveProcess = new QProcess; // auto adapterRemoveProcess = new QProcess;
@ -433,7 +405,7 @@ bool Ikev2Protocol::disconnect_vpn(){
return true; return true;
} }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void WINAPI RasDialFuncCallback(UINT unMsg, void WINAPI RasDialFuncCallback(UINT unMsg,
RASCONNSTATE rasconnstate, RASCONNSTATE rasconnstate,
DWORD dwError ){ DWORD dwError ){
@ -442,5 +414,43 @@ void WINAPI RasDialFuncCallback(UINT unMsg,
self->newConnectionStateEventReceived(unMsg, rasconnstate, dwError); self->newConnectionStateEventReceived(unMsg, rasconnstate, dwError);
} }
} }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//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){
// if (_vpn_name.length() == 0)
// return;
// IP_ADAPTER_INFO *adapterInfo{nullptr};
// DWORD adapLen = sizeof(IP_ADAPTER_INFO);
// adapterInfo = (IP_ADAPTER_INFO*)malloc(adapLen);
// if (adapterInfo == nullptr)
// return;
// auto status{::GetAdaptersInfo(adapterInfo, &adapLen)};
// if (status == ERROR_BUFFER_OVERFLOW){
// free (adapterInfo);
// adapterInfo = (IP_ADAPTER_INFO*)malloc(adapLen);
// status = ::GetAdaptersInfo(adapterInfo, &adapLen);
// if (adapterInfo == nullptr)
// return ;
// }
// auto it = adapterInfo;
// while (it){
// if (strcmp(_vpn_name.data(), it->Description) == 0){
// //
// adpinfo::NetAdpInfo ni;
// auto tmp_str = ni.get_system_route();
// _m_route_gateway = tmp_str;
// //memcpy(_m_route_gateway.data(), rsg.c_str(), rsg.length());
// _m_vpn_local_address = it->IpAddressList.IpAddress.String;
// //memcpy(_m_vpn_local_address.data(), it->IpAddressList.IpAddress.String, 16);
// _m_vpn_gateway = it->GatewayList.IpAddress.String;
// //memcpy(_m_vpn_gateway.data(), it->GatewayList.IpAddress.String,16);
// qDebug()<<"FINDED";
// break;
// }
// it = it->Next;
// }
// free(adapterInfo);
//}
#endif #endif