Use networkchecker for all protocols

This commit is contained in:
Mykola Baibuz 2025-03-27 21:06:47 +02:00
parent 9b41ed66bb
commit 4b86425992
7 changed files with 45 additions and 16 deletions

View file

@ -50,14 +50,7 @@ LocalSocketController::LocalSocketController() {
connect(&m_initializingTimer, &QTimer::timeout, this,
&LocalSocketController::initializeInternal);
connect(IpcClient::Interface().data(), &IpcInterfaceReplica::connectionLose,
this, [this]() {
logger.debug() << "Connection Lose";
auto result = IpcClient::Interface()->stopNetworkCheck();
result.waitForFinished(3000);
this->deactivate();
this->activate(m_RawConfig);
});
}
@ -126,8 +119,6 @@ void LocalSocketController::daemonConnected() {
}
void LocalSocketController::activate(const QJsonObject &rawConfig) {
m_RawConfig = rawConfig;
QString protocolName = rawConfig.value("protocol").toString();
int splitTunnelType = rawConfig.value("splitTunnelType").toInt();
@ -143,6 +134,7 @@ void LocalSocketController::activate(const QJsonObject &rawConfig) {
// json.insert("hopindex", QJsonValue((double)hop.m_hopindex));
json.insert("privateKey", wgConfig.value(amnezia::config_key::client_priv_key));
json.insert("deviceIpv4Address", wgConfig.value(amnezia::config_key::client_ip));
m_deviceIpv4 = wgConfig.value(amnezia::config_key::client_ip).toString();
// set up IPv6 unique-local-address, ULA, with "fd00::/8" prefix, not globally routable.
// this will be default IPv6 gateway, OS recognizes that IPv6 link
@ -435,9 +427,6 @@ void LocalSocketController::parseCommand(const QByteArray& command) {
return;
}
IpcClient::Interface()->startNetworkCheck(serverIpv4Gateway.toString(),
deviceIpv4Address.toString());
QJsonValue txBytes = obj.value("txBytes");
if (!txBytes.isDouble()) {
logger.error() << "Unexpected txBytes value";
@ -470,8 +459,14 @@ void LocalSocketController::parseCommand(const QByteArray& command) {
logger.debug() << "Handshake completed with:"
<< pubkey.toString();
emit connected(pubkey.toString());
checkStatus();
emit statusUpdated("",
m_deviceIpv4, 0,
0);
emit connected(pubkey.toString());
return;
}

View file

@ -60,10 +60,9 @@ class LocalSocketController final : public ControllerImpl {
QByteArray m_buffer;
QString m_deviceIpv4;
std::function<void(const QString&)> m_logCallback = nullptr;
QJsonObject m_RawConfig;
QTimer m_initializingTimer;
uint32_t m_initializingRetry = 0;
};

View file

@ -103,6 +103,11 @@ QString VpnProtocol::vpnGateway() const
return m_vpnGateway;
}
QString VpnProtocol::vpnLocalAddress() const
{
return m_vpnLocalAddress;
}
VpnProtocol *VpnProtocol::factory(DockerContainer container, const QJsonObject &configuration)
{
switch (container) {

View file

@ -63,6 +63,7 @@ public:
QString routeGateway() const;
QString vpnGateway() const;
QString vpnLocalAddress() const;
static VpnProtocol* factory(amnezia::DockerContainer container, const QJsonObject &configuration);

View file

@ -17,6 +17,13 @@ WireguardProtocol::WireguardProtocol(const QJsonObject &configuration, QObject *
[this](const QString &pubkey, const QDateTime &connectionTimestamp) {
emit connectionStateChanged(Vpn::ConnectionState::Connected);
});
connect(m_impl.get(), &ControllerImpl::statusUpdated, this,
[this](const QString& serverIpv4Gateway,
const QString& deviceIpv4Address, uint64_t txBytes,
uint64_t rxBytes) {
m_vpnLocalAddress = deviceIpv4Address;
});
connect(m_impl.get(), &ControllerImpl::disconnected, this,
[this]() { emit connectionStateChanged(Vpn::ConnectionState::Disconnected); });
m_impl->initialize(nullptr, nullptr);

View file

@ -86,8 +86,13 @@ void VpnConnection::onConnectionStateChanged(Vpn::ConnectionState state)
}
}
if (container != DockerContainer::Ipsec) {
IpcClient::Interface()->startNetworkCheck(remoteAddress(), m_vpnProtocol->vpnLocalAddress());
}
} else if (state == Vpn::ConnectionState::Error) {
IpcClient::Interface()->flushDns();
IpcClient::Interface()->stopNetworkCheck();
if (m_settings->isSitesSplitTunnelingEnabled()) {
if (m_settings->routeMode() == Settings::VpnOnlyForwardSites) {
@ -97,6 +102,7 @@ void VpnConnection::onConnectionStateChanged(Vpn::ConnectionState state)
} else if (state == Vpn::ConnectionState::Connecting) {
} else if (state == Vpn::ConnectionState::Disconnected) {
IpcClient::Interface()->stopNetworkCheck();
}
}
#endif
@ -237,6 +243,9 @@ void VpnConnection::connectToVpn(int serverIndex, const ServerCredentials &crede
emit connectionStateChanged(Vpn::ConnectionState::Connecting);
m_vpnConfiguration = vpnConfiguration;
m_serverIndex = serverIndex;
m_serverCredentials = credentials;
m_dockerContainer = container;
#ifdef AMNEZIA_DESKTOP
if (m_vpnProtocol) {
@ -281,6 +290,15 @@ void VpnConnection::createProtocolConnections()
connect(m_vpnProtocol.data(), SIGNAL(connectionStateChanged(Vpn::ConnectionState)), this,
SLOT(onConnectionStateChanged(Vpn::ConnectionState)));
connect(m_vpnProtocol.data(), SIGNAL(bytesChanged(quint64, quint64)), this, SLOT(onBytesChanged(quint64, quint64)));
connect(IpcClient::Interface().data(), &IpcInterfaceReplica::connectionLose,
this, [this]() {
qDebug() << "Connection Lose";
auto result = IpcClient::Interface()->stopNetworkCheck();
result.waitForFinished(3000);
this->disconnectFromVpn();
this->connectToVpn(m_serverIndex, m_serverCredentials, m_dockerContainer, m_vpnConfiguration);
});
}
void VpnConnection::appendKillSwitchConfig()

View file

@ -77,6 +77,10 @@ private:
QJsonObject m_routeMode;
QString m_remoteAddress;
ServerCredentials m_serverCredentials;
int m_serverIndex;
DockerContainer m_dockerContainer;
// Only for iOS for now, check counters
QTimer m_checkTimer;