2022-03-01 22:49:26 +00:00
|
|
|
//
|
|
|
|
// Created by oupson on 01/03/2022.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef FOOTERM_SSH_HPP
|
|
|
|
#define FOOTERM_SSH_HPP
|
|
|
|
|
|
|
|
#include <libssh2.h>
|
2022-03-04 07:49:21 +00:00
|
|
|
#include <libssh2_sftp.h>
|
2022-03-01 22:49:26 +00:00
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
class Session {
|
|
|
|
private:
|
|
|
|
LIBSSH2_SESSION *session;
|
|
|
|
LIBSSH2_CHANNEL *channel;
|
|
|
|
int sock;
|
2022-03-04 07:49:21 +00:00
|
|
|
LIBSSH2_SFTP* sftpSession;
|
2022-03-01 22:49:26 +00:00
|
|
|
|
|
|
|
static int openSocket(const char *addr, int port);
|
|
|
|
|
|
|
|
static bool authWithPublicKey(LIBSSH2_AGENT *agent, const char *username);
|
|
|
|
|
|
|
|
static bool authWithPassword(LIBSSH2_SESSION *session, const char *username, const char *password);
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit Session();
|
|
|
|
|
|
|
|
~Session();
|
|
|
|
|
|
|
|
void openConnection(const char *addr, int port, const char *username, const char *password);
|
|
|
|
|
|
|
|
void disconnect();
|
|
|
|
|
|
|
|
bool isConnected();
|
|
|
|
|
2022-03-04 07:49:21 +00:00
|
|
|
[[nodiscard]] int getSock() const {
|
2022-03-01 22:49:26 +00:00
|
|
|
return this->sock;
|
|
|
|
};
|
|
|
|
|
2022-03-04 07:49:21 +00:00
|
|
|
LIBSSH2_SESSION *getSession() {
|
|
|
|
return this->session;
|
|
|
|
}
|
|
|
|
|
2022-03-01 22:49:26 +00:00
|
|
|
LIBSSH2_CHANNEL *getChannel() {
|
|
|
|
return this->channel;
|
|
|
|
}
|
2022-03-04 07:49:21 +00:00
|
|
|
|
|
|
|
LIBSSH2_SFTP* getSftp() {
|
|
|
|
return this->sftpSession;
|
|
|
|
}
|
2022-03-01 22:49:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class SessionConnectException : public std::exception {
|
|
|
|
public:
|
|
|
|
explicit SessionConnectException(std::string msg) : m_msg(std::move(msg)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] const char *what() const noexcept override {
|
|
|
|
return m_msg.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string m_msg;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif //FOOTERM_SSH_HPP
|