Add network status check for AWG/WG protocol

This commit is contained in:
Mykola Baibuz 2025-03-12 23:32:00 +02:00
parent 9dea98f020
commit e792117be1
11 changed files with 304 additions and 16 deletions

View file

@ -13,6 +13,7 @@
#include <QJsonObject>
#include <QJsonValue>
#include <QStandardPaths>
#include <QThread>
#include "ipaddress.h"
#include "leakdetector.h"
@ -48,6 +49,15 @@ LocalSocketController::LocalSocketController() {
m_initializingTimer.setSingleShot(true);
connect(&m_initializingTimer, &QTimer::timeout, this,
&LocalSocketController::initializeInternal);
connect(&m_pingHelper, &PingHelper::connectionLose, this, [this]() {
logger.debug() << "Connection Lose";
m_pingHelper.stop();
this->deactivate();
QThread::msleep(3000);
this->activate(m_RawConfig);
});
}
LocalSocketController::~LocalSocketController() {
@ -116,6 +126,8 @@ void LocalSocketController::daemonConnected() {
void LocalSocketController::activate(const QJsonObject &rawConfig) {
m_RawConfig = rawConfig;
QString protocolName = rawConfig.value("protocol").toString();
int splitTunnelType = rawConfig.value("splitTunnelType").toInt();
@ -258,6 +270,7 @@ void LocalSocketController::activate(const QJsonObject &rawConfig) {
json.insert(amnezia::config_key::transportPacketMagicHeader, wgConfig.value(amnezia::config_key::transportPacketMagicHeader));
}
write(json);
}
@ -362,6 +375,8 @@ void LocalSocketController::parseCommand(const QByteArray& command) {
return;
}
qDebug() << command;
QJsonObject obj = json.object();
QJsonValue typeValue = obj.value("type");
if (!typeValue.isString()) {
@ -406,6 +421,7 @@ void LocalSocketController::parseCommand(const QByteArray& command) {
}
if (type == "status") {
QJsonValue serverIpv4Gateway = obj.value("serverIpv4Gateway");
if (!serverIpv4Gateway.isString()) {
logger.error() << "Unexpected serverIpv4Gateway value";
@ -418,6 +434,8 @@ void LocalSocketController::parseCommand(const QByteArray& command) {
return;
}
m_pingHelper.start(serverIpv4Gateway.toString(), deviceIpv4Address.toString());
QJsonValue txBytes = obj.value("txBytes");
if (!txBytes.isDouble()) {
logger.error() << "Unexpected txBytes value";
@ -451,6 +469,7 @@ void LocalSocketController::parseCommand(const QByteArray& command) {
logger.debug() << "Handshake completed with:"
<< pubkey.toString();
emit connected(pubkey.toString());
checkStatus();
return;
}

View file

@ -11,6 +11,9 @@
#include <functional>
#include "controllerimpl.h"
#include "mozilla/pinghelper.h"
#include "qjsonobject.h"
class QJsonObject;
@ -60,7 +63,11 @@ class LocalSocketController final : public ControllerImpl {
std::function<void(const QString&)> m_logCallback = nullptr;
QJsonObject m_RawConfig;
PingHelper m_pingHelper;
QTimer m_initializingTimer;
QTimer m_statusTimer;
uint32_t m_initializingRetry = 0;
};

View file

@ -41,6 +41,7 @@ void PingHelper::start(const QString& serverIpv4Gateway,
m_gateway = QHostAddress(serverIpv4Gateway);
m_source = QHostAddress(deviceIpv4Address.section('/', 0, 0));
m_pingSender = PingSenderFactory::create(m_source, this);
// Some platforms require root access to send and receive ICMP pings. If
@ -53,8 +54,10 @@ void PingHelper::start(const QString& serverIpv4Gateway,
connect(m_pingSender, &PingSender::recvPing, this, &PingHelper::pingReceived,
Qt::QueuedConnection);
connect(m_pingSender, &PingSender::criticalPingError, this,
[]() { logger.info() << "Encountered Unrecoverable ping error"; });
connect(m_pingSender, &PingSender::criticalPingError, this, [this]() {
logger.info() << "Encountered Unrecoverable ping error";
emit connectionLose();
});
// Reset the ping statistics
m_sequence = 0;

View file

@ -33,6 +33,8 @@ class PingHelper final : public QObject {
signals:
void pingSentAndReceived(qint64 msec);
void connectionLose();
private:
void nextPing();

View file

@ -5,27 +5,26 @@
#include "pingsenderfactory.h"
#if defined(MZ_LINUX) || defined(MZ_ANDROID)
//# include "platforms/linux/linuxpingsender.h"
# include "platforms/linux/linuxpingsender.h"
#elif defined(MZ_MACOS) || defined(MZ_IOS)
# include "platforms/macos/macospingsender.h"
# include "platforms/macos/macospingsender.h"
#elif defined(MZ_WINDOWS)
# include "platforms/windows/windowspingsender.h"
#elif defined(MZ_DUMMY) || defined(UNIT_TEST)
# include "platforms/dummy/dummypingsender.h"
# include "platforms/windows/windowspingsender.h"
#elif defined(MZ_WASM) || defined(UNIT_TEST)
# include "platforms/dummy/dummypingsender.h"
#else
# error "Unsupported platform"
# error "Unsupported platform"
#endif
PingSender* PingSenderFactory::create(const QHostAddress& source,
QObject* parent) {
#if defined(MZ_LINUX) || defined(MZ_ANDROID)
return nullptr;
// return new LinuxPingSender(source, parent);
return new LinuxPingSender(source, parent);
#elif defined(MZ_MACOS) || defined(MZ_IOS)
return new MacOSPingSender(source, parent);
return new MacOSPingSender(source, parent);
#elif defined(MZ_WINDOWS)
return new WindowsPingSender(source, parent);
return new WindowsPingSender(source, parent);
#else
return new DummyPingSender(source, parent);
return new DummyPingSender(source, parent);
#endif
}

View file

@ -10,9 +10,10 @@ class QHostAddress;
class QObject;
class PingSenderFactory final {
public:
PingSenderFactory() = delete;
static PingSender* create(const QHostAddress& source, QObject* parent);
public:
PingSenderFactory() = delete;
static PingSender* create(const QHostAddress& source, QObject* parent);
};
#endif // PINGSENDERFACTORY_H