refactor: remove serverConfig struct (#2595)

* refactor: remove serverConfig struct

* refactor: add warnings for api v1 configs

* refactor: moved the server type definition to a separate namespace

* refactor: simplified gateway stacks

* fix: fixed server description

* fix: fixed postAsync reply usage

* fix: fixed validateConfig call

* fix: fixed server name in notifications

* fix: fixed initPrepareConfigHandler for lagacy configs
This commit is contained in:
vkamn
2026-05-15 12:33:36 +08:00
committed by GitHub
parent 009ca981d5
commit 06372c8fd7
123 changed files with 3558 additions and 3026 deletions
@@ -2,14 +2,13 @@
#include "amneziaApplication.h"
#include "core/configurators/wireguardConfigurator.h"
#include "core/utils/api/apiEnums.h"
#include "core/utils/serverConfigUtils.h"
#include "core/utils/constants/apiKeys.h"
#include "core/utils/constants/apiConstants.h"
#include "core/utils/api/apiUtils.h"
#include "core/utils/qrCodeUtils.h"
#include "ui/controllers/systemController.h"
#include "version.h"
#include "core/models/serverConfig.h"
#include <QClipboard>
#include <QDebug>
#include <QSet>
@@ -67,7 +66,17 @@ SubscriptionUiController::SubscriptionUiController(ServersController* serversCon
ApiDevicesModel* apiDevicesModel,
SettingsController* settingsController,
QObject *parent)
: QObject(parent), m_serversController(serversController), m_apiServicesModel(apiServicesModel), m_servicesCatalogController(servicesCatalogController), m_subscriptionController(subscriptionController), m_apiSubscriptionPlansModel(apiSubscriptionPlansModel), m_apiBenefitsModel(apiBenefitsModel), m_apiAccountInfoModel(apiAccountInfoModel), m_apiCountryModel(apiCountryModel), m_apiDevicesModel(apiDevicesModel), m_settingsController(settingsController)
: QObject(parent),
m_serversController(serversController),
m_apiServicesModel(apiServicesModel),
m_servicesCatalogController(servicesCatalogController),
m_subscriptionController(subscriptionController),
m_apiSubscriptionPlansModel(apiSubscriptionPlansModel),
m_apiBenefitsModel(apiBenefitsModel),
m_apiAccountInfoModel(apiAccountInfoModel),
m_apiCountryModel(apiCountryModel),
m_apiDevicesModel(apiDevicesModel),
m_settingsController(settingsController)
{
connect(m_apiServicesModel, &ApiServicesModel::serviceSelectionChanged, this, [this]() {
ApiServicesModel::ApiServicesData selectedServiceData = m_apiServicesModel->selectedServiceData();
@@ -76,14 +85,14 @@ SubscriptionUiController::SubscriptionUiController(ServersController* serversCon
});
}
bool SubscriptionUiController::exportVpnKey(int serverIndex, const QString &fileName)
bool SubscriptionUiController::exportVpnKey(const QString &serverId, const QString &fileName)
{
if (fileName.isEmpty()) {
emit errorOccurred(ErrorCode::PermissionsError);
return false;
}
prepareVpnKeyExport(serverIndex);
prepareVpnKeyExport(serverId);
if (m_vpnKey.isEmpty()) {
emit errorOccurred(ErrorCode::ApiConfigEmptyError);
return false;
@@ -93,7 +102,8 @@ bool SubscriptionUiController::exportVpnKey(int serverIndex, const QString &file
return true;
}
bool SubscriptionUiController::exportNativeConfig(int serverIndex, const QString &serverCountryCode, const QString &fileName)
bool SubscriptionUiController::exportNativeConfig(const QString &serverId, const QString &serverCountryCode, const QString &fileName)
{
if (fileName.isEmpty()) {
emit errorOccurred(ErrorCode::PermissionsError);
@@ -101,7 +111,7 @@ bool SubscriptionUiController::exportNativeConfig(int serverIndex, const QString
}
QString nativeConfig;
ErrorCode errorCode = m_subscriptionController->exportNativeConfig(serverIndex, serverCountryCode, nativeConfig);
ErrorCode errorCode = m_subscriptionController->exportNativeConfig(serverId, serverCountryCode, nativeConfig);
if (errorCode != ErrorCode::NoError) {
emit errorOccurred(errorCode);
return false;
@@ -111,9 +121,10 @@ bool SubscriptionUiController::exportNativeConfig(int serverIndex, const QString
return true;
}
bool SubscriptionUiController::revokeNativeConfig(int serverIndex, const QString &serverCountryCode)
bool SubscriptionUiController::revokeNativeConfig(const QString &serverId, const QString &serverCountryCode)
{
ErrorCode errorCode = m_subscriptionController->revokeNativeConfig(serverIndex, serverCountryCode);
ErrorCode errorCode = m_subscriptionController->revokeNativeConfig(serverId, serverCountryCode);
if (errorCode != ErrorCode::NoError) {
emit errorOccurred(errorCode);
return false;
@@ -121,10 +132,11 @@ bool SubscriptionUiController::revokeNativeConfig(int serverIndex, const QString
return true;
}
void SubscriptionUiController::prepareVpnKeyExport(int serverIndex)
void SubscriptionUiController::prepareVpnKeyExport(const QString &serverId)
{
QString vpnKey;
ErrorCode errorCode = m_subscriptionController->prepareVpnKeyExport(serverIndex, vpnKey);
ErrorCode errorCode = m_subscriptionController->prepareVpnKeyExport(serverId, vpnKey);
if (errorCode != ErrorCode::NoError) {
emit errorOccurred(errorCode);
return;
@@ -140,6 +152,7 @@ void SubscriptionUiController::prepareVpnKeyExport(int serverIndex)
emit vpnKeyExportReady();
}
void SubscriptionUiController::copyVpnKeyToClipboard()
{
auto clipboard = amnApp->getClipboard();
@@ -170,14 +183,12 @@ bool SubscriptionUiController::importPremiumFromAppStore(const QString &storePro
productId = QStringLiteral("amnezia_premium_6_month");
}
ServerConfig serverConfig;
int duplicateServerIndex = -1;
ErrorCode errorCode = m_subscriptionController->processAppStorePurchase(
m_apiServicesModel->getCountryCode(),
m_apiServicesModel->getSelectedServiceType(),
m_apiServicesModel->getSelectedServiceProtocol(),
productId,
serverConfig,
&duplicateServerIndex);
if (errorCode != ErrorCode::NoError) {
@@ -260,11 +271,8 @@ bool SubscriptionUiController::importFreeFromGateway()
}
SubscriptionController::ProtocolData protocolData = m_subscriptionController->generateProtocolData(serviceProtocol);
ServerConfig serverConfig;
ErrorCode errorCode = m_subscriptionController->importServiceFromGateway(userCountryCode, serviceType,
serviceProtocol, protocolData,
serverConfig);
serviceProtocol, protocolData);
if (errorCode == ErrorCode::NoError) {
emit installServerFromApiFinished(tr("%1 installed successfully.").arg(m_apiServicesModel->getSelectedServiceName()));
@@ -278,12 +286,10 @@ bool SubscriptionUiController::importFreeFromGateway()
bool SubscriptionUiController::importTrialFromGateway(const QString &email)
{
emit trialEmailError(QString());
ServerConfig serverConfig;
ErrorCode errorCode = m_subscriptionController->importTrialFromGateway(m_apiServicesModel->getCountryCode(),
m_apiServicesModel->getSelectedServiceType(),
m_apiServicesModel->getSelectedServiceProtocol(),
email,
serverConfig);
email);
if (errorCode != ErrorCode::NoError) {
if (errorCode == ErrorCode::ApiTrialAlreadyUsedError) {
emit trialEmailError(
@@ -298,21 +304,17 @@ bool SubscriptionUiController::importTrialFromGateway(const QString &email)
return true;
}
bool SubscriptionUiController::updateServiceFromGateway(const int serverIndex, const QString &newCountryCode, const QString &newCountryName,
bool SubscriptionUiController::updateServiceFromGateway(const QString &serverId, const QString &newCountryCode, const QString &newCountryName,
bool reloadServiceConfig)
{
bool isConnectEvent = newCountryCode.isEmpty() && newCountryName.isEmpty() && !reloadServiceConfig;
bool wasSubscriptionExpired = false;
ServerConfig oldServerConfig = m_serversController->getServerConfig(serverIndex);
if (oldServerConfig.isApiV2()) {
const ApiV2ServerConfig *oldApiV2 = oldServerConfig.as<ApiV2ServerConfig>();
if (oldApiV2) {
wasSubscriptionExpired = oldApiV2->apiConfig.subscriptionExpiredByServer
|| oldApiV2->apiConfig.isSubscriptionExpired();
}
if (const auto oldApiV2 = m_serversController->apiV2Config(serverId)) {
wasSubscriptionExpired = oldApiV2->apiConfig.subscriptionExpiredByServer
|| oldApiV2->apiConfig.isSubscriptionExpired();
}
ErrorCode errorCode = m_subscriptionController->updateServiceFromGateway(serverIndex, newCountryCode, isConnectEvent);
ErrorCode errorCode = m_subscriptionController->updateServiceFromGateway(serverId, newCountryCode, isConnectEvent);
if (errorCode == ErrorCode::NoError) {
if (wasSubscriptionExpired) {
@@ -336,27 +338,10 @@ bool SubscriptionUiController::updateServiceFromGateway(const int serverIndex, c
}
}
bool SubscriptionUiController::updateServiceFromTelegram(const int serverIndex)
bool SubscriptionUiController::deactivateDevice(const QString &serverId)
{
#ifdef Q_OS_IOS
IosController::Instance()->requestInetAccess();
QThread::msleep(10);
#endif
ErrorCode errorCode = m_subscriptionController->updateServiceFromTelegram(serverIndex);
if (errorCode == ErrorCode::NoError) {
emit updateServerFromApiFinished();
return true;
} else {
emit errorOccurred(errorCode);
return false;
}
}
bool SubscriptionUiController::deactivateDevice(int serverIndex)
{
ErrorCode errorCode = m_subscriptionController->deactivateDevice(serverIndex);
ErrorCode errorCode = m_subscriptionController->deactivateDevice(serverId);
if (errorCode != ErrorCode::NoError) {
emit errorOccurred(errorCode);
return false;
@@ -365,9 +350,10 @@ bool SubscriptionUiController::deactivateDevice(int serverIndex)
return true;
}
bool SubscriptionUiController::deactivateExternalDevice(int serverIndex, const QString &uuid, const QString &serverCountryCode)
bool SubscriptionUiController::deactivateExternalDevice(const QString &serverId, const QString &uuid, const QString &serverCountryCode)
{
ErrorCode errorCode = m_subscriptionController->deactivateExternalDevice(serverIndex, uuid, serverCountryCode);
ErrorCode errorCode = m_subscriptionController->deactivateExternalDevice(serverId, uuid, serverCountryCode);
if (errorCode != ErrorCode::NoError) {
emit errorOccurred(errorCode);
return false;
@@ -376,12 +362,19 @@ bool SubscriptionUiController::deactivateExternalDevice(int serverIndex, const Q
return true;
}
void SubscriptionUiController::validateConfig()
{
int serverIndex = m_serversController->getDefaultServerIndex();
bool hasInstalledContainers = m_serversController->hasInstalledContainers(serverIndex);
const QString serverId = m_serversController->getDefaultServerId();
if (!serverId.isEmpty() && m_serversController->isLegacyApiV1Server(serverId)) {
emit unsupportedConnectDrawerRequested();
emit configValidated(false);
return;
}
ErrorCode errorCode = m_subscriptionController->validateAndUpdateConfig(serverIndex, hasInstalledContainers);
bool hasInstalledContainers = m_serversController->hasInstalledContainers(serverId);
ErrorCode errorCode = m_subscriptionController->validateAndUpdateConfig(serverId, hasInstalledContainers);
if (errorCode != ErrorCode::NoError) {
if (errorCode == ErrorCode::ApiSubscriptionExpiredError) {
@@ -395,22 +388,25 @@ void SubscriptionUiController::validateConfig()
emit configValidated(true);
}
void SubscriptionUiController::setCurrentProtocol(int serverIndex, const QString &protocolName)
void SubscriptionUiController::setCurrentProtocol(const QString &serverId, const QString &protocolName)
{
m_subscriptionController->setCurrentProtocol(serverIndex, protocolName);
m_subscriptionController->setCurrentProtocol(serverId, protocolName);
}
bool SubscriptionUiController::isVlessProtocol(int serverIndex)
bool SubscriptionUiController::isVlessProtocol(const QString &serverId)
{
return m_subscriptionController->isVlessProtocol(serverIndex);
return m_subscriptionController->isVlessProtocol(serverId);
}
void SubscriptionUiController::removeApiConfig(int serverIndex)
void SubscriptionUiController::removeApiConfig(const QString &serverId)
{
m_subscriptionController->removeApiConfig(serverIndex);
m_subscriptionController->removeApiConfig(serverId);
emit apiConfigRemoved(tr("Api config removed"));
}
QList<QString> SubscriptionUiController::getQrCodes()
{
return m_qrCodes;
@@ -426,7 +422,7 @@ QString SubscriptionUiController::getVpnKey()
return m_vpnKey;
}
bool SubscriptionUiController::getAccountInfo(int serverIndex, bool reload)
bool SubscriptionUiController::getAccountInfo(const QString &serverId, bool reload)
{
if (reload) {
QEventLoop wait;
@@ -434,15 +430,18 @@ bool SubscriptionUiController::getAccountInfo(int serverIndex, bool reload)
wait.exec(QEventLoop::ExcludeUserInputEvents);
}
QJsonObject accountInfo;
ErrorCode errorCode = m_subscriptionController->getAccountInfo(serverIndex, accountInfo);
ErrorCode errorCode = m_subscriptionController->getAccountInfo(serverId, accountInfo);
if (errorCode != ErrorCode::NoError) {
emit errorOccurred(errorCode);
return false;
}
ServerConfig serverConfig = m_serversController->getServerConfig(serverIndex);
QJsonObject serverConfigJson = serverConfig.toJson();
m_apiAccountInfoModel->updateModel(accountInfo, serverConfigJson);
const auto apiV2 = m_serversController->apiV2Config(serverId);
if (!apiV2.has_value()) {
emit errorOccurred(ErrorCode::InternalError);
return false;
}
m_apiAccountInfoModel->updateModel(accountInfo, apiV2->toJson());
if (reload) {
updateApiCountryModel();
@@ -463,9 +462,9 @@ void SubscriptionUiController::updateApiDevicesModel()
m_apiDevicesModel->updateModel(m_apiAccountInfoModel->getIssuedConfigsInfo(), m_settingsController->getInstallationUuid(false));
}
void SubscriptionUiController::getRenewalLink(int serverIndex)
void SubscriptionUiController::getRenewalLink(const QString &serverId)
{
if (serverIndex < 0) {
if (serverId.isEmpty()) {
emit errorOccurred(ErrorCode::InternalError);
return;
}
@@ -483,6 +482,6 @@ void SubscriptionUiController::getRenewalLink(int serverIndex)
}
emit renewalLinkReceived(url);
});
watcher->setFuture(m_subscriptionController->getRenewalLink(serverIndex));
watcher->setFuture(m_subscriptionController->getRenewalLink(serverId));
}
@@ -13,6 +13,7 @@
#include "ui/models/api/apiAccountInfoModel.h"
#include "ui/models/api/apiCountryModel.h"
#include "ui/models/api/apiDevicesModel.h"
class SubscriptionUiController : public QObject
{
Q_OBJECT
@@ -34,10 +35,10 @@ public:
Q_PROPERTY(QString vpnKey READ getVpnKey NOTIFY vpnKeyExportReady)
public slots:
bool exportNativeConfig(int serverIndex, const QString &serverCountryCode, const QString &fileName);
bool revokeNativeConfig(int serverIndex, const QString &serverCountryCode);
bool exportVpnKey(int serverIndex, const QString &fileName);
void prepareVpnKeyExport(int serverIndex);
bool exportNativeConfig(const QString &serverId, const QString &serverCountryCode, const QString &fileName);
bool revokeNativeConfig(const QString &serverId, const QString &serverCountryCode);
bool exportVpnKey(const QString &serverId, const QString &fileName);
void prepareVpnKeyExport(const QString &serverId);
void copyVpnKeyToClipboard();
bool fillAvailableServices();
@@ -45,21 +46,21 @@ public slots:
bool importFreeFromGateway();
bool restoreServiceFromAppStore();
bool importTrialFromGateway(const QString &email);
bool updateServiceFromGateway(const int serverIndex, const QString &newCountryCode, const QString &newCountryName,
bool updateServiceFromGateway(const QString &serverId, const QString &newCountryCode, const QString &newCountryName,
bool reloadServiceConfig = false);
bool updateServiceFromTelegram(const int serverIndex);
bool deactivateDevice(int serverIndex);
bool deactivateExternalDevice(int serverIndex, const QString &uuid, const QString &serverCountryCode);
bool deactivateDevice(const QString &serverId);
bool deactivateExternalDevice(const QString &serverId, const QString &uuid, const QString &serverCountryCode);
void validateConfig();
void setCurrentProtocol(int serverIndex, const QString &protocolName);
bool isVlessProtocol(int serverIndex);
void setCurrentProtocol(const QString &serverId, const QString &protocolName);
bool isVlessProtocol(const QString &serverId);
void removeApiConfig(int serverIndex);
void removeApiConfig(const QString &serverId);
bool getAccountInfo(const QString &serverId, bool reload);
void getRenewalLink(const QString &serverId);
bool getAccountInfo(int serverIndex, bool reload);
void getRenewalLink(int serverIndex);
void updateApiCountryModel();
void updateApiDevicesModel();
@@ -80,6 +81,8 @@ signals:
void vpnKeyExportReady();
void unsupportedConnectDrawerRequested();
private:
QList<QString> getQrCodes();
int getQrCodesCount();