diff --git a/client/core/api/apiUtils.cpp b/client/core/api/apiUtils.cpp index 7c58e0e1..088fdba1 100644 --- a/client/core/api/apiUtils.cpp +++ b/client/core/api/apiUtils.cpp @@ -49,3 +49,32 @@ apiDefs::ConfigSource apiUtils::getConfigSource(const QJsonObject &serverConfigO { return static_cast(serverConfigObject.value(apiDefs::key::configVersion).toInt()); } + +amnezia::ErrorCode apiUtils::checkNetworkReplyErrors(const QList &sslErrors, QNetworkReply *reply) +{ + const int httpStatusCodeConflict = 409; + + if (!sslErrors.empty()) { + qDebug().noquote() << sslErrors; + return amnezia::ErrorCode::ApiConfigSslError; + } else if (reply->error() == QNetworkReply::NoError) { + return amnezia::ErrorCode::NoError; + } else if (reply->error() == QNetworkReply::NetworkError::OperationCanceledError + || reply->error() == QNetworkReply::NetworkError::TimeoutError) { + return amnezia::ErrorCode::ApiConfigTimeoutError; + } else { + QString err = reply->errorString(); + int httpStatusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + qDebug() << QString::fromUtf8(reply->readAll()); + qDebug() << reply->error(); + qDebug() << err; + qDebug() << httpStatusCode; + if (httpStatusCode == httpStatusCodeConflict) { + return amnezia::ErrorCode::ApiConfigLimitError; + } + return amnezia::ErrorCode::ApiConfigDownloadError; + } + + qDebug() << "something went wrong"; + return amnezia::ErrorCode::InternalError; +} diff --git a/client/core/api/apiUtils.h b/client/core/api/apiUtils.h index bb122736..82ac315b 100644 --- a/client/core/api/apiUtils.h +++ b/client/core/api/apiUtils.h @@ -1,9 +1,11 @@ #ifndef APIUTILS_H #define APIUTILS_H +#include #include #include "apiDefs.h" +#include "core/defs.h" namespace apiUtils { @@ -13,6 +15,8 @@ namespace apiUtils apiDefs::ConfigType getConfigType(const QJsonObject &serverConfigObject); apiDefs::ConfigSource getConfigSource(const QJsonObject &serverConfigObject); + + amnezia::ErrorCode checkNetworkReplyErrors(const QList &sslErrors, QNetworkReply *reply); } #endif // APIUTILS_H diff --git a/client/core/controllers/gatewayController.cpp b/client/core/controllers/gatewayController.cpp index 44a3d5d1..268561ae 100644 --- a/client/core/controllers/gatewayController.cpp +++ b/client/core/controllers/gatewayController.cpp @@ -9,7 +9,7 @@ #include "QRsa.h" #include "amnezia_application.h" -#include "core/networkUtilities.h" +#include "core/api/apiUtils.h" #include "utilities.h" namespace @@ -75,7 +75,7 @@ ErrorCode GatewayController::get(const QString &endpoint, QByteArray &responseBo bypassProxy(endpoint, reply, requestFunction, replyProcessingFunction); } - auto errorCode = NetworkUtilities::checkNetworkReplyErrors(sslErrors, reply); + auto errorCode = apiUtils::checkNetworkReplyErrors(sslErrors, reply); reply->deleteLater(); return errorCode; @@ -165,7 +165,7 @@ ErrorCode GatewayController::post(const QString &endpoint, const QJsonObject api bypassProxy(endpoint, reply, requestFunction, replyProcessingFunction); } - auto errorCode = NetworkUtilities::checkNetworkReplyErrors(sslErrors, reply); + auto errorCode = apiUtils::checkNetworkReplyErrors(sslErrors, reply); reply->deleteLater(); if (errorCode) { return errorCode; diff --git a/client/core/defs.h b/client/core/defs.h index c7fde0ee..330ebc48 100644 --- a/client/core/defs.h +++ b/client/core/defs.h @@ -109,6 +109,7 @@ namespace amnezia ApiMissingAgwPublicKey = 1105, ApiConfigDecryptionError = 1106, ApiServicesMissingError = 1107, + ApiConfigLimitError = 1108, // QFile errors OpenError = 1200, diff --git a/client/core/errorstrings.cpp b/client/core/errorstrings.cpp index 976915c2..c45b1eaf 100644 --- a/client/core/errorstrings.cpp +++ b/client/core/errorstrings.cpp @@ -66,6 +66,7 @@ QString errorString(ErrorCode code) { case (ErrorCode::ApiMissingAgwPublicKey): errorMessage = QObject::tr("Missing AGW public key"); break; 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; // QFile errors case(ErrorCode::OpenError): errorMessage = QObject::tr("QFile error: The file could not be opened"); break; diff --git a/client/core/networkUtilities.cpp b/client/core/networkUtilities.cpp index 7d98e6a1..a5825f0d 100644 --- a/client/core/networkUtilities.cpp +++ b/client/core/networkUtilities.cpp @@ -107,26 +107,6 @@ QStringList NetworkUtilities::summarizeRoutes(const QStringList &ips, const QStr return QStringList(); } -amnezia::ErrorCode NetworkUtilities::checkNetworkReplyErrors(const QList &sslErrors, QNetworkReply *reply) -{ - if (!sslErrors.empty()) { - qDebug().noquote() << sslErrors; - return amnezia::ErrorCode::ApiConfigSslError; - } else if (reply->error() == QNetworkReply::NoError) { - return amnezia::ErrorCode::NoError; - } else if (reply->error() == QNetworkReply::NetworkError::OperationCanceledError - || reply->error() == QNetworkReply::NetworkError::TimeoutError) { - return amnezia::ErrorCode::ApiConfigTimeoutError; - } else { - QString err = reply->errorString(); - qDebug() << QString::fromUtf8(reply->readAll()); - qDebug() << reply->error(); - qDebug() << err; - qDebug() << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute); - return amnezia::ErrorCode::ApiConfigDownloadError; - } -} - QString NetworkUtilities::getIPAddress(const QString &host) { QHostAddress address(host); diff --git a/client/core/networkUtilities.h b/client/core/networkUtilities.h index 805ce9e5..3b64b547 100644 --- a/client/core/networkUtilities.h +++ b/client/core/networkUtilities.h @@ -7,8 +7,6 @@ #include #include -#include "core/defs.h" - class NetworkUtilities : public QObject { @@ -33,9 +31,6 @@ public: static QString ipAddressFromIpWithSubnet(const QString ip); static QStringList summarizeRoutes(const QStringList &ips, const QString cidr); - - static amnezia::ErrorCode checkNetworkReplyErrors(const QList &sslErrors, QNetworkReply *reply); - }; #endif // NETWORKUTILITIES_H diff --git a/client/ui/controllers/api/apiConfigsController.cpp b/client/ui/controllers/api/apiConfigsController.cpp index ee133253..7085d9a2 100644 --- a/client/ui/controllers/api/apiConfigsController.cpp +++ b/client/ui/controllers/api/apiConfigsController.cpp @@ -269,7 +269,7 @@ bool ApiConfigsController::updateServiceFromTelegram(const int serverIndex) connect(reply, &QNetworkReply::sslErrors, [this, &sslErrors](const QList &errors) { sslErrors = errors; }); wait.exec(); - auto errorCode = NetworkUtilities::checkNetworkReplyErrors(sslErrors, reply); + auto errorCode = apiUtils::checkNetworkReplyErrors(sslErrors, reply); if (errorCode != ErrorCode::NoError) { reply->deleteLater(); emit errorOccurred(errorCode);