diff --git a/client/core/defs.h b/client/core/defs.h index a441ee1c3..d87ce86e4 100644 --- a/client/core/defs.h +++ b/client/core/defs.h @@ -96,6 +96,7 @@ namespace amnezia // import and install errors ImportInvalidConfigError = 900, + ImportQrDecodingError = 901, // Android errors AndroidError = 1000, diff --git a/client/core/errorstrings.cpp b/client/core/errorstrings.cpp index 645ec6c5a..47b481268 100644 --- a/client/core/errorstrings.cpp +++ b/client/core/errorstrings.cpp @@ -50,6 +50,7 @@ QString errorString(ErrorCode code) { case (ErrorCode::AddressPoolError): errorMessage = QObject::tr("VPN pool error: no available addresses"); break; case (ErrorCode::ImportInvalidConfigError): errorMessage = QObject::tr("The config does not contain any containers and credentials for connecting to the server"); break; + case (ErrorCode::ImportQrDecodingError): errorMessage = QObject::tr("Failed to decode qr-code"); break; // Android errors case (ErrorCode::AndroidError): errorMessage = QObject::tr("VPN connection error"); break; diff --git a/client/ui/controllers/importController.cpp b/client/ui/controllers/importController.cpp index 60a90e4e9..530eb68c4 100644 --- a/client/ui/controllers/importController.cpp +++ b/client/ui/controllers/importController.cpp @@ -197,19 +197,7 @@ bool ImportController::extractConfigFromData(QString data) bool ImportController::extractConfigFromQr(const QByteArray &data) { - QJsonObject dataObj = QJsonDocument::fromJson(data).object(); - if (!dataObj.isEmpty()) { - m_config = dataObj; - return true; - } - - QByteArray ba_uncompressed = qUncompress(data); - if (!ba_uncompressed.isEmpty()) { - m_config = QJsonDocument::fromJson(ba_uncompressed).object(); - return true; - } - - return false; + return extractConfigFromData(data.toBase64(QByteArray::Base64UrlEncoding | QByteArray::OmitTrailingEquals)); } QString ImportController::getConfig() @@ -533,8 +521,12 @@ void ImportController::startDecodingQr() #endif } -void ImportController::stopDecodingQr() +void ImportController::stopDecodingQr(const QByteArray &data) { + if (!extractConfigFromQr(data)) { + emit qrDecodingError(ErrorCode::ImportQrDecodingError); + return; + } emit qrDecodingFinished(); } @@ -571,25 +563,27 @@ bool ImportController::parseQrCodeChunk(const QString &code) data.append(m_qrCodeChunks.value(i)); } - bool ok = extractConfigFromQr(data); - if (ok) { - m_isQrCodeProcessed = false; - qDebug() << "stopDecodingQr"; - stopDecodingQr(); - return true; - } else { + data = qUncompress(data); + auto format = checkConfigFormat(data); + if (format == ConfigTypes::Invalid) { qDebug() << "error while extracting data from qr"; m_qrCodeChunks.clear(); m_totalQrCodeChunksCount = 0; m_receivedQrCodeChunksCount = 0; + } else { + qDebug() << "stopDecodingQr"; + m_isQrCodeProcessed = false; + stopDecodingQr(data); + return true; } } } else { - bool ok = extractConfigFromQr(ba); - if (ok) { - m_isQrCodeProcessed = false; + auto data = code.toUtf8(); + auto format = checkConfigFormat(data); + if (format != ConfigTypes::Invalid) { qDebug() << "stopDecodingQr"; - stopDecodingQr(); + m_isQrCodeProcessed = false; + stopDecodingQr(data); return true; } } diff --git a/client/ui/controllers/importController.h b/client/ui/controllers/importController.h index ea1ba6b04..e7034e655 100644 --- a/client/ui/controllers/importController.h +++ b/client/ui/controllers/importController.h @@ -61,6 +61,10 @@ public slots: void restoreAppConfig(const QByteArray &data); +#if defined Q_OS_ANDROID || defined Q_OS_IOS + void qrDecodingError(ErrorCode errorCode); +#endif + private: QJsonObject extractOpenVpnConfig(const QString &data); QJsonObject extractWireGuardConfig(const QString &data); @@ -69,7 +73,7 @@ public slots: void checkForMaliciousStrings(const QJsonObject &protocolConfig); #if defined Q_OS_ANDROID || defined Q_OS_IOS - void stopDecodingQr(); + void stopDecodingQr(const QByteArray &data); #endif QSharedPointer m_serversModel; diff --git a/client/ui/qml/Pages2/PageSetupWizardConfigSource.qml b/client/ui/qml/Pages2/PageSetupWizardConfigSource.qml index f7b8949b8..bd694060d 100644 --- a/client/ui/qml/Pages2/PageSetupWizardConfigSource.qml +++ b/client/ui/qml/Pages2/PageSetupWizardConfigSource.qml @@ -20,6 +20,10 @@ PageType { PageController.closePage() PageController.goToPage(PageEnum.PageSetupWizardViewConfig) } + + function onQrDecodingError() { + PageController.closePage() + } } defaultActiveFocusItem: focusItem