-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
69 lines (57 loc) · 1.8 KB
/
app.js
File metadata and controls
69 lines (57 loc) · 1.8 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
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const WebSocket = require('ws');
const http = require('http');
const indexRouter = require('./routes/index');
const authService = require('./services/auth');
const { terminalService } = require('./services/terminal');
const app = express();
// 视图引擎设置
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// 中间件
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// 认证中间件
app.use(authService.validateToken.bind(authService));
// 路由
app.use('/', indexRouter);
// 错误处理
app.use(function(err, req, res, next) {
console.error(err.stack);
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: req.app.get('env') === 'development' ? err : {}
});
});
// WebSocket服务器设置
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });
wss.on('connection', async (ws, req) => {
try {
// 解析token
const url = new URL(req.url, `http://${req.headers.host}`);
const token = url.searchParams.get('token');
// 验证token
req.headers.authorization = `Bearer ${token}`;
await new Promise((resolve, reject) => {
authService.validateToken(
req,
{ status: () => ({ json: (data) => reject(new Error(data.error)) }) },
resolve
);
});
// 处理终端连接
await terminalService.handleConnection(ws, req);
} catch (error) {
console.error('WebSocket连接错误:', error);
ws.close(1008, 'Authentication failed');
}
});
module.exports = { app, server };