mirror of
https://github.com/amnezia-vpn/amnezia-client.git
synced 2026-05-08 14:33:23 +00:00
* refactor: move business logic from servers model * refactor: move containersModel initialization * refactor: added protocol ui controller and removed settings class from protocols model * refactor: moved cli management to separate controller * refactor: moved app split to separate controller * refactor: moved site split to separate controller * refactor: moved allowed dns to separate controller * refactor: moved language logic to separate ui controller * refactor: removed Settings from devices model * refactor: moved configs and services api logit to separate core controller * refactor: added a layer with a repository between the storage and controllers * refactor: use child parent system instead of smart pointers for controllers and models initialization * refactor: moved install functions from server controller to install controller * refactor: install controller refactoring * chore: renamed exportController to exportUiController * refactor: separate export controller * refactor: removed VpnConfigurationsController * chore: renamed ServerController to SshSession * refactor: replaced ServerController to SshSession * chore: moved qml controllers to separate folder * chore: include fixes * chore: moved utils from core root to core/utils * chore: include fixes * chore: rename core/utils files to camelCase foramt * chore: include fixes * chore: moved some utils to api and selfhosted folders * chore: include fixes * chore: remove unused file * chore: moved serialization folder to core/utils * chore: include fixes * chore: moved some files from client root to core/utils * chore: include fixes * chore: moved ui utils to ui/utils folder * chore: include fixes * chore: move utils from root to ui/utils * chore: include fixes * chore: moved configurators to core/configurators * chore: include fixes * refactor: moved iap logic from ui controller to core * refactor: moved remaining core logic from ApiConfigsController to SubscriptionController * chore: rename apiNewsController to apiNewsUiController * refactor: moved core logic from news ui controller to core * chore: renamed apiConfigsController to subscriptionUiController * chore: include fixes * refactor: merge ApiSettingsController with SubscriptionUiController * chore: moved ui selfhosted controllers to separate folder * chore: include fixes * chore: rename connectionController to connectiomUiController * refactor: moved core logic from connectionUiController * chore: rename settingsController to settingsUiController * refactor: move core logic from settingsUiController * refactor: moved core controller signal/slot connections to separate class * fix: newsController fixes after refactoring * chore: rename model to camelCase * chore: include fixes * chore: remove unused code * chore: move selfhosted core to separate folder * chore: include fixes * chore: rename importController to importUiController * refactor: move core logic from importUiController * chore: minor fixes * chore: remove prem v1 migration * refactor: remove openvpn over cloak and openvpn over shadowsocks * refactor: removed protocolsForContainer function * refactor: add core models * refactor: replace json with c++ structs for server config * refactor: move getDnsPair to ServerConfigUtils * feat: add admin selfhosted config export test * feat: add multi import test * refactor: use coreController for tests * feat: add few simple tests * chore: qrepos in all core controllers * feat: add test for settings * refactor: remove repo dependency from configurators * chore: moved protocols to core folder * chore: include fixes * refactor: moved containersDefs, defs, apiDefs, protocolsDefs to different places * chore: include fixes * chore: build fixes * chore: build fixes * refactor: remove q repo and interface repo * feat: add test for ui servers model and controller * chore: renamed to camelCase * chore: include fixes * refactor: moved core logic from sites ui controller * fix: fixed api config processing * fix: fixed processed server index processing * refactor: protocol models now use c++ structs instead of json configs * refactor: servers model now use c++ struct instead of json config * fix: fixed default server index processing * fix: fix logs init * fix: fix secure settings load keys * chore: build fixes * fix: fixed clear settings * fix: fixed restore backup * fix: sshSession usage * fix: fixed export functions signatures * fix: return missing part from buildContainerWorker * fix: fixed server description on page home * refactor: add container config helpers functions * refactor: c++ structs instead of json * chore: add dns protocol config struct * refactor: move config utils functions to config structs * feat: add test for selfhosted server setup * refactor: separate resources.qrc * fix: fixed server rename * chore: return nameOverriddenByUser * fix: build fixes * fix: fixed models init * refactor: cleanup models usage * fix: fixed models init * chore: cleanup connections and functions signatures * chore: cleanup updateModel calls * feat: added cache to servers repo * chore: cleanup unused functions * chore: ssxray processing * chore: remove transportProtoWithDefault and portWithDefault functions * chore: removed proto types any and l2tp * refactor: moved some constants * fix: fixed native configs export * refactor: remove json from processConfigWith functions * fix: fixed processed server index usage * fix: qml warning fixes * chore: merge fixes * chore: update tests * fix: fixed xray config processing * fix: fixed split tunneling processing * chore: rename sites controllers and model * chore: rename fixes * chore: minor fixes * chore: remove ability to load backup from "file with connection settings" button * fix: fixed api device revoke * fix: remove full model update when renaming a user * fix: fixed premium/free server rename * fix: fixed selfhosted new server install * fix: fixed updateContainer function * fix: fixed revoke for external premium configs * feat: add native configs qr processing * chore: codestyle fixes * fix: fixed admin config create * chore: again remove ability to load backup from "file with connection settings" button * chore: minor fixes * fix: fixed variables initialization * fix: fixed qml imports * fix: minor fixes * fix: fix vpnConnection function calls * feat: add buckup error handling * fix: fixed admin config revok * fix: fixed selfhosted awg installation * fix: ad visability * feat: add empty check for primary dns * chore: minor fixes
535 lines
21 KiB
C++
535 lines
21 KiB
C++
#include "vpnConnection.h"
|
|
|
|
#include <QDebug>
|
|
#include <QEventLoop>
|
|
#include <QFile>
|
|
#include <QHostInfo>
|
|
#include <QJsonObject>
|
|
#include <QObject>
|
|
#include <QSharedPointer>
|
|
#include <QString>
|
|
#include <QStringList>
|
|
#include <QTimer>
|
|
|
|
#include <core/configurators/openVpnConfigurator.h>
|
|
#include <core/configurators/wireguardConfigurator.h>
|
|
|
|
#ifdef AMNEZIA_DESKTOP
|
|
#include "core/utils/ipcClient.h"
|
|
#include <core/protocols/wireGuardProtocol.h>
|
|
#endif
|
|
|
|
#ifdef Q_OS_ANDROID
|
|
#include "platforms/android/android_controller.h"
|
|
#include <QThread>
|
|
|
|
#endif
|
|
|
|
#if defined(Q_OS_IOS) || defined(MACOS_NE)
|
|
#include "platforms/ios/ios_controller.h"
|
|
#endif
|
|
|
|
#include "core/utils/networkUtilities.h"
|
|
#include "vpnConnection.h"
|
|
|
|
using namespace ProtocolUtils;
|
|
|
|
VpnConnection::VpnConnection(SecureServersRepository* serversRepository, SecureAppSettingsRepository* appSettingsRepository, QObject *parent)
|
|
: QObject(parent), m_serversRepository(serversRepository), m_appSettingsRepository(appSettingsRepository), m_checkTimer(this)
|
|
{
|
|
#if defined(Q_OS_IOS) || defined(MACOS_NE)
|
|
m_checkTimer.setInterval(1000);
|
|
connect(IosController::Instance(), &IosController::connectionStateChanged, this, &VpnConnection::setConnectionState);
|
|
connect(IosController::Instance(), &IosController::bytesChanged, this, &VpnConnection::onBytesChanged);
|
|
#endif
|
|
}
|
|
|
|
VpnConnection::~VpnConnection()
|
|
{
|
|
}
|
|
|
|
void VpnConnection::onBytesChanged(quint64 receivedBytes, quint64 sentBytes)
|
|
{
|
|
emit bytesChanged(receivedBytes, sentBytes);
|
|
}
|
|
|
|
void VpnConnection::onKillSwitchModeChanged(bool enabled)
|
|
{
|
|
#ifdef AMNEZIA_DESKTOP
|
|
IpcClient::withInterface([enabled](QSharedPointer<IpcInterfaceReplica> iface){
|
|
QRemoteObjectPendingReply<bool> reply = iface->refreshKillSwitch(enabled);
|
|
if (reply.waitForFinished() && reply.returnValue())
|
|
qDebug() << "VpnConnection::onKillSwitchModeChanged: Killswitch refreshed";
|
|
else
|
|
qWarning() << "VpnConnection::onKillSwitchModeChanged: Failed to execute remote refreshKillSwitch call";
|
|
});
|
|
#endif
|
|
}
|
|
|
|
void VpnConnection::onConnectionStateChanged(Vpn::ConnectionState state)
|
|
{
|
|
#ifdef AMNEZIA_DESKTOP
|
|
if (!m_serversRepository || !m_appSettingsRepository) {
|
|
qCritical() << "VpnConnection::onConnectionStateChanged: repositories not initialized";
|
|
return;
|
|
}
|
|
|
|
ServerConfig defaultServer = m_serversRepository->server(m_serversRepository->defaultServerIndex());
|
|
DockerContainer container = defaultServer.defaultContainer();
|
|
|
|
IpcClient::withInterface([&](QSharedPointer<IpcInterfaceReplica> iface) {
|
|
switch (state) {
|
|
case Vpn::ConnectionState::Connected: {
|
|
iface->resetIpStack();
|
|
|
|
auto flushDns = iface->flushDns();
|
|
if (flushDns.waitForFinished() && flushDns.returnValue())
|
|
qDebug() << "VpnConnection::onConnectionStateChanged: Successfully flushed DNS";
|
|
else
|
|
qWarning() << "VpnConnection::onConnectionStateChanged: Failed to flush DNS";
|
|
|
|
if (!ContainerUtils::isAwgContainer(container) && container != DockerContainer::WireGuard) {
|
|
QString dns1 = m_vpnConfiguration.value(configKey::dns1).toString();
|
|
QString dns2 = m_vpnConfiguration.value(configKey::dns2).toString();
|
|
|
|
iface->routeAddList(m_vpnProtocol->vpnGateway(), QStringList() << dns1 << dns2);
|
|
|
|
if (m_appSettingsRepository->isSitesSplitTunnelingEnabled()) {
|
|
iface->routeDeleteList(m_vpnProtocol->vpnGateway(), QStringList() << "0.0.0.0");
|
|
RouteMode routeMode = m_appSettingsRepository->routeMode();
|
|
if (routeMode == amnezia::RouteMode::VpnOnlyForwardSites) {
|
|
QTimer::singleShot(1000, m_vpnProtocol.data(),
|
|
[this, routeMode]() { addSitesRoutes(m_vpnProtocol->vpnGateway(), routeMode); });
|
|
} else if (routeMode == amnezia::RouteMode::VpnAllExceptSites) {
|
|
iface->routeAddList(m_vpnProtocol->vpnGateway(), QStringList() << "0.0.0.0/1");
|
|
iface->routeAddList(m_vpnProtocol->vpnGateway(), QStringList() << "128.0.0.0/1");
|
|
|
|
iface->routeAddList(m_vpnProtocol->routeGateway(), QStringList() << remoteAddress());
|
|
addSitesRoutes(m_vpnProtocol->routeGateway(), routeMode);
|
|
}
|
|
}
|
|
}
|
|
} break;
|
|
case Vpn::ConnectionState::Disconnected:
|
|
case Vpn::ConnectionState::Error: {
|
|
auto flushDns = iface->flushDns();
|
|
if (flushDns.waitForFinished() && flushDns.returnValue())
|
|
qDebug() << "VpnConnection::onConnectionStateChanged: Successfully flushed DNS";
|
|
else
|
|
qWarning() << "VpnConnection::onConnectionStateChanged: Failed to flush DNS";
|
|
|
|
auto clearSavedRoutes = iface->clearSavedRoutes();
|
|
if (clearSavedRoutes.waitForFinished() && clearSavedRoutes.returnValue())
|
|
qDebug() << "VpnConnection::onConnectionStateChanged: Successfully cleared saved routes";
|
|
else
|
|
qWarning() << "VpnConnection::onConnectionStateChanged: Failed to clear saved routes";
|
|
} break;
|
|
default:
|
|
break;
|
|
}
|
|
});
|
|
#endif
|
|
|
|
#if defined(Q_OS_IOS) || defined(MACOS_NE)
|
|
if (state == Vpn::ConnectionState::Connected ||
|
|
state == Vpn::ConnectionState::Connecting ||
|
|
state == Vpn::ConnectionState::Reconnecting) {
|
|
m_checkTimer.start();
|
|
} else {
|
|
m_checkTimer.stop();
|
|
}
|
|
#endif
|
|
}
|
|
|
|
const QString &VpnConnection::remoteAddress() const
|
|
{
|
|
return m_remoteAddress;
|
|
}
|
|
|
|
void VpnConnection::setRepositories(SecureServersRepository* serversRepository, SecureAppSettingsRepository* appSettingsRepository)
|
|
{
|
|
m_serversRepository = serversRepository;
|
|
m_appSettingsRepository = appSettingsRepository;
|
|
}
|
|
|
|
void VpnConnection::addSitesRoutes(const QString &gw, amnezia::RouteMode mode)
|
|
{
|
|
#ifdef AMNEZIA_DESKTOP
|
|
if (!m_appSettingsRepository) {
|
|
qCritical() << "VpnConnection::addSitesRoutes: repositories not initialized";
|
|
return;
|
|
}
|
|
|
|
QStringList ips;
|
|
QStringList sites;
|
|
const QVariantMap &m = m_appSettingsRepository->vpnSites(mode);
|
|
for (auto i = m.constBegin(); i != m.constEnd(); ++i) {
|
|
if (NetworkUtilities::checkIpSubnetFormat(i.key())) {
|
|
ips.append(i.key());
|
|
} else {
|
|
if (NetworkUtilities::checkIpSubnetFormat(i.value().toString())) {
|
|
ips.append(i.value().toString());
|
|
}
|
|
sites.append(i.key());
|
|
}
|
|
}
|
|
ips.removeDuplicates();
|
|
|
|
IpcClient::withInterface([&](QSharedPointer<IpcInterfaceReplica> iface) {
|
|
iface->routeAddList(gw, ips);
|
|
});
|
|
|
|
// re-resolve domains
|
|
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)) {
|
|
IpcClient::withInterface([&gw, &ip](QSharedPointer<IpcInterfaceReplica> iface) {
|
|
iface->routeAddList(gw, QStringList() << ip);
|
|
});
|
|
m_appSettingsRepository->addVpnSite(mode, site, ip);
|
|
}
|
|
IpcClient::withInterface([](QSharedPointer<IpcInterfaceReplica> iface) {
|
|
auto reply = iface->flushDns();
|
|
if (reply.waitForFinished() || !reply.returnValue())
|
|
qWarning() << "VpnConnection::addSitesRoutes: Failed to flush DNS";
|
|
});
|
|
break;
|
|
}
|
|
}
|
|
};
|
|
QHostInfo::lookupHost(site, this, cbResolv);
|
|
}
|
|
#endif
|
|
}
|
|
|
|
QSharedPointer<VpnProtocol> VpnConnection::vpnProtocol() const
|
|
{
|
|
return m_vpnProtocol;
|
|
}
|
|
|
|
void VpnConnection::disconnectSlots()
|
|
{
|
|
if (m_vpnProtocol) {
|
|
m_vpnProtocol->disconnect();
|
|
}
|
|
}
|
|
|
|
ErrorCode VpnConnection::lastError() const
|
|
{
|
|
#ifdef Q_OS_ANDROID
|
|
return ErrorCode::AndroidError;
|
|
#endif
|
|
|
|
if (m_vpnProtocol.isNull()) {
|
|
return ErrorCode::InternalError;
|
|
}
|
|
|
|
return m_vpnProtocol.data()->lastError();
|
|
}
|
|
|
|
Vpn::ConnectionState VpnConnection::connectionState() const
|
|
{
|
|
return m_connectionState;
|
|
}
|
|
|
|
void VpnConnection::connectToVpn(int serverIndex, DockerContainer container, const QJsonObject &vpnConfiguration)
|
|
{
|
|
if (!m_appSettingsRepository || !m_serversRepository) {
|
|
qCritical() << "VpnConnection::connectToVpn: repositories not initialized";
|
|
setConnectionState(Vpn::ConnectionState::Error);
|
|
return;
|
|
}
|
|
|
|
qDebug() << QString("Trying to connect to VPN, server index is %1, container is %2, route mode is")
|
|
.arg(serverIndex)
|
|
.arg(ContainerUtils::containerToString(container))
|
|
<< m_appSettingsRepository->routeMode();
|
|
|
|
m_remoteAddress = NetworkUtilities::getIPAddress(vpnConfiguration.value(configKey::hostName).toString());
|
|
setConnectionState(Vpn::ConnectionState::Connecting);
|
|
|
|
m_vpnConfiguration = vpnConfiguration;
|
|
|
|
#ifdef AMNEZIA_DESKTOP
|
|
if (m_vpnProtocol) {
|
|
disconnect(m_vpnProtocol.data(), &VpnProtocol::protocolError, this, &VpnConnection::vpnProtocolError);
|
|
m_vpnProtocol->stop();
|
|
m_vpnProtocol.reset();
|
|
}
|
|
appendKillSwitchConfig();
|
|
#endif
|
|
|
|
appendSplitTunnelingConfig();
|
|
|
|
#if !defined(Q_OS_ANDROID) && !defined(Q_OS_IOS) && !defined(MACOS_NE)
|
|
m_vpnProtocol.reset(VpnProtocol::factory(container, m_vpnConfiguration));
|
|
if (!m_vpnProtocol) {
|
|
setConnectionState(Vpn::ConnectionState::Error);
|
|
return;
|
|
}
|
|
m_vpnProtocol->prepare();
|
|
#elif defined Q_OS_ANDROID
|
|
androidVpnProtocol = createDefaultAndroidVpnProtocol();
|
|
createAndroidConnections();
|
|
|
|
m_vpnProtocol.reset(androidVpnProtocol);
|
|
#elif defined Q_OS_IOS || defined(MACOS_NE)
|
|
Proto proto = ContainerUtils::defaultProtocol(container);
|
|
IosController::Instance()->connectVpn(proto, m_vpnConfiguration);
|
|
connect(&m_checkTimer, &QTimer::timeout, IosController::Instance(), &IosController::checkStatus);
|
|
return;
|
|
#endif
|
|
|
|
createProtocolConnections();
|
|
|
|
if (ErrorCode err = m_vpnProtocol->start(); err != ErrorCode::NoError) {
|
|
setConnectionState(Vpn::ConnectionState::Error);
|
|
emit vpnProtocolError(err);
|
|
}
|
|
}
|
|
|
|
void VpnConnection::createProtocolConnections()
|
|
{
|
|
connect(m_vpnProtocol.data(), &VpnProtocol::protocolError, this, &VpnConnection::vpnProtocolError);
|
|
connect(m_vpnProtocol.data(), &VpnProtocol::connectionStateChanged, this, &VpnConnection::setConnectionState);
|
|
connect(m_vpnProtocol.data(), SIGNAL(bytesChanged(quint64, quint64)), this, SLOT(onBytesChanged(quint64, quint64)));
|
|
|
|
#ifdef AMNEZIA_DESKTOP
|
|
IpcClient::withInterface([this](QSharedPointer<IpcInterfaceReplica> rep) {
|
|
connect(rep.data(), &IpcInterfaceReplica::networkChanged, this, &VpnConnection::reconnectToVpn, Qt::QueuedConnection);
|
|
connect(rep.data(), &IpcInterfaceReplica::wakeup, this, &VpnConnection::reconnectToVpn, Qt::QueuedConnection);
|
|
});
|
|
#endif
|
|
}
|
|
|
|
void VpnConnection::appendKillSwitchConfig()
|
|
{
|
|
if (!m_appSettingsRepository) {
|
|
qCritical() << "VpnConnection::appendKillSwitchConfig: repositories not initialized";
|
|
return;
|
|
}
|
|
|
|
m_vpnConfiguration.insert(configKey::killSwitchOption, QVariant(m_appSettingsRepository->isKillSwitchEnabled()).toString());
|
|
m_vpnConfiguration.insert(configKey::allowedDnsServers, QVariant(m_appSettingsRepository->getAllowedDnsServers()).toJsonValue());
|
|
}
|
|
|
|
void VpnConnection::appendSplitTunnelingConfig()
|
|
{
|
|
if (!m_appSettingsRepository) {
|
|
qCritical() << "VpnConnection::appendSplitTunnelingConfig: repositories not initialized";
|
|
return;
|
|
}
|
|
|
|
bool allowSiteBasedSplitTunneling = true;
|
|
|
|
// this block is for old native configs and for old self-hosted configs
|
|
auto protocolName = m_vpnConfiguration.value(configKey::vpnProto).toString();
|
|
if (protocolName == ProtocolUtils::protoToString(Proto::Awg) || protocolName == ProtocolUtils::protoToString(Proto::WireGuard)) {
|
|
allowSiteBasedSplitTunneling = false;
|
|
auto configData = m_vpnConfiguration.value(protocolName + "_config_data").toObject();
|
|
if (configData.value(configKey::allowedIps).isString()) {
|
|
QJsonArray allowedIpsJsonArray = QJsonArray::fromStringList(configData.value(configKey::allowedIps).toString().split(", "));
|
|
configData.insert(configKey::allowedIps, allowedIpsJsonArray);
|
|
m_vpnConfiguration.insert(protocolName + "_config_data", configData);
|
|
} else if (configData.value(configKey::allowedIps).isUndefined()) {
|
|
auto nativeConfig = configData.value(configKey::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(configKey::allowedIps, allowedIpsJsonArray);
|
|
m_vpnConfiguration.insert(protocolName + "_config_data", configData);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (configData.value(configKey::persistentKeepAlive).isUndefined()) {
|
|
auto nativeConfig = configData.value(configKey::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(configKey::persistentKeepAlive, persistentKeepaliveString.at(1));
|
|
m_vpnConfiguration.insert(protocolName + "_config_data", configData);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
QJsonArray allowedIpsJsonArray = configData.value(configKey::allowedIps).toArray();
|
|
if (allowedIpsJsonArray.contains("0.0.0.0/0") && allowedIpsJsonArray.contains("::/0")) {
|
|
allowSiteBasedSplitTunneling = true;
|
|
}
|
|
}
|
|
|
|
amnezia::RouteMode routeMode = amnezia::RouteMode::VpnAllSites;
|
|
QJsonArray sitesJsonArray;
|
|
if (m_appSettingsRepository->isSitesSplitTunnelingEnabled()) {
|
|
routeMode = m_appSettingsRepository->routeMode();
|
|
|
|
if (allowSiteBasedSplitTunneling) {
|
|
QStringList sites;
|
|
const QVariantMap &m = m_appSettingsRepository->vpnSites(routeMode);
|
|
for (auto i = m.constBegin(); i != m.constEnd(); ++i) {
|
|
if (NetworkUtilities::checkIpSubnetFormat(i.key())) {
|
|
sites.append(i.key());
|
|
} else if (NetworkUtilities::checkIpSubnetFormat(i.value().toString())) {
|
|
sites.append(i.value().toString());
|
|
}
|
|
}
|
|
sites.removeDuplicates();
|
|
for (const auto &site : sites) {
|
|
sitesJsonArray.append(site);
|
|
}
|
|
|
|
if (sitesJsonArray.isEmpty()) {
|
|
routeMode = amnezia::RouteMode::VpnAllSites;
|
|
} else if (routeMode == amnezia::RouteMode::VpnOnlyForwardSites) {
|
|
// Allow traffic to Amnezia DNS
|
|
sitesJsonArray.append(m_vpnConfiguration.value(configKey::dns1).toString());
|
|
sitesJsonArray.append(m_vpnConfiguration.value(configKey::dns2).toString());
|
|
}
|
|
}
|
|
}
|
|
|
|
m_vpnConfiguration.insert(configKey::splitTunnelType, routeMode);
|
|
m_vpnConfiguration.insert(configKey::splitTunnelSites, sitesJsonArray);
|
|
|
|
amnezia::AppsRouteMode appsRouteMode = amnezia::AppsRouteMode::VpnAllApps;
|
|
QJsonArray appsJsonArray;
|
|
if (m_appSettingsRepository->isAppsSplitTunnelingEnabled()) {
|
|
appsRouteMode = m_appSettingsRepository->appsRouteMode();
|
|
|
|
auto apps = m_appSettingsRepository->vpnApps(appsRouteMode);
|
|
for (const auto &app : apps) {
|
|
appsJsonArray.append(app.appPath.isEmpty() ? app.packageName : app.appPath);
|
|
}
|
|
|
|
if (appsJsonArray.isEmpty()) {
|
|
appsRouteMode = amnezia::AppsRouteMode::VpnAllApps;
|
|
}
|
|
}
|
|
|
|
m_vpnConfiguration.insert(configKey::appSplitTunnelType, appsRouteMode);
|
|
m_vpnConfiguration.insert(configKey::splitTunnelApps, appsJsonArray);
|
|
|
|
qDebug() << QString("Site split tunneling is %1, route mode is %2")
|
|
.arg(m_appSettingsRepository->isSitesSplitTunnelingEnabled() ? "enabled" : "disabled")
|
|
.arg(routeMode);
|
|
qDebug() << QString("App split tunneling is %1, route mode is %2")
|
|
.arg(m_appSettingsRepository->isAppsSplitTunnelingEnabled() ? "enabled" : "disabled")
|
|
.arg(appsRouteMode);
|
|
}
|
|
|
|
#ifdef Q_OS_ANDROID
|
|
void VpnConnection::restoreConnection()
|
|
{
|
|
createAndroidConnections();
|
|
|
|
m_vpnProtocol.reset(androidVpnProtocol);
|
|
|
|
createProtocolConnections();
|
|
}
|
|
|
|
void VpnConnection::createAndroidConnections()
|
|
{
|
|
androidVpnProtocol = createDefaultAndroidVpnProtocol();
|
|
|
|
connect(AndroidController::instance(), &AndroidController::connectionStateChanged, androidVpnProtocol,
|
|
&AndroidVpnProtocol::setConnectionState);
|
|
connect(AndroidController::instance(), &AndroidController::statisticsUpdated, androidVpnProtocol, &AndroidVpnProtocol::setBytesChanged);
|
|
}
|
|
|
|
AndroidVpnProtocol *VpnConnection::createDefaultAndroidVpnProtocol()
|
|
{
|
|
return new AndroidVpnProtocol(m_vpnConfiguration);
|
|
}
|
|
#endif
|
|
|
|
QString VpnConnection::bytesPerSecToText(quint64 bytes)
|
|
{
|
|
double mbps = bytes * 8 / 1e6;
|
|
return QString("%1 %2").arg(QString::number(mbps, 'f', 2)).arg(tr("Mbps")); // Mbit/s
|
|
}
|
|
|
|
void VpnConnection::reconnectToVpn() {
|
|
if (m_vpnProtocol.isNull())
|
|
return;
|
|
|
|
if (m_connectionState != Vpn::ConnectionState::Connected) {
|
|
qWarning() << QString("Reconnect triggered on %1 during inappropriate state: %2; ignoring slot")
|
|
.arg(QMetaEnum::fromType<Vpn::ConnectionState>().valueToKey(m_connectionState));
|
|
return;
|
|
}
|
|
|
|
qDebug() << "Reconnect triggered. Reconnecting to the server";
|
|
|
|
setConnectionState(Vpn::ConnectionState::Reconnecting);
|
|
|
|
m_vpnProtocol->stop();
|
|
if (ErrorCode err = m_vpnProtocol->start(); err != ErrorCode::NoError) {
|
|
setConnectionState(Vpn::ConnectionState::Error);
|
|
emit vpnProtocolError(err);
|
|
}
|
|
}
|
|
|
|
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
|
|
|
|
if (m_vpnProtocol.isNull()) {
|
|
setConnectionState(Vpn::ConnectionState::Disconnected);
|
|
return;
|
|
}
|
|
|
|
setConnectionState(Vpn::ConnectionState::Disconnecting);
|
|
|
|
#ifdef Q_OS_ANDROID
|
|
auto *const connection = new QMetaObject::Connection;
|
|
*connection = connect(AndroidController::instance(), &AndroidController::vpnStateChanged, this,
|
|
[this, connection](AndroidController::ConnectionState state) {
|
|
if (state == AndroidController::ConnectionState::DISCONNECTED) {
|
|
setConnectionState(Vpn::ConnectionState::Disconnected);
|
|
disconnect(*connection);
|
|
delete connection;
|
|
}
|
|
});
|
|
#endif
|
|
|
|
m_vpnProtocol->stop();
|
|
|
|
#if !defined(Q_OS_ANDROID) && !defined(AMNEZIA_DESKTOP)
|
|
m_vpnProtocol->deleteLater();
|
|
#endif
|
|
|
|
m_vpnProtocol = nullptr;
|
|
}
|
|
|
|
void VpnConnection::setConnectionState(Vpn::ConnectionState state) {
|
|
onConnectionStateChanged(state);
|
|
|
|
if (state == Vpn::Disconnected && m_connectionState == Vpn::Reconnecting)
|
|
return;
|
|
|
|
m_connectionState = state;
|
|
emit connectionStateChanged(state);
|
|
}
|