-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
78 lines (63 loc) · 1.96 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
const express = require('express');
const { Client } = require('ssh2');
const http = require('http');
const socketIO = require('socket.io');
const ansiHTML = require('ansi-html');
const app = express();
const server = http.createServer(app);
const io = socketIO(server);
const port = 3000;
app.use(express.urlencoded({ extended: true }));
app.use(express.static('public'));
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
app.get('/main.css', (req, res) => {
res.sendFile(__dirname + '/main.css');
});
app.get('/favicon.svg', (req, res) => {
res.sendFile(__dirname + '/favicon.svg');
});
// 不需要猫咪背景图片了~
// app.get('/cat.jpg', (req, res) => {
// res.sendFile(__dirname + '/cat.jpg');
// });
io.on('connection', (socket) => {
//获取当前时间
var date = new Date();
//获取客户端IP地址
var clientIp = socket.request.connection.remoteAddress;
console.log('[' + date.toLocaleString() + '] ' + clientIp + ' 已连接 ');
socket.on('execute', (command, host, port, username, password) => {
const conn = new Client();
conn.on('ready', () => {
conn.exec(command, (err, stream) => {
if (err) {
socket.emit('output', '错误:' + err.message);
conn.end();
return;
}
stream.on('close', (code, signal) => {
conn.end();
socket.disconnect(); // 断开连接
}).on('data', (data) => {
socket.emit('output', ansiHTML(data.toString()));
}).stderr.on('data', (data) => {
socket.emit('output', ansiHTML(data.toString()));
});
});
});
conn.on('error', (err) => {
socket.emit('output', 'SSH 连接错误:' + err.message);
});
conn.connect({
host: host,
port: port,
username: username,
password: password
});
});
});
server.listen(port, () => {
console.log(`服务器已启动,访问 http://localhost:${port}`);
});