Files
amnezia-client/client/ui/pages_logic/StartPageLogic.cpp
T

217 lines
6.4 KiB
C++
Raw Normal View History

2021-09-04 12:53:58 +03:00
#include "StartPageLogic.h"
2022-08-29 01:32:42 +03:00
#include "ViewConfigLogic.h"
2021-09-04 12:53:58 +03:00
#include "core/errorstrings.h"
#include "configurators/ssh_configurator.h"
2022-08-25 17:35:28 +03:00
#include "configurators/vpn_configurator.h"
2021-09-06 12:29:56 +03:00
#include "../uilogic.h"
#include "utilities.h"
2021-09-04 12:53:58 +03:00
2021-12-20 02:29:23 +03:00
#include <QFileDialog>
#include <QStandardPaths>
#ifdef Q_OS_ANDROID
2022-09-23 21:50:25 +03:00
#include <QtAndroid>
2021-12-20 02:29:23 +03:00
#include "platforms/android/android_controller.h"
#endif
2021-09-07 21:01:56 +03:00
StartPageLogic::StartPageLogic(UiLogic *logic, QObject *parent):
PageLogicBase(logic, parent),
2021-09-08 14:23:02 +03:00
m_pushButtonConnectEnabled{true},
m_pushButtonConnectText{tr("Connect")},
m_pushButtonConnectKeyChecked{false},
m_labelWaitInfoVisible{true},
2021-09-04 12:53:58 +03:00
m_pushButtonBackFromStartVisible{true},
2021-12-25 23:01:53 +03:00
m_ipAddressPortRegex{Utils::ipAddressPortRegExp()}
2021-09-04 12:53:58 +03:00
{
2022-09-23 21:50:25 +03:00
#ifdef Q_OS_ANDROID
// Set security screen for Android app
QtAndroid::runOnAndroidThread([]() {
QAndroidJniObject window = QtAndroid::androidActivity().callObjectMethod("getWindow", "()Landroid/view/Window;");
if (window.isValid()){
const int FLAG_SECURE = 8192;
window.callMethod<void>("addFlags", "(I)V", FLAG_SECURE);
}
});
#endif
2021-09-04 12:53:58 +03:00
}
void StartPageLogic::onUpdatePage()
2021-09-04 12:53:58 +03:00
{
2021-09-08 13:52:36 +03:00
set_lineEditStartExistingCodeText("");
2021-09-08 14:23:02 +03:00
set_textEditSshKeyText("");
set_lineEditIpText("");
set_lineEditPasswordText("");
set_textEditSshKeyText("");
set_lineEditLoginText("");
2021-09-04 12:53:58 +03:00
2021-09-08 14:23:02 +03:00
set_labelWaitInfoVisible(false);
set_labelWaitInfoText("");
set_pushButtonConnectKeyChecked(false);
2021-11-06 13:47:52 +03:00
set_pushButtonBackFromStartVisible(uiLogic()->pagesStackDepth() > 0);
2021-09-04 12:53:58 +03:00
}
2021-09-08 14:23:02 +03:00
void StartPageLogic::onPushButtonConnect()
2021-09-04 12:53:58 +03:00
{
2021-09-08 14:23:02 +03:00
if (pushButtonConnectKeyChecked()){
if (lineEditIpText().isEmpty() ||
lineEditLoginText().isEmpty() ||
textEditSshKeyText().isEmpty() ) {
set_labelWaitInfoText(tr("Please fill in all fields"));
2021-09-04 12:53:58 +03:00
return;
}
}
else {
2021-09-08 14:23:02 +03:00
if (lineEditIpText().isEmpty() ||
lineEditLoginText().isEmpty() ||
lineEditPasswordText().isEmpty() ) {
set_labelWaitInfoText(tr("Please fill in all fields"));
2021-09-04 12:53:58 +03:00
return;
}
}
ServerCredentials serverCredentials;
2021-09-08 14:23:02 +03:00
serverCredentials.hostName = lineEditIpText();
2021-09-04 12:53:58 +03:00
if (serverCredentials.hostName.contains(":")) {
serverCredentials.port = serverCredentials.hostName.split(":").at(1).toInt();
serverCredentials.hostName = serverCredentials.hostName.split(":").at(0);
}
2021-09-08 14:23:02 +03:00
serverCredentials.userName = lineEditLoginText();
if (pushButtonConnectKeyChecked()){
QString key = textEditSshKeyText();
2021-09-04 12:53:58 +03:00
if (key.startsWith("ssh-rsa")) {
2021-09-07 21:01:56 +03:00
emit uiLogic()->showPublicKeyWarning();
2021-09-04 12:53:58 +03:00
return;
}
if (key.contains("OPENSSH") && key.contains("BEGIN") && key.contains("PRIVATE KEY")) {
2022-08-25 17:35:28 +03:00
key = m_configurator->sshConfigurator->convertOpenSShKey(key);
2021-09-04 12:53:58 +03:00
}
serverCredentials.password = key;
}
else {
2021-09-08 14:23:02 +03:00
serverCredentials.password = lineEditPasswordText();
2021-09-04 12:53:58 +03:00
}
2021-09-08 14:23:02 +03:00
set_pushButtonConnectEnabled(false);
set_pushButtonConnectText(tr("Connecting..."));
2021-09-04 12:53:58 +03:00
ErrorCode e = ErrorCode::NoError;
#ifdef Q_DEBUG
2022-08-25 17:35:28 +03:00
//QString output = m_serverController->checkSshConnection(serverCredentials, &e);
2021-09-04 12:53:58 +03:00
#else
QString output;
#endif
bool ok = true;
if (e) {
2021-09-08 14:23:02 +03:00
set_labelWaitInfoVisible(true);
set_labelWaitInfoText(errorString(e));
2021-09-04 12:53:58 +03:00
ok = false;
}
else {
if (output.contains("Please login as the user")) {
output.replace("\n", "");
2021-09-08 14:23:02 +03:00
set_labelWaitInfoVisible(true);
set_labelWaitInfoText(output);
2021-09-04 12:53:58 +03:00
ok = false;
}
}
2021-09-08 14:23:02 +03:00
set_pushButtonConnectEnabled(true);
set_pushButtonConnectText(tr("Connect"));
2021-09-04 12:53:58 +03:00
2021-09-07 21:01:56 +03:00
uiLogic()->installCredentials = serverCredentials;
2021-11-19 23:04:35 +03:00
if (ok) emit uiLogic()->goToPage(Page::NewServer);
2021-09-04 12:53:58 +03:00
}
2021-09-08 14:23:02 +03:00
void StartPageLogic::onPushButtonImport()
2021-09-04 12:53:58 +03:00
{
2021-12-20 02:29:23 +03:00
importConnectionFromCode(lineEditStartExistingCodeText());
}
2021-09-04 12:53:58 +03:00
2021-12-20 02:29:23 +03:00
void StartPageLogic::onPushButtonImportOpenFile()
{
QString fileName = QFileDialog::getOpenFileName(nullptr, tr("Open profile"),
QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation), "*.vpn");
2021-09-04 12:53:58 +03:00
2021-12-20 02:29:23 +03:00
if (fileName.isEmpty()) return;
2021-09-04 12:53:58 +03:00
2021-12-20 02:29:23 +03:00
QFile file(fileName);
file.open(QIODevice::ReadOnly);
QByteArray data = file.readAll();
2021-09-04 12:53:58 +03:00
2021-12-20 02:29:23 +03:00
importConnectionFromCode(QString(data));
}
2021-09-04 12:53:58 +03:00
2021-12-20 02:29:23 +03:00
bool StartPageLogic::importConnection(const QJsonObject &profile)
{
ServerCredentials credentials;
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();
2021-09-04 12:53:58 +03:00
2021-12-20 02:29:23 +03:00
if (credentials.isValid() || profile.contains(config_key::containers)) {
2022-08-29 01:32:42 +03:00
// check config
uiLogic()->pageLogic<ViewConfigLogic>()->set_configJson(profile);
emit uiLogic()->goToPage(Page::ViewConfig);
2021-09-04 12:53:58 +03:00
}
else {
qDebug() << "Failed to import profile";
2021-12-20 02:29:23 +03:00
qDebug().noquote() << QJsonDocument(profile).toJson();
return false;
2021-09-04 12:53:58 +03:00
}
2021-12-20 02:29:23 +03:00
if (!profile.contains(config_key::containers)) {
2022-08-25 12:47:02 +03:00
uiLogic()->selectedServerIndex = m_settings->defaultServerIndex();
uiLogic()->selectedDockerContainer = m_settings->defaultContainer(uiLogic()->selectedServerIndex);
2021-11-19 23:04:35 +03:00
uiLogic()->onUpdateAllPages();
emit uiLogic()->goToPage(Page::ServerContainers);
2021-09-04 12:53:58 +03:00
}
2021-12-20 02:29:23 +03:00
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::fromJson(ba).object();
2021-12-20 02:29:23 +03:00
if (!o.isEmpty()) {
return importConnection(o);
}
return false;
}
bool StartPageLogic::importConnectionFromQr(const QByteArray &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;
2021-09-04 12:53:58 +03:00
}