mirror of
https://github.com/amnezia-vpn/amnezia-client.git
synced 2026-06-22 02:01:08 +07:00
feat: introduce Tunnel wrapping VpnProtocol with two-phase lifecycle
This commit is contained in:
@@ -106,6 +106,13 @@ QString VpnProtocol::vpnLocalAddress() const
|
||||
return m_vpnLocalAddress;
|
||||
}
|
||||
|
||||
bool VpnProtocol::isWireGuardBased(amnezia::DockerContainer container)
|
||||
{
|
||||
return container == amnezia::DockerContainer::Awg
|
||||
|| container == amnezia::DockerContainer::Awg2
|
||||
|| container == amnezia::DockerContainer::WireGuard;
|
||||
}
|
||||
|
||||
VpnProtocol *VpnProtocol::factory(DockerContainer container, const QJsonObject &configuration)
|
||||
{
|
||||
switch (container) {
|
||||
@@ -137,6 +144,7 @@ QString VpnProtocol::textConnectionState(Vpn::ConnectionState connectionState)
|
||||
case Vpn::ConnectionState::Preparing: return tr("Preparing");
|
||||
case Vpn::ConnectionState::Connecting: return tr("Connecting...");
|
||||
case Vpn::ConnectionState::Connected: return tr("Connected");
|
||||
case Vpn::ConnectionState::Switching: return tr("Switching...");
|
||||
case Vpn::ConnectionState::Disconnecting: return tr("Disconnecting...");
|
||||
case Vpn::ConnectionState::Reconnecting: return tr("Reconnecting...");
|
||||
case Vpn::ConnectionState::Error: return tr("Error");
|
||||
|
||||
@@ -27,6 +27,7 @@ namespace Vpn
|
||||
Preparing,
|
||||
Connecting,
|
||||
Connected,
|
||||
Switching,
|
||||
Disconnecting,
|
||||
Reconnecting,
|
||||
Error
|
||||
@@ -60,6 +61,7 @@ public:
|
||||
virtual bool isDisconnected() const;
|
||||
virtual ErrorCode start() = 0;
|
||||
virtual void stop() = 0;
|
||||
virtual void setPrimary(const QJsonObject& config) { Q_UNUSED(config) }
|
||||
|
||||
Vpn::ConnectionState connectionState() const;
|
||||
ErrorCode lastError() const;
|
||||
@@ -71,6 +73,7 @@ public:
|
||||
QString vpnLocalAddress() const;
|
||||
|
||||
static VpnProtocol* factory(amnezia::DockerContainer container, const QJsonObject &configuration);
|
||||
static bool isWireGuardBased(amnezia::DockerContainer container);
|
||||
|
||||
signals:
|
||||
void bytesChanged(quint64 receivedBytes, quint64 sentBytes);
|
||||
@@ -78,6 +81,8 @@ signals:
|
||||
void timeoutTimerEvent();
|
||||
void protocolError(amnezia::ErrorCode e);
|
||||
void tunnelAddressesUpdated(const QString& gateway, const QString& localAddress);
|
||||
void primaryReady();
|
||||
void primaryFailed();
|
||||
|
||||
public slots:
|
||||
virtual void onTimeout(); // todo: remove?
|
||||
|
||||
@@ -15,7 +15,8 @@ WireguardProtocol::WireguardProtocol(const QJsonObject &configuration, QObject *
|
||||
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) {
|
||||
[this](const QString& pubkey, const QDateTime&) {
|
||||
Q_UNUSED(pubkey)
|
||||
setConnectionState(Vpn::ConnectionState::Connected);
|
||||
});
|
||||
connect(m_impl.get(), &ControllerImpl::statusUpdated, this,
|
||||
@@ -42,6 +43,10 @@ WireguardProtocol::WireguardProtocol(const QJsonObject &configuration, QObject *
|
||||
|
||||
connect(m_impl.get(), &ControllerImpl::disconnected, this,
|
||||
[this]() { setConnectionState(Vpn::ConnectionState::Disconnected); });
|
||||
connect(m_impl.get(), &ControllerImpl::primaryReady,
|
||||
this, &WireguardProtocol::primaryReady);
|
||||
connect(m_impl.get(), &ControllerImpl::primaryFailed,
|
||||
this, &WireguardProtocol::primaryFailed);
|
||||
m_impl->initialize(nullptr, nullptr);
|
||||
}
|
||||
|
||||
@@ -51,13 +56,7 @@ WireguardProtocol::~WireguardProtocol()
|
||||
QThread::msleep(200);
|
||||
}
|
||||
|
||||
void WireguardProtocol::stop()
|
||||
{
|
||||
stopMzImpl();
|
||||
return;
|
||||
}
|
||||
|
||||
ErrorCode WireguardProtocol::startMzImpl()
|
||||
ErrorCode WireguardProtocol::start()
|
||||
{
|
||||
QString protocolName = m_rawConfig.value("protocol").toString();
|
||||
QJsonObject vpnConfigData = m_rawConfig.value(protocolName + "_config_data").toObject();
|
||||
@@ -65,18 +64,19 @@ ErrorCode WireguardProtocol::startMzImpl()
|
||||
m_rawConfig.insert(protocolName + "_config_data", vpnConfigData);
|
||||
m_rawConfig[configKey::hostName] = NetworkUtilities::getIPAddress(m_rawConfig[configKey::hostName].toString());
|
||||
|
||||
m_stopped = false;
|
||||
m_impl->activate(m_rawConfig);
|
||||
return ErrorCode::NoError;
|
||||
}
|
||||
|
||||
ErrorCode WireguardProtocol::stopMzImpl()
|
||||
void WireguardProtocol::stop()
|
||||
{
|
||||
if (m_stopped) return;
|
||||
m_stopped = true;
|
||||
m_impl->deactivate();
|
||||
return ErrorCode::NoError;
|
||||
}
|
||||
|
||||
|
||||
ErrorCode WireguardProtocol::start()
|
||||
void WireguardProtocol::setPrimary(const QJsonObject& config)
|
||||
{
|
||||
return startMzImpl();
|
||||
m_impl->setPrimary(config);
|
||||
}
|
||||
|
||||
@@ -21,11 +21,10 @@ public:
|
||||
|
||||
ErrorCode start() override;
|
||||
void stop() override;
|
||||
|
||||
ErrorCode startMzImpl();
|
||||
ErrorCode stopMzImpl();
|
||||
void setPrimary(const QJsonObject& config) override;
|
||||
|
||||
private:
|
||||
bool m_stopped = false;
|
||||
|
||||
QScopedPointer<ControllerImpl> m_impl;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user