mirror of
https://github.com/amnezia-vpn/amnezia-client.git
synced 2026-06-22 02:01:08 +07:00
feat: add auto switch from AWG to Xray
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
|
||||
#include "amneziaApplication.h"
|
||||
#include "core/controllers/serversController.h"
|
||||
#include "core/utils/containers/containerUtils.h"
|
||||
|
||||
ConnectionUiController::ConnectionUiController(ConnectionController* connectionController,
|
||||
ServersController* serversController,
|
||||
@@ -20,6 +21,9 @@ ConnectionUiController::ConnectionUiController(ConnectionController* connectionC
|
||||
|
||||
connect(this, &ConnectionUiController::connectButtonClicked, this, &ConnectionUiController::toggleConnection, Qt::QueuedConnection);
|
||||
|
||||
m_awgStateTimer.setSingleShot(true);
|
||||
connect(&m_awgStateTimer, &QTimer::timeout, this, &ConnectionUiController::onAwgStateTimeout);
|
||||
|
||||
m_state = Vpn::ConnectionState::Disconnected;
|
||||
}
|
||||
|
||||
@@ -56,6 +60,7 @@ void ConnectionUiController::onConnectionStateChanged(Vpn::ConnectionState state
|
||||
m_connectionStateText = tr("Connecting...");
|
||||
switch (state) {
|
||||
case Vpn::ConnectionState::Connected: {
|
||||
m_awgStateTimer.stop();
|
||||
amnApp->networkManager()->clearConnectionCache();
|
||||
|
||||
m_isConnectionInProgress = false;
|
||||
@@ -64,36 +69,54 @@ void ConnectionUiController::onConnectionStateChanged(Vpn::ConnectionState state
|
||||
break;
|
||||
}
|
||||
case Vpn::ConnectionState::Connecting: {
|
||||
{
|
||||
const QString serverId = m_serversController->getDefaultServerId();
|
||||
if (!serverId.isEmpty()) {
|
||||
const DockerContainer container = m_serversController->getDefaultContainer(serverId);
|
||||
const Proto proto = ContainerUtils::defaultProtocol(container);
|
||||
if (proto == Proto::Awg) {
|
||||
m_awgStateTimer.start(10000);
|
||||
} else {
|
||||
m_awgStateTimer.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
m_isConnectionInProgress = true;
|
||||
break;
|
||||
}
|
||||
case Vpn::ConnectionState::Reconnecting: {
|
||||
m_awgStateTimer.stop();
|
||||
m_isConnectionInProgress = true;
|
||||
m_connectionStateText = tr("Reconnecting...");
|
||||
break;
|
||||
}
|
||||
case Vpn::ConnectionState::Disconnected: {
|
||||
m_awgStateTimer.stop();
|
||||
m_isConnectionInProgress = false;
|
||||
m_connectionStateText = tr("Connect");
|
||||
break;
|
||||
}
|
||||
case Vpn::ConnectionState::Disconnecting: {
|
||||
m_awgStateTimer.stop();
|
||||
m_isConnectionInProgress = true;
|
||||
m_connectionStateText = tr("Disconnecting...");
|
||||
break;
|
||||
}
|
||||
case Vpn::ConnectionState::Preparing: {
|
||||
m_awgStateTimer.stop();
|
||||
m_isConnectionInProgress = true;
|
||||
m_connectionStateText = tr("Preparing...");
|
||||
break;
|
||||
}
|
||||
case Vpn::ConnectionState::Error: {
|
||||
m_awgStateTimer.stop();
|
||||
m_isConnectionInProgress = false;
|
||||
m_connectionStateText = tr("Connect");
|
||||
emit connectionErrorOccurred(getLastConnectionError());
|
||||
break;
|
||||
}
|
||||
case Vpn::ConnectionState::Unknown: {
|
||||
m_awgStateTimer.stop();
|
||||
m_isConnectionInProgress = false;
|
||||
m_connectionStateText = tr("Connect");
|
||||
emit connectionErrorOccurred(getLastConnectionError());
|
||||
@@ -143,3 +166,40 @@ bool ConnectionUiController::isConnected() const
|
||||
{
|
||||
return m_isConnected;
|
||||
}
|
||||
|
||||
void ConnectionUiController::onAwgStateTimeout()
|
||||
{
|
||||
if (m_state != Vpn::ConnectionState::Connecting) {
|
||||
return;
|
||||
}
|
||||
|
||||
const QString serverId = m_serversController->getDefaultServerId();
|
||||
if (serverId.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
const DockerContainer container = m_serversController->getDefaultContainer(serverId);
|
||||
const Proto proto = ContainerUtils::defaultProtocol(container);
|
||||
if (proto != Proto::Awg) {
|
||||
return;
|
||||
}
|
||||
|
||||
const QMap<DockerContainer, ContainerConfig> containersMap = m_serversController->getServerContainersMap(serverId);
|
||||
if (!containersMap.contains(DockerContainer::Xray)) {
|
||||
qDebug().noquote() << "AWG connect timeout: no Xray container available";
|
||||
return;
|
||||
}
|
||||
|
||||
qDebug().noquote() << "AWG connect timeout (10s), switching default container to Xray and reconnecting";
|
||||
|
||||
m_serversController->setDefaultContainer(serverId, DockerContainer::Xray);
|
||||
emit requestSetCurrentProtocol(QStringLiteral("vless"));
|
||||
|
||||
closeConnection();
|
||||
|
||||
QTimer::singleShot(500, this, [this]() {
|
||||
if (!m_isConnected && !m_isConnectionInProgress) {
|
||||
emit prepareConfig();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user