新增进程内限流并挂到订单/档案写接口;WS 设 maxPayload、同连接串行处理、按用户限频;鉴权只读请求头并区分登录失效与用户服务故障;访问日志只记路径,避免 token 落盘;陪诊师列表分页夹紧。
204 lines
6.3 KiB
JavaScript
204 lines
6.3 KiB
JavaScript
import WebSocket, { WebSocketServer } from 'ws';
|
|
import http from 'http';
|
|
import { DBModel } from "./models/index.js";
|
|
import { chatTask } from "./agent/bots/bot-escort/task.js";
|
|
import { adminAgent } from "./agent/bots/bot-escort-admin/agent.js"
|
|
import { getUserByToken } from "./utils/user_service.js";
|
|
import { isAdmin } from "./utils/permissions.js";
|
|
import { createUserLimiter } from "./middleware/ratelimit.js";
|
|
|
|
let systemMessageSequence = 0;
|
|
|
|
// 单连接上行帧上限:超限会被 ws 直接断开,避免超大 JSON 占用内存与 CPU
|
|
const MAX_PAYLOAD_BYTES = 256 * 1024;
|
|
// 单用户聊天频次限制:AI 调用有成本,且同一用户的会话本就串行处理
|
|
const chatLimiter = createUserLimiter(20);
|
|
|
|
// 所有下行帧保持同一结构,调用方无需按消息类型猜测字段。
|
|
const sendFrame = (ws, source, type, content = '', id = '') => {
|
|
if (ws.readyState !== WebSocket.OPEN) return;
|
|
ws.send(JSON.stringify({
|
|
source,
|
|
type,
|
|
content,
|
|
id: id || `system_${Date.now()}_${++systemMessageSequence}`
|
|
}));
|
|
};
|
|
|
|
export default class WebSocketServerManager {
|
|
constructor(port = 8080) {
|
|
this.port = port;
|
|
this.wss = null;
|
|
this.server = null;
|
|
}
|
|
|
|
start() {
|
|
this.server = http.createServer((req, res) => {
|
|
res.writeHead(426, { 'Content-Type': 'text/plain' });
|
|
res.end('Upgrade Required: WebSocket expected');
|
|
});
|
|
|
|
this.wss = new WebSocketServer({
|
|
server: this.server,
|
|
maxPayload: MAX_PAYLOAD_BYTES,
|
|
perMessageDeflate: {
|
|
zlibDeflateOptions: {
|
|
chunkSize: 1024,
|
|
memLevel: 7,
|
|
level: 3
|
|
},
|
|
zlibInflateOptions: {
|
|
chunkSize: 10 * 1024
|
|
},
|
|
clientNoContextTakeover: true,
|
|
serverNoContextTakeover: true,
|
|
serverMaxWindowBits: 10,
|
|
concurrencyLimit: 10,
|
|
threshold: 1024
|
|
}
|
|
});
|
|
|
|
this.wss.on('connection', (ws, req) => {
|
|
const path = req.url;
|
|
console.log(`WebSocket connected: ${req.socket.remoteAddress}, path: ${path}`);
|
|
|
|
if (path === '/chat') {
|
|
this._handleChatConnection(ws);
|
|
} else {
|
|
sendFrame(ws, 'system', 'error', 'Unknown path');
|
|
ws.close(1000, 'Unknown path');
|
|
}
|
|
});
|
|
|
|
this.server.listen(this.port, () => {
|
|
console.log(`WebSocket server started on ws://localhost:${this.port}`);
|
|
});
|
|
}
|
|
|
|
_handleChatConnection(ws) {
|
|
sendFrame(ws, 'system', 'connected', 'Connected to chat server');
|
|
|
|
// 同一连接的消息串行处理:agent 的 messages 数组是共享状态,
|
|
// 并发跑同一个 agent 会让上下文交错、回复错乱
|
|
ws.messageQueue = Promise.resolve();
|
|
ws.on('message', (data) => {
|
|
ws.messageQueue = ws.messageQueue
|
|
.then(() => this._handleMessage(ws, data))
|
|
.catch((err) => console.error('WebSocket message handling error:', err));
|
|
});
|
|
|
|
ws.on('close', () => {
|
|
// 断开后释放队列引用,避免连接对象被闭包长期持有
|
|
ws.messageQueue = null;
|
|
console.log('Chat client disconnected');
|
|
});
|
|
|
|
ws.on('error', (err) => {
|
|
console.error('WebSocket error:', err);
|
|
});
|
|
}
|
|
|
|
async _handleMessage(ws, data) {
|
|
try {
|
|
const raw = data.toString();
|
|
|
|
const msg = JSON.parse(raw);
|
|
// 只记录消息类型:正文含健康描述、token/userId 属敏感信息,不进日志
|
|
console.log('Received message type:', msg.type);
|
|
|
|
if (msg.type === 'ping') {
|
|
sendFrame(ws, 'system', 'pong', '', msg.id);
|
|
return;
|
|
}
|
|
|
|
if (msg.type === 'chat' || msg.type === 'clear') {
|
|
// 客户端断开后不再处理消息
|
|
if (ws.readyState !== WebSocket.OPEN) return;
|
|
|
|
const send = (source, type, content, id) => {
|
|
sendFrame(ws, source, type, content, id);
|
|
};
|
|
|
|
try {
|
|
// 身份只认 token:不再接受凭 userId 明文接入,防止冒充他人聊天
|
|
if (!msg.token) {
|
|
send('system', 'error', '请先登录', msg.id);
|
|
return;
|
|
}
|
|
const userInfo = await getUserByToken(msg.token);
|
|
if (!userInfo) {
|
|
send('system', 'error', '请先登录', msg.id);
|
|
return;
|
|
}
|
|
|
|
// 频次限制按用户维度:防止单账号刷 AI 调用(同一账号多连接共享额度)
|
|
if (msg.type === 'chat') {
|
|
const limit = chatLimiter(String(userInfo._id));
|
|
if (!limit.allowed) {
|
|
send('system', 'error', `操作过于频繁,请 ${limit.retryAfter} 秒后重试`, msg.id);
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (msg.agent === 'escort-admin') {
|
|
// 管理端 agent 含写库/环境变量等高危工具,必须校验管理员角色,防止普通用户越权
|
|
if (!isAdmin(userInfo)) {
|
|
send('system', 'error', '无管理员权限', msg.id);
|
|
return;
|
|
}
|
|
await adminAgent.streamChat(userInfo, [msg], (source, type, content, id) => {
|
|
send(source, type, content, id);
|
|
});
|
|
} else {
|
|
await chatTask.streamChat(userInfo, msg, (source, type, content, id) => {
|
|
send(source, type, content, id);
|
|
});
|
|
}
|
|
// 流式 chunk 可能携带不同的模型消息 ID,结束帧统一关联本次客户端请求。
|
|
send('system', 'end', '', msg.id);
|
|
} catch (err) {
|
|
console.error('streamChat error:', err);
|
|
send('system', 'error', '处理消息失败,请稍后重试', msg.id);
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (msg.type === 'system') {
|
|
console.log('[System]', msg.content);
|
|
return;
|
|
}
|
|
|
|
console.log('[Unknown message type]', msg.type);
|
|
} catch (e) {
|
|
console.error('Message parse error:', e);
|
|
sendFrame(ws, 'system', 'error', '消息格式错误');
|
|
}
|
|
}
|
|
|
|
broadcast(message) {
|
|
if (!this.wss) return;
|
|
this.wss.clients.forEach((client) => {
|
|
if (client.readyState === WebSocket.OPEN) {
|
|
client.send(message);
|
|
}
|
|
});
|
|
}
|
|
|
|
stop() {
|
|
if (this.wss) {
|
|
this.wss.close();
|
|
this.wss = null;
|
|
}
|
|
if (this.server) {
|
|
this.server.close();
|
|
this.server = null;
|
|
}
|
|
console.log('WebSocket server stopped');
|
|
}
|
|
|
|
getClientsCount() {
|
|
if (!this.wss) return 0;
|
|
return this.wss.clients.size;
|
|
}
|
|
}
|