Repository navigation
Expand file tree
/
Copy pathwebconsole.cpp
More file actions
170 lines (144 loc) · 4.77 KB
/
Copy pathwebconsole.cpp
File metadata and controls
170 lines (144 loc) · 4.77 KB
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
166
167
168
169
170
#include "webconsole.h"
#include "ui_webconsole.h"
#include <QFile>
#include <QFileInfo>
#include <QDir>
#include "common.h"
#include <QTextCodec>
#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();
webChannel = new QWebChannel(this);
webChannel->registerObject(QStringLiteral("core"), this);
webView = new QWebEngineView(this);
webView->page()->setWebChannel(webChannel);
QFile file(":/html/xterm4/index.html");
if (!file.open(QIODevice::ReadOnly))
{
return;
}
QString htmlData = file.readAll().constData();
webView->setHtml(htmlData);
// webView->load(QUrl("http://localhost:8080"));
webView->show();
connect(webView,SIGNAL(loadFinished(bool)) ,this,SLOT(pageLoadFinished(bool)));
}
void WebConsole::resizeEvent(QResizeEvent *)
{
webView->resize(this->size());
}
void WebConsole::pageLoadFinished(bool flag){
// clientId=QString::number(QDateTime::currentDateTime().toMSecsSinceEpoch());
// webView->page()->runJavaScript("setClientId("+clientId+")");
this->ssh2connect("");
}
void WebConsole::connectSuccess(){
}
void WebConsole::ssh2connect(const QString& jsMsg){
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;
sshClient->start();
connect(sshClient,SIGNAL(connectSuccess()),this,SLOT(connectSuccess()));
connect(sshClient,&SSHClient::errorMsg,this,[=](QString errMsg){
QMessageBox::warning(this,"错误提示",errMsg);
close();
});
connect(sshClient,&SSHClient::authSuccess,this,[=](){
});
connect(sshClient,&SSHClient::openChannelSuccess,this,[=](){
openChannelSeccess=true;
QTimer::singleShot(1000,this,[=]{
webView->page()->runJavaScript("connectSuccess()");
});
});
connect(sshClient,&SSHClient::readChannelData,this,[&](QString data){
QJsonObject obj;
QJsonDocument doc;
// QByteArray buffer = data.toUtf8();
// int length = buffer.length();
// if(length>3){
// int i=length-2;
// char c1 = buffer.at(i);
// char c2 = buffer.at(i+1);
// if(c1&0x80){
// QByteArray dataArray=buffer.mid(0,i);
// ba.append(cnBuffer);
// cnBuffer.clear();
// ba.append(dataArray);
// cnBuffer.append(c1);
// cnBuffer.append(c2);
// }else if(c2&0x80){
// QByteArray dataArray=buffer.mid(0,i+1);
// ba.append(cnBuffer);
// cnBuffer.clear();
// ba.append(dataArray);
// cnBuffer.append(c1);
// cnBuffer.append(c2);
// }else{
// ba.append(cnBuffer);
// cnBuffer.clear();
// ba.append(buffer);
// }
// }else{
// ba.append(cnBuffer);
// cnBuffer.clear();
// ba.append(buffer);
// }
// QString d(ba);
// ba.clear();
// obj["data"]=d;
obj["data"]=data;
doc.setObject(obj);
data=doc.toJson(QJsonDocument::Compact);
webView->page()->runJavaScript("xtermWrite("+data+")");
// WebSocketServer::instance->sendMsg(clientId,data);
});
}
void WebConsole::recieveJsMessage(const QString& shell){
sshClient->exec(shell.toStdString().c_str());
// 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){
qDebug() << "close window";
sshClient->stop();
QTimer::singleShot(1000,this,[&](){
WebSocketServer::deleteClient(clientId);
delete this;
});
}
void WebConsole::paintEvent(QPaintEvent *event)
{
// Q_UNUSED(event);
// QPainter p(this);
// p.setPen(Qt::NoPen);
// p.setBrush(Qt::red);
// p.drawRect(rect());
}
WebConsole::~WebConsole()
{
ba.clear();
delete ui;
delete sshClient;
delete webView;
delete webChannel;
}