103 lines
2.8 KiB
C++
103 lines
2.8 KiB
C++
#ifndef SSHMANAGER_H
|
|
#define SSHMANAGER_H
|
|
|
|
// 如果libssh2版本 < 1.9.0 在包含头文件之前添加:
|
|
#define LIBSSH2_CHANNEL_EXTENDED_DATA_DEFAULT 0
|
|
#define LIBSSH2_CHANNEL_EXTENDED_DATA_IGNORE 1
|
|
#define LIBSSH2_CHANNEL_EXTENDED_DATA_MERGE 2
|
|
|
|
#include <QObject>
|
|
#include <QString>
|
|
#include <QTimer>
|
|
#include <QProcess>
|
|
#include <QTcpSocket>
|
|
#include "libssh2.h"
|
|
#include <libssh2_sftp.h>
|
|
#include <QJsonDocument>
|
|
#include <QtNetwork>
|
|
#include <QJsonObject>
|
|
#include <QJsonArray>
|
|
#include <QString>
|
|
#include <QFile>
|
|
#include <QRegularExpression>
|
|
#include <QSocketNotifier>
|
|
|
|
class SSHManager : public QObject {
|
|
Q_OBJECT
|
|
Q_DISABLE_COPY(SSHManager) // 禁止拷贝
|
|
|
|
public:
|
|
static SSHManager* instance(); // 单例访问方法
|
|
//判断是否连接了服务器
|
|
bool isConnected();
|
|
//重连
|
|
void reconnect();
|
|
//连接至服务器
|
|
Q_INVOKABLE void connectAndStartShell(const QString& ip,
|
|
const QString& port,
|
|
const QString& username,
|
|
const QString& password);
|
|
//写入控制台内容
|
|
Q_INVOKABLE void sendInput(const QString &input);
|
|
//断开连接
|
|
Q_INVOKABLE void disconnectFromServer();
|
|
|
|
public:
|
|
// 新增枚举类型
|
|
enum ConnectionStep {
|
|
CONNECTION_STARTUP,
|
|
AUTHENTICATION,
|
|
CHANNEL_CREATION,
|
|
PTY_SETUP,
|
|
SHELL_START
|
|
};
|
|
// 新增成员变量
|
|
ConnectionStep m_connectionStep;
|
|
QSocketNotifier* m_notifier = nullptr;
|
|
|
|
// 新增成员函数声明
|
|
void handleConnected();
|
|
void trySessionStartup();
|
|
void tryAuthentication();
|
|
void tryChannelCreation();
|
|
void tryPtySetup();
|
|
void tryShellStart();
|
|
void setupSocketNotifier();
|
|
void handleNextStep();
|
|
void cleanup();
|
|
void readShellOutput();
|
|
signals :
|
|
// 连接成功
|
|
void connectionSuccess();
|
|
// 连接失败
|
|
void connectionFailed(const QString &error);
|
|
// 状态消息
|
|
void statusMessage(const QString &message);
|
|
// 断开连接
|
|
void disconnected();
|
|
//执行命令输出信号
|
|
void shellOutput(const QString &data); // 实时输出信号
|
|
|
|
private:
|
|
explicit SSHManager(QObject *parent = nullptr); // 私有构造函数
|
|
~SSHManager(); // 私有析构函数
|
|
|
|
|
|
QString Myip,Myport,Myuser,Mypass;
|
|
|
|
static SSHManager* m_instance; // 单例实例指针
|
|
LIBSSH2_SESSION *m_session = nullptr;
|
|
LIBSSH2_CHANNEL *m_channel = nullptr;
|
|
int m_sock = -1;
|
|
QTcpSocket *m_socket;
|
|
|
|
// 添加成员变量
|
|
int m_timeout = 30000; // 命令执行超时时间(毫秒)
|
|
// 新增发送相关成员变量
|
|
QByteArray m_writeBuffer;
|
|
qint64 m_writeOffset = 0;
|
|
void trySendData(); // 新增发送尝试方法
|
|
};
|
|
|
|
#endif // SSHMANAGER_H
|