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}/..)
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/ssh/qssh.cmake)
#include(${CLIENT_ROOT_DIR}/3rd/QtSsh/src/botan/botan.cmake)
if(NOT IOS AND NOT ANDROID)
include(${CLIENT_ROOT_DIR}/3rd/SingleApplication/singleapplication.cmake)

View file

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

View file

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

View file

@ -22,6 +22,7 @@
#include <thread>
#include "containers/containers_defs.h"
#include "debug.h"
#include "server_defs.h"
#include "settings.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 &)> &cbReadStdErr) {
std::shared_ptr<SshSession> session = m_sshClient.getSession();
std::shared_ptr<libssh::Session> session = m_sshClient.getSession();
if (!session) {
return ErrorCode::SshInternalError;
}
@ -117,7 +118,7 @@ ErrorCode ServerController::runContainerScript(const ServerCredentials &credenti
ErrorCode ServerController::uploadTextFileToContainer(DockerContainer container,
const ServerCredentials &credentials, const QString &file, const QString &path,
QSsh::SftpOverwriteMode overwriteMode)
libssh::SftpOverwriteMode overwriteMode)
{
ErrorCode e = ErrorCode::NoError;
QString tmpFileName = QString("/tmp/%1.tmp").arg(Utils::getRandomString(16));
@ -141,14 +142,14 @@ ErrorCode ServerController::uploadTextFileToContainer(DockerContainer container,
if (e) return e;
if (overwriteMode == QSsh::SftpOverwriteMode::SftpOverwriteExisting) {
if (overwriteMode == libssh::SftpOverwriteMode::SftpOverwriteExisting) {
e = runScript(credentials,
replaceVars(QString("sudo docker cp %1 $CONTAINER_NAME:/%2").arg(tmpFileName).arg(path),
genVarsForScript(credentials, container)), cbReadStd, cbReadStd);
if (e) return e;
}
else if (overwriteMode == QSsh::SftpOverwriteMode::SftpAppendToExisting) {
else if (overwriteMode == libssh::SftpOverwriteMode::SftpAppendToExisting) {
e = runScript(credentials,
replaceVars(QString("sudo docker cp %1 $CONTAINER_NAME:/%2").arg(tmpFileName).arg(tmpFileName),
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,
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) {
return ErrorCode::SshInternalError;
}
@ -249,7 +250,7 @@ ErrorCode ServerController::uploadFileToHost(const ServerCredentials &credential
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) {
return error;
}

View file

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

View file

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

View file

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

View file

@ -4,15 +4,19 @@
#include <QtConcurrent>
#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) {
ssh_channel_send_eof(m_channel);
}
@ -26,10 +30,10 @@ SshSession::~SshSession()
ssh_disconnect(m_session);
}
ssh_free(m_session);
}
}
ErrorCode SshSession::connectToHost(const ServerCredentials &credentials)
{
ErrorCode Session::connectToHost(const ServerCredentials &credentials)
{
if (m_session == NULL) {
return ErrorCode::InternalError;
}
@ -71,10 +75,10 @@ ErrorCode SshSession::connectToHost(const ServerCredentials &credentials)
}
return ErrorCode::NoError;
}
}
ErrorCode SshSession::initChannel(const ServerCredentials &credentials)
{
ErrorCode Session::initChannel(const ServerCredentials &credentials)
{
m_session = ssh_new();
ErrorCode error = connectToHost(credentials);
@ -117,19 +121,19 @@ ErrorCode SshSession::initChannel(const ServerCredentials &credentials)
}
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 &)> &cbReadStdErr)
{
{
if (m_channel == NULL) {
qDebug() << "ssh channel not initialized";
return ErrorCode::SshAuthenticationError;
}
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]() {
const size_t bufferSize = 2048;
@ -157,7 +161,7 @@ ErrorCode SshSession::writeToChannel(const QString &data,
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;
@ -176,14 +180,14 @@ ErrorCode SshSession::writeToChannel(const QString &data,
QEventLoop wait;
QObject::connect(this, &SshSession::writeToChannelFinished, &wait, &QEventLoop::quit);
QObject::connect(this, &Session::writeToChannelFinished, &wait, &QEventLoop::quit);
wait.exec();
return watcher.result();
}
}
ErrorCode SshSession::initSftp(const ServerCredentials &credentials)
{
ErrorCode Session::initSftp(const ServerCredentials &credentials)
{
m_session = ssh_new();
ErrorCode error = connectToHost(credentials);
@ -206,25 +210,25 @@ ErrorCode SshSession::initSftp(const ServerCredentials &credentials)
}
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) {
qDebug() << "ssh sftp session not initialized";
return ErrorCode::SshSftpError;
}
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]() {
int accessType = O_WRONLY | O_CREAT | O_TRUNC;
QFuture<ErrorCode> future = QtConcurrent::run([this, overwriteMode, &localPath, &remotePath, &fileDesc]() {
int accessType = O_WRONLY | O_CREAT | overwriteMode;
sftp_file file;
const size_t bufferSize = 16384;
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) {
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);
std::string chunk(buffer, bufferSize);
qDebug() << "write -> " << QString(chunk.c_str());
qDebug() << "sftp write: " << QString(chunk.c_str());
if (bytesWritten != bufferSize) {
fin.close();
@ -259,7 +263,7 @@ ErrorCode SshSession::sftpFileCopy(const std::string& localPath, const std::stri
fin.read(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);
@ -270,7 +274,6 @@ ErrorCode SshSession::sftpFileCopy(const std::string& localPath, const std::stri
return ErrorCode::SshSftpError;
}
}
} else {
sftp_close(file);
qDebug() << ssh_get_error(m_session);
@ -291,8 +294,9 @@ ErrorCode SshSession::sftpFileCopy(const std::string& localPath, const std::stri
QEventLoop wait;
QObject::connect(this, &SshSession::sftpFileCopyFinished, &wait, &QEventLoop::quit);
QObject::connect(this, &Session::sftpFileCopyFinished, &wait, &QEventLoop::quit);
wait.exec();
return watcher.result();
}
}

View file

@ -3,6 +3,8 @@
#include <QObject>
#include <fcntl.h>
#include <libssh/libssh.h>
#include <libssh/sftp.h>
@ -10,20 +12,28 @@
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
public:
SshSession(QObject *parent = nullptr);
~SshSession();
public:
Session(QObject *parent = nullptr);
~Session();
ErrorCode initChannel(const ServerCredentials &credentials);
ErrorCode initSftp(const ServerCredentials &credentials);
ErrorCode writeToChannel(const QString &data,
const std::function<void(const QString &)> &cbReadStdOut,
const std::function<void(const QString &)> &cbReadStdErr);
ErrorCode sftpFileCopy(const std::string& localPath, const std::string& remotePath, const std::string& fileDesc);
private:
ErrorCode sftpFileCopy(const SftpOverwriteMode overwriteMode, const std::string& localPath, const std::string& remotePath, const std::string& fileDesc);
private:
ErrorCode connectToHost(const ServerCredentials &credentials);
ssh_session m_session;
@ -34,9 +44,10 @@ private:
bool m_isSessionConnected = false;
bool m_isNeedSendChannelEof = false;
bool m_isSftpInitialized = false;
signals:
signals:
void writeToChannelFinished();
void sftpFileCopyFinished();
};
};
}
#endif // SSHSESSION_H

View file

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