From a14e8ec0aa68e4ebef4e523a6c7446ed0c30fd26 Mon Sep 17 00:00:00 2001 From: "vladimir.kuznetsov" Date: Wed, 15 May 2024 23:02:21 +0200 Subject: [PATCH 1/3] added processing of native qr codes for wg/awg --- client/ui/controllers/importController.cpp | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/client/ui/controllers/importController.cpp b/client/ui/controllers/importController.cpp index 8d1ff53e9..cbc8d1b9b 100644 --- a/client/ui/controllers/importController.cpp +++ b/client/ui/controllers/importController.cpp @@ -144,19 +144,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() @@ -519,7 +507,7 @@ bool ImportController::parseQrCodeChunk(const QString &code) } } } else { - bool ok = extractConfigFromQr(ba); + bool ok = extractConfigFromQr(code.toUtf8()); if (ok) { m_isQrCodeProcessed = false; qDebug() << "stopDecodingQr"; From ae5ac166118dd02716a0ef64b5e22bb7817f4a34 Mon Sep 17 00:00:00 2001 From: "vladimir.kuznetsov" Date: Fri, 5 Jul 2024 14:21:25 +0200 Subject: [PATCH 2/3] stopDecodingQr before extract config --- client/ui/controllers/importController.cpp | 23 ++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/client/ui/controllers/importController.cpp b/client/ui/controllers/importController.cpp index 616849de0..f2cabbd92 100644 --- a/client/ui/controllers/importController.cpp +++ b/client/ui/controllers/importController.cpp @@ -559,25 +559,28 @@ 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 { + 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(); + extractConfigFromQr(data); + return true; } } } else { - bool ok = extractConfigFromQr(code.toUtf8()); - if (ok) { - m_isQrCodeProcessed = false; + auto data = code.toUtf8(); + auto format = checkConfigFormat(data); + if (format != ConfigTypes::Invalid) { qDebug() << "stopDecodingQr"; + m_isQrCodeProcessed = false; stopDecodingQr(); + extractConfigFromQr(data); return true; } } From 54e69c4fe4c462be452a7ef8ddd3b5520913e1ab Mon Sep 17 00:00:00 2001 From: "vladimir.kuznetsov" Date: Mon, 8 Jul 2024 21:58:12 +0200 Subject: [PATCH 3/3] now extract config after stop qr decoding --- client/core/defs.h | 1 + client/core/errorstrings.cpp | 1 + client/ui/controllers/importController.cpp | 13 ++++++++----- client/ui/controllers/importController.h | 6 +++++- .../ui/qml/Pages2/PageSetupWizardConfigSource.qml | 4 ++++ 5 files changed, 19 insertions(+), 6 deletions(-) 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 f2cabbd92..530eb68c4 100644 --- a/client/ui/controllers/importController.cpp +++ b/client/ui/controllers/importController.cpp @@ -521,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(); } @@ -559,6 +563,7 @@ bool ImportController::parseQrCodeChunk(const QString &code) data.append(m_qrCodeChunks.value(i)); } + data = qUncompress(data); auto format = checkConfigFormat(data); if (format == ConfigTypes::Invalid) { qDebug() << "error while extracting data from qr"; @@ -568,8 +573,7 @@ bool ImportController::parseQrCodeChunk(const QString &code) } else { qDebug() << "stopDecodingQr"; m_isQrCodeProcessed = false; - stopDecodingQr(); - extractConfigFromQr(data); + stopDecodingQr(data); return true; } } @@ -579,8 +583,7 @@ bool ImportController::parseQrCodeChunk(const QString &code) if (format != ConfigTypes::Invalid) { qDebug() << "stopDecodingQr"; m_isQrCodeProcessed = false; - stopDecodingQr(); - extractConfigFromQr(data); + 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