From f2ff8a7b3bb3b676733e8574c9abad2200ee6810 Mon Sep 17 00:00:00 2001 From: cd-amn Date: Mon, 18 May 2026 16:47:04 +0000 Subject: [PATCH] refactor: thread interface name through LocalSocketController --- client/core/protocols/wireGuardProtocol.cpp | 3 +- client/mozilla/controllerimpl.h | 6 +++- client/mozilla/localsocketcontroller.cpp | 40 +++++++++++++++++++-- client/mozilla/localsocketcontroller.h | 7 +++- 4 files changed, 50 insertions(+), 6 deletions(-) diff --git a/client/core/protocols/wireGuardProtocol.cpp b/client/core/protocols/wireGuardProtocol.cpp index 631251619..643f8045f 100644 --- a/client/core/protocols/wireGuardProtocol.cpp +++ b/client/core/protocols/wireGuardProtocol.cpp @@ -12,7 +12,8 @@ WireguardProtocol::WireguardProtocol(const QJsonObject &configuration, QObject *parent) : VpnProtocol(configuration, parent) { - m_impl.reset(new LocalSocketController()); + const QString ifname = configuration.value("ifname").toString(); + m_impl.reset(new LocalSocketController(ifname)); connect(m_impl.get(), &ControllerImpl::connected, this, [this](const QString &pubkey, const QDateTime &connectionTimestamp) { setConnectionState(Vpn::ConnectionState::Connected); diff --git a/client/mozilla/controllerimpl.h b/client/mozilla/controllerimpl.h index 6da9f7c12..c34178be1 100644 --- a/client/mozilla/controllerimpl.h +++ b/client/mozilla/controllerimpl.h @@ -44,6 +44,8 @@ class ControllerImpl : public QObject { // "disconnecting" state until the "disconnected" signal is received. virtual void deactivate() = 0; + virtual void setPrimary(const QJsonObject& config) { Q_UNUSED(config) } + // This method is used to retrieve the VPN tunnel status (mainly the number // of bytes sent and received). It's called always when the VPN tunnel is // active. @@ -71,11 +73,13 @@ class ControllerImpl : public QObject { void initialized(bool status, bool connected, const QDateTime& connectionDate); - // These 2 signals can be dispatched at any time. void connected(const QString& pubkey, const QDateTime& connectionTimestamp = QDateTime()); void disconnected(); + void primaryReady(); + void primaryFailed(); + // This method should be emitted after a checkStatus() call. // "serverIpv4Gateway" is the current VPN tunnel gateway. // "deviceIpv4Address" is the address of the VPN client. diff --git a/client/mozilla/localsocketcontroller.cpp b/client/mozilla/localsocketcontroller.cpp index f1f9ca693..514252247 100644 --- a/client/mozilla/localsocketcontroller.cpp +++ b/client/mozilla/localsocketcontroller.cpp @@ -39,7 +39,8 @@ namespace { Logger logger("LocalSocketController"); } -LocalSocketController::LocalSocketController() { +LocalSocketController::LocalSocketController(const QString& ifname) + : m_ifname(ifname) { MZ_COUNT_CTOR(LocalSocketController); m_socket = new QLocalSocket(this); @@ -121,7 +122,7 @@ void LocalSocketController::daemonConnected() { checkStatus(); } -void LocalSocketController::activate(const QJsonObject &rawConfig) { +QJsonObject LocalSocketController::buildActivateJson(const QJsonObject& rawConfig) { QString protocolName = rawConfig.value("protocol").toString(); int splitTunnelType = rawConfig.value("splitTunnelType").toInt(); @@ -134,7 +135,6 @@ void LocalSocketController::activate(const QJsonObject &rawConfig) { QJsonObject wgConfig = rawConfig.value(protocolName + "_config_data").toObject(); QJsonObject json; - json.insert("type", "activate"); // json.insert("hopindex", QJsonValue((double)hop.m_hopindex)); json.insert("privateKey", wgConfig.value(amnezia::configKey::clientPrivKey)); json.insert("deviceIpv4Address", wgConfig.value(amnezia::configKey::clientIp)); @@ -292,6 +292,19 @@ void LocalSocketController::activate(const QJsonObject &rawConfig) { json.insert(amnezia::configKey::specialJunk5, wgConfig.value(amnezia::configKey::specialJunk5)); } + json.insert("ifname", m_ifname); + return json; +} + +void LocalSocketController::activate(const QJsonObject& rawConfig) { + QJsonObject json = buildActivateJson(rawConfig); + json.insert("type", "activate"); + write(json); +} + +void LocalSocketController::setPrimary(const QJsonObject& rawConfig) { + QJsonObject json = buildActivateJson(rawConfig); + json.insert("type", "setPrimary"); write(json); } @@ -306,6 +319,7 @@ void LocalSocketController::deactivate() { QJsonObject json; json.insert("type", "deactivate"); + json.insert("ifname", m_ifname); write(json); emit disconnected(); } @@ -471,12 +485,20 @@ void LocalSocketController::parseCommand(const QByteArray& command) { return; } + auto belongsToThisTunnel = [this, &obj]() { + const QJsonValue val = obj.value("ifname"); + return !val.isString() || val.toString() == m_ifname; + }; + if (type == "disconnected") { + if (!belongsToThisTunnel()) return; disconnectInternal(); return; } if (type == "connected") { + if (!belongsToThisTunnel()) return; + QJsonValue pubkey = obj.value("pubkey"); if (!pubkey.isString()) { logger.error() << "Unexpected pubkey value"; @@ -494,6 +516,18 @@ void LocalSocketController::parseCommand(const QByteArray& command) { return; } + if (type == "primaryReady") { + if (!belongsToThisTunnel()) return; + emit primaryReady(); + return; + } + + if (type == "primaryFailed") { + if (!belongsToThisTunnel()) return; + emit primaryFailed(); + return; + } + if (type == "backendFailure") { if (!obj.contains("errorCode")) { // report a generic error if we dont know what it is. diff --git a/client/mozilla/localsocketcontroller.h b/client/mozilla/localsocketcontroller.h index 4070c7527..672565dad 100644 --- a/client/mozilla/localsocketcontroller.h +++ b/client/mozilla/localsocketcontroller.h @@ -19,7 +19,7 @@ class LocalSocketController final : public ControllerImpl { Q_DISABLE_COPY_MOVE(LocalSocketController) public: - LocalSocketController(); + explicit LocalSocketController(const QString& ifname); ~LocalSocketController(); void initialize(const Device* device, const Keys* keys) override; @@ -28,6 +28,8 @@ class LocalSocketController final : public ControllerImpl { void deactivate() override; + void setPrimary(const QJsonObject& rawConfig) override; + void checkStatus() override; void getBackendLogs(std::function&& callback) override; @@ -40,6 +42,8 @@ class LocalSocketController final : public ControllerImpl { void initializeInternal(); void disconnectInternal(); + QJsonObject buildActivateJson(const QJsonObject& rawConfig); + void daemonConnected(); void errorOccurred(QLocalSocket::LocalSocketError socketError); void readData(); @@ -59,6 +63,7 @@ class LocalSocketController final : public ControllerImpl { QByteArray m_buffer; + QString m_ifname; QString m_deviceIpv4; std::function m_logCallback = nullptr;