mirror of
https://github.com/amnezia-vpn/amnezia-client.git
synced 2026-05-08 14:33:23 +00:00
* feat: add reconnect in case of changing network * fix: reconnect to VPN on wakeup * fix: linux wakeup build
80 lines
2.6 KiB
C++
80 lines
2.6 KiB
C++
#include <QCoreApplication>
|
|
#include <QFileInfo>
|
|
#include <QProcess>
|
|
#include <QTcpSocket>
|
|
#include <QThread>
|
|
|
|
#include "wireguardprotocol.h"
|
|
#include "core/networkUtilities.h"
|
|
|
|
#include "mozilla/localsocketcontroller.h"
|
|
|
|
WireguardProtocol::WireguardProtocol(const QJsonObject &configuration, QObject *parent)
|
|
: VpnProtocol(configuration, parent)
|
|
{
|
|
m_impl.reset(new LocalSocketController());
|
|
connect(m_impl.get(), &ControllerImpl::connected, this,
|
|
[this](const QString &pubkey, const QDateTime &connectionTimestamp) {
|
|
setConnectionState(Vpn::ConnectionState::Connected);
|
|
});
|
|
connect(m_impl.get(), &ControllerImpl::statusUpdated, this,
|
|
[this](const QString& serverIpv4Gateway,
|
|
const QString& deviceIpv4Address, uint64_t txBytes,
|
|
uint64_t rxBytes) {
|
|
const QString previousGateway = m_vpnGateway;
|
|
const QString previousLocal = m_vpnLocalAddress;
|
|
|
|
if (!serverIpv4Gateway.isEmpty()) {
|
|
m_vpnGateway = serverIpv4Gateway;
|
|
}
|
|
if (!deviceIpv4Address.isEmpty()) {
|
|
m_vpnLocalAddress = deviceIpv4Address;
|
|
}
|
|
|
|
if ((!m_vpnGateway.isEmpty() && m_vpnGateway != previousGateway) ||
|
|
(!m_vpnLocalAddress.isEmpty() && m_vpnLocalAddress != previousLocal)) {
|
|
emit tunnelAddressesUpdated(m_vpnGateway, m_vpnLocalAddress);
|
|
}
|
|
});
|
|
|
|
connect(m_impl.get(), &ControllerImpl::disconnected, this,
|
|
[this]() { setConnectionState(Vpn::ConnectionState::Disconnected); });
|
|
m_impl->initialize(nullptr, nullptr);
|
|
}
|
|
|
|
WireguardProtocol::~WireguardProtocol()
|
|
{
|
|
WireguardProtocol::stop();
|
|
QThread::msleep(200);
|
|
}
|
|
|
|
void WireguardProtocol::stop()
|
|
{
|
|
stopMzImpl();
|
|
return;
|
|
}
|
|
|
|
ErrorCode WireguardProtocol::startMzImpl()
|
|
{
|
|
QString protocolName = m_rawConfig.value("protocol").toString();
|
|
QJsonObject vpnConfigData = m_rawConfig.value(protocolName + "_config_data").toObject();
|
|
vpnConfigData[config_key::hostName] = NetworkUtilities::getIPAddress(vpnConfigData.value(config_key::hostName).toString());
|
|
m_rawConfig.insert(protocolName + "_config_data", vpnConfigData);
|
|
m_rawConfig[config_key::hostName] = NetworkUtilities::getIPAddress(m_rawConfig[config_key::hostName].toString());
|
|
|
|
m_impl->activate(m_rawConfig);
|
|
return ErrorCode::NoError;
|
|
}
|
|
|
|
ErrorCode WireguardProtocol::stopMzImpl()
|
|
{
|
|
m_impl->deactivate();
|
|
return ErrorCode::NoError;
|
|
}
|
|
|
|
|
|
ErrorCode WireguardProtocol::start()
|
|
{
|
|
return startMzImpl();
|
|
}
|