refactor: thread interface name through LocalSocketController

This commit is contained in:
cd-amn
2026-05-18 16:47:04 +00:00
parent 864b8c6f8a
commit f2ff8a7b3b
4 changed files with 50 additions and 6 deletions
+2 -1
View File
@@ -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);
+5 -1
View File
@@ -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.
+37 -3
View File
@@ -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.
+6 -1
View File
@@ -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<void(const QString&)>&& 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<void(const QString&)> m_logCallback = nullptr;