mirror of
https://github.com/amnezia-vpn/amnezia-client.git
synced 2026-06-21 02:01:03 +07:00
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:
@@ -1,11 +1,12 @@
|
||||
#include "serversModel.h"
|
||||
|
||||
#include "core/models/serverDescription.h"
|
||||
|
||||
#include <QHash>
|
||||
#include <QSet>
|
||||
#include <QJsonDocument>
|
||||
|
||||
#include "core/models/serverConfig.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/selfhosted/sshSession.h"
|
||||
@@ -19,42 +20,15 @@
|
||||
|
||||
using namespace amnezia;
|
||||
|
||||
namespace
|
||||
{
|
||||
namespace configKey
|
||||
{
|
||||
constexpr char apiConfig[] = "api_config";
|
||||
constexpr char serviceInfo[] = "service_info";
|
||||
constexpr char availableCountries[] = "available_countries";
|
||||
constexpr char serverCountryCode[] = "server_country_code";
|
||||
constexpr char serverCountryName[] = "server_country_name";
|
||||
constexpr char userCountryCode[] = "user_country_code";
|
||||
constexpr char serviceType[] = "service_type";
|
||||
constexpr char serviceProtocol[] = "service_protocol";
|
||||
|
||||
constexpr char publicKeyInfo[] = "public_key";
|
||||
constexpr char expiresAt[] = "expires_at";
|
||||
}
|
||||
|
||||
QString normalizeVpnKey(const QString &vpnKey)
|
||||
{
|
||||
QString normalized = vpnKey.trimmed();
|
||||
if (normalized.startsWith(QStringLiteral("vpn://"), Qt::CaseInsensitive)) {
|
||||
normalized = normalized.mid(QStringLiteral("vpn://").size());
|
||||
}
|
||||
return normalized;
|
||||
}
|
||||
}
|
||||
|
||||
ServersModel::ServersModel(QObject *parent) : QAbstractListModel(parent)
|
||||
{
|
||||
connect(this, &ServersModel::defaultServerIndexChanged, this, &ServersModel::defaultServerNameChanged);
|
||||
|
||||
connect(this, &ServersModel::defaultServerIndexChanged, this, [this](const int serverIndex) {
|
||||
if (serverIndex < 0 || serverIndex >= m_servers.size()) {
|
||||
if (serverIndex < 0 || serverIndex >= m_descriptions.size()) {
|
||||
return;
|
||||
}
|
||||
auto defaultContainer = m_servers.at(serverIndex).defaultContainer();
|
||||
auto defaultContainer = m_descriptions.at(serverIndex).defaultContainer;
|
||||
emit ServersModel::defaultServerDefaultContainerChanged(defaultContainer);
|
||||
emit ServersModel::defaultServerNameChanged();
|
||||
});
|
||||
@@ -65,148 +39,73 @@ ServersModel::ServersModel(QObject *parent) : QAbstractListModel(parent)
|
||||
int ServersModel::rowCount(const QModelIndex &parent) const
|
||||
{
|
||||
Q_UNUSED(parent);
|
||||
return static_cast<int>(m_servers.size());
|
||||
return static_cast<int>(m_descriptions.size());
|
||||
}
|
||||
|
||||
QVariant ServersModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
if (!index.isValid() || index.row() < 0 || index.row() >= static_cast<int>(m_servers.size())) {
|
||||
if (!index.isValid() || index.row() < 0 || index.row() >= static_cast<int>(m_descriptions.size())) {
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
const ServerConfig &server = m_servers.at(index.row());
|
||||
const int configVersion = server.configVersion();
|
||||
|
||||
const ServerDescription &row = m_descriptions.at(index.row());
|
||||
const int configVersion = row.configVersion;
|
||||
|
||||
switch (role) {
|
||||
case NameRole: {
|
||||
if (configVersion) {
|
||||
if (server.isApiV1()) {
|
||||
return server.as<ApiV1ServerConfig>()->name;
|
||||
} else if (server.isApiV2()) {
|
||||
return server.as<ApiV2ServerConfig>()->name;
|
||||
}
|
||||
}
|
||||
QString name = server.description();
|
||||
if (name.isEmpty()) {
|
||||
return server.hostName();
|
||||
}
|
||||
return name;
|
||||
}
|
||||
case ServerDescriptionRole: {
|
||||
auto description = getServerDescription(server, index.row());
|
||||
return configVersion ? description : description + server.hostName();
|
||||
}
|
||||
case HostNameRole: return server.hostName();
|
||||
case CredentialsRole: return QVariant::fromValue(serverCredentials(index.row()));
|
||||
case CredentialsLoginRole: return serverCredentials(index.row()).userName;
|
||||
case IsDefaultRole: return index.row() == m_defaultServerIndex;
|
||||
case IsCurrentlyProcessedRole: return index.row() == m_processedServerIndex;
|
||||
case HasWriteAccessRole: {
|
||||
auto credentials = serverCredentials(index.row());
|
||||
return (!credentials.userName.isEmpty() && !credentials.secretData.isEmpty());
|
||||
}
|
||||
case ContainsAmneziaDnsRole: {
|
||||
QString primaryDns = server.dns1();
|
||||
return primaryDns == protocols::dns::amneziaDnsIp;
|
||||
}
|
||||
case DefaultContainerRole: {
|
||||
return server.defaultContainer();
|
||||
}
|
||||
case HasInstalledContainers: {
|
||||
return serverHasInstalledContainers(index.row());
|
||||
}
|
||||
case IsServerFromTelegramApiRole: {
|
||||
return configVersion == apiDefs::ConfigSource::Telegram;
|
||||
}
|
||||
case IsServerFromGatewayApiRole: {
|
||||
return configVersion == apiDefs::ConfigSource::AmneziaGateway;
|
||||
}
|
||||
case ApiConfigRole: {
|
||||
case NameRole:
|
||||
return row.serverName;
|
||||
case ServerDescriptionRole:
|
||||
return configVersion ? row.baseDescription : (row.baseDescription + row.hostName);
|
||||
case CollapsedServerDescriptionRole:
|
||||
return row.collapsedServerDescription;
|
||||
case ExpandedServerDescriptionRole:
|
||||
return row.expandedServerDescription;
|
||||
case HostNameRole:
|
||||
return row.hostName;
|
||||
case CredentialsRole:
|
||||
return QVariant::fromValue(serverCredentials(index.row()));
|
||||
case CredentialsLoginRole:
|
||||
return serverCredentials(index.row()).userName;
|
||||
case IsDefaultRole:
|
||||
return index.row() == m_defaultServerIndex;
|
||||
case IsCurrentlyProcessedRole:
|
||||
return index.row() == m_processedServerIndex;
|
||||
case HasWriteAccessRole:
|
||||
return row.hasWriteAccess;
|
||||
case ContainsAmneziaDnsRole:
|
||||
return row.primaryDnsIsAmnezia;
|
||||
case DefaultContainerRole:
|
||||
return QVariant::fromValue(row.defaultContainer);
|
||||
case HasInstalledContainers:
|
||||
return row.hasInstalledVpnContainers;
|
||||
case IsServerFromTelegramApiRole:
|
||||
return false;
|
||||
case IsServerFromGatewayApiRole:
|
||||
return row.isServerFromGatewayApi;
|
||||
case ApiConfigRole:
|
||||
return QVariant();
|
||||
}
|
||||
case IsCountrySelectionAvailableRole: {
|
||||
if (server.isApiV2()) {
|
||||
return !server.as<ApiV2ServerConfig>()->apiConfig.availableCountries.isEmpty();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
case ApiAvailableCountriesRole: {
|
||||
if (server.isApiV2()) {
|
||||
return server.as<ApiV2ServerConfig>()->apiConfig.availableCountries;
|
||||
}
|
||||
return QJsonArray();
|
||||
}
|
||||
case ApiServerCountryCodeRole: {
|
||||
if (server.isApiV2()) {
|
||||
return server.as<ApiV2ServerConfig>()->apiConfig.serverCountryCode;
|
||||
}
|
||||
return QString();
|
||||
}
|
||||
case HasAmneziaDns: {
|
||||
QString primaryDns = server.dns1();
|
||||
return primaryDns == protocols::dns::amneziaDnsIp;
|
||||
}
|
||||
case IsAdVisibleRole: {
|
||||
if (server.isApiV2()) {
|
||||
return server.as<ApiV2ServerConfig>()->apiConfig.serviceInfo.isAdVisible;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
case AdHeaderRole: {
|
||||
if (server.isApiV2()) {
|
||||
return server.as<ApiV2ServerConfig>()->apiConfig.serviceInfo.adHeader;
|
||||
}
|
||||
return QString();
|
||||
}
|
||||
case AdDescriptionRole: {
|
||||
if (server.isApiV2()) {
|
||||
return server.as<ApiV2ServerConfig>()->apiConfig.serviceInfo.adDescription;
|
||||
}
|
||||
return QString();
|
||||
}
|
||||
case AdEndpointRole: {
|
||||
if (server.isApiV2()) {
|
||||
return server.as<ApiV2ServerConfig>()->apiConfig.serviceInfo.adEndpoint;
|
||||
}
|
||||
return QString();
|
||||
}
|
||||
case IsRenewalAvailableRole: {
|
||||
if (server.isApiV2()) {
|
||||
return server.as<ApiV2ServerConfig>()->apiConfig.serviceInfo.isRenewalAvailable;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
case IsSubscriptionExpiredRole: {
|
||||
if (!server.isApiV2()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const ApiConfig &apiConfig = server.as<ApiV2ServerConfig>()->apiConfig;
|
||||
if (apiConfig.isInAppPurchase) {
|
||||
return false;
|
||||
}
|
||||
if (apiConfig.subscriptionExpiredByServer) {
|
||||
return true;
|
||||
}
|
||||
if (apiConfig.subscription.endDate.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
return apiUtils::isSubscriptionExpired(apiConfig.subscription.endDate);
|
||||
}
|
||||
case IsSubscriptionExpiringSoonRole: {
|
||||
if (!server.isApiV2()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const ApiConfig &apiConfig = server.as<ApiV2ServerConfig>()->apiConfig;
|
||||
if (apiConfig.isInAppPurchase) {
|
||||
return false;
|
||||
}
|
||||
if (apiConfig.subscription.endDate.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
return apiUtils::isSubscriptionExpiringSoon(apiConfig.subscription.endDate);
|
||||
}
|
||||
case IsCountrySelectionAvailableRole:
|
||||
return row.isCountrySelectionAvailable;
|
||||
case ApiAvailableCountriesRole:
|
||||
return row.apiAvailableCountries;
|
||||
case ApiServerCountryCodeRole:
|
||||
return row.apiServerCountryCode;
|
||||
case HasAmneziaDns:
|
||||
return row.primaryDnsIsAmnezia;
|
||||
case IsAdVisibleRole:
|
||||
return row.isAdVisible;
|
||||
case AdHeaderRole:
|
||||
return row.adHeader;
|
||||
case AdDescriptionRole:
|
||||
return row.adDescription;
|
||||
case AdEndpointRole:
|
||||
return row.adEndpoint;
|
||||
case IsRenewalAvailableRole:
|
||||
return row.isRenewalAvailable;
|
||||
case IsSubscriptionExpiredRole:
|
||||
return row.isSubscriptionExpired;
|
||||
case IsSubscriptionExpiringSoonRole:
|
||||
return row.isSubscriptionExpiringSoon;
|
||||
}
|
||||
|
||||
return QVariant();
|
||||
@@ -218,12 +117,11 @@ QVariant ServersModel::data(const int index, int role) const
|
||||
return data(modelIndex, role);
|
||||
}
|
||||
|
||||
void ServersModel::updateModel(const QVector<ServerConfig> &servers, int defaultServerIndex, bool isAmneziaDnsEnabled)
|
||||
void ServersModel::updateModel(const QVector<ServerDescription> &descriptions, int defaultServerIndex)
|
||||
{
|
||||
beginResetModel();
|
||||
m_servers = servers;
|
||||
m_descriptions = descriptions;
|
||||
m_defaultServerIndex = defaultServerIndex;
|
||||
m_isAmneziaDnsEnabled = isAmneziaDnsEnabled;
|
||||
endResetModel();
|
||||
emit defaultServerIndexChanged(m_defaultServerIndex);
|
||||
emit processedServerChanged();
|
||||
@@ -234,43 +132,15 @@ const int ServersModel::getDefaultServerIndex()
|
||||
return m_defaultServerIndex;
|
||||
}
|
||||
|
||||
QString ServersModel::getServerDescription(const ServerConfig &server, const int index) const
|
||||
{
|
||||
const int configVersion = server.configVersion();
|
||||
QString description;
|
||||
|
||||
if (server.isApiV2()) {
|
||||
const ApiV2ServerConfig *apiV2 = server.as<ApiV2ServerConfig>();
|
||||
if (apiV2 && !apiV2->apiConfig.serverCountryCode.isEmpty()) {
|
||||
return apiV2->apiConfig.serverCountryName;
|
||||
}
|
||||
return apiV2 ? apiV2->description : server.description();
|
||||
} else if (server.isApiV1()) {
|
||||
const ApiV1ServerConfig *apiV1 = server.as<ApiV1ServerConfig>();
|
||||
return apiV1 ? apiV1->description : server.description();
|
||||
} else if (data(index, HasWriteAccessRole).toBool()) {
|
||||
QMap<DockerContainer, ContainerConfig> containers = server.containers();
|
||||
bool isDnsInstalled = containers.contains(DockerContainer::Dns);
|
||||
if (m_isAmneziaDnsEnabled && isDnsInstalled) {
|
||||
description += "Amnezia DNS | ";
|
||||
}
|
||||
} else {
|
||||
if (data(index, HasAmneziaDns).toBool()) {
|
||||
description += "Amnezia DNS | ";
|
||||
}
|
||||
}
|
||||
return description;
|
||||
}
|
||||
|
||||
const int ServersModel::getServersCount()
|
||||
{
|
||||
return m_servers.size();
|
||||
return m_descriptions.size();
|
||||
}
|
||||
|
||||
bool ServersModel::hasServerWithWriteAccess()
|
||||
{
|
||||
for (size_t i = 0; i < getServersCount(); i++) {
|
||||
if (qvariant_cast<bool>(data(i, HasWriteAccessRole))) {
|
||||
if (qvariant_cast<bool>(data(static_cast<int>(i), HasWriteAccessRole))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -350,29 +220,17 @@ QHash<int, QByteArray> ServersModel::roleNames() const
|
||||
roles[IsSubscriptionExpiredRole] = "isSubscriptionExpired";
|
||||
roles[IsSubscriptionExpiringSoonRole] = "isSubscriptionExpiringSoon";
|
||||
|
||||
roles[HasAmneziaDns] = "hasAmneziaDns";
|
||||
|
||||
return roles;
|
||||
}
|
||||
|
||||
ServerCredentials ServersModel::serverCredentials(int index) const
|
||||
{
|
||||
if (index < 0 || index >= m_servers.size()) {
|
||||
if (index < 0 || index >= m_descriptions.size()) {
|
||||
return ServerCredentials();
|
||||
}
|
||||
const ServerConfig &server = m_servers.at(index);
|
||||
|
||||
if (server.isSelfHosted()) {
|
||||
const SelfHostedServerConfig *selfHosted = server.as<SelfHostedServerConfig>();
|
||||
if (selfHosted) {
|
||||
ServerCredentials credentials;
|
||||
credentials.hostName = selfHosted->hostName;
|
||||
credentials.userName = selfHosted->userName.value_or("");
|
||||
credentials.secretData = selfHosted->password.value_or("");
|
||||
credentials.port = selfHosted->port.value_or(22);
|
||||
return credentials;
|
||||
}
|
||||
}
|
||||
|
||||
return ServerCredentials();
|
||||
return m_descriptions.at(index).selfHostedSshCredentials;
|
||||
}
|
||||
|
||||
bool ServersModel::isServerFromApi(const int serverIndex)
|
||||
@@ -405,21 +263,10 @@ QVariant ServersModel::getProcessedServerData(const QString &roleString)
|
||||
return {};
|
||||
}
|
||||
|
||||
|
||||
bool ServersModel::serverHasInstalledContainers(const int serverIndex) const
|
||||
{
|
||||
const ServerConfig &server = m_servers.at(serverIndex);
|
||||
QMap<DockerContainer, ContainerConfig> containers = server.containers();
|
||||
|
||||
for (auto it = containers.begin(); it != containers.end(); ++it) {
|
||||
DockerContainer container = it.key();
|
||||
if (ContainerUtils::containerService(container) == ServiceType::Vpn) {
|
||||
return true;
|
||||
}
|
||||
if (container == DockerContainer::SSXray) {
|
||||
return true;
|
||||
}
|
||||
if (serverIndex < 0 || serverIndex >= m_descriptions.size()) {
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
return m_descriptions.at(serverIndex).hasInstalledVpnContainers;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user