2022-12-20 13:43:46 +03:00
|
|
|
#ifndef SSHCLIENT_H
|
|
|
|
|
#define SSHCLIENT_H
|
|
|
|
|
|
|
|
|
|
#include <QObject>
|
2024-03-06 02:24:28 +01:00
|
|
|
#include <QFile>
|
2022-12-20 13:43:46 +03:00
|
|
|
|
2022-12-26 12:43:51 +03:00
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
|
|
#include <libssh/libssh.h>
|
|
|
|
|
|
2026-04-30 14:53:03 +08:00
|
|
|
#include "core/utils/errorCodes.h"
|
|
|
|
|
#include "core/utils/routeModes.h"
|
|
|
|
|
#include "core/utils/commonStructs.h"
|
2022-12-20 13:43:46 +03:00
|
|
|
|
|
|
|
|
using namespace amnezia;
|
|
|
|
|
|
2022-12-23 10:13:06 +03:00
|
|
|
namespace libssh {
|
2024-03-06 02:24:28 +01:00
|
|
|
enum ScpOverwriteMode {
|
2022-12-26 12:43:51 +03:00
|
|
|
/*! Overwrite any existing files */
|
2024-03-06 02:24:28 +01:00
|
|
|
ScpOverwriteExisting = O_TRUNC,
|
2022-12-26 12:43:51 +03:00
|
|
|
/*! Append new content if the file already exists */
|
2024-03-06 02:24:28 +01:00
|
|
|
ScpAppendToExisting = O_APPEND
|
2022-12-26 12:43:51 +03:00
|
|
|
};
|
2022-12-23 10:13:06 +03:00
|
|
|
class Client : public QObject
|
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
public:
|
2024-03-06 02:24:28 +01:00
|
|
|
Client() = default;
|
|
|
|
|
~Client() = default;
|
2022-12-20 13:43:46 +03:00
|
|
|
|
2023-04-03 17:27:55 +03:00
|
|
|
ErrorCode connectToHost(const ServerCredentials &credentials);
|
2022-12-26 12:43:51 +03:00
|
|
|
void disconnectFromHost();
|
|
|
|
|
ErrorCode executeCommand(const QString &data,
|
|
|
|
|
const std::function<ErrorCode (const QString &, Client &)> &cbReadStdOut,
|
|
|
|
|
const std::function<ErrorCode (const QString &, Client &)> &cbReadStdErr);
|
|
|
|
|
ErrorCode writeResponse(const QString &data);
|
2024-03-06 02:24:28 +01:00
|
|
|
ErrorCode scpFileCopy(const ScpOverwriteMode overwriteMode,
|
2024-02-17 13:07:17 -08:00
|
|
|
const QString &localPath,
|
|
|
|
|
const QString &remotePath,
|
2024-03-06 02:24:28 +01:00
|
|
|
const QString &fileDesc);
|
2023-04-04 13:32:37 +03:00
|
|
|
ErrorCode getDecryptedPrivateKey(const ServerCredentials &credentials, QString &decryptedPrivateKey, const std::function<QString()> &passphraseCallback);
|
2022-12-26 12:43:51 +03:00
|
|
|
private:
|
|
|
|
|
ErrorCode closeChannel();
|
2024-03-06 02:24:28 +01:00
|
|
|
void closeScpSession();
|
2023-06-10 05:25:41 +03:00
|
|
|
ErrorCode fromLibsshErrorCode();
|
2024-03-06 02:24:28 +01:00
|
|
|
ErrorCode fromFileErrorCode(QFileDevice::FileError fileError);
|
2023-04-02 09:09:20 +03:00
|
|
|
static int callback(const char *prompt, char *buf, size_t len, int echo, int verify, void *userdata);
|
2022-12-26 12:43:51 +03:00
|
|
|
|
2023-02-01 09:56:55 +03:00
|
|
|
ssh_session m_session = nullptr;
|
|
|
|
|
ssh_channel m_channel = nullptr;
|
2024-03-06 02:24:28 +01:00
|
|
|
ssh_scp m_scpSession = nullptr;
|
2023-04-02 09:09:20 +03:00
|
|
|
|
|
|
|
|
static std::function<QString()> m_passphraseCallback;
|
2022-12-26 12:43:51 +03:00
|
|
|
signals:
|
|
|
|
|
void writeToChannelFinished();
|
2024-03-06 02:24:28 +01:00
|
|
|
void scpFileCopyFinished();
|
2022-12-23 10:13:06 +03:00
|
|
|
};
|
|
|
|
|
}
|
2022-12-20 13:43:46 +03:00
|
|
|
|
|
|
|
|
#endif // SSHCLIENT_H
|