2023-05-12 23:54:31 +08:00
|
|
|
#include "connectionController.h"
|
|
|
|
|
|
2025-08-10 06:12:19 +03:00
|
|
|
#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS) || defined(MACOS_NE)
|
2023-08-31 16:00:41 +05:00
|
|
|
#include <QGuiApplication>
|
|
|
|
|
#else
|
|
|
|
|
#include <QApplication>
|
|
|
|
|
#endif
|
2023-05-12 23:54:31 +08:00
|
|
|
|
2024-04-01 20:20:02 +07:00
|
|
|
#include "core/controllers/vpnConfigurationController.h"
|
2024-05-10 13:06:04 +03:00
|
|
|
#include "version.h"
|
2023-06-05 22:40:35 +08:00
|
|
|
|
2023-05-12 23:54:31 +08:00
|
|
|
ConnectionController::ConnectionController(const QSharedPointer<ServersModel> &serversModel,
|
|
|
|
|
const QSharedPointer<ContainersModel> &containersModel,
|
2024-04-01 20:20:02 +07:00
|
|
|
const QSharedPointer<ClientManagementModel> &clientManagementModel,
|
2024-04-12 20:00:21 +05:00
|
|
|
const QSharedPointer<VpnConnection> &vpnConnection, const std::shared_ptr<Settings> &settings,
|
|
|
|
|
QObject *parent)
|
2024-04-01 20:20:02 +07:00
|
|
|
: QObject(parent),
|
|
|
|
|
m_serversModel(serversModel),
|
|
|
|
|
m_containersModel(containersModel),
|
|
|
|
|
m_clientManagementModel(clientManagementModel),
|
|
|
|
|
m_vpnConnection(vpnConnection),
|
|
|
|
|
m_settings(settings)
|
2023-05-12 23:54:31 +08:00
|
|
|
{
|
2024-04-12 20:00:21 +05:00
|
|
|
connect(m_vpnConnection.get(), &VpnConnection::connectionStateChanged, this, &ConnectionController::onConnectionStateChanged);
|
|
|
|
|
connect(this, &ConnectionController::connectToVpn, m_vpnConnection.get(), &VpnConnection::connectToVpn, Qt::QueuedConnection);
|
|
|
|
|
connect(this, &ConnectionController::disconnectFromVpn, m_vpnConnection.get(), &VpnConnection::disconnectFromVpn, Qt::QueuedConnection);
|
2023-10-01 11:12:27 +08:00
|
|
|
|
2025-02-15 11:50:42 +07:00
|
|
|
connect(this, &ConnectionController::connectButtonClicked, this, &ConnectionController::toggleConnection, Qt::QueuedConnection);
|
2024-05-16 06:19:56 -07:00
|
|
|
|
2023-10-01 11:12:27 +08:00
|
|
|
m_state = Vpn::ConnectionState::Disconnected;
|
2023-05-12 23:54:31 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-05 15:49:10 +08:00
|
|
|
void ConnectionController::openConnection()
|
2023-05-12 23:54:31 +08:00
|
|
|
{
|
2025-08-10 06:12:19 +03:00
|
|
|
#if !defined(Q_OS_ANDROID) && !defined(Q_OS_IOS) && !defined(MACOS_NE)
|
|
|
|
|
if (!Utils::processIsRunning(Utils::executable(SERVICE_NAME, false), true))
|
|
|
|
|
{
|
2024-09-23 01:19:46 +03:00
|
|
|
emit connectionErrorOccurred(ErrorCode::AmneziaServiceNotRunning);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2024-05-10 13:06:04 +03:00
|
|
|
|
2024-02-29 17:22:17 +07:00
|
|
|
int serverIndex = m_serversModel->getDefaultServerIndex();
|
2024-05-16 06:19:56 -07:00
|
|
|
QJsonObject serverConfig = m_serversModel->getServerConfig(serverIndex);
|
2024-04-01 20:20:02 +07:00
|
|
|
|
2025-02-15 11:50:42 +07:00
|
|
|
DockerContainer container = qvariant_cast<DockerContainer>(m_serversModel->data(serverIndex, ServersModel::Roles::DefaultContainerRole));
|
2024-04-01 20:20:02 +07:00
|
|
|
|
2025-02-15 11:50:42 +07:00
|
|
|
if (!m_containersModel->isSupportedByCurrentPlatform(container)) {
|
|
|
|
|
emit connectionErrorOccurred(ErrorCode::NotSupportedOnThisPlatform);
|
|
|
|
|
return;
|
2024-04-01 20:20:02 +07:00
|
|
|
}
|
2025-02-15 11:50:42 +07:00
|
|
|
|
|
|
|
|
QSharedPointer<ServerController> serverController(new ServerController(m_settings));
|
|
|
|
|
VpnConfigurationsController vpnConfigurationController(m_settings, serverController);
|
|
|
|
|
|
|
|
|
|
QJsonObject containerConfig = m_containersModel->getContainerConfig(container);
|
|
|
|
|
ServerCredentials credentials = m_serversModel->getServerCredentials(serverIndex);
|
|
|
|
|
|
|
|
|
|
auto dns = m_serversModel->getDnsPair(serverIndex);
|
|
|
|
|
|
|
|
|
|
auto vpnConfiguration = vpnConfigurationController.createVpnConfiguration(dns, serverConfig, containerConfig, container);
|
|
|
|
|
emit connectToVpn(serverIndex, credentials, container, vpnConfiguration);
|
2023-06-05 15:49:10 +08:00
|
|
|
}
|
2023-05-12 23:54:31 +08:00
|
|
|
|
2023-06-05 15:49:10 +08:00
|
|
|
void ConnectionController::closeConnection()
|
|
|
|
|
{
|
|
|
|
|
emit disconnectFromVpn();
|
|
|
|
|
}
|
2023-05-12 23:54:31 +08:00
|
|
|
|
2024-05-25 13:00:51 +03:00
|
|
|
ErrorCode ConnectionController::getLastConnectionError()
|
2023-06-05 22:40:35 +08:00
|
|
|
{
|
2024-05-25 13:00:51 +03:00
|
|
|
return m_vpnConnection->lastError();
|
2023-06-05 22:40:35 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-21 20:56:00 +09:00
|
|
|
void ConnectionController::onConnectionStateChanged(Vpn::ConnectionState state)
|
2023-06-05 15:49:10 +08:00
|
|
|
{
|
2023-10-01 11:12:27 +08:00
|
|
|
m_state = state;
|
|
|
|
|
|
2023-06-21 20:56:00 +09:00
|
|
|
m_isConnected = false;
|
2024-03-21 21:34:51 +01:00
|
|
|
m_connectionStateText = tr("Connecting...");
|
2023-06-21 20:56:00 +09:00
|
|
|
switch (state) {
|
|
|
|
|
case Vpn::ConnectionState::Connected: {
|
|
|
|
|
m_isConnectionInProgress = false;
|
|
|
|
|
m_isConnected = true;
|
2023-08-28 11:06:58 +03:00
|
|
|
m_connectionStateText = tr("Connected");
|
2023-06-21 20:56:00 +09:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case Vpn::ConnectionState::Connecting: {
|
|
|
|
|
m_isConnectionInProgress = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case Vpn::ConnectionState::Reconnecting: {
|
|
|
|
|
m_isConnectionInProgress = true;
|
2024-03-21 21:34:51 +01:00
|
|
|
m_connectionStateText = tr("Reconnecting...");
|
2023-06-21 20:56:00 +09:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case Vpn::ConnectionState::Disconnected: {
|
|
|
|
|
m_isConnectionInProgress = false;
|
|
|
|
|
m_connectionStateText = tr("Connect");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case Vpn::ConnectionState::Disconnecting: {
|
|
|
|
|
m_isConnectionInProgress = true;
|
2024-03-21 21:34:51 +01:00
|
|
|
m_connectionStateText = tr("Disconnecting...");
|
2023-06-21 20:56:00 +09:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case Vpn::ConnectionState::Preparing: {
|
|
|
|
|
m_isConnectionInProgress = true;
|
2024-04-01 20:20:02 +07:00
|
|
|
m_connectionStateText = tr("Preparing...");
|
2023-06-21 20:56:00 +09:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case Vpn::ConnectionState::Error: {
|
|
|
|
|
m_isConnectionInProgress = false;
|
2023-06-27 19:07:42 +09:00
|
|
|
m_connectionStateText = tr("Connect");
|
2023-06-21 20:56:00 +09:00
|
|
|
emit connectionErrorOccurred(getLastConnectionError());
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case Vpn::ConnectionState::Unknown: {
|
|
|
|
|
m_isConnectionInProgress = false;
|
2023-06-27 19:07:42 +09:00
|
|
|
m_connectionStateText = tr("Connect");
|
2023-06-21 20:56:00 +09:00
|
|
|
emit connectionErrorOccurred(getLastConnectionError());
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
emit connectionStateChanged();
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-17 17:03:39 +05:00
|
|
|
void ConnectionController::onCurrentContainerUpdated()
|
2023-09-13 20:49:44 +08:00
|
|
|
{
|
2023-09-17 17:03:39 +05:00
|
|
|
if (m_isConnected || m_isConnectionInProgress) {
|
2024-06-19 02:14:22 +03:00
|
|
|
emit reconnectWithUpdatedContainer(tr("Settings updated successfully, reconnnection..."));
|
2023-09-13 20:49:44 +08:00
|
|
|
openConnection();
|
2023-12-01 14:16:27 +07:00
|
|
|
} else {
|
|
|
|
|
emit reconnectWithUpdatedContainer(tr("Settings updated successfully"));
|
2023-09-13 20:49:44 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-06 14:37:10 +08:00
|
|
|
void ConnectionController::onTranslationsUpdated()
|
2023-10-01 11:12:27 +08:00
|
|
|
{
|
2023-10-06 14:37:10 +08:00
|
|
|
// get translated text of current state
|
2023-10-01 11:12:27 +08:00
|
|
|
onConnectionStateChanged(getCurrentConnectionState());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Vpn::ConnectionState ConnectionController::getCurrentConnectionState()
|
|
|
|
|
{
|
|
|
|
|
return m_state;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-21 20:56:00 +09:00
|
|
|
QString ConnectionController::connectionStateText() const
|
|
|
|
|
{
|
|
|
|
|
return m_connectionStateText;
|
2023-05-12 23:54:31 +08:00
|
|
|
}
|
|
|
|
|
|
2024-04-01 20:20:02 +07:00
|
|
|
void ConnectionController::toggleConnection()
|
2024-02-09 23:23:26 +05:00
|
|
|
{
|
2024-04-01 20:20:02 +07:00
|
|
|
if (m_state == Vpn::ConnectionState::Preparing) {
|
|
|
|
|
emit preparingConfig();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isConnectionInProgress()) {
|
2024-02-09 23:23:26 +05:00
|
|
|
closeConnection();
|
|
|
|
|
} else if (isConnected()) {
|
|
|
|
|
closeConnection();
|
|
|
|
|
} else {
|
2025-02-15 11:50:42 +07:00
|
|
|
emit prepareConfig();
|
2024-02-09 23:23:26 +05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-21 20:56:00 +09:00
|
|
|
bool ConnectionController::isConnectionInProgress() const
|
2023-05-12 23:54:31 +08:00
|
|
|
{
|
2023-06-21 20:56:00 +09:00
|
|
|
return m_isConnectionInProgress;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ConnectionController::isConnected() const
|
|
|
|
|
{
|
|
|
|
|
return m_isConnected;
|
2023-05-12 23:54:31 +08:00
|
|
|
}
|