-
Notifications
You must be signed in to change notification settings - Fork 2
/
webconsole.cpp
165 lines (140 loc) · 4.91 KB
/
webconsole.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "webconsole.h"
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QTextCodec>
#include "common.h"
#include "ui_webconsole.h"
#include "websocketserver.h"
WebConsole::WebConsole(QWidget *parent, ConnectInfo *connectInfo)
: QWidget(parent), ui(new Ui::WebConsole) {
ui->setupUi(this);
this->connectInfo = *connectInfo;
qDebug() << "QSslSocket=" << QSslSocket::sslLibraryBuildVersionString();
// qDebug() << "OpenSSL支持情况:" << QSslSocket::supportsSsl();
clientId = QString::number(QDateTime::currentDateTime().toMSecsSinceEpoch());
webChannel = new QWebChannel(this);
webChannel->registerObject(QStringLiteral("core"), this);
webView = new QWebEngineView(this);
webView->page()->setWebChannel(webChannel);
webView->resize(parent->size());
QFile file(":/html/xterm5/index.html");
if (!file.open(QIODevice::ReadOnly)) {
return;
}
QString htmlData = file.readAll().constData();
webView->setHtml(htmlData);
// webView->setUrl(QUrl("http://192.168.0.110:8080/"));
// webView->setUrl(QUrl("chrome://gpu"));
webView->show();
connect(webView, SIGNAL(loadFinished(bool)), this,
SLOT(pageLoadFinished(bool)));
}
void WebConsole::resizeEvent(QResizeEvent *) { webView->resize(this->size()); }
void WebConsole::pageLoadFinished(bool flag) {
webView->page()->runJavaScript(
"setWebSocketServerPort(" +
QString::number(
WebSocketServer::getInstance()->getWebSocketServerPort()) +
")");
webView->page()->runJavaScript("setClientId(" + clientId + ")");
this->ssh2connect("");
}
void WebConsole::channelDataHandle(QString data) {
#ifdef ENABLE_WS
WebSocketServer::getInstance()->sendQStringData(clientId, data);
#else
QJsonObject obj;
QJsonDocument doc;
obj["data"] = data;
doc.setObject(obj);
data = doc.toJson(QJsonDocument::Compact);
webView->page()->runJavaScript("xtermWrite(" + data + ")");
#endif
}
void WebConsole::connectSuccess() {
webView->page()->runJavaScript("connectSuccess()");
}
void WebConsole::ssh2connect(const QString &jsMsg) {
if (sshClient) {
disconnect(sshClient);
sshClient->stop();
delete sshClient;
sshClient = NULL;
}
if (connectInfo.authType == 1) {
sshClient =
new SSHClient(connectInfo.hostName, QString::number(connectInfo.port),
connectInfo.username, connectInfo.password);
}
if (connectInfo.authType == 2) {
sshClient =
new SSHClient(connectInfo.hostName, QString::number(connectInfo.port),
connectInfo.username, connectInfo.publicKeyPath,
connectInfo.privateKeyPath, connectInfo.passPhrase);
}
sshClient->pty_cols = cols;
sshClient->pty_rows = rows;
connect(sshClient, SIGNAL(connectSuccess()), this, SLOT(connectSuccess()));
connect(sshClient, &SSHClient::errorMsg, this, [=](QString errMsg) {
// QMessageBox::warning(this, "错误提示", errMsg);
AlertWindow *alertWindow = new AlertWindow(webView, true);
alertWindow->setTitleText("错误提示");
alertWindow->setContentText(errMsg + "\n是否重新连接");
alertWindow->setConfirmButtonText("重连");
connect(alertWindow, &AlertWindow::confirmEvent, this,
[=]() { this->ssh2connect(""); });
alertWindow->show();
this->installEventFilter(alertWindow);
// close();
});
connect(sshClient, &SSHClient::disconnected, this, [=]() {
AlertWindow *alertWindow = new AlertWindow(webView, true);
alertWindow->setContentText("当前连接已关闭,是否重新连接");
alertWindow->setConfirmButtonText("重连");
connect(alertWindow, &AlertWindow::confirmEvent, this,
[=]() { this->ssh2connect(""); });
alertWindow->show();
this->installEventFilter(alertWindow);
});
connect(sshClient, &SSHClient::openChannelSuccess, this,
[=]() { openChannelSeccess = true; });
connect(sshClient, &SSHClient::readChannelData, this,
&WebConsole::channelDataHandle, Qt::DirectConnection);
// connect(sshClient, &SSHClient::readChannelData, this,
// &WebConsole::channelDataHandle, Qt::DirectConnection);
sshClient->start();
}
void WebConsole::recieveJsMessage(const QString &shell) {
sshClient->exec(shell);
// qDebug() << "input shell:" << shell;
}
void WebConsole::setChannelRequestPtySize(const QString &size) {
QStringList ss = size.split(",");
rows = ss[0].toInt();
cols = ss[1].toInt();
if (openChannelSeccess) {
sshClient->setChannelRequestPtySize(rows, cols);
}
}
void WebConsole::closeEvent(QCloseEvent *event) {
webView->page()->runJavaScript("closews()");
if (sshClient) {
sshClient->stop();
}
}
void WebConsole::paintEvent(QPaintEvent *event) {
// Q_UNUSED(event);
// QPainter p(this);
// p.setPen(Qt::NoPen);
// p.setBrush(Qt::red);
// p.drawRect(rect());
}
WebConsole::~WebConsole() {
delete ui;
if (sshClient) {
delete sshClient;
}
delete webView;
delete webChannel;
}