mirror of
https://github.com/amnezia-vpn/amnezia-client.git
synced 2026-06-20 02:00:55 +07:00
refactor: thread interface name through LocalSocketController
This commit is contained in:
@@ -12,7 +12,8 @@
|
|||||||
WireguardProtocol::WireguardProtocol(const QJsonObject &configuration, QObject *parent)
|
WireguardProtocol::WireguardProtocol(const QJsonObject &configuration, QObject *parent)
|
||||||
: VpnProtocol(configuration, 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,
|
connect(m_impl.get(), &ControllerImpl::connected, this,
|
||||||
[this](const QString &pubkey, const QDateTime &connectionTimestamp) {
|
[this](const QString &pubkey, const QDateTime &connectionTimestamp) {
|
||||||
setConnectionState(Vpn::ConnectionState::Connected);
|
setConnectionState(Vpn::ConnectionState::Connected);
|
||||||
|
|||||||
@@ -44,6 +44,8 @@ class ControllerImpl : public QObject {
|
|||||||
// "disconnecting" state until the "disconnected" signal is received.
|
// "disconnecting" state until the "disconnected" signal is received.
|
||||||
virtual void deactivate() = 0;
|
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
|
// 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
|
// of bytes sent and received). It's called always when the VPN tunnel is
|
||||||
// active.
|
// active.
|
||||||
@@ -71,11 +73,13 @@ class ControllerImpl : public QObject {
|
|||||||
void initialized(bool status, bool connected,
|
void initialized(bool status, bool connected,
|
||||||
const QDateTime& connectionDate);
|
const QDateTime& connectionDate);
|
||||||
|
|
||||||
// These 2 signals can be dispatched at any time.
|
|
||||||
void connected(const QString& pubkey,
|
void connected(const QString& pubkey,
|
||||||
const QDateTime& connectionTimestamp = QDateTime());
|
const QDateTime& connectionTimestamp = QDateTime());
|
||||||
void disconnected();
|
void disconnected();
|
||||||
|
|
||||||
|
void primaryReady();
|
||||||
|
void primaryFailed();
|
||||||
|
|
||||||
// This method should be emitted after a checkStatus() call.
|
// This method should be emitted after a checkStatus() call.
|
||||||
// "serverIpv4Gateway" is the current VPN tunnel gateway.
|
// "serverIpv4Gateway" is the current VPN tunnel gateway.
|
||||||
// "deviceIpv4Address" is the address of the VPN client.
|
// "deviceIpv4Address" is the address of the VPN client.
|
||||||
|
|||||||
@@ -39,7 +39,8 @@ namespace {
|
|||||||
Logger logger("LocalSocketController");
|
Logger logger("LocalSocketController");
|
||||||
}
|
}
|
||||||
|
|
||||||
LocalSocketController::LocalSocketController() {
|
LocalSocketController::LocalSocketController(const QString& ifname)
|
||||||
|
: m_ifname(ifname) {
|
||||||
MZ_COUNT_CTOR(LocalSocketController);
|
MZ_COUNT_CTOR(LocalSocketController);
|
||||||
|
|
||||||
m_socket = new QLocalSocket(this);
|
m_socket = new QLocalSocket(this);
|
||||||
@@ -121,7 +122,7 @@ void LocalSocketController::daemonConnected() {
|
|||||||
checkStatus();
|
checkStatus();
|
||||||
}
|
}
|
||||||
|
|
||||||
void LocalSocketController::activate(const QJsonObject &rawConfig) {
|
QJsonObject LocalSocketController::buildActivateJson(const QJsonObject& rawConfig) {
|
||||||
QString protocolName = rawConfig.value("protocol").toString();
|
QString protocolName = rawConfig.value("protocol").toString();
|
||||||
|
|
||||||
int splitTunnelType = rawConfig.value("splitTunnelType").toInt();
|
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 wgConfig = rawConfig.value(protocolName + "_config_data").toObject();
|
||||||
|
|
||||||
QJsonObject json;
|
QJsonObject json;
|
||||||
json.insert("type", "activate");
|
|
||||||
// json.insert("hopindex", QJsonValue((double)hop.m_hopindex));
|
// json.insert("hopindex", QJsonValue((double)hop.m_hopindex));
|
||||||
json.insert("privateKey", wgConfig.value(amnezia::configKey::clientPrivKey));
|
json.insert("privateKey", wgConfig.value(amnezia::configKey::clientPrivKey));
|
||||||
json.insert("deviceIpv4Address", wgConfig.value(amnezia::configKey::clientIp));
|
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(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);
|
write(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -306,6 +319,7 @@ void LocalSocketController::deactivate() {
|
|||||||
|
|
||||||
QJsonObject json;
|
QJsonObject json;
|
||||||
json.insert("type", "deactivate");
|
json.insert("type", "deactivate");
|
||||||
|
json.insert("ifname", m_ifname);
|
||||||
write(json);
|
write(json);
|
||||||
emit disconnected();
|
emit disconnected();
|
||||||
}
|
}
|
||||||
@@ -471,12 +485,20 @@ void LocalSocketController::parseCommand(const QByteArray& command) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto belongsToThisTunnel = [this, &obj]() {
|
||||||
|
const QJsonValue val = obj.value("ifname");
|
||||||
|
return !val.isString() || val.toString() == m_ifname;
|
||||||
|
};
|
||||||
|
|
||||||
if (type == "disconnected") {
|
if (type == "disconnected") {
|
||||||
|
if (!belongsToThisTunnel()) return;
|
||||||
disconnectInternal();
|
disconnectInternal();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type == "connected") {
|
if (type == "connected") {
|
||||||
|
if (!belongsToThisTunnel()) return;
|
||||||
|
|
||||||
QJsonValue pubkey = obj.value("pubkey");
|
QJsonValue pubkey = obj.value("pubkey");
|
||||||
if (!pubkey.isString()) {
|
if (!pubkey.isString()) {
|
||||||
logger.error() << "Unexpected pubkey value";
|
logger.error() << "Unexpected pubkey value";
|
||||||
@@ -494,6 +516,18 @@ void LocalSocketController::parseCommand(const QByteArray& command) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (type == "primaryReady") {
|
||||||
|
if (!belongsToThisTunnel()) return;
|
||||||
|
emit primaryReady();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type == "primaryFailed") {
|
||||||
|
if (!belongsToThisTunnel()) return;
|
||||||
|
emit primaryFailed();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (type == "backendFailure") {
|
if (type == "backendFailure") {
|
||||||
if (!obj.contains("errorCode")) {
|
if (!obj.contains("errorCode")) {
|
||||||
// report a generic error if we dont know what it is.
|
// report a generic error if we dont know what it is.
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ class LocalSocketController final : public ControllerImpl {
|
|||||||
Q_DISABLE_COPY_MOVE(LocalSocketController)
|
Q_DISABLE_COPY_MOVE(LocalSocketController)
|
||||||
|
|
||||||
public:
|
public:
|
||||||
LocalSocketController();
|
explicit LocalSocketController(const QString& ifname);
|
||||||
~LocalSocketController();
|
~LocalSocketController();
|
||||||
|
|
||||||
void initialize(const Device* device, const Keys* keys) override;
|
void initialize(const Device* device, const Keys* keys) override;
|
||||||
@@ -28,6 +28,8 @@ class LocalSocketController final : public ControllerImpl {
|
|||||||
|
|
||||||
void deactivate() override;
|
void deactivate() override;
|
||||||
|
|
||||||
|
void setPrimary(const QJsonObject& rawConfig) override;
|
||||||
|
|
||||||
void checkStatus() override;
|
void checkStatus() override;
|
||||||
|
|
||||||
void getBackendLogs(std::function<void(const QString&)>&& callback) override;
|
void getBackendLogs(std::function<void(const QString&)>&& callback) override;
|
||||||
@@ -40,6 +42,8 @@ class LocalSocketController final : public ControllerImpl {
|
|||||||
void initializeInternal();
|
void initializeInternal();
|
||||||
void disconnectInternal();
|
void disconnectInternal();
|
||||||
|
|
||||||
|
QJsonObject buildActivateJson(const QJsonObject& rawConfig);
|
||||||
|
|
||||||
void daemonConnected();
|
void daemonConnected();
|
||||||
void errorOccurred(QLocalSocket::LocalSocketError socketError);
|
void errorOccurred(QLocalSocket::LocalSocketError socketError);
|
||||||
void readData();
|
void readData();
|
||||||
@@ -59,6 +63,7 @@ class LocalSocketController final : public ControllerImpl {
|
|||||||
|
|
||||||
QByteArray m_buffer;
|
QByteArray m_buffer;
|
||||||
|
|
||||||
|
QString m_ifname;
|
||||||
QString m_deviceIpv4;
|
QString m_deviceIpv4;
|
||||||
std::function<void(const QString&)> m_logCallback = nullptr;
|
std::function<void(const QString&)> m_logCallback = nullptr;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user