Skip to content

Commit

Permalink
chore: added 404 handling for revoke configs
Browse files Browse the repository at this point in the history
- added revoke before remove api server for premium v2
  • Loading branch information
Nethius committed Feb 23, 2025
1 parent d19017f commit 19fcddf
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
3 changes: 3 additions & 0 deletions client/core/api/apiUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ apiDefs::ConfigSource apiUtils::getConfigSource(const QJsonObject &serverConfigO
amnezia::ErrorCode apiUtils::checkNetworkReplyErrors(const QList<QSslError> &sslErrors, QNetworkReply *reply)
{
const int httpStatusCodeConflict = 409;
const int httpStatusCodeNotFound = 404;

if (!sslErrors.empty()) {
qDebug().noquote() << sslErrors;
Expand All @@ -75,6 +76,8 @@ amnezia::ErrorCode apiUtils::checkNetworkReplyErrors(const QList<QSslError> &ssl
qDebug() << httpStatusCode;
if (httpStatusCode == httpStatusCodeConflict) {
return amnezia::ErrorCode::ApiConfigLimitError;
} else if (httpStatusCode == httpStatusCodeNotFound) {
return amnezia::ErrorCode::ApiNotFoundError;
}
return amnezia::ErrorCode::ApiConfigDownloadError;
}
Expand Down
1 change: 1 addition & 0 deletions client/core/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ namespace amnezia
ApiConfigDecryptionError = 1106,
ApiServicesMissingError = 1107,
ApiConfigLimitError = 1108,
ApiNotFoundError = 1109,

// QFile errors
OpenError = 1200,
Expand Down
1 change: 1 addition & 0 deletions client/core/errorstrings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ QString errorString(ErrorCode code) {
case (ErrorCode::ApiConfigDecryptionError): errorMessage = QObject::tr("Failed to decrypt response payload"); break;
case (ErrorCode::ApiServicesMissingError): errorMessage = QObject::tr("Missing list of available services"); break;
case (ErrorCode::ApiConfigLimitError): errorMessage = QObject::tr("The limit of allowed configurations per subscription has been exceeded"); break;
case (ErrorCode::ApiNotFoundError): errorMessage = QObject::tr("Error when retrieving configuration from API"); break;

// QFile errors
case(ErrorCode::OpenError): errorMessage = QObject::tr("QFile error: The file could not be opened"); break;
Expand Down
15 changes: 12 additions & 3 deletions client/ui/controllers/api/apiConfigsController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ bool ApiConfigsController::revokeNativeConfig(const QString &serverCountryCode)

QByteArray responseBody;
ErrorCode errorCode = gatewayController.post(QString("%1v1/revoke_native_config"), apiPayload, responseBody);
if (errorCode != ErrorCode::NoError) {
if (errorCode != ErrorCode::NoError && errorCode != ErrorCode::ApiNotFoundError) {
emit errorOccurred(errorCode);
return false;
}
Expand Down Expand Up @@ -323,9 +323,14 @@ bool ApiConfigsController::deactivateDevice()
{
GatewayController gatewayController(m_settings->getGatewayEndpoint(), m_settings->isDevGatewayEnv(), apiDefs::requestTimeoutMsecs);

auto serverConfigObject = m_serversModel->getServerConfig(m_serversModel->getProcessedServerIndex());
auto serverIndex = m_serversModel->getProcessedServerIndex();
auto serverConfigObject = m_serversModel->getServerConfig(serverIndex);
auto apiConfigObject = serverConfigObject.value(configKey::apiConfig).toObject();

if (apiUtils::getConfigType(serverConfigObject) != apiDefs::ConfigType::AmneziaPremiumV2) {
return true;
}

QString protocol = apiConfigObject.value(configKey::serviceProtocol).toString();
ApiPayloadData apiPayloadData = generateApiPayloadData(protocol);

Expand All @@ -338,10 +343,14 @@ bool ApiConfigsController::deactivateDevice()

QByteArray responseBody;
ErrorCode errorCode = gatewayController.post(QString("%1v1/revoke_config"), apiPayload, responseBody);
if (errorCode != ErrorCode::NoError) {
if (errorCode != ErrorCode::NoError && errorCode != ErrorCode::ApiNotFoundError) {
emit errorOccurred(errorCode);
return false;
}

serverConfigObject.remove(config_key::containers);
m_serversModel->editServer(serverConfigObject, serverIndex);

return true;
}

Expand Down
9 changes: 6 additions & 3 deletions client/ui/qml/Pages2/PageSettingsApiServerInfo.qml
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ PageType {
Layout.fillWidth: true
Layout.topMargin: 32

visible: footer.isVisibleForAmneziaFree
visible: false //footer.isVisibleForAmneziaFree

text: qsTr("Subscription key")
rightImageSource: "qrc:/images/controls/chevron-right.svg"
Expand All @@ -187,11 +187,12 @@ PageType {
}

DividerType {
visible: footer.isVisibleForAmneziaFree
visible: false //footer.isVisibleForAmneziaFree
}

LabelWithButtonType {
Layout.fillWidth: true
Layout.topMargin: 32

visible: footer.isVisibleForAmneziaFree

Expand Down Expand Up @@ -336,7 +337,9 @@ PageType {
PageController.showNotificationMessage(qsTr("Cannot remove server during active connection"))
} else {
PageController.showBusyIndicator(true)
InstallController.removeProcessedServer()
if (ApiConfigsController.deactivateDevice()) {
InstallController.removeProcessedServer()
}
PageController.showBusyIndicator(false)
}
}
Expand Down

0 comments on commit 19fcddf

Please sign in to comment.