-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.js
162 lines (138 loc) · 5.65 KB
/
app.js
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
const express = require("express");
const server = express();
const compression = require('compression');
require("express-ws")(server, null, {
// 设置最大负载大小为16MB
wsOptions: {
maxPayload: 16 * 1024 * 1024
}
});
// 启用压缩
server.use(compression());
server.use(express.static('./dist/'));
const readline = require('readline');
const { NodeSSH } = require('node-ssh');
const ssh = new NodeSSH();
async function handleConnection (socket) {
console.log('连接已建立');
let isAlive = true;
let timer = null;
let sshCredentials;
let sshCredentialsReceived = false; // 新增状态变量
// 设置最大监听器数量
socket.setMaxListeners(15);
// 检查客户端是否活跃
const checkAlive = () => {
if (!isAlive) {
console.log('WebSocket 连接已超时');
socket.terminate();
return;
}
isAlive = false;
socket.ping(null, { mask: false, binary: true });
timer = setTimeout(checkAlive, 1000 * 60 * 10); // 10 分钟
};
socket.on('message', (data) => {
// 如果尚未接收到 SSH 凭据,则尝试解析 JSON
if (!sshCredentialsReceived) {
try {
sshCredentials = JSON.parse(data);
sshCredentialsReceived = true; // 更新状态变量
} catch (err) {
console.error('解析 SSH 凭据时出错', err);
socket.terminate();
return;
}
// 使用解析后的 SSH 凭据连接 SSH 服务器
ssh.connect({
host: sshCredentials.ip,
port: sshCredentials.port,
username: sshCredentials.username,
password: sshCredentials.password,
})
.then(() => {
console.log('SSH 连接已建立');
// 创建 SSH Shell
ssh.requestShell().then((stream) => {
// 使用 readline 处理终端输入
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.on('line', (line) => {
stream.write(line);
});
// 处理 SSH Shell 输入和输出
stream.on('data', (data) => {
isAlive = true;
socket.send(data.toString());
});
// socket.on('message', (data) => {
// // if (sshCredentialsReceived) { // 确保 SSH 凭据已接收
// // isAlive = true;
// // stream.write(data);
// // }
// });
socket.on('message', (data) => {
if (typeof data !== 'string') {
console.error('接收到非字符串信息');
return; // 提前返回以避免深层嵌套
}
try {
const message = JSON.parse(data);
if (message.action === 'resize') {
// 调整终端大小
stream.setWindow(message.rows, message.cols);
} else if (sshCredentialsReceived) {
// 处理非 'resize' 操作的消息
isAlive = true;
stream.write(data);
}
} catch (e) {
// 解析失败,但数据是字符串
if (sshCredentialsReceived) {
isAlive = true;
stream.write(data);
} else {
console.error('SSH 凭据未接收');
}
}
});
socket.on('close', () => {
console.log('WebSocket 连接已关闭');
isAlive = true;
ssh.dispose(); // 关闭 SSH 连接
socket.terminate();
});
// 开始心跳检测
timer = setTimeout(checkAlive, 1000 * 60 * 10); // 10 分钟
}).catch((err) => {
console.error('创建 SSH Shell 失败', err);
socket.terminate();
ssh.dispose(); // 关闭 SSH 连接
});
}).catch((err) => {
console.error('SSH 连接错误', err);
socket.terminate();
});
} else {
// 处理非 SSH 凭据的数据(终端输入)
if (sshCredentialsReceived) {
isAlive = true;
}
}
});
// 监听 WebSocket 连接错误
socket.on('error', (err) => {
console.error('WebSocket 连接错误', err);
ssh.dispose(); // 关闭 SSH 连接
});
}
server.ws("/ssh", (ws, req) => {
// 连接成功后发送 test success
console.info("WebSocket 连接已建立.");
handleConnection(ws);
});
server.listen(8080, () => {
console.info("服务器已启动");
});