Files
amnezia-client/client/ui/pages_logic/protocols/OtherProtocolsLogic.cpp
T

171 lines
4.6 KiB
C++
Raw Normal View History

2021-09-24 13:14:35 +03:00
#include <QDesktopServices>
#include <QTimer>
#include <QProcess>
#include <QStorageInfo>
2022-01-24 14:29:37 -08:00
#include <QStandardPaths>
2021-09-24 13:14:35 +03:00
#include "OtherProtocolsLogic.h"
#include "core/servercontroller.h"
#include <functional>
#include "../../uilogic.h"
2021-09-24 13:14:35 +03:00
#include "utils.h"
2021-09-30 15:56:48 +03:00
#ifdef Q_OS_WINDOWS
2021-09-24 13:14:35 +03:00
#include <Windows.h>
2021-09-30 15:56:48 +03:00
#endif
using namespace amnezia;
using namespace PageEnumNS;
OtherProtocolsLogic::OtherProtocolsLogic(UiLogic *logic, QObject *parent):
2022-01-30 17:35:57 +03:00
PageProtocolLogicBase(logic, parent),
m_checkBoxSftpRestoreChecked{false}
{
}
2021-09-24 13:14:35 +03:00
OtherProtocolsLogic::~OtherProtocolsLogic()
{
2021-09-30 15:56:48 +03:00
#ifdef Q_OS_WINDOWS
for (QProcess *p: m_sftpMountProcesses) {
2021-09-24 13:14:35 +03:00
if (p) Utils::signalCtrl(p->processId(), CTRL_C_EVENT);
if (p) p->kill();
if (p) p->waitForFinished();
if (p) delete p;
}
2021-09-30 15:56:48 +03:00
#endif
2021-09-24 13:14:35 +03:00
}
void OtherProtocolsLogic::updateProtocolPage(const QJsonObject &config, DockerContainer container, bool haveAuthData)
{
set_labelTftpUserNameText(config.value(config_key::userName).toString());
set_labelTftpPasswordText(config.value(config_key::password).toString(protocols::sftp::defaultUserName));
2021-09-24 13:14:35 +03:00
set_labelTftpPortText(config.value(config_key::port).toString());
set_labelTorWebSiteAddressText(config.value(config_key::site).toString());
2022-01-24 14:29:37 -08:00
set_pushButtonSftpMountEnabled(true);
}
2021-09-24 13:14:35 +03:00
#ifdef Q_OS_WINDOWS
2022-01-24 14:29:37 -08:00
QString OtherProtocolsLogic::getNextDriverLetter() const
{
2021-09-24 13:14:35 +03:00
QProcess drivesProc;
drivesProc.start("wmic logicaldisk get caption");
drivesProc.waitForFinished();
QString drives = drivesProc.readAll();
qDebug() << drives;
QString letters = "CFGHIJKLMNOPQRSTUVWXYZ";
QString letter;
for (int i = letters.size() - 1; i > 0; i--) {
letter = letters.at(i);
if (!drives.contains(letter + ":")) break;
}
if (letter == "C:") {
// set err info
qDebug() << "Can't find free drive letter";
2022-01-24 14:29:37 -08:00
return "";
}
return letter;
}
#endif
//QJsonObject OtherProtocolsLogic::getProtocolConfigFromPage(QJsonObject oldConfig)
//{
//}
void OtherProtocolsLogic::onPushButtonSftpMountDriveClicked()
{
QString mountPath;
QString cmd;
QString host = m_settings.serverCredentials(uiLogic()->selectedServerIndex).hostName;
#ifdef Q_OS_WINDOWS
mountPath = getNextDriverLetter() + ":";
// QString cmd = QString("net use \\\\sshfs\\%1@x.x.x.x!%2 /USER:%1 %3")
// .arg(labelTftpUserNameText())
// .arg(labelTftpPortText())
// .arg(labelTftpPasswordText());
cmd = "C:\\Program Files\\SSHFS-Win\\bin\\sshfs.exe";
#elif defined AMNEZIA_DESKTOP
mountPath = QString("%1/sftp:%2:%3")
.arg(QStandardPaths::writableLocation(QStandardPaths::HomeLocation))
.arg(host)
.arg(labelTftpPortText());
QDir dir(mountPath);
if (!dir.exists()){
dir.mkpath(mountPath);
2021-09-24 13:14:35 +03:00
}
2022-01-24 14:29:37 -08:00
cmd = "/usr/local/bin/sshfs";
#endif
2021-09-24 13:14:35 +03:00
2022-01-24 14:29:37 -08:00
#ifdef AMNEZIA_DESKTOP
2021-09-24 13:14:35 +03:00
set_pushButtonSftpMountEnabled(false);
QProcess *p = new QProcess;
2021-09-30 18:16:41 +03:00
m_sftpMountProcesses.append(p);
2021-09-24 13:14:35 +03:00
p->setProcessChannelMode(QProcess::MergedChannels);
2022-01-24 14:29:37 -08:00
connect(p, &QProcess::readyRead, this, [this, p, mountPath](){
2021-09-24 13:14:35 +03:00
QString s = p->readAll();
if (s.contains("The service sshfs has been started")) {
2022-01-24 14:29:37 -08:00
QDesktopServices::openUrl(QUrl("file:///" + mountPath));
2021-09-24 13:14:35 +03:00
set_pushButtonSftpMountEnabled(true);
}
2022-01-24 14:29:37 -08:00
qDebug() << s;
2021-09-24 13:14:35 +03:00
});
2022-01-24 14:29:37 -08:00
p->setProgram(cmd);
2021-09-24 13:14:35 +03:00
QString args = QString(
2022-01-24 14:29:37 -08:00
"%1@%2:/ %3 "
2021-09-24 13:14:35 +03:00
"-o port=%4 "
"-f "
2022-01-24 14:29:37 -08:00
"-o reconnect "
"-o rellinks "
"-o fstypename=SSHFS "
2021-09-24 13:14:35 +03:00
"-o ssh_command=/usr/bin/ssh.exe "
2022-01-24 14:29:37 -08:00
"-o UserKnownHostsFile=/dev/null "
"-o StrictHostKeyChecking=no "
2021-09-24 13:14:35 +03:00
"-o password_stdin")
.arg(labelTftpUserNameText())
.arg(host)
2022-01-24 14:29:37 -08:00
.arg(mountPath)
2021-09-24 13:14:35 +03:00
.arg(labelTftpPortText());
2022-01-24 14:29:37 -08:00
// args.replace("\n", " ");
// args.replace("\r", " ");
//#ifndef Q_OS_WIN
// args.replace("reconnect-orellinks", "");
//#endif
p->setArguments(args.split(" ", QString::SkipEmptyParts));
2021-09-24 13:14:35 +03:00
p->start();
p->waitForStarted(50);
if (p->state() != QProcess::Running) {
qDebug() << "onPushButtonSftpMountDriveClicked process not started";
2022-01-24 14:29:37 -08:00
qDebug() << args;
2021-09-24 13:14:35 +03:00
}
else {
p->write((labelTftpPasswordText() + "\n").toUtf8());
}
//qDebug().noquote() << "onPushButtonSftpMountDriveClicked" << args;
2021-11-19 13:57:00 +03:00
set_pushButtonSftpMountEnabled(true);
2021-09-24 13:14:35 +03:00
#endif
}
void OtherProtocolsLogic::checkBoxSftpRestoreClicked()
{
}