removed linking with botan and qssh

This commit is contained in:
vladimir.kuznetsov 2022-12-23 10:13:06 +03:00
parent 81cf108471
commit f9b2829396
10 changed files with 329 additions and 318 deletions

View file

@ -1,7 +1,7 @@
set(CLIENT_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/..) set(CLIENT_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/..)
include(${CLIENT_ROOT_DIR}/3rd/QtSsh/src/ssh/qssh.cmake) #include(${CLIENT_ROOT_DIR}/3rd/QtSsh/src/ssh/qssh.cmake)
include(${CLIENT_ROOT_DIR}/3rd/QtSsh/src/botan/botan.cmake) #include(${CLIENT_ROOT_DIR}/3rd/QtSsh/src/botan/botan.cmake)
if(NOT IOS AND NOT ANDROID) if(NOT IOS AND NOT ANDROID)
include(${CLIENT_ROOT_DIR}/3rd/SingleApplication/singleapplication.cmake) include(${CLIENT_ROOT_DIR}/3rd/SingleApplication/singleapplication.cmake)

View file

@ -14,9 +14,6 @@
#include "core/server_defs.h" #include "core/server_defs.h"
#include "utilities.h" #include "utilities.h"
#include "sftpdefs.h"
using namespace QSsh;
SshConfigurator::SshConfigurator(std::shared_ptr<Settings> settings, std::shared_ptr<ServerController> serverController, QObject *parent): SshConfigurator::SshConfigurator(std::shared_ptr<Settings> settings, std::shared_ptr<ServerController> serverController, QObject *parent):
ConfiguratorBase(settings, serverController, parent) ConfiguratorBase(settings, serverController, parent)

View file

