Files
amnezia-client/client/vpnconnection.h
T

111 lines
2.7 KiB
C++
Raw Normal View History

2020-12-26 15:03:51 +03:00
#ifndef VPNCONNECTION_H
#define VPNCONNECTION_H
#include <QObject>
#include <QMetaObject>
2020-12-26 15:03:51 +03:00
#include <QString>
#include <QScopedPointer>
2021-02-02 01:47:40 +03:00
#include <QRemoteObjectNode>
2023-08-08 19:02:41 -07:00
#include <QTimer>
2020-12-26 15:03:51 +03:00
2021-01-06 17:12:24 +03:00
#include "protocols/vpnprotocol.h"
#include "core/defs.h"
2021-01-26 15:01:15 +03:00
#include "settings.h"
2022-12-12 14:41:54 +01:00
2022-02-22 02:08:57 +03:00
#ifdef AMNEZIA_DESKTOP
#include "core/ipcclient.h"
#endif
#ifdef Q_OS_ANDROID
#include "protocols/android_vpnprotocol.h"
#endif
2021-01-06 17:12:24 +03:00
using namespace amnezia;
2020-12-26 15:03:51 +03:00
class VpnConnection : public QObject
{
Q_OBJECT
public:
explicit VpnConnection(std::shared_ptr<Settings> settings, QObject* parent = nullptr);
2021-02-18 15:00:41 +03:00
~VpnConnection() override;
2020-12-26 15:03:51 +03:00
2021-01-09 19:55:16 +03:00
static QString bytesPerSecToText(quint64 bytes);
2020-12-26 15:03:51 +03:00
2021-01-06 17:12:24 +03:00
ErrorCode lastError() const;
2021-02-18 15:00:41 +03:00
bool isConnected() const;
bool isDisconnected() const;
2023-05-14 21:11:19 +08:00
Vpn::ConnectionState connectionState();
2021-02-18 15:00:41 +03:00
QSharedPointer<VpnProtocol> vpnProtocol() const;
2021-06-12 11:59:36 +03:00
const QString &remoteAddress() const;
2022-01-22 20:00:06 +03:00
void addSitesRoutes(const QString &gw, Settings::RouteMode mode);
2021-06-12 11:59:36 +03:00
#ifdef Q_OS_ANDROID
void restoreConnection();
#endif
2021-10-26 12:59:20 +03:00
public slots:
void connectToVpn(int serverIndex,
2025-05-02 23:54:36 -07:00
const ServerCredentials &credentials, DockerContainer container, const QJsonObject &vpnConfiguration);
2021-10-26 12:59:20 +03:00
void disconnectFromVpn();
void restartConnection();
2021-10-26 12:59:20 +03:00
2023-08-08 16:41:00 -07:00
void addRoutes(const QStringList &ips);
void deleteRoutes(const QStringList &ips);
void flushDns();
2025-05-02 23:54:36 -07:00
void onKillSwitchModeChanged(bool enabled);
2025-11-30 18:49:16 -08:00
void disconnectSlots();
2023-08-08 16:41:00 -07:00
2020-12-26 15:03:51 +03:00
signals:
void bytesChanged(quint64 receivedBytes, quint64 sentBytes);
2023-05-14 21:11:19 +08:00
void connectionStateChanged(Vpn::ConnectionState state);
2021-01-08 16:51:58 +03:00
void vpnProtocolError(amnezia::ErrorCode error);
2020-12-26 15:03:51 +03:00
2021-02-18 15:00:41 +03:00
void serviceIsNotReady();
2020-12-26 15:03:51 +03:00
protected slots:
void onBytesChanged(quint64 receivedBytes, quint64 sentBytes);
2023-05-14 21:11:19 +08:00
void onConnectionStateChanged(Vpn::ConnectionState state);
2022-12-12 14:41:54 +01:00
2020-12-26 15:03:51 +03:00
protected:
2021-02-18 15:00:41 +03:00
QSharedPointer<VpnProtocol> m_vpnProtocol;
QMetaObject::Connection m_connectionLoseHandle;
QMetaObject::Connection m_networkChangeHandle;
2021-01-26 15:01:15 +03:00
private:
2022-08-25 12:47:02 +03:00
std::shared_ptr<Settings> m_settings;
2021-02-18 15:00:41 +03:00
QJsonObject m_vpnConfiguration;
2021-06-01 18:18:09 +03:00
QJsonObject m_routeMode;
2021-06-12 11:59:36 +03:00
QString m_remoteAddress;
2023-08-08 19:02:41 -07:00
ServerCredentials m_serverCredentials;
int m_serverIndex;
DockerContainer m_dockerContainer;
// Track VPN state before sleep for smart reconnection
bool m_wasConnectedBeforeSleep = false;
bool m_pendingNetworkCheck = false;
2023-08-08 19:02:41 -07:00
// Only for iOS for now, check counters
QTimer m_checkTimer;
2021-02-02 01:47:40 +03:00
#ifdef Q_OS_ANDROID
AndroidVpnProtocol* androidVpnProtocol = nullptr;
2023-12-04 18:23:08 +03:00
AndroidVpnProtocol* createDefaultAndroidVpnProtocol();
void createAndroidConnections();
#endif
void createProtocolConnections();
2023-10-13 15:45:06 +05:00
void appendSplitTunnelingConfig();
2024-04-25 20:01:00 +07:00
void appendKillSwitchConfig();
bool startNetworkCheckIfReady();
2020-12-26 15:03:51 +03:00
};
#endif // VPNCONNECTION_H