Files
amnezia-client/client/vpnconnection.cpp
T

613 lines
23 KiB
C++
Raw Normal View History

#include "vpnconnection.h"
2023-08-30 15:10:44 +05:00
2020-12-26 15:03:51 +03:00
#include <QDebug>
#include <QEventLoop>
2021-01-06 17:12:24 +03:00
#include <QFile>
2022-01-22 20:00:06 +03:00
#include <QHostInfo>
2021-02-18 15:00:41 +03:00
#include <QJsonObject>
#include <QObject>
#include <QSharedPointer>
#include <QString>
#include <QStringList>
#include <QTimer>
2020-12-26 15:03:51 +03:00
2021-04-04 23:12:36 +03:00
#include <configurators/cloak_configurator.h>
2023-08-28 22:03:28 +03:00
#include <configurators/openvpn_configurator.h>
2021-05-07 23:28:37 +03:00
#include <configurators/shadowsocks_configurator.h>
2023-08-28 22:03:28 +03:00
#include <configurators/wireguard_configurator.h>
2022-02-22 02:08:57 +03:00
#ifdef AMNEZIA_DESKTOP
2023-08-28 22:03:28 +03:00
#include "core/ipcclient.h"
#include <protocols/wireguardprotocol.h>
2022-02-22 02:08:57 +03:00
#endif
2021-01-06 17:12:24 +03:00
2021-09-30 18:16:41 +03:00
#ifdef Q_OS_ANDROID
2023-12-04 18:23:08 +03:00
#include "platforms/android/android_controller.h"
#include <QThread>
2021-09-30 18:16:41 +03:00
#endif
#if defined(Q_OS_IOS) || defined(MACOS_NE)
2023-08-28 22:03:28 +03:00
#include "platforms/ios/ios_controller.h"
#endif
2024-03-28 17:13:48 +00:00
#include "core/networkUtilities.h"
2020-12-26 15:03:51 +03:00
#include "vpnconnection.h"
VpnConnection::VpnConnection(std::shared_ptr<Settings> settings, QObject *parent)
: QObject(parent), m_settings(settings), m_checkTimer(new QTimer(this))
2021-12-20 15:43:36 +03:00
{
#if defined(Q_OS_IOS) || defined(MACOS_NE)
2025-11-30 18:49:16 -08:00
m_checkTimer.setInterval(1000);
connect(IosController::Instance(), &IosController::connectionStateChanged, this, &VpnConnection::onConnectionStateChanged);
2023-08-27 10:46:41 -07:00
connect(IosController::Instance(), &IosController::bytesChanged, this, &VpnConnection::onBytesChanged);
2023-08-28 22:03:28 +03:00
2023-08-27 10:46:41 -07:00
#endif
2021-02-18 15:00:41 +03:00
}
2021-02-03 15:42:36 +03:00
2021-02-18 15:00:41 +03:00
VpnConnection::~VpnConnection()
{
2020-12-26 15:03:51 +03:00
}
void VpnConnection::onBytesChanged(quint64 receivedBytes, quint64 sentBytes)
{
emit bytesChanged(receivedBytes, sentBytes);
2020-12-26 15:03:51 +03:00
}
2025-05-02 23:54:36 -07:00
void VpnConnection::onKillSwitchModeChanged(bool enabled)
{
#ifdef AMNEZIA_DESKTOP
2025-12-19 04:09:50 +01:00
IpcClient::withInterface([enabled](QSharedPointer<IpcInterfaceReplica> iface){
QRemoteObjectPendingReply<bool> reply = iface->refreshKillSwitch(enabled);
if (reply.waitForFinished(1000) && reply.returnValue())
qDebug() << "VpnConnection::onKillSwitchModeChanged: Killswitch refreshed";
else
qWarning() << "VpnConnection::onKillSwitchModeChanged: Failed to execute remote refreshKillSwitch call";
});
2025-05-02 23:54:36 -07:00
#endif
}
2023-05-14 21:11:19 +08:00
void VpnConnection::onConnectionStateChanged(Vpn::ConnectionState state)
2020-12-26 15:03:51 +03:00
{
2022-02-22 02:08:57 +03:00
#ifdef AMNEZIA_DESKTOP
auto container = m_settings->defaultContainer(m_settings->defaultServerIndex());
2025-12-19 04:09:50 +01:00
IpcClient::withInterface([&](QSharedPointer<IpcInterfaceReplica> iface) {
2023-08-28 22:03:28 +03:00
if (state == Vpn::ConnectionState::Connected) {
2025-12-19 04:09:50 +01:00
iface->resetIpStack();
iface->flushDns();
2021-02-18 15:00:41 +03:00
2025-12-11 15:18:36 +08:00
if (!ContainerProps::isAwgContainer(container) &&
container != DockerContainer::WireGuard) {
QString dns1 = m_vpnConfiguration.value(config_key::dns1).toString();
QString dns2 = m_vpnConfiguration.value(config_key::dns2).toString();
2022-02-01 19:48:59 +03:00
2025-12-19 04:09:50 +01:00
iface->routeAddList(m_vpnProtocol->vpnGateway(), QStringList() << dns1 << dns2);
2021-01-26 15:01:15 +03:00
2024-04-25 20:01:00 +07:00
if (m_settings->isSitesSplitTunnelingEnabled()) {
2025-12-19 04:09:50 +01:00
iface->routeDeleteList(m_vpnProtocol->vpnGateway(), QStringList() << "0.0.0.0");
// qDebug() << "VpnConnection::onConnectionStateChanged :: adding custom routes, count:" << forwardIps.size();
if (m_settings->routeMode() == Settings::VpnOnlyForwardSites) {
QTimer::singleShot(1000, m_vpnProtocol.data(),
[this]() { addSitesRoutes(m_vpnProtocol->vpnGateway(), m_settings->routeMode()); });
} else if (m_settings->routeMode() == Settings::VpnAllExceptSites) {
2025-12-19 04:09:50 +01:00
iface->routeAddList(m_vpnProtocol->vpnGateway(), QStringList() << "0.0.0.0/1");
iface->routeAddList(m_vpnProtocol->vpnGateway(), QStringList() << "128.0.0.0/1");
2025-12-19 04:09:50 +01:00
iface->routeAddList(m_vpnProtocol->routeGateway(), QStringList() << remoteAddress());
addSitesRoutes(m_vpnProtocol->routeGateway(), m_settings->routeMode());
}
}
2021-02-18 15:00:41 +03:00
}
2021-06-01 18:18:09 +03:00
if (container != DockerContainer::Ipsec) {
if (startNetworkCheckIfReady()) {
m_pendingNetworkCheck = false;
} else {
m_pendingNetworkCheck = true;
qWarning() << "Deferring startNetworkCheck; missing gateway/local address"
<< m_vpnProtocol->vpnGateway() << m_vpnProtocol->vpnLocalAddress();
}
} else {
m_pendingNetworkCheck = false;
}
2023-08-28 22:03:28 +03:00
} else if (state == Vpn::ConnectionState::Error) {
m_pendingNetworkCheck = false;
2025-12-19 04:09:50 +01:00
iface->flushDns();
2021-01-26 15:01:15 +03:00
2024-04-25 20:01:00 +07:00
if (m_settings->isSitesSplitTunnelingEnabled()) {
if (m_settings->routeMode() == Settings::VpnOnlyForwardSites) {
2025-12-19 04:09:50 +01:00
iface->clearSavedRoutes();
}
2021-02-18 15:00:41 +03:00
}
2024-03-27 11:02:34 +00:00
} else if (state == Vpn::ConnectionState::Connecting) {
} else if (state == Vpn::ConnectionState::Disconnected) {
m_pendingNetworkCheck = false;
2025-12-19 04:09:50 +01:00
auto result = iface->stopNetworkCheck();
result.waitForFinished(3000);
2021-02-18 15:00:41 +03:00
}
2025-12-19 04:09:50 +01:00
});
2022-02-22 02:08:57 +03:00
#endif
2022-12-12 16:16:12 +04:00
#if defined(Q_OS_IOS) || defined(MACOS_NE)
2023-08-13 11:28:32 +05:00
if (state == Vpn::ConnectionState::Connected) {
2023-08-08 19:02:41 -07:00
m_checkTimer.start();
2023-08-28 22:03:28 +03:00
} else {
2023-08-08 19:02:41 -07:00
m_checkTimer.stop();
2022-12-12 16:16:12 +04:00
}
#endif
2020-12-26 15:03:51 +03:00
emit connectionStateChanged(state);
}
2021-06-12 11:59:36 +03:00
const QString &VpnConnection::remoteAddress() const
{
return m_remoteAddress;
}
2022-01-22 20:00:06 +03:00
void VpnConnection::addSitesRoutes(const QString &gw, Settings::RouteMode mode)
{
2022-02-22 02:08:57 +03:00
#ifdef AMNEZIA_DESKTOP
2022-01-22 20:00:06 +03:00
QStringList ips;
QStringList sites;
2022-08-25 12:47:02 +03:00
const QVariantMap &m = m_settings->vpnSites(mode);
2022-01-22 20:00:06 +03:00
for (auto i = m.constBegin(); i != m.constEnd(); ++i) {
2024-03-27 11:02:34 +00:00
if (NetworkUtilities::checkIpSubnetFormat(i.key())) {
2022-01-22 20:00:06 +03:00
ips.append(i.key());
2023-08-28 22:03:28 +03:00
} else {
2024-03-27 11:02:34 +00:00
if (NetworkUtilities::checkIpSubnetFormat(i.value().toString())) {
2022-01-22 20:00:06 +03:00
ips.append(i.value().toString());
}
sites.append(i.key());
}
}
ips.removeDuplicates();
2025-12-19 04:09:50 +01:00
IpcClient::withInterface([&](QSharedPointer<IpcInterfaceReplica> iface) {
iface->routeAddList(gw, ips);
});
2022-01-22 20:00:06 +03:00
// re-resolve domains
2023-08-28 22:03:28 +03:00
for (const QString &site : sites) {
const auto &cbResolv = [this, site, gw, mode, ips](const QHostInfo &hostInfo) {
const QList<QHostAddress> &addresses = hostInfo.addresses();
QString ipv4Addr;
for (const QHostAddress &addr : hostInfo.addresses()) {
if (addr.protocol() == QAbstractSocket::NetworkLayerProtocol::IPv4Protocol) {
const QString &ip = addr.toString();
// qDebug() << "VpnConnection::addSitesRoutes updating site" << site << ip;
if (!ips.contains(ip)) {
2025-12-19 04:09:50 +01:00
IpcClient::withInterface([&gw, &ip](QSharedPointer<IpcInterfaceReplica> iface) {
iface->routeAddList(gw, QStringList() << ip);
});
2023-08-28 22:03:28 +03:00
m_settings->addVpnSite(mode, site, ip);
2022-01-22 20:00:06 +03:00
}
2023-08-28 22:03:28 +03:00
flushDns();
break;
2022-01-22 20:00:06 +03:00
}
2023-08-28 22:03:28 +03:00
}
};
QHostInfo::lookupHost(site, this, cbResolv);
2022-01-22 20:00:06 +03:00
}
2022-02-22 02:08:57 +03:00
#endif
2022-01-22 20:00:06 +03:00
}
2021-02-18 15:00:41 +03:00
QSharedPointer<VpnProtocol> VpnConnection::vpnProtocol() const
{
return m_vpnProtocol;
}
2021-06-01 18:18:09 +03:00
void VpnConnection::addRoutes(const QStringList &ips)
{
2022-02-22 02:08:57 +03:00
#ifdef AMNEZIA_DESKTOP
2025-12-19 04:09:50 +01:00
IpcClient::withInterface([&](QSharedPointer<IpcInterfaceReplica> iface) {
if (connectionState() == Vpn::ConnectionState::Connected) {
if (m_settings->routeMode() == Settings::VpnOnlyForwardSites) {
iface->routeAddList(m_vpnProtocol->vpnGateway(), ips);
} else if (m_settings->routeMode() == Settings::VpnAllExceptSites) {
iface->routeAddList(m_vpnProtocol->routeGateway(), ips);
}
2021-06-01 18:18:09 +03:00
}
2025-12-19 04:09:50 +01:00
});
2022-02-22 02:08:57 +03:00
#endif
2021-06-01 18:18:09 +03:00
}
void VpnConnection::deleteRoutes(const QStringList &ips)
{
2022-02-22 02:08:57 +03:00
#ifdef AMNEZIA_DESKTOP
2025-12-19 04:09:50 +01:00
IpcClient::withInterface([&](QSharedPointer<IpcInterfaceReplica> iface) {
if (connectionState() == Vpn::ConnectionState::Connected) {
if (m_settings->routeMode() == Settings::VpnOnlyForwardSites) {
iface->routeDeleteList(vpnProtocol()->vpnGateway(), ips);
} else if (m_settings->routeMode() == Settings::VpnAllExceptSites) {
iface->routeDeleteList(m_vpnProtocol->routeGateway(), ips);
}
2021-06-01 18:18:09 +03:00
}
2025-12-19 04:09:50 +01:00
});
2022-02-22 02:08:57 +03:00
#endif
2021-06-01 18:18:09 +03:00
}
void VpnConnection::flushDns()
{
2022-02-22 02:08:57 +03:00
#ifdef AMNEZIA_DESKTOP
2025-12-19 04:09:50 +01:00
IpcClient::withInterface([](QSharedPointer<IpcInterfaceReplica> iface) {
auto reply = iface->flushDns();
if (reply.waitForFinished(1000) || !reply.returnValue()) {
qWarning() << "VpnConnection::flushDns(): Failed to flush DNS";
}
});
2022-02-22 02:08:57 +03:00
#endif
2021-06-01 18:18:09 +03:00
}
2025-11-30 18:49:16 -08:00
void VpnConnection::disconnectSlots()
{
if (m_vpnProtocol) {
m_vpnProtocol->disconnect();
}
}
2021-01-06 17:12:24 +03:00
ErrorCode VpnConnection::lastError() const
2020-12-26 23:17:20 +03:00
{
2023-12-22 15:35:24 +03:00
#ifdef Q_OS_ANDROID
return ErrorCode::AndroidError;
#endif
2025-12-19 04:09:50 +01:00
if (m_vpnProtocol.isNull()) {
2021-01-06 17:12:24 +03:00
return ErrorCode::InternalError;
2020-12-26 23:17:20 +03:00
}
return m_vpnProtocol.data()->lastError();
}
2023-08-28 22:03:28 +03:00
void VpnConnection::connectToVpn(int serverIndex, const ServerCredentials &credentials, DockerContainer container,
const QJsonObject &vpnConfiguration)
2021-01-06 17:12:24 +03:00
{
qDebug() << QString("ConnectToVpn, Server index is %1, container is %2, route mode is")
2023-08-28 22:03:28 +03:00
.arg(serverIndex)
.arg(ContainerProps::containerToString(container))
<< m_settings->routeMode();
2021-10-26 12:59:20 +03:00
m_remoteAddress = NetworkUtilities::getIPAddress(credentials.hostName);
2023-05-14 21:11:19 +08:00
emit connectionStateChanged(Vpn::ConnectionState::Connecting);
2021-02-18 15:00:41 +03:00
m_pendingNetworkCheck = false;
2024-04-25 20:01:00 +07:00
m_vpnConfiguration = vpnConfiguration;
m_serverIndex = serverIndex;
m_serverCredentials = credentials;
m_dockerContainer = container;
2024-04-25 20:01:00 +07:00
2023-08-27 10:46:41 -07:00
#ifdef AMNEZIA_DESKTOP
2021-02-18 15:00:41 +03:00
if (m_vpnProtocol) {
disconnect(m_vpnProtocol.data(), &VpnProtocol::protocolError, this, &VpnConnection::vpnProtocolError);
m_vpnProtocol->stop();
2021-02-25 18:05:42 +03:00
m_vpnProtocol.reset();
2021-02-18 15:00:41 +03:00
}
2024-04-25 20:01:00 +07:00
appendKillSwitchConfig();
2023-08-27 10:46:41 -07:00
#endif
2021-02-18 15:00:41 +03:00
2023-10-13 15:45:06 +05:00
appendSplitTunnelingConfig();
#if !defined(Q_OS_ANDROID) && !defined(Q_OS_IOS) && !defined(MACOS_NE)
2021-10-04 21:13:07 +03:00
m_vpnProtocol.reset(VpnProtocol::factory(container, m_vpnConfiguration));
2021-10-04 19:07:49 +03:00
if (!m_vpnProtocol) {
emit connectionStateChanged(Vpn::ConnectionState::Error);
2021-10-26 12:59:20 +03:00
return;
2021-04-04 23:12:36 +03:00
}
2021-10-04 19:07:49 +03:00
m_vpnProtocol->prepare();
#elif defined Q_OS_ANDROID
2023-12-04 18:23:08 +03:00
androidVpnProtocol = createDefaultAndroidVpnProtocol();
createAndroidConnections();
2021-11-26 17:43:02 +03:00
2021-10-07 22:52:13 +03:00
m_vpnProtocol.reset(androidVpnProtocol);
#elif defined Q_OS_IOS || defined(MACOS_NE)
Proto proto = ContainerProps::defaultProtocol(container);
2023-08-27 10:46:41 -07:00
IosController::Instance()->connectVpn(proto, m_vpnConfiguration);
connect(&m_checkTimer, &QTimer::timeout, IosController::Instance(), &IosController::checkStatus);
return;
2021-09-30 18:16:41 +03:00
#endif
2020-12-26 15:03:51 +03:00
createProtocolConnections();
2020-12-26 15:03:51 +03:00
2025-12-19 04:09:50 +01:00
ErrorCode errorCode = m_vpnProtocol->start();
if (errorCode != ErrorCode::NoError)
2023-08-28 22:03:28 +03:00
emit connectionStateChanged(Vpn::ConnectionState::Error);
2020-12-26 15:03:51 +03:00
}
void VpnConnection::restartConnection()
{
// Only reconnect if VPN was connected before sleep/network change
if (!m_wasConnectedBeforeSleep) {
qDebug() << "VPN was not connected before sleep/network change, skipping reconnection";
return;
}
qDebug() << "VPN was connected before sleep/network change, attempting reconnection";
this->disconnectFromVpn();
#ifdef Q_OS_LINUX
QThread::msleep(5000);
#endif
this->connectToVpn(m_serverIndex, m_serverCredentials, m_dockerContainer, m_vpnConfiguration);
// Reset the flag after reconnection attempt
m_wasConnectedBeforeSleep = false;
}
2023-08-28 22:03:28 +03:00
void VpnConnection::createProtocolConnections()
{
2023-03-15 17:46:22 +03:00
connect(m_vpnProtocol.data(), &VpnProtocol::protocolError, this, &VpnConnection::vpnProtocolError);
2023-08-28 22:03:28 +03:00
connect(m_vpnProtocol.data(), SIGNAL(connectionStateChanged(Vpn::ConnectionState)), this,
SLOT(onConnectionStateChanged(Vpn::ConnectionState)));
2023-03-15 17:46:22 +03:00
connect(m_vpnProtocol.data(), SIGNAL(bytesChanged(quint64, quint64)), this, SLOT(onBytesChanged(quint64, quint64)));
#ifdef AMNEZIA_DESKTOP
if (m_connectionLoseHandle)
disconnect(m_connectionLoseHandle);
if (m_networkChangeHandle)
disconnect(m_networkChangeHandle);
m_connectionLoseHandle = QMetaObject::Connection();
m_networkChangeHandle = QMetaObject::Connection();
2025-12-19 04:09:50 +01:00
// TODO: replace unsafe IpcClient::Interface() calls
m_connectionLoseHandle = connect(IpcClient::Interface().data(), &IpcInterfaceReplica::connectionLose,
this, [this]() {
qDebug() << "Connection Lose";
auto result = IpcClient::Interface()->stopNetworkCheck();
result.waitForFinished(3000);
// Track VPN state before connection loss
m_wasConnectedBeforeSleep = isConnected();
qDebug() << "VPN was connected before connection loss:" << m_wasConnectedBeforeSleep;
this->restartConnection();
});
m_networkChangeHandle = connect(IpcClient::Interface().data(), &IpcInterfaceReplica::networkChange,
this, [this]() {
qDebug() << "Network change";
// Track VPN state before network change (including sleep/wake)
m_wasConnectedBeforeSleep = isConnected();
qDebug() << "VPN was connected before network change:" << m_wasConnectedBeforeSleep;
this->restartConnection();
});
connect(m_vpnProtocol.data(), &VpnProtocol::tunnelAddressesUpdated,
this, [this](const QString& gateway, const QString& localAddress) {
Q_UNUSED(gateway)
Q_UNUSED(localAddress)
if (connectionState() != Vpn::ConnectionState::Connected) {
return;
}
if (startNetworkCheckIfReady()) {
m_pendingNetworkCheck = false;
}
});
#endif
2023-03-15 17:46:22 +03:00
}
2024-04-25 20:01:00 +07:00
void VpnConnection::appendKillSwitchConfig()
{
m_vpnConfiguration.insert(config_key::killSwitchOption, QVariant(m_settings->isKillSwitchEnabled()).toString());
2025-05-02 23:54:36 -07:00
m_vpnConfiguration.insert(config_key::allowedDnsServers, QVariant(m_settings->allowedDnsServers()).toJsonValue());
2024-04-25 20:01:00 +07:00
}
2023-10-13 15:45:06 +05:00
void VpnConnection::appendSplitTunnelingConfig()
{
bool allowSiteBasedSplitTunneling = true;
// this block is for old native configs and for old self-hosted configs
auto protocolName = m_vpnConfiguration.value(config_key::vpnproto).toString();
if (protocolName == ProtocolProps::protoToString(Proto::Awg) || protocolName == ProtocolProps::protoToString(Proto::WireGuard)) {
allowSiteBasedSplitTunneling = false;
auto configData = m_vpnConfiguration.value(protocolName + "_config_data").toObject();
if (configData.value(config_key::allowed_ips).isString()) {
QJsonArray allowedIpsJsonArray = QJsonArray::fromStringList(configData.value(config_key::allowed_ips).toString().split(", "));
configData.insert(config_key::allowed_ips, allowedIpsJsonArray);
m_vpnConfiguration.insert(protocolName + "_config_data", configData);
} else if (configData.value(config_key::allowed_ips).isUndefined()) {
auto nativeConfig = configData.value(config_key::config).toString();
auto nativeConfigLines = nativeConfig.split("\n");
for (auto &line : nativeConfigLines) {
if (line.contains("AllowedIPs")) {
auto allowedIpsString = line.split(" = ");
if (allowedIpsString.size() < 1) {
break;
}
QJsonArray allowedIpsJsonArray = QJsonArray::fromStringList(allowedIpsString.at(1).split(", "));
configData.insert(config_key::allowed_ips, allowedIpsJsonArray);
m_vpnConfiguration.insert(protocolName + "_config_data", configData);
break;
}
}
}
2024-04-26 18:44:27 +05:00
if (configData.value(config_key::persistent_keep_alive).isUndefined()) {
auto nativeConfig = configData.value(config_key::config).toString();
auto nativeConfigLines = nativeConfig.split("\n");
for (auto &line : nativeConfigLines) {
if (line.contains("PersistentKeepalive")) {
auto persistentKeepaliveString = line.split(" = ");
if (persistentKeepaliveString.size() < 1) {
break;
}
configData.insert(config_key::persistent_keep_alive, persistentKeepaliveString.at(1));
m_vpnConfiguration.insert(protocolName + "_config_data", configData);
break;
}
}
}
QJsonArray allowedIpsJsonArray = configData.value(config_key::allowed_ips).toArray();
if (allowedIpsJsonArray.contains("0.0.0.0/0") && allowedIpsJsonArray.contains("::/0")) {
allowSiteBasedSplitTunneling = true;
}
}
Settings::RouteMode routeMode = Settings::RouteMode::VpnAllSites;
QJsonArray sitesJsonArray;
if (m_settings->isSitesSplitTunnelingEnabled()) {
routeMode = m_settings->routeMode();
if (allowSiteBasedSplitTunneling) {
auto sites = m_settings->getVpnIps(routeMode);
2024-04-26 18:44:27 +05:00
for (const auto &site : sites) {
sitesJsonArray.append(site);
}
2023-10-13 15:45:06 +05:00
2025-04-10 11:24:33 +04:00
if (sitesJsonArray.isEmpty()) {
routeMode = Settings::RouteMode::VpnAllSites;
} else if (routeMode == Settings::VpnOnlyForwardSites) {
2025-04-10 11:24:33 +04:00
// Allow traffic to Amnezia DNS
2024-04-26 18:44:27 +05:00
sitesJsonArray.append(m_vpnConfiguration.value(config_key::dns1).toString());
sitesJsonArray.append(m_vpnConfiguration.value(config_key::dns2).toString());
}
}
2023-10-25 22:19:07 +03:00
}
m_vpnConfiguration.insert(config_key::splitTunnelType, routeMode);
m_vpnConfiguration.insert(config_key::splitTunnelSites, sitesJsonArray);
2024-04-06 22:29:51 +07:00
Settings::AppsRouteMode appsRouteMode = Settings::AppsRouteMode::VpnAllApps;
QJsonArray appsJsonArray;
2024-04-25 20:01:00 +07:00
if (m_settings->isAppsSplitTunnelingEnabled()) {
2024-04-06 22:29:51 +07:00
appsRouteMode = m_settings->getAppsRouteMode();
2024-04-01 18:45:00 +07:00
auto apps = m_settings->getVpnApps(appsRouteMode);
for (const auto &app : apps) {
appsJsonArray.append(app.appPath.isEmpty() ? app.packageName : app.appPath);
}
2025-04-10 11:24:33 +04:00
if (appsJsonArray.isEmpty()) {
appsRouteMode = Settings::AppsRouteMode::VpnAllApps;
}
2024-04-01 18:45:00 +07:00
}
m_vpnConfiguration.insert(config_key::appSplitTunnelType, appsRouteMode);
m_vpnConfiguration.insert(config_key::splitTunnelApps, appsJsonArray);
2023-10-13 15:45:06 +05:00
}
bool VpnConnection::startNetworkCheckIfReady()
{
#ifdef AMNEZIA_DESKTOP
if (!m_vpnProtocol || m_dockerContainer == DockerContainer::Ipsec) {
return false;
}
const QString gateway = m_vpnProtocol->vpnGateway();
const QString localAddress = m_vpnProtocol->vpnLocalAddress();
if (gateway.isEmpty() || localAddress.isEmpty()) {
return false;
}
2025-12-19 04:09:50 +01:00
return IpcClient::withInterface([&](QSharedPointer<IpcInterfaceReplica> iface) {
QRemoteObjectPendingReply<bool> reply = iface->startNetworkCheck(gateway, localAddress);
return reply.waitForFinished() && reply.returnValue();
});
#else
return false;
#endif
}
2023-03-15 17:46:22 +03:00
#ifdef Q_OS_ANDROID
2023-08-28 22:03:28 +03:00
void VpnConnection::restoreConnection()
{
createAndroidConnections();
m_vpnProtocol.reset(androidVpnProtocol);
createProtocolConnections();
}
void VpnConnection::createAndroidConnections()
{
2023-12-04 18:23:08 +03:00
androidVpnProtocol = createDefaultAndroidVpnProtocol();
2023-08-28 22:03:28 +03:00
connect(AndroidController::instance(), &AndroidController::connectionStateChanged, androidVpnProtocol,
&AndroidVpnProtocol::setConnectionState);
connect(AndroidController::instance(), &AndroidController::statisticsUpdated, androidVpnProtocol, &AndroidVpnProtocol::setBytesChanged);
}
2023-12-04 18:23:08 +03:00
AndroidVpnProtocol *VpnConnection::createDefaultAndroidVpnProtocol()
{
2023-12-04 18:23:08 +03:00
return new AndroidVpnProtocol(m_vpnConfiguration);
}
2023-03-15 17:46:22 +03:00
#endif
2021-01-09 19:55:16 +03:00
QString VpnConnection::bytesPerSecToText(quint64 bytes)
2020-12-26 15:03:51 +03:00
{
2021-01-09 19:55:16 +03:00
double mbps = bytes * 8 / 1e6;
return QString("%1 %2").arg(QString::number(mbps, 'f', 2)).arg(tr("Mbps")); // Mbit/s
2020-12-26 15:03:51 +03:00
}
void VpnConnection::disconnectFromVpn()
{
#if defined(Q_OS_IOS) || defined(MACOS_NE)
// iOS/macOS NE use IosController directly; m_vpnProtocol is not set there.
IosController::Instance()->disconnectVpn();
disconnect(&m_checkTimer, &QTimer::timeout, IosController::Instance(), &IosController::checkStatus);
#endif
2025-12-19 04:09:50 +01:00
if (m_vpnProtocol.isNull()) {
emit connectionStateChanged(Vpn::ConnectionState::Disconnected);
return;
}
2025-11-30 18:49:16 -08:00
2025-12-19 04:09:50 +01:00
m_vpnProtocol->stop();
2025-11-30 18:49:16 -08:00
2025-12-19 04:09:50 +01:00
#ifdef AMNEZIA_DESKTOP
IpcClient::withInterface([](QSharedPointer<IpcInterfaceReplica> iface) {
QRemoteObjectPendingReply<bool> flushReply = iface->flushDns();
if (flushReply.waitForFinished(5000) && flushReply.returnValue())
qDebug() << "VpnConnection::disconnectFromVpn(): Successfully flushed DNS";
else
qWarning() << "VpnConnection::disconnectFromVpn(): Failed to flush DNS";
QRemoteObjectPendingReply<bool> clearSavedRoutesReply = iface->clearSavedRoutes();
if (clearSavedRoutesReply.waitForFinished(5000) && clearSavedRoutesReply.returnValue())
qDebug() << "VpnConnection::disconnectFromVpn(): Successfully cleared saved routes";
else
qWarning() << "VpnConnection::disconnectFromVpn(): Failed to clear saved routes";
});
2022-02-22 02:08:57 +03:00
#endif
#ifdef Q_OS_ANDROID
2025-12-19 04:09:50 +01:00
auto *const connection = new QMetaObject::Connection;
*connection = connect(AndroidController::instance(), &AndroidController::vpnStateChanged, this,
[this, connection](AndroidController::ConnectionState state) {
if (state == AndroidController::ConnectionState::DISCONNECTED) {
onConnectionStateChanged(Vpn::ConnectionState::Disconnected);
disconnect(*connection);
delete connection;
}
});
m_vpnProtocol->stop();
2022-02-22 02:08:57 +03:00
#endif
2023-08-23 17:16:40 -04:00
2025-11-30 18:49:16 -08:00
#if !defined(Q_OS_ANDROID) && !defined(AMNEZIA_DESKTOP)
2025-12-19 04:09:50 +01:00
m_vpnProtocol->deleteLater();
#endif
2025-11-30 18:49:16 -08:00
m_vpnProtocol = nullptr;
2020-12-26 15:03:51 +03:00
}
2023-05-14 21:11:19 +08:00
Vpn::ConnectionState VpnConnection::connectionState()
2021-01-15 23:36:35 +03:00
{
2023-08-28 22:03:28 +03:00
if (!m_vpnProtocol)
return Vpn::ConnectionState::Disconnected;
2021-01-15 23:36:35 +03:00
return m_vpnProtocol->connectionState();
}
2021-02-18 15:00:41 +03:00
bool VpnConnection::isConnected() const
2020-12-26 15:03:51 +03:00
{
2025-12-19 04:09:50 +01:00
if (m_vpnProtocol.isNull()) {
2020-12-26 15:03:51 +03:00
return false;
}
2025-12-19 04:09:50 +01:00
return m_vpnProtocol->isConnected();
2020-12-26 15:03:51 +03:00
}
2021-02-18 15:00:41 +03:00
bool VpnConnection::isDisconnected() const
2020-12-26 15:03:51 +03:00
{
2025-12-19 04:09:50 +01:00
if (m_vpnProtocol.isNull()) {
2020-12-26 15:03:51 +03:00
return true;
}
2025-12-19 04:09:50 +01:00
return m_vpnProtocol->isDisconnected();
2020-12-26 15:03:51 +03:00
}