mirror of
https://github.com/amnezia-vpn/amnezia-client.git
synced 2026-06-22 02:01:08 +07:00
QR codes rework
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
#include "QrDecoderLogic.h"
|
||||
|
||||
#include "ui/uilogic.h"
|
||||
#include "ui/pages_logic/StartPageLogic.h"
|
||||
|
||||
#if defined(Q_OS_ANDROID)
|
||||
#include "android_controller.h"
|
||||
#endif
|
||||
|
||||
using namespace amnezia;
|
||||
using namespace PageEnumNS;
|
||||
|
||||
QrDecoderLogic::QrDecoderLogic(UiLogic *logic, QObject *parent):
|
||||
PageLogicBase(logic, parent)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void QrDecoderLogic::onUpdatePage()
|
||||
{
|
||||
m_chunks.clear();
|
||||
set_detectingEnabled(true);
|
||||
set_totalChunksCount(0);
|
||||
set_receivedChunksCount(0);
|
||||
emit startDecode();
|
||||
}
|
||||
|
||||
void QrDecoderLogic::onDetectedQrCode(const QString &code)
|
||||
{
|
||||
//qDebug() << code;
|
||||
|
||||
if (!detectingEnabled()) return;
|
||||
|
||||
// check if chunk received
|
||||
QByteArray ba = QByteArray::fromBase64(code.toUtf8(), QByteArray::Base64UrlEncoding | QByteArray::OmitTrailingEquals);
|
||||
QDataStream s(&ba, QIODevice::ReadOnly);
|
||||
qint16 magic; s >> magic;
|
||||
|
||||
|
||||
if (magic == amnezia::qrMagicCode) {
|
||||
qDebug() << "QrDecoderLogic::onDetectedQrCode magic code detected" << magic << ba.size();
|
||||
|
||||
quint8 chunksCount; s >> chunksCount;
|
||||
if (totalChunksCount() != chunksCount) {
|
||||
m_chunks.clear();
|
||||
}
|
||||
set_totalChunksCount(chunksCount);
|
||||
|
||||
quint8 chunkId; s >> chunkId;
|
||||
s >> m_chunks[chunkId];
|
||||
set_receivedChunksCount(m_chunks.size());
|
||||
|
||||
qDebug() << "Received chunks:" << receivedChunksCount() << "/" << chunksCount << "cur" << chunkId << m_chunks[chunkId].size();
|
||||
qDebug() << chunkId << m_chunks[chunkId];
|
||||
|
||||
if (m_chunks.size() == totalChunksCount()) {
|
||||
QByteArray data;
|
||||
for (int i = 0; i < totalChunksCount(); ++i) {
|
||||
data.append(m_chunks.value(i));
|
||||
}
|
||||
|
||||
bool ok = uiLogic()->startPageLogic()->importConnectionFromQr(data);
|
||||
if (ok) {
|
||||
set_detectingEnabled(false);
|
||||
emit stopDecode();
|
||||
}
|
||||
else {
|
||||
m_chunks.clear();
|
||||
set_totalChunksCount(0);
|
||||
set_receivedChunksCount(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
bool ok = uiLogic()->startPageLogic()->importConnectionFromQr(ba);
|
||||
if (ok) {
|
||||
set_detectingEnabled(false);
|
||||
emit stopDecode();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
#ifndef QR_DECODER_LOGIC_H
|
||||
#define QR_DECODER_LOGIC_H
|
||||
|
||||
#include "PageLogicBase.h"
|
||||
|
||||
class UiLogic;
|
||||
|
||||
class QrDecoderLogic : public PageLogicBase
|
||||
{
|
||||
Q_OBJECT
|
||||
AUTO_PROPERTY(bool, detectingEnabled)
|
||||
AUTO_PROPERTY(int, totalChunksCount)
|
||||
AUTO_PROPERTY(int, receivedChunksCount)
|
||||
|
||||
public:
|
||||
Q_INVOKABLE void onUpdatePage() override;
|
||||
Q_INVOKABLE void onDetectedQrCode(const QString &code);
|
||||
|
||||
public:
|
||||
explicit QrDecoderLogic(UiLogic *uiLogic, QObject *parent = nullptr);
|
||||
~QrDecoderLogic() = default;
|
||||
|
||||
signals:
|
||||
void startDecode();
|
||||
void stopDecode();
|
||||
|
||||
private:
|
||||
QMap<int, QByteArray> m_chunks;
|
||||
};
|
||||
#endif // QR_DECODER_LOGIC_H
|
||||
@@ -1,11 +1,7 @@
|
||||
#include <QApplication>
|
||||
#include <QBuffer>
|
||||
#include <QClipboard>
|
||||
#include <QFileDialog>
|
||||
#include <QTimer>
|
||||
#include <QSaveFile>
|
||||
#include <QStandardPaths>
|
||||
#include <QImage>
|
||||
#include <QDataStream>
|
||||
#include <QZXing>
|
||||
|
||||
#include "ShareConnectionLogic.h"
|
||||
|
||||
@@ -18,6 +14,7 @@
|
||||
#include "configurators/ssh_configurator.h"
|
||||
|
||||
#include "defines.h"
|
||||
#include "core/defs.h"
|
||||
#include <functional>
|
||||
|
||||
#include "../uilogic.h"
|
||||
@@ -35,7 +32,8 @@ ShareConnectionLogic::ShareConnectionLogic(UiLogic *logic, QObject *parent):
|
||||
void ShareConnectionLogic::onUpdatePage()
|
||||
{
|
||||
set_textEditShareAmneziaCodeText(tr(""));
|
||||
set_shareAmneziaQrCodeText("");
|
||||
set_shareAmneziaQrCodeTextSeries({});
|
||||
set_shareAmneziaQrCodeTextSeriesLength(0);
|
||||
|
||||
set_textEditShareOpenVpnCodeText("");
|
||||
|
||||
@@ -56,7 +54,8 @@ void ShareConnectionLogic::onUpdatePage()
|
||||
void ShareConnectionLogic::onPushButtonShareAmneziaGenerateClicked()
|
||||
{
|
||||
set_textEditShareAmneziaCodeText("");
|
||||
set_shareAmneziaQrCodeText("");
|
||||
set_shareAmneziaQrCodeTextSeries({});
|
||||
set_shareAmneziaQrCodeTextSeriesLength(0);
|
||||
|
||||
QJsonObject serverConfig;
|
||||
// Full access
|
||||
@@ -97,15 +96,15 @@ void ShareConnectionLogic::onPushButtonShareAmneziaGenerateClicked()
|
||||
}
|
||||
}
|
||||
|
||||
QByteArray ba = QJsonDocument(serverConfig).toBinaryData();
|
||||
QByteArray ba = QJsonDocument(serverConfig).toJson();
|
||||
ba = qCompress(ba, 8);
|
||||
QString code = QString("vpn://%1").arg(QString(ba.toBase64(QByteArray::Base64UrlEncoding | QByteArray::OmitTrailingEquals)));
|
||||
set_textEditShareAmneziaCodeText(code);
|
||||
|
||||
if (ba.size() < 2900) {
|
||||
QImage qr = updateQRCodeImage(ba.toBase64(QByteArray::Base64UrlEncoding | QByteArray::OmitTrailingEquals));
|
||||
set_shareAmneziaQrCodeText(imageToBase64(qr));
|
||||
}
|
||||
|
||||
QList<QString> qrChunks = genQrCodeImageSeries(ba);
|
||||
set_shareAmneziaQrCodeTextSeries(qrChunks);
|
||||
set_shareAmneziaQrCodeTextSeriesLength(qrChunks.size());
|
||||
}
|
||||
|
||||
void ShareConnectionLogic::onPushButtonShareOpenVpnGenerateClicked()
|
||||
@@ -147,7 +146,7 @@ void ShareConnectionLogic::onPushButtonShareShadowSocksGenerateClicked()
|
||||
ssString = "ss://" + ssString.toUtf8().toBase64();
|
||||
set_lineEditShareShadowSocksStringText(ssString);
|
||||
|
||||
QImage qr = updateQRCodeImage(ssString.toUtf8());
|
||||
QImage qr = QZXing::encodeData(ssString.toUtf8(), QZXing::EncoderFormat_QR_CODE, QSize(512,512), QZXing::EncodeErrorCorrectionLevel_L);
|
||||
set_shareShadowSocksQrCodeText(imageToBase64(qr));
|
||||
|
||||
QString humanString = QString("Server: %3\n"
|
||||
@@ -200,7 +199,8 @@ void ShareConnectionLogic::onPushButtonShareWireGuardGenerateClicked()
|
||||
|
||||
set_textEditShareWireGuardCodeText(cfg);
|
||||
|
||||
QImage qr = updateQRCodeImage(cfg.toUtf8());
|
||||
QImage qr = QZXing::encodeData(cfg.toUtf8(), QZXing::EncoderFormat_QR_CODE, QSize(512,512), QZXing::EncodeErrorCorrectionLevel_L);
|
||||
|
||||
set_shareWireGuardQrCodeText(imageToBase64(qr));
|
||||
}
|
||||
|
||||
@@ -234,30 +234,29 @@ void ShareConnectionLogic::updateSharingPage(int serverIndex, DockerContainer co
|
||||
uiLogic()->selectedDockerContainer = container;
|
||||
uiLogic()->selectedServerIndex = serverIndex;
|
||||
set_shareFullAccess(container == DockerContainer::None);
|
||||
|
||||
m_shareAmneziaQrCodeTextSeries.clear();
|
||||
set_shareAmneziaQrCodeTextSeriesLength(0);
|
||||
}
|
||||
|
||||
QImage ShareConnectionLogic::updateQRCodeImage(const QByteArray &data)
|
||||
QList<QString> ShareConnectionLogic::genQrCodeImageSeries(const QByteArray &data)
|
||||
{
|
||||
int levelIndex = 1;
|
||||
int versionIndex = 0;
|
||||
bool bExtent = true;
|
||||
int maskIndex = -1;
|
||||
double k = 1500;
|
||||
|
||||
m_qrEncode.EncodeData( levelIndex, versionIndex, bExtent, maskIndex, data.data() );
|
||||
quint8 chunksCount = std::ceil(data.size() / k);
|
||||
QList<QString> chunks;
|
||||
for (int i = 0; i < data.size(); i = i + k) {
|
||||
QByteArray chunk;
|
||||
QDataStream s(&chunk, QIODevice::WriteOnly);
|
||||
s << amnezia::qrMagicCode << chunksCount << (quint8)std::round(i/k) << data.mid(i, k);
|
||||
|
||||
int qrImageSize = m_qrEncode.m_nSymbleSize;
|
||||
QByteArray ba = chunk.toBase64(QByteArray::Base64UrlEncoding | QByteArray::OmitTrailingEquals);
|
||||
|
||||
int encodeImageSize = qrImageSize + ( QR_MARGIN * 2 );
|
||||
QImage encodeImage( encodeImageSize, encodeImageSize, QImage::Format_Mono );
|
||||
QImage qr = QZXing::encodeData(ba, QZXing::EncoderFormat_QR_CODE, QSize(512,512), QZXing::EncodeErrorCorrectionLevel_L);
|
||||
chunks.append(imageToBase64(qr));
|
||||
}
|
||||
|
||||
encodeImage.fill( 1 );
|
||||
|
||||
for ( int i = 0; i < qrImageSize; i++ )
|
||||
for ( int j = 0; j < qrImageSize; j++ )
|
||||
if ( m_qrEncode.m_byModuleData[i][j] )
|
||||
encodeImage.setPixel( i + QR_MARGIN, j + QR_MARGIN, 0 );
|
||||
|
||||
return encodeImage;
|
||||
return chunks;
|
||||
}
|
||||
|
||||
QString ShareConnectionLogic::imageToBase64(const QImage &image)
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
#define SHARE_CONNECTION_LOGIC_H
|
||||
|
||||
#include "PageLogicBase.h"
|
||||
#include "3rd/QRCodeGenerator/QRCodeGenerator.h"
|
||||
|
||||
class UiLogic;
|
||||
|
||||
@@ -14,7 +13,8 @@ public:
|
||||
AUTO_PROPERTY(bool, shareFullAccess)
|
||||
|
||||
AUTO_PROPERTY(QString, textEditShareAmneziaCodeText)
|
||||
AUTO_PROPERTY(QString, shareAmneziaQrCodeText)
|
||||
AUTO_PROPERTY(QStringList, shareAmneziaQrCodeTextSeries)
|
||||
AUTO_PROPERTY(int, shareAmneziaQrCodeTextSeriesLength)
|
||||
|
||||
AUTO_PROPERTY(QString, textEditShareOpenVpnCodeText)
|
||||
|
||||
@@ -46,11 +46,10 @@ public:
|
||||
~ShareConnectionLogic() = default;
|
||||
|
||||
void updateSharingPage(int serverIndex, DockerContainer container);
|
||||
QImage updateQRCodeImage(const QByteArray &data);
|
||||
QList<QString> genQrCodeImageSeries(const QByteArray &data);
|
||||
|
||||
QString imageToBase64(const QImage &image);
|
||||
|
||||
private:
|
||||
CQR_Encode m_qrEncode;
|
||||
|
||||
};
|
||||
#endif // SHARE_CONNECTION_LOGIC_H
|
||||
|
||||
@@ -3,6 +3,13 @@
|
||||
#include "configurators/ssh_configurator.h"
|
||||
#include "../uilogic.h"
|
||||
|
||||
#include <QFileDialog>
|
||||
#include <QStandardPaths>
|
||||
|
||||
#ifdef Q_OS_ANDROID
|
||||
#include "platforms/android/android_controller.h"
|
||||
#endif
|
||||
|
||||
StartPageLogic::StartPageLogic(UiLogic *logic, QObject *parent):
|
||||
PageLogicBase(logic, parent),
|
||||
m_pushButtonConnectEnabled{true},
|
||||
@@ -119,52 +126,40 @@ void StartPageLogic::onPushButtonConnect()
|
||||
|
||||
void StartPageLogic::onPushButtonImport()
|
||||
{
|
||||
QString s = lineEditStartExistingCodeText();
|
||||
s.replace("vpn://", "");
|
||||
QByteArray ba = QByteArray::fromBase64(s.toUtf8(), QByteArray::Base64UrlEncoding | QByteArray::OmitTrailingEquals);
|
||||
QByteArray ba_uncompressed = qUncompress(ba);
|
||||
importConnectionFromCode(lineEditStartExistingCodeText());
|
||||
}
|
||||
|
||||
QJsonObject o;
|
||||
if (!ba_uncompressed.isEmpty()) {
|
||||
o = QJsonDocument::fromBinaryData(ba_uncompressed).object();
|
||||
}
|
||||
else {
|
||||
o = QJsonDocument::fromJson(ba).object();
|
||||
}
|
||||
void StartPageLogic::onPushButtonImportOpenFile()
|
||||
{
|
||||
QString fileName = QFileDialog::getOpenFileName(nullptr, tr("Open profile"),
|
||||
QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation), "*.vpn");
|
||||
|
||||
if (fileName.isEmpty()) return;
|
||||
|
||||
QFile file(fileName);
|
||||
file.open(QIODevice::ReadOnly);
|
||||
QByteArray data = file.readAll();
|
||||
|
||||
importConnectionFromCode(QString(data));
|
||||
}
|
||||
|
||||
bool StartPageLogic::importConnection(const QJsonObject &profile)
|
||||
{
|
||||
ServerCredentials credentials;
|
||||
credentials.hostName = o.value("h").toString();
|
||||
if (credentials.hostName.isEmpty()) credentials.hostName = o.value(config_key::hostName).toString();
|
||||
credentials.hostName = profile.value(config_key::hostName).toString();
|
||||
credentials.port = profile.value(config_key::port).toInt();
|
||||
credentials.userName = profile.value(config_key::userName).toString();
|
||||
credentials.password = profile.value(config_key::password).toString();
|
||||
|
||||
credentials.port = o.value("p").toInt();
|
||||
if (credentials.port == 0) credentials.port = o.value(config_key::port).toInt();
|
||||
|
||||
credentials.userName = o.value("u").toString();
|
||||
if (credentials.userName.isEmpty()) credentials.userName = o.value(config_key::userName).toString();
|
||||
|
||||
credentials.password = o.value("w").toString();
|
||||
if (credentials.password.isEmpty()) credentials.password = o.value(config_key::password).toString();
|
||||
|
||||
if (credentials.isValid()) {
|
||||
o.insert(config_key::hostName, credentials.hostName);
|
||||
o.insert(config_key::port, credentials.port);
|
||||
o.insert(config_key::userName, credentials.userName);
|
||||
o.insert(config_key::password, credentials.password);
|
||||
|
||||
o.remove("h");
|
||||
o.remove("p");
|
||||
o.remove("u");
|
||||
o.remove("w");
|
||||
}
|
||||
qDebug() << QString("Added server %3@%1:%2").
|
||||
arg(credentials.hostName).
|
||||
arg(credentials.port).
|
||||
arg(credentials.userName);
|
||||
// qDebug() << QString("Added server %3@%1:%2").
|
||||
// arg(credentials.hostName).
|
||||
// arg(credentials.port).
|
||||
// arg(credentials.userName);
|
||||
|
||||
//qDebug() << QString("Password") << credentials.password;
|
||||
|
||||
if (credentials.isValid() || o.contains(config_key::containers)) {
|
||||
m_settings.addServer(o);
|
||||
if (credentials.isValid() || profile.contains(config_key::containers)) {
|
||||
m_settings.addServer(profile);
|
||||
m_settings.setDefaultServer(m_settings.serversCount() - 1);
|
||||
|
||||
emit uiLogic()->goToPage(Page::Vpn);
|
||||
@@ -172,15 +167,56 @@ void StartPageLogic::onPushButtonImport()
|
||||
}
|
||||
else {
|
||||
qDebug() << "Failed to import profile";
|
||||
qDebug().noquote() << QJsonDocument(o).toJson();
|
||||
return;
|
||||
qDebug().noquote() << QJsonDocument(profile).toJson();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!o.contains(config_key::containers)) {
|
||||
if (!profile.contains(config_key::containers)) {
|
||||
uiLogic()->selectedServerIndex = m_settings.defaultServerIndex();
|
||||
uiLogic()->selectedDockerContainer = m_settings.defaultContainer(uiLogic()->selectedServerIndex);
|
||||
uiLogic()->onUpdateAllPages();
|
||||
|
||||
emit uiLogic()->goToPage(Page::ServerContainers);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool StartPageLogic::importConnectionFromCode(QString code)
|
||||
{
|
||||
code.replace("vpn://", "");
|
||||
QByteArray ba = QByteArray::fromBase64(code.toUtf8(), QByteArray::Base64UrlEncoding | QByteArray::OmitTrailingEquals);
|
||||
|
||||
QByteArray ba_uncompressed = qUncompress(ba);
|
||||
if (!ba_uncompressed.isEmpty()) {
|
||||
ba = ba_uncompressed;
|
||||
}
|
||||
|
||||
QJsonObject o;
|
||||
o = QJsonDocument::fromJson(ba).object();
|
||||
if (!o.isEmpty()) {
|
||||
return importConnection(o);
|
||||
}
|
||||
|
||||
o = QJsonDocument::fromBinaryData(ba).object();
|
||||
if (!o.isEmpty()) {
|
||||
return importConnection(o);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool StartPageLogic::importConnectionFromQr(const QByteArray &data)
|
||||
{
|
||||
qDebug() << "StartPageLogic::importConnectionFromQr" << data;
|
||||
QJsonObject dataObj = QJsonDocument::fromJson(data).object();
|
||||
if (!dataObj.isEmpty()) {
|
||||
return importConnection(dataObj);
|
||||
}
|
||||
|
||||
QByteArray ba_uncompressed = qUncompress(data);
|
||||
if (!ba_uncompressed.isEmpty()) {
|
||||
return importConnection(QJsonDocument::fromJson(ba_uncompressed).object());
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -27,6 +27,11 @@ public:
|
||||
|
||||
Q_INVOKABLE void onPushButtonConnect();
|
||||
Q_INVOKABLE void onPushButtonImport();
|
||||
Q_INVOKABLE void onPushButtonImportOpenFile();
|
||||
|
||||
bool importConnection(const QJsonObject &profile);
|
||||
bool importConnectionFromCode(QString code);
|
||||
bool importConnectionFromQr(const QByteArray &data);
|
||||
|
||||
public:
|
||||
explicit StartPageLogic(UiLogic *uiLogic, QObject *parent = nullptr);
|
||||
|
||||
Reference in New Issue
Block a user