Update index.mjs
Browse files
index.mjs
CHANGED
|
@@ -1,61 +1,40 @@
|
|
|
|
|
| 1 |
import http from 'http';
|
| 2 |
import net from 'net';
|
| 3 |
|
| 4 |
const WS_PATH = '/api/v1/chat/streaming';
|
| 5 |
|
| 6 |
const server = http.createServer((req, res) => {
|
| 7 |
-
res.writeHead(200
|
| 8 |
-
res.end('<h1>AI Inference Server</h1>
|
| 9 |
});
|
| 10 |
|
| 11 |
-
//
|
|
|
|
|
|
|
| 12 |
server.on('upgrade', (req, socket, head) => {
|
| 13 |
-
// 1. 路径校验
|
| 14 |
if (req.url !== WS_PATH) {
|
| 15 |
-
|
| 16 |
-
socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
|
| 17 |
return;
|
| 18 |
}
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
console.log('[错误] 缺少 x-target-host 头部');
|
| 25 |
-
socket.end('HTTP/1.1 400 Missing Header\r\n\r\n');
|
| 26 |
-
return;
|
| 27 |
-
}
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
'HTTP/1.1 101 Switching Protocols\r\n' +
|
| 37 |
-
'Upgrade: websocket\r\n' +
|
| 38 |
-
'Connection: Upgrade\r\n' +
|
| 39 |
-
'Sec-WebSocket-Accept: icY+zO7FOHYpWfWv7mYF80uJ68Y=\r\n' + // 模拟一个固定的 Accept Key
|
| 40 |
-
'\r\n'
|
| 41 |
-
);
|
| 42 |
-
|
| 43 |
-
if (head && head.length > 0) targetSocket.write(head);
|
| 44 |
-
targetSocket.pipe(socket);
|
| 45 |
-
socket.pipe(targetSocket);
|
| 46 |
-
});
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
socket.destroy();
|
| 51 |
-
});
|
| 52 |
-
|
| 53 |
-
socket.on('error', (err) => {
|
| 54 |
-
console.log(`[客户端连接错误]: ${err.message}`);
|
| 55 |
-
targetSocket.destroy();
|
| 56 |
});
|
| 57 |
});
|
| 58 |
|
| 59 |
-
server.listen(7860
|
| 60 |
-
console.log('原生 WebSocket 隧道服务已启动');
|
| 61 |
-
});
|
|
|
|
| 1 |
+
import { WebSocketServer } from 'ws';
|
| 2 |
import http from 'http';
|
| 3 |
import net from 'net';
|
| 4 |
|
| 5 |
const WS_PATH = '/api/v1/chat/streaming';
|
| 6 |
|
| 7 |
const server = http.createServer((req, res) => {
|
| 8 |
+
res.writeHead(200);
|
| 9 |
+
res.end('<h1>AI Inference Server</h1>');
|
| 10 |
});
|
| 11 |
|
| 12 |
+
// 创建 WebSocket 服务
|
| 13 |
+
const wss = new WebSocketServer({ noServer: true });
|
| 14 |
+
|
| 15 |
server.on('upgrade', (req, socket, head) => {
|
|
|
|
| 16 |
if (req.url !== WS_PATH) {
|
| 17 |
+
socket.destroy();
|
|
|
|
| 18 |
return;
|
| 19 |
}
|
| 20 |
|
| 21 |
+
wss.handleUpgrade(req, socket, head, (ws) => {
|
| 22 |
+
const targetHost = req.headers['x-target-host'];
|
| 23 |
+
const targetPort = parseInt(req.headers['x-target-port']) || 443;
|
| 24 |
|
| 25 |
+
console.log(`[隧道] 准备连接: ${targetHost}:${targetPort}`);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
const targetSocket = net.connect(targetPort, targetHost, () => {
|
| 28 |
+
// 将 WebSokcet 包装为原始流进行双向转发
|
| 29 |
+
const duplex = WebSocket.createWebSocketStream(ws);
|
| 30 |
+
duplex.pipe(targetSocket);
|
| 31 |
+
targetSocket.pipe(duplex);
|
| 32 |
+
console.log(`[成功] 链路已接通`);
|
| 33 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
targetSocket.on('error', () => ws.terminate());
|
| 36 |
+
ws.on('error', () => targetSocket.destroy());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
});
|
| 38 |
});
|
| 39 |
|
| 40 |
+
server.listen(7860);
|
|
|
|
|
|