vk-ssl-auto-deploy 0.6.7 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/app.js +18 -7
- package/config.json +7 -7
- package/package.json +4 -4
- package/routes/admin.js +266 -0
- package/routes/cert.js +134 -17
- package/utils/websocket.js +116 -0
- package/views/admin.ejs +1319 -0
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
const WebSocket = require('ws');
|
|
2
|
+
|
|
3
|
+
// WebSocket 客户端集合
|
|
4
|
+
const wsClients = new Set();
|
|
5
|
+
|
|
6
|
+
// 清理ANSI颜色代码
|
|
7
|
+
function stripAnsiCodes(str) {
|
|
8
|
+
// 移除所有ANSI转义序列
|
|
9
|
+
return str.replace(/\x1b\[[0-9;]*m/g, '');
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// 广播日志到所有WebSocket客户端
|
|
13
|
+
function broadcastLog(message, type = 'info') {
|
|
14
|
+
// 清理ANSI颜色代码
|
|
15
|
+
const cleanMessage = stripAnsiCodes(message);
|
|
16
|
+
const data = JSON.stringify({ type, message: cleanMessage });
|
|
17
|
+
wsClients.forEach(client => {
|
|
18
|
+
if (client.readyState === WebSocket.OPEN) {
|
|
19
|
+
try {
|
|
20
|
+
client.send(data);
|
|
21
|
+
} catch (error) {
|
|
22
|
+
// 使用原始console.error避免递归
|
|
23
|
+
originalConsoleError('[WebSocket] 发送失败:', error.message);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// 保存原始的 console 方法
|
|
30
|
+
const originalConsoleLog = console.log;
|
|
31
|
+
const originalConsoleError = console.error;
|
|
32
|
+
const originalConsoleWarn = console.warn;
|
|
33
|
+
|
|
34
|
+
// 拦截 console 方法,将日志广播到 WebSocket
|
|
35
|
+
function interceptConsole() {
|
|
36
|
+
console.log = function(...args) {
|
|
37
|
+
originalConsoleLog.apply(console, args);
|
|
38
|
+
const message = args.map(arg =>
|
|
39
|
+
typeof arg === 'object' ? JSON.stringify(arg) : String(arg)
|
|
40
|
+
).join(' ');
|
|
41
|
+
|
|
42
|
+
// 根据消息内容判断日志类型
|
|
43
|
+
let type = 'info';
|
|
44
|
+
// 优先判断错误类型
|
|
45
|
+
if ((message.includes('失败') || message.includes('错误') || message.includes('Error') || message.includes('error') || message.includes('[失败]')) && !message.includes('✓') && !message.includes('成功') && !message.includes('统计') ) {
|
|
46
|
+
type = 'error';
|
|
47
|
+
} else if (message.includes('[警告]') || message.includes('[需要更新]') || message.includes('[需要下载]')) {
|
|
48
|
+
type = 'warning';
|
|
49
|
+
} else if (message.includes('[成功]') || message.includes('✓')) {
|
|
50
|
+
type = 'success';
|
|
51
|
+
} else if (message.includes('[步骤') || message.includes('[任务')) {
|
|
52
|
+
type = 'info';
|
|
53
|
+
} else {
|
|
54
|
+
type = 'dim';
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
broadcastLog(message, type);
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
console.error = function(...args) {
|
|
61
|
+
originalConsoleError.apply(console, args);
|
|
62
|
+
const message = args.map(arg =>
|
|
63
|
+
typeof arg === 'object' ? JSON.stringify(arg) : String(arg)
|
|
64
|
+
).join(' ');
|
|
65
|
+
broadcastLog(message, 'error');
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
console.warn = function(...args) {
|
|
69
|
+
originalConsoleWarn.apply(console, args);
|
|
70
|
+
const message = args.map(arg =>
|
|
71
|
+
typeof arg === 'object' ? JSON.stringify(arg) : String(arg)
|
|
72
|
+
).join(' ');
|
|
73
|
+
broadcastLog(message, 'warning');
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// 设置 WebSocket 服务器
|
|
78
|
+
function setupWebSocket(server) {
|
|
79
|
+
// 创建 WebSocket 服务器
|
|
80
|
+
const wss = new WebSocket.Server({
|
|
81
|
+
server,
|
|
82
|
+
path: '/ws'
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
// WebSocket 连接处理
|
|
86
|
+
wss.on('connection', (ws) => {
|
|
87
|
+
console.log('[WebSocket] 新客户端连接');
|
|
88
|
+
wsClients.add(ws);
|
|
89
|
+
|
|
90
|
+
// 发送欢迎消息
|
|
91
|
+
ws.send(JSON.stringify({
|
|
92
|
+
type: 'success',
|
|
93
|
+
message: '服务器 连接成功'
|
|
94
|
+
}));
|
|
95
|
+
|
|
96
|
+
ws.on('close', () => {
|
|
97
|
+
console.log('[WebSocket] 客户端断开连接');
|
|
98
|
+
wsClients.delete(ws);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
ws.on('error', (error) => {
|
|
102
|
+
console.error('[WebSocket] 错误:', error.message);
|
|
103
|
+
wsClients.delete(ws);
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
// 拦截 console 方法
|
|
108
|
+
interceptConsole();
|
|
109
|
+
|
|
110
|
+
return wss;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
module.exports = {
|
|
114
|
+
setupWebSocket,
|
|
115
|
+
broadcastLog
|
|
116
|
+
};
|