feature: added isAvailable flag support (#1032)

* feature: added isAvailable flag support
* added the option to switch to dev env
This commit is contained in:
Nethius 2024-09-09 16:27:29 +04:00 committed by GitHub
parent 175477d31f
commit 918be16372
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 100 additions and 21 deletions

View file

@ -27,6 +27,9 @@ add_definitions(-DGIT_COMMIT_HASH="${GIT_COMMIT_HASH}")
add_definitions(-DPROD_AGW_PUBLIC_KEY="$ENV{PROD_AGW_PUBLIC_KEY}") add_definitions(-DPROD_AGW_PUBLIC_KEY="$ENV{PROD_AGW_PUBLIC_KEY}")
add_definitions(-DPROD_PROXY_STORAGE_KEY="$ENV{PROD_PROXY_STORAGE_KEY}") add_definitions(-DPROD_PROXY_STORAGE_KEY="$ENV{PROD_PROXY_STORAGE_KEY}")
add_definitions(-DDEV_AGW_PUBLIC_KEY="$ENV{DEV_AGW_PUBLIC_KEY}")
add_definitions(-DDEV_AGW_ENDPOINT="$ENV{DEV_AGW_ENDPOINT}")
if(IOS) if(IOS)
set(PACKAGES ${PACKAGES} Multimedia) set(PACKAGES ${PACKAGES} Multimedia)
endif() endif()

View file

@ -9,8 +9,8 @@
#include "QRsa.h" #include "QRsa.h"
#include "amnezia_application.h" #include "amnezia_application.h"
#include "core/enums/apiEnums.h"
#include "configurators/wireguard_configurator.h" #include "configurators/wireguard_configurator.h"
#include "core/enums/apiEnums.h"
#include "version.h" #include "version.h"
namespace namespace
@ -42,7 +42,7 @@ namespace
constexpr char keyPayload[] = "key_payload"; constexpr char keyPayload[] = "key_payload";
} }
const QStringList proxyStorageUrl = {""}; const QStringList proxyStorageUrl = { "" };
ErrorCode checkErrors(const QList<QSslError> &sslErrors, QNetworkReply *reply) ErrorCode checkErrors(const QList<QSslError> &sslErrors, QNetworkReply *reply)
{ {
@ -65,7 +65,8 @@ namespace
} }
} }
ApiController::ApiController(const QString &gatewayEndpoint, QObject *parent) : QObject(parent), m_gatewayEndpoint(gatewayEndpoint) ApiController::ApiController(const QString &gatewayEndpoint, bool isDevEnvironment, QObject *parent)
: QObject(parent), m_gatewayEndpoint(gatewayEndpoint), m_isDevEnvironment(isDevEnvironment)
{ {
} }
@ -143,7 +144,7 @@ QStringList ApiController::getProxyUrls()
QEventLoop wait; QEventLoop wait;
QList<QSslError> sslErrors; QList<QSslError> sslErrors;
QNetworkReply* reply; QNetworkReply *reply;
for (const auto &proxyStorageUrl : proxyStorageUrl) { for (const auto &proxyStorageUrl : proxyStorageUrl) {
request.setUrl(proxyStorageUrl); request.setUrl(proxyStorageUrl);
@ -281,7 +282,7 @@ ErrorCode ApiController::getServicesList(QByteArray &responseBody)
request.setUrl(QString("%1v1/services").arg(m_gatewayEndpoint)); request.setUrl(QString("%1v1/services").arg(m_gatewayEndpoint));
QNetworkReply* reply; QNetworkReply *reply;
reply = amnApp->manager()->get(request); reply = amnApp->manager()->get(request);
QEventLoop wait; QEventLoop wait;
@ -300,7 +301,8 @@ ErrorCode ApiController::getServicesList(QByteArray &responseBody)
QObject::connect(reply, &QNetworkReply::finished, &wait, &QEventLoop::quit); QObject::connect(reply, &QNetworkReply::finished, &wait, &QEventLoop::quit);
connect(reply, &QNetworkReply::sslErrors, [this, &sslErrors](const QList<QSslError> &errors) { sslErrors = errors; }); connect(reply, &QNetworkReply::sslErrors, [this, &sslErrors](const QList<QSslError> &errors) { sslErrors = errors; });
wait.exec(); wait.exec();
if (reply->error() != QNetworkReply::NetworkError::TimeoutError && reply->error() != QNetworkReply::NetworkError::OperationCanceledError) { if (reply->error() != QNetworkReply::NetworkError::TimeoutError
&& reply->error() != QNetworkReply::NetworkError::OperationCanceledError) {
break; break;
} }
reply->deleteLater(); reply->deleteLater();
@ -355,7 +357,7 @@ ErrorCode ApiController::getConfigForService(const QString &installationUuid, co
EVP_PKEY *publicKey = nullptr; EVP_PKEY *publicKey = nullptr;
try { try {
QByteArray key = PROD_AGW_PUBLIC_KEY; QByteArray key = m_isDevEnvironment ? DEV_AGW_PUBLIC_KEY : PROD_AGW_PUBLIC_KEY;
QSimpleCrypto::QRsa rsa; QSimpleCrypto::QRsa rsa;
publicKey = rsa.getPublicKeyFromByteArray(key); publicKey = rsa.getPublicKeyFromByteArray(key);
} catch (...) { } catch (...) {
@ -375,7 +377,7 @@ ErrorCode ApiController::getConfigForService(const QString &installationUuid, co
requestBody[configKey::keyPayload] = QString(encryptedKeyPayload.toBase64()); requestBody[configKey::keyPayload] = QString(encryptedKeyPayload.toBase64());
requestBody[configKey::apiPayload] = QString(encryptedApiPayload.toBase64()); requestBody[configKey::apiPayload] = QString(encryptedApiPayload.toBase64());
QNetworkReply* reply = manager.post(request, QJsonDocument(requestBody).toJson()); QNetworkReply *reply = manager.post(request, QJsonDocument(requestBody).toJson());
QEventLoop wait; QEventLoop wait;
connect(reply, &QNetworkReply::finished, &wait, &QEventLoop::quit); connect(reply, &QNetworkReply::finished, &wait, &QEventLoop::quit);
@ -395,7 +397,8 @@ ErrorCode ApiController::getConfigForService(const QString &installationUuid, co
QObject::connect(reply, &QNetworkReply::finished, &wait, &QEventLoop::quit); QObject::connect(reply, &QNetworkReply::finished, &wait, &QEventLoop::quit);
connect(reply, &QNetworkReply::sslErrors, [this, &sslErrors](const QList<QSslError> &errors) { sslErrors = errors; }); connect(reply, &QNetworkReply::sslErrors, [this, &sslErrors](const QList<QSslError> &errors) { sslErrors = errors; });
wait.exec(); wait.exec();
if (reply->error() != QNetworkReply::NetworkError::TimeoutError && reply->error() != QNetworkReply::NetworkError::OperationCanceledError) { if (reply->error() != QNetworkReply::NetworkError::TimeoutError
&& reply->error() != QNetworkReply::NetworkError::OperationCanceledError) {
break; break;
} }
reply->deleteLater(); reply->deleteLater();

View file

@ -14,7 +14,7 @@ class ApiController : public QObject
Q_OBJECT Q_OBJECT
public: public:
explicit ApiController(const QString &gatewayEndpoint, QObject *parent = nullptr); explicit ApiController(const QString &gatewayEndpoint, bool isDevEnvironment, QObject *parent = nullptr);
public slots: public slots:
void updateServerConfigFromApi(const QString &installationUuid, const int serverIndex, QJsonObject serverConfig); void updateServerConfigFromApi(const QString &installationUuid, const int serverIndex, QJsonObject serverConfig);
@ -44,6 +44,7 @@ private:
QString m_gatewayEndpoint; QString m_gatewayEndpoint;
QStringList m_proxyUrls; QStringList m_proxyUrls;
bool m_isDevEnvironment;
}; };
#endif // APICONTROLLER_H #endif // APICONTROLLER_H

View file

@ -519,7 +519,22 @@ void Settings::setGatewayEndpoint(const QString &endpoint)
m_gatewayEndpoint = endpoint; m_gatewayEndpoint = endpoint;
} }
void Settings::setDevGatewayEndpoint()
{
m_gatewayEndpoint = DEV_AGW_ENDPOINT;
}
QString Settings::getGatewayEndpoint() QString Settings::getGatewayEndpoint()
{ {
return m_gatewayEndpoint; return m_gatewayEndpoint;
} }
bool Settings::isDevGatewayEnv()
{
return m_isDevGatewayEnv;
}
void Settings::toggleDevGatewayEnv(bool enabled)
{
m_isDevGatewayEnv = enabled;
}

View file

@ -217,7 +217,10 @@ public:
void resetGatewayEndpoint(); void resetGatewayEndpoint();
void setGatewayEndpoint(const QString &endpoint); void setGatewayEndpoint(const QString &endpoint);
void setDevGatewayEndpoint();
QString getGatewayEndpoint(); QString getGatewayEndpoint();
bool isDevGatewayEnv();
void toggleDevGatewayEnv(bool enabled);
signals: signals:
void saveLogsChanged(bool enabled); void saveLogsChanged(bool enabled);
@ -234,6 +237,7 @@ private:
mutable SecureQSettings m_settings; mutable SecureQSettings m_settings;
QString m_gatewayEndpoint; QString m_gatewayEndpoint;
bool m_isDevGatewayEnv;
}; };
#endif // SETTINGS_H #endif // SETTINGS_H

View file

@ -799,7 +799,7 @@ void InstallController::addEmptyServer()
bool InstallController::fillAvailableServices() bool InstallController::fillAvailableServices()
{ {
ApiController apiController(m_settings->getGatewayEndpoint()); ApiController apiController(m_settings->getGatewayEndpoint(), m_settings->isDevGatewayEnv());
QByteArray responseBody; QByteArray responseBody;
ErrorCode errorCode = apiController.getServicesList(responseBody); ErrorCode errorCode = apiController.getServicesList(responseBody);
@ -821,7 +821,7 @@ bool InstallController::installServiceFromApi()
return false; return false;
} }
ApiController apiController(m_settings->getGatewayEndpoint()); ApiController apiController(m_settings->getGatewayEndpoint(), m_settings->isDevGatewayEnv());
QJsonObject serverConfig; QJsonObject serverConfig;
ErrorCode errorCode = apiController.getConfigForService(m_settings->getInstallationUuid(true), m_apiServicesModel->getCountryCode(), ErrorCode errorCode = apiController.getConfigForService(m_settings->getInstallationUuid(true), m_apiServicesModel->getCountryCode(),
@ -849,7 +849,7 @@ bool InstallController::installServiceFromApi()
bool InstallController::updateServiceFromApi(const int serverIndex, const QString &newCountryCode, const QString &newCountryName, bool InstallController::updateServiceFromApi(const int serverIndex, const QString &newCountryCode, const QString &newCountryName,
bool reloadServiceConfig) bool reloadServiceConfig)
{ {
ApiController apiController(m_settings->getGatewayEndpoint()); ApiController apiController(m_settings->getGatewayEndpoint(), m_settings->isDevGatewayEnv());
auto serverConfig = m_serversModel->getServerConfig(serverIndex); auto serverConfig = m_serversModel->getServerConfig(serverIndex);
auto apiConfig = serverConfig.value(configKey::apiConfig).toObject(); auto apiConfig = serverConfig.value(configKey::apiConfig).toObject();
@ -885,7 +885,7 @@ bool InstallController::updateServiceFromApi(const int serverIndex, const QStrin
void InstallController::updateServiceFromTelegram(const int serverIndex) void InstallController::updateServiceFromTelegram(const int serverIndex)
{ {
ApiController *apiController = new ApiController(m_settings->getGatewayEndpoint()); ApiController *apiController = new ApiController(m_settings->getGatewayEndpoint(), m_settings->isDevGatewayEnv());
auto serverConfig = m_serversModel->getServerConfig(serverIndex); auto serverConfig = m_serversModel->getServerConfig(serverIndex);

View file

@ -283,7 +283,24 @@ void SettingsController::setGatewayEndpoint(const QString &endpoint)
QString SettingsController::getGatewayEndpoint() QString SettingsController::getGatewayEndpoint()
{ {
return m_settings->getGatewayEndpoint(); return m_settings->isDevGatewayEnv() ? "Dev endpoint" : m_settings->getGatewayEndpoint();
}
bool SettingsController::isDevGatewayEnv()
{
return m_settings->isDevGatewayEnv();
}
void SettingsController::toggleDevGatewayEnv(bool enabled)
{
m_settings->toggleDevGatewayEnv(enabled);
if (enabled) {
m_settings->setDevGatewayEndpoint();
} else {
m_settings->resetGatewayEndpoint();
}
emit gatewayEndpointChanged(m_settings->getGatewayEndpoint());
emit devGatewayEnvChanged(enabled);
} }
bool SettingsController::isOnTv() bool SettingsController::isOnTv()

View file

@ -27,6 +27,7 @@ public:
Q_PROPERTY(bool isDevModeEnabled READ isDevModeEnabled NOTIFY devModeEnabled) Q_PROPERTY(bool isDevModeEnabled READ isDevModeEnabled NOTIFY devModeEnabled)
Q_PROPERTY(QString gatewayEndpoint READ getGatewayEndpoint WRITE setGatewayEndpoint NOTIFY gatewayEndpointChanged) Q_PROPERTY(QString gatewayEndpoint READ getGatewayEndpoint WRITE setGatewayEndpoint NOTIFY gatewayEndpointChanged)
Q_PROPERTY(bool isDevGatewayEnv READ isDevGatewayEnv WRITE toggleDevGatewayEnv NOTIFY devGatewayEnvChanged)
public slots: public slots:
void toggleAmneziaDns(bool enable); void toggleAmneziaDns(bool enable);
@ -81,6 +82,8 @@ public slots:
void resetGatewayEndpoint(); void resetGatewayEndpoint();
void setGatewayEndpoint(const QString &endpoint); void setGatewayEndpoint(const QString &endpoint);
QString getGatewayEndpoint(); QString getGatewayEndpoint();
bool isDevGatewayEnv();
void toggleDevGatewayEnv(bool enabled);
bool isOnTv(); bool isOnTv();
@ -105,6 +108,7 @@ signals:
void devModeEnabled(); void devModeEnabled();
void gatewayEndpointChanged(const QString &endpoint); void gatewayEndpointChanged(const QString &endpoint);
void devGatewayEnvChanged(bool enabled);
private: private:
QSharedPointer<ServersModel> m_serversModel; QSharedPointer<ServersModel> m_serversModel;

View file

@ -25,6 +25,8 @@ namespace
constexpr char availableCountries[] = "available_countries"; constexpr char availableCountries[] = "available_countries";
constexpr char storeEndpoint[] = "store_endpoint"; constexpr char storeEndpoint[] = "store_endpoint";
constexpr char isAvailable[] = "is_available";
} }
namespace serviceType namespace serviceType
@ -63,8 +65,12 @@ QVariant ApiServicesModel::data(const QModelIndex &index, int role) const
return tr("Classic VPN for comfortable work, downloading large files and watching videos. " return tr("Classic VPN for comfortable work, downloading large files and watching videos. "
"Works for any sites. Speed up to %1 MBit/s") "Works for any sites. Speed up to %1 MBit/s")
.arg(speed); .arg(speed);
} else { } else if (serviceType == serviceType::amneziaFree){
return tr("VPN to access blocked sites in regions with high levels of Internet censorship. "); QString description = tr("VPN to access blocked sites in regions with high levels of Internet censorship. ");
if (service.value(configKey::isAvailable).isBool() && !service.value(configKey::isAvailable).toBool()) {
description += tr("<p><a style=\"color: #EB5757;\">Not available in your region. If you have VPN enabled, disable it, return to the previous screen, and try again.</a>");
}
return description;
} }
} }
case ServiceDescriptionRole: { case ServiceDescriptionRole: {
@ -75,6 +81,14 @@ QVariant ApiServicesModel::data(const QModelIndex &index, int role) const
return tr("Amnezia Free is a free VPN to bypass blocking in countries with high levels of internet censorship"); return tr("Amnezia Free is a free VPN to bypass blocking in countries with high levels of internet censorship");
} }
} }
case IsServiceAvailableRole: {
if (serviceType == serviceType::amneziaFree) {
if (service.value(configKey::isAvailable).isBool() && !service.value(configKey::isAvailable).toBool()) {
return false;
}
}
return true;
}
case SpeedRole: { case SpeedRole: {
auto speed = serviceInfo.value(configKey::speed).toString(); auto speed = serviceInfo.value(configKey::speed).toString();
return tr("%1 MBit/s").arg(speed); return tr("%1 MBit/s").arg(speed);
@ -193,6 +207,7 @@ QHash<int, QByteArray> ApiServicesModel::roleNames() const
roles[NameRole] = "name"; roles[NameRole] = "name";
roles[CardDescriptionRole] = "cardDescription"; roles[CardDescriptionRole] = "cardDescription";
roles[ServiceDescriptionRole] = "serviceDescription"; roles[ServiceDescriptionRole] = "serviceDescription";
roles[IsServiceAvailableRole] = "isServiceAvailable";
roles[SpeedRole] = "speed"; roles[SpeedRole] = "speed";
roles[WorkPeriodRole] = "workPeriod"; roles[WorkPeriodRole] = "workPeriod";
roles[RegionRole] = "region"; roles[RegionRole] = "region";

View file

@ -13,6 +13,7 @@ public:
NameRole = Qt::UserRole + 1, NameRole = Qt::UserRole + 1,
CardDescriptionRole, CardDescriptionRole,
ServiceDescriptionRole, ServiceDescriptionRole,
IsServiceAvailableRole,
SpeedRole, SpeedRole,
WorkPeriodRole, WorkPeriodRole,
RegionRole, RegionRole,

View file

@ -79,6 +79,7 @@ Button {
visible: text !== "" visible: text !== ""
color: AmneziaStyle.color.mutedGray color: AmneziaStyle.color.mutedGray
textFormat: Text.RichText
Layout.fillWidth: true Layout.fillWidth: true
Layout.rightMargin: 16 Layout.rightMargin: 16

View file

@ -89,6 +89,21 @@ PageType {
// KeyNavigation.tab: saveButton // KeyNavigation.tab: saveButton
} }
SwitcherType {
id: switcher
Layout.fillWidth: true
Layout.rightMargin: 16
Layout.leftMargin: 16
Layout.topMargin: 16
text: qsTr("Dev gateway environment")
checked: SettingsController.isDevGatewayEnv
onToggled: function() {
SettingsController.isDevGatewayEnv = checked
}
}
} }
} }
} }

View file

@ -88,8 +88,10 @@ PageType {
rightImageSource: "qrc:/images/controls/chevron-right.svg" rightImageSource: "qrc:/images/controls/chevron-right.svg"
onClicked: { onClicked: {
ApiServicesModel.setServiceIndex(index) if (isServiceAvailable) {
PageController.goToPage(PageEnum.PageSetupWizardApiServiceInfo) ApiServicesModel.setServiceIndex(index)
PageController.goToPage(PageEnum.PageSetupWizardApiServiceInfo)
}
} }
} }
} }

View file

@ -119,8 +119,6 @@ PageType {
CardWithIconsType { CardWithIconsType {
id: apiInstalling id: apiInstalling
visible: false
Layout.fillWidth: true Layout.fillWidth: true
Layout.rightMargin: 16 Layout.rightMargin: 16
Layout.leftMargin: 16 Layout.leftMargin: 16