mirror of
https://github.com/amnezia-vpn/amnezia-client.git
synced 2026-06-02 08:32:28 +03:00
* fix: AUTOMOC and AUTOUIC added * update: native exports test * update: export test * update: vless serialization/deserialization test added * update: language model and controller test * update: sites UiController and Model test * update: sites test * update: app ui model and controller test * update: allowed dns ui model and controller test * update: env vars and removed some lines * update: news ui model and controller test (incomplete) * update: api services ui model and controller test (incompleted) * update: job for tests (Linux) * update: job for tests (Windows) * update: proper artifact names * update: added envs * update: added 'get sources' and changed steps order * update: tests jobs remake * update: 'get sources' step and windows shell * update: using ctest * search for exe files * changed path to run tests * update: 'Build' step * update: changed path to deploy qt dependencies * update: dependencies only for tests executables * update: ctest dir * update: include ctest * update: set dir for tests exe * update: qt path * update: serialization test * update: removed api tests from cmake * update: changed tests dir * added ctest to client cmake * update: installing msvc and additional checks * removed mcvs install * update: path to ssh.dll * fixed issue with ssh path * update: removed unneccessary step and line * update: linux job step 'Run tests' * update: linux 'Install dependencies' * update: modified qtest include * update: changed QVERIFY to QVERIFY2 * update: some qverify2 messages * update: linux additional dependencies * update: offscreen for linux tests * update: MacOS tests job * update: Android tests job * update: rewrited env's, qtest include and clear clients in some tests * update: added local vars file for tests * proper path for some vars * some fixes due merge * update: windows tests deploy * python and conan installation to tests jobs * chore: minor fixes after merge with dev * chore: move selfhosted admin tests to separate folder * refactor: some rename * chore: fixes after merge * refactor: moved tests to separate repo * refactor: remove tests from core controller * chore: add more protected getters to core controller * chore: add more protected getters * chore: remove ctest * chore: return xray model default values --------- Co-authored-by: vkamn <vk@amnezia.org>
190 lines
5.2 KiB
C++
190 lines
5.2 KiB
C++
#include "apiV2ServerConfig.h"
|
|
|
|
#include <QJsonArray>
|
|
#include <QJsonDocument>
|
|
|
|
#include "core/utils/containerEnum.h"
|
|
#include "core/utils/containers/containerUtils.h"
|
|
#include "core/utils/protocolEnum.h"
|
|
#include "core/utils/protocolEnum.h"
|
|
#include "core/protocols/protocolUtils.h"
|
|
#include "core/utils/constants/configKeys.h"
|
|
#include "core/utils/constants/protocolConstants.h"
|
|
#include "core/utils/api/apiUtils.h"
|
|
#include "core/models/api/apiConfig.h"
|
|
#include "core/models/api/authData.h"
|
|
#include "core/utils/networkUtilities.h"
|
|
|
|
namespace amnezia
|
|
{
|
|
|
|
using namespace ContainerEnumNS;
|
|
|
|
QString ApiV2ServerConfig::vpnKey() const
|
|
{
|
|
if (!apiConfig.vpnKey.isEmpty()) {
|
|
return apiConfig.vpnKey;
|
|
}
|
|
|
|
QJsonObject json = toJson();
|
|
return apiUtils::getPremiumV2VpnKey(json);
|
|
}
|
|
|
|
QString ApiV2ServerConfig::serviceType() const
|
|
{
|
|
return apiConfig.serviceType;
|
|
}
|
|
|
|
QString ApiV2ServerConfig::serviceProtocol() const
|
|
{
|
|
return apiConfig.serviceProtocol;
|
|
}
|
|
|
|
bool ApiV2ServerConfig::isPremium() const
|
|
{
|
|
return apiConfig.isPremium();
|
|
}
|
|
|
|
bool ApiV2ServerConfig::isFree() const
|
|
{
|
|
return apiConfig.isFree();
|
|
}
|
|
|
|
bool ApiV2ServerConfig::isExternalPremium() const
|
|
{
|
|
return apiConfig.isExternalPremium();
|
|
}
|
|
|
|
bool ApiV2ServerConfig::hasContainers() const
|
|
{
|
|
return !containers.isEmpty();
|
|
}
|
|
|
|
ContainerConfig ApiV2ServerConfig::containerConfig(DockerContainer container) const
|
|
{
|
|
if (!containers.contains(container)) {
|
|
return ContainerConfig{};
|
|
}
|
|
return containers.value(container);
|
|
}
|
|
|
|
QPair<QString, QString> ApiV2ServerConfig::getDnsPair(const QString &primaryDns, const QString &secondaryDns) const
|
|
{
|
|
QString d1 = dns1;
|
|
QString d2 = dns2;
|
|
|
|
if (d1.isEmpty() || !NetworkUtilities::checkIPv4Format(d1)) {
|
|
d1 = primaryDns;
|
|
}
|
|
if (d2.isEmpty() || !NetworkUtilities::checkIPv4Format(d2)) {
|
|
d2 = secondaryDns;
|
|
}
|
|
return { d1, d2 };
|
|
}
|
|
|
|
QJsonObject ApiV2ServerConfig::toJson() const
|
|
{
|
|
QJsonObject obj;
|
|
|
|
if (!name.isEmpty()) {
|
|
obj[configKey::name] = name;
|
|
}
|
|
if (nameOverriddenByUser) {
|
|
obj[configKey::nameOverriddenByUser] = true;
|
|
}
|
|
if (!description.isEmpty()) {
|
|
obj[configKey::description] = description;
|
|
}
|
|
|
|
obj[configKey::configVersion] = configVersion;
|
|
|
|
if (!hostName.isEmpty()) {
|
|
obj[configKey::hostName] = hostName;
|
|
}
|
|
|
|
QJsonArray containersArray;
|
|
for (auto it = containers.begin(); it != containers.end(); ++it) {
|
|
QJsonObject containerObj = it.value().toJson();
|
|
containersArray.append(containerObj);
|
|
}
|
|
if (!containersArray.isEmpty()) {
|
|
obj[configKey::containers] = containersArray;
|
|
}
|
|
|
|
if (defaultContainer != DockerContainer::None) {
|
|
obj[configKey::defaultContainer] = ContainerUtils::containerToString(defaultContainer);
|
|
}
|
|
|
|
if (!dns1.isEmpty()) {
|
|
obj[configKey::dns1] = dns1;
|
|
}
|
|
if (!dns2.isEmpty()) {
|
|
obj[configKey::dns2] = dns2;
|
|
}
|
|
|
|
if (crc > 0) {
|
|
obj[configKey::crc] = crc;
|
|
}
|
|
|
|
QJsonObject apiConfigObj = apiConfig.toJson();
|
|
if (!apiConfigObj.isEmpty()) {
|
|
obj[apiDefs::key::apiConfig] = apiConfigObj;
|
|
}
|
|
|
|
QJsonObject authDataObj = authData.toJson();
|
|
if (!authDataObj.isEmpty()) {
|
|
obj[QLatin1String("auth_data")] = authDataObj;
|
|
}
|
|
|
|
return obj;
|
|
}
|
|
|
|
ApiV2ServerConfig ApiV2ServerConfig::fromJson(const QJsonObject& json)
|
|
{
|
|
ApiV2ServerConfig config;
|
|
|
|
config.name = json.value(configKey::name).toString();
|
|
config.nameOverriddenByUser = json.value(configKey::nameOverriddenByUser).toBool(false);
|
|
config.description = json.value(configKey::description).toString();
|
|
config.configVersion = json.value(configKey::configVersion).toInt(2);
|
|
config.hostName = json.value(configKey::hostName).toString();
|
|
|
|
QJsonArray containersArray = json.value(configKey::containers).toArray();
|
|
for (const QJsonValue& val : containersArray) {
|
|
QJsonObject containerObj = val.toObject();
|
|
ContainerConfig containerConfig = ContainerConfig::fromJson(containerObj);
|
|
|
|
QString containerStr = containerObj.value(configKey::container).toString();
|
|
DockerContainer container = ContainerUtils::containerFromString(containerStr);
|
|
|
|
config.containers.insert(container, containerConfig);
|
|
}
|
|
|
|
QString defaultContainerStr = json.value(configKey::defaultContainer).toString();
|
|
config.defaultContainer = ContainerUtils::containerFromString(defaultContainerStr);
|
|
|
|
config.dns1 = json.value(configKey::dns1).toString();
|
|
config.dns2 = json.value(configKey::dns2).toString();
|
|
|
|
config.crc = json.value(configKey::crc).toInt(0);
|
|
|
|
QJsonObject apiConfigObj = json.value(apiDefs::key::apiConfig).toObject();
|
|
if (!apiConfigObj.isEmpty()) {
|
|
config.apiConfig = ApiConfig::fromJson(apiConfigObj);
|
|
}
|
|
|
|
QJsonObject authDataObj = json.value(QLatin1String("auth_data")).toObject();
|
|
if (!authDataObj.isEmpty()) {
|
|
config.authData = AuthData::fromJson(authDataObj);
|
|
}
|
|
|
|
if (config.displayName.isEmpty()) {
|
|
config.displayName = config.name.isEmpty() ? config.description : config.name;
|
|
}
|
|
|
|
return config;
|
|
}
|
|
|
|
} // namespace amnezia
|
|
|