@ -144,7 +144,7 @@ WireguardConfigurator::ConnectionData WireguardConfigurator::prepareWireguardCon
arg(connData.clientIP); arg(connData.clientIP);
e = m_serverController->uploadTextFileToContainer(container, credentials, configPart, e = m_serverController->uploadTextFileToContainer(container, credentials, configPart,
protocols::wireguard::serverConfigPath, QSsh::SftpOverwriteMode::SftpAppendToExisting); protocols::wireguard::serverConfigPath, libssh::SftpOverwriteMode::SftpAppendToExisting);
if (e) { if (e) {
if (errorCode) *errorCode = e; if (errorCode) *errorCode = e;

View file

@ -22,6 +22,7 @@
#include <thread> #include <thread>
#include "containers/containers_defs.h" #include "containers/containers_defs.h"
#include "debug.h"
#include "server_defs.h" #include "server_defs.h"
#include "settings.h" #include "settings.h"
#include "scripts_registry.h" #include "scripts_registry.h"
@ -43,7 +44,7 @@ ErrorCode ServerController::runScript(const ServerCredentials &credentials, QStr
const std::function<void(const QString &)> &cbReadStdOut, const std::function<void(const QString &)> &cbReadStdOut,
const std::function<void(const QString &)> &cbReadStdErr) { const std::function<void(const QString &)> &cbReadStdErr) {
std::shared_ptr<SshSession> session = m_sshClient.getSession(); std::shared_ptr<libssh::Session> session = m_sshClient.getSession();
if (!session) { if (!session) {
return ErrorCode::SshInternalError; return ErrorCode::SshInternalError;
} }
@ -117,7 +118,7 @@ ErrorCode ServerController::runContainerScript(const ServerCredentials &credenti
ErrorCode ServerController::uploadTextFileToContainer(DockerContainer container, ErrorCode ServerController::uploadTextFileToContainer(DockerContainer container,
const ServerCredentials &credentials, const QString &file, const QString &path, const ServerCredentials &credentials, const QString &file, const QString &path,
QSsh::SftpOverwriteMode overwriteMode) libssh::SftpOverwriteMode overwriteMode)
{ {
ErrorCode e = ErrorCode::NoError; ErrorCode e = ErrorCode::NoError;
QString tmpFileName = QString("/tmp/%1.tmp").arg(Utils::getRandomString(16)); QString tmpFileName = QString("/tmp/%1.tmp").arg(Utils::getRandomString(16));
@ -141,14 +142,14 @@ ErrorCode ServerController::uploadTextFileToContainer(DockerContainer container,
if (e) return e; if (e) return e;
if (overwriteMode == QSsh::SftpOverwriteMode::SftpOverwriteExisting) { if (overwriteMode == libssh::SftpOverwriteMode::SftpOverwriteExisting) {
e = runScript(credentials, e = runScript(credentials,
replaceVars(QString("sudo docker cp %1 $CONTAINER_NAME:/%2").arg(tmpFileName).arg(path), replaceVars(QString("sudo docker cp %1 $CONTAINER_NAME:/%2").arg(tmpFileName).arg(path),
genVarsForScript(credentials, container)), cbReadStd, cbReadStd); genVarsForScript(credentials, container)), cbReadStd, cbReadStd);
if (e) return e; if (e) return e;
} }
else if (overwriteMode == QSsh::SftpOverwriteMode::SftpAppendToExisting) { else if (overwriteMode == libssh::SftpOverwriteMode::SftpAppendToExisting) {
e = runScript(credentials, e = runScript(credentials,
replaceVars(QString("sudo docker cp %1 $CONTAINER_NAME:/%2").arg(tmpFileName).arg(tmpFileName), replaceVars(QString("sudo docker cp %1 $CONTAINER_NAME:/%2").arg(tmpFileName).arg(tmpFileName),
genVarsForScript(credentials, container)), cbReadStd, cbReadStd); genVarsForScript(credentials, container)), cbReadStd, cbReadStd);
@ -231,9 +232,9 @@ ErrorCode ServerController::checkOpenVpnServer(DockerContainer container, const
} }
ErrorCode ServerController::uploadFileToHost(const ServerCredentials &credentials, const QByteArray &data, const QString &remotePath, ErrorCode ServerController::uploadFileToHost(const ServerCredentials &credentials, const QByteArray &data, const QString &remotePath,
QSsh::SftpOverwriteMode overwriteMode) libssh::SftpOverwriteMode overwriteMode)
{ {
std::shared_ptr<SshSession> session = m_sshClient.getSession(); std::shared_ptr<libssh::Session> session = m_sshClient.getSession();
if (!session) { if (!session) {
return ErrorCode::SshInternalError; return ErrorCode::SshInternalError;
} }
@ -249,7 +250,7 @@ ErrorCode ServerController::uploadFileToHost(const ServerCredentials &credential
qDebug() << "remotePath" << remotePath; qDebug() << "remotePath" << remotePath;
error = session->sftpFileCopy(localFile.fileName().toStdString(), remotePath.toStdString(), "non_desc"); error = session->sftpFileCopy(overwriteMode, localFile.fileName().toStdString(), remotePath.toStdString(), "non_desc");
if (error != ErrorCode::NoError) { if (error != ErrorCode::NoError) {
return error; return error;
} }

View file

@ -4,12 +4,9 @@
#include <QJsonObject> #include <QJsonObject>
#include <QObject> #include <QObject>
#include "debug.h"
#include "defs.h" #include "defs.h"
#include "containers/containers_defs.h" #include "containers/containers_defs.h"
#include "sftpdefs.h"
#include "sshclient.h" #include "sshclient.h"
class Settings; class Settings;
@ -48,11 +45,11 @@ public:
ErrorCode checkOpenVpnServer(DockerContainer container, const ServerCredentials &credentials); ErrorCode checkOpenVpnServer(DockerContainer container, const ServerCredentials &credentials);
ErrorCode uploadFileToHost(const ServerCredentials &credentials, const QByteArray &data, ErrorCode uploadFileToHost(const ServerCredentials &credentials, const QByteArray &data,
const QString &remotePath, QSsh::SftpOverwriteMode overwriteMode = QSsh::SftpOverwriteMode::SftpOverwriteExisting); const QString &remotePath, libssh::SftpOverwriteMode overwriteMode = libssh::SftpOverwriteMode::SftpOverwriteExisting);
ErrorCode uploadTextFileToContainer(DockerContainer container, ErrorCode uploadTextFileToContainer(DockerContainer container,
const ServerCredentials &credentials, const QString &file, const QString &path, const ServerCredentials &credentials, const QString &file, const QString &path,
QSsh::SftpOverwriteMode overwriteMode = QSsh::SftpOverwriteMode::SftpOverwriteExisting); libssh::SftpOverwriteMode overwriteMode = libssh::SftpOverwriteMode::SftpOverwriteExisting);
QByteArray getTextFileFromContainer(DockerContainer container, QByteArray getTextFileFromContainer(DockerContainer container,
const ServerCredentials &credentials, const QString &path, ErrorCode *errorCode = nullptr); const ServerCredentials &credentials, const QString &path, ErrorCode *errorCode = nullptr);
@ -84,7 +81,7 @@ private:
std::shared_ptr<Settings> m_settings; std::shared_ptr<Settings> m_settings;
std::shared_ptr<VpnConfigurator> m_configurator; std::shared_ptr<VpnConfigurator> m_configurator;
SshClient m_sshClient; libssh::Client m_sshClient;
}; };
#endif // SERVERCONTROLLER_H #endif // SERVERCONTROLLER_H

View file

@ -1,19 +1,18 @@
#include "sshclient.h" #include "sshclient.h"
#include <libssh/libssh.h> namespace libssh {
#include <libssh/sftp.h> Client::Client(QObject *parent) : QObject(parent)
SshClient::SshClient(QObject *parent) : QObject(parent)
{ {
ssh_init(); ssh_init();
} }
SshClient::~SshClient() Client::~Client()
{ {
ssh_finalize(); ssh_finalize();
} }
std::shared_ptr<SshSession> SshClient::getSession() std::shared_ptr<Session> Client::getSession()
{ {
return std::make_shared<SshSession>(); return std::make_shared<Session>();
}
} }

View file

@ -7,14 +7,16 @@
using namespace amnezia; using namespace amnezia;
class SshClient : public QObject namespace libssh {
class Client : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
SshClient(QObject *parent = nullptr); Client(QObject *parent = nullptr);
~SshClient(); ~Client();
std::shared_ptr<SshSession> getSession(); std::shared_ptr<Session> getSession();
}; };
}
#endif // SSHCLIENT_H #endif // SSHCLIENT_H

View file

@ -4,14 +4,18 @@
#include <QtConcurrent> #include <QtConcurrent>
#include <fstream> #include <fstream>
#include <fcntl.h>
SshSession::SshSession(QObject *parent) : QObject(parent) #ifdef Q_OS_WINDOWS
#define S_IRWXU 0
#endif
namespace libssh {
Session::Session(QObject *parent) : QObject(parent)
{ {
} }
SshSession::~SshSession() Session::~Session()
{ {
if (m_isNeedSendChannelEof) { if (m_isNeedSendChannelEof) {
ssh_channel_send_eof(m_channel); ssh_channel_send_eof(m_channel);
@ -28,7 +32,7 @@ SshSession::~SshSession()
ssh_free(m_session); ssh_free(m_session);
} }
ErrorCode SshSession::connectToHost(const ServerCredentials &credentials) ErrorCode Session::connectToHost(const ServerCredentials &credentials)
{ {
if (m_session == NULL) { if (m_session == NULL) {
return ErrorCode::InternalError; return ErrorCode::InternalError;
@ -73,7 +77,7 @@ ErrorCode SshSession::connectToHost(const ServerCredentials &credentials)
return ErrorCode::NoError; return ErrorCode::NoError;
} }
ErrorCode SshSession::initChannel(const ServerCredentials &credentials) ErrorCode Session::initChannel(const ServerCredentials &credentials)
{ {
m_session = ssh_new(); m_session = ssh_new();
@ -119,7 +123,7 @@ ErrorCode SshSession::initChannel(const ServerCredentials &credentials)
return ErrorCode::NoError; return ErrorCode::NoError;
} }
ErrorCode SshSession::writeToChannel(const QString &data, ErrorCode Session::writeToChannel(const QString &data,
const std::function<void(const QString &)> &cbReadStdOut, const std::function<void(const QString &)> &cbReadStdOut,
const std::function<void(const QString &)> &cbReadStdErr) const std::function<void(const QString &)> &cbReadStdErr)
{ {
@ -129,7 +133,7 @@ ErrorCode SshSession::writeToChannel(const QString &data,
} }
QFutureWatcher<ErrorCode> watcher; QFutureWatcher<ErrorCode> watcher;
connect(&watcher, &QFutureWatcher<ErrorCode>::finished, this, &SshSession::writeToChannelFinished); connect(&watcher, &QFutureWatcher<ErrorCode>::finished, this, &Session::writeToChannelFinished);
QFuture<ErrorCode> future = QtConcurrent::run([this, &data, &cbReadStdOut, &cbReadStdErr]() { QFuture<ErrorCode> future = QtConcurrent::run([this, &data, &cbReadStdOut, &cbReadStdErr]() {
const size_t bufferSize = 2048; const size_t bufferSize = 2048;
@ -157,7 +161,7 @@ ErrorCode SshSession::writeToChannel(const QString &data,
cbReadStdErr(output.c_str()); cbReadStdErr(output.c_str());
} }
} }
bytesRead = ssh_channel_read_timeout(m_channel, buffer, sizeof(buffer), isStdErr, 500); bytesRead = ssh_channel_read_timeout(m_channel, buffer, sizeof(buffer), isStdErr, 5000);
} }
} }
return output; return output;
@ -176,13 +180,13 @@ ErrorCode SshSession::writeToChannel(const QString &data,
QEventLoop wait; QEventLoop wait;
QObject::connect(this, &SshSession::writeToChannelFinished, &wait, &QEventLoop::quit); QObject::connect(this, &Session::writeToChannelFinished, &wait, &QEventLoop::quit);
wait.exec(); wait.exec();
return watcher.result(); return watcher.result();
} }
ErrorCode SshSession::initSftp(const ServerCredentials &credentials) ErrorCode Session::initSftp(const ServerCredentials &credentials)
{ {
m_session = ssh_new(); m_session = ssh_new();
@ -208,7 +212,7 @@ ErrorCode SshSession::initSftp(const ServerCredentials &credentials)
return ErrorCode::NoError; return ErrorCode::NoError;
} }
ErrorCode SshSession::sftpFileCopy(const std::string& localPath, const std::string& remotePath, const std::string& fileDesc) ErrorCode Session::sftpFileCopy(const SftpOverwriteMode overwriteMode, const std::string& localPath, const std::string& remotePath, const std::string& fileDesc)
{ {
if (m_sftpSession == NULL) { if (m_sftpSession == NULL) {
qDebug() << "ssh sftp session not initialized"; qDebug() << "ssh sftp session not initialized";
@ -216,15 +220,15 @@ ErrorCode SshSession::sftpFileCopy(const std::string& localPath, const std::stri
} }
QFutureWatcher<ErrorCode> watcher; QFutureWatcher<ErrorCode> watcher;
connect(&watcher, &QFutureWatcher<ErrorCode>::finished, this, &SshSession::sftpFileCopyFinished); connect(&watcher, &QFutureWatcher<ErrorCode>::finished, this, &Session::sftpFileCopyFinished);
QFuture<ErrorCode> future = QtConcurrent::run([this, &localPath, &remotePath, &fileDesc]() { QFuture<ErrorCode> future = QtConcurrent::run([this, overwriteMode, &localPath, &remotePath, &fileDesc]() {
int accessType = O_WRONLY | O_CREAT | O_TRUNC; int accessType = O_WRONLY | O_CREAT | overwriteMode;
sftp_file file; sftp_file file;
const size_t bufferSize = 16384; const size_t bufferSize = 16384;
char buffer[bufferSize]; char buffer[bufferSize];
file = sftp_open(m_sftpSession, remotePath.c_str(), accessType, 0);//S_IRWXU); file = sftp_open(m_sftpSession, remotePath.c_str(), accessType, S_IRWXU);
if (file == NULL) { if (file == NULL) {
qDebug() << ssh_get_error(m_session); qDebug() << ssh_get_error(m_session);
@ -243,7 +247,7 @@ ErrorCode SshSession::sftpFileCopy(const std::string& localPath, const std::stri
int bytesWritten = sftp_write(file, buffer, bufferSize); int bytesWritten = sftp_write(file, buffer, bufferSize);
std::string chunk(buffer, bufferSize); std::string chunk(buffer, bufferSize);
qDebug() << "write -> " << QString(chunk.c_str()); qDebug() << "sftp write: " << QString(chunk.c_str());
if (bytesWritten != bufferSize) { if (bytesWritten != bufferSize) {
fin.close(); fin.close();
@ -259,7 +263,7 @@ ErrorCode SshSession::sftpFileCopy(const std::string& localPath, const std::stri
fin.read(buffer, lastChunkSize); fin.read(buffer, lastChunkSize);
std::string chunk(buffer, lastChunkSize); std::string chunk(buffer, lastChunkSize);
qDebug() << "write -> " << QString(chunk.c_str()); qDebug() << "sftp write: " << QString(chunk.c_str());
int bytesWritten = sftp_write(file, buffer, lastChunkSize); int bytesWritten = sftp_write(file, buffer, lastChunkSize);
@ -270,7 +274,6 @@ ErrorCode SshSession::sftpFileCopy(const std::string& localPath, const std::stri
return ErrorCode::SshSftpError; return ErrorCode::SshSftpError;
} }
} }
} else { } else {
sftp_close(file); sftp_close(file);
qDebug() << ssh_get_error(m_session); qDebug() << ssh_get_error(m_session);
@ -291,8 +294,9 @@ ErrorCode SshSession::sftpFileCopy(const std::string& localPath, const std::stri
QEventLoop wait; QEventLoop wait;
QObject::connect(this, &SshSession::sftpFileCopyFinished, &wait, &QEventLoop::quit); QObject::connect(this, &Session::sftpFileCopyFinished, &wait, &QEventLoop::quit);
wait.exec(); wait.exec();
return watcher.result(); return watcher.result();
} }
}

View file

@ -3,6 +3,8 @@
#include <QObject> #include <QObject>
#include <fcntl.h>
#include <libssh/libssh.h> #include <libssh/libssh.h>
#include <libssh/sftp.h> #include <libssh/sftp.h>
@ -10,19 +12,27 @@
using namespace amnezia; using namespace amnezia;
class SshSession : public QObject namespace libssh {
enum SftpOverwriteMode {
/*! Overwrite any existing files */
SftpOverwriteExisting = O_TRUNC,
/*! Append new content if the file already exists */
SftpAppendToExisting = O_APPEND
};
class Session : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
SshSession(QObject *parent = nullptr); Session(QObject *parent = nullptr);
~SshSession(); ~Session();
ErrorCode initChannel(const ServerCredentials &credentials); ErrorCode initChannel(const ServerCredentials &credentials);
ErrorCode initSftp(const ServerCredentials &credentials); ErrorCode initSftp(const ServerCredentials &credentials);
ErrorCode writeToChannel(const QString &data, ErrorCode writeToChannel(const QString &data,
const std::function<void(const QString &)> &cbReadStdOut, const std::function<void(const QString &)> &cbReadStdOut,
const std::function<void(const QString &)> &cbReadStdErr); const std::function<void(const QString &)> &cbReadStdErr);
ErrorCode sftpFileCopy(const std::string& localPath, const std::string& remotePath, const std::string& fileDesc); ErrorCode sftpFileCopy(const SftpOverwriteMode overwriteMode, const std::string& localPath, const std::string& remotePath, const std::string& fileDesc);
private: private:
ErrorCode connectToHost(const ServerCredentials &credentials); ErrorCode connectToHost(const ServerCredentials &credentials);
@ -38,5 +48,6 @@ signals:
void writeToChannelFinished(); void writeToChannelFinished();
void sftpFileCopyFinished(); void sftpFileCopyFinished();
}; };
}
#endif // SSHSESSION_H #endif // SSHSESSION_H

View file

@ -49,9 +49,9 @@ void ManagementServer::onNewConnection()
m_socket = QPointer<QTcpSocket>(m_tcpServer->nextPendingConnection()); m_socket = QPointer<QTcpSocket>(m_tcpServer->nextPendingConnection());
if (m_tcpServer) m_tcpServer->close(); if (m_tcpServer) m_tcpServer->close();
QObject::connect(m_socket.data(), SIGNAL(disconnected()), this, SLOT(onSocketDisconnected())); QObject::connect(m_socket.data(), &QTcpSocket::disconnected, this, &ManagementServer::onSocketDisconnected);
QObject::connect(m_socket.data(), SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onSocketError(QAbstractSocket::SocketError))); QObject::connect(m_socket.data(), &QTcpSocket::errorOccurred, this, &ManagementServer::onSocketError);
QObject::connect(m_socket.data(), SIGNAL(readyRead()), this, SLOT(onReadyRead())); QObject::connect(m_socket.data(), &QTcpSocket::readyRead, this, &ManagementServer::onReadyRead);
} }
void ManagementServer::onSocketError(QAbstractSocket::SocketError socketError) void ManagementServer::onSocketError(QAbstractSocket::SocketError socketError)