更新依赖项,优化 WebSocket 处理,添加文件流管道功能,改进用户认证逻辑

This commit is contained in:
2025-12-21 02:06:38 +08:00
parent d22be3a840
commit 15fcfdad18
16 changed files with 150 additions and 262 deletions

View File

@@ -1,62 +1,18 @@
import { myConfig as config } from './modules/config.ts';
import { app } from './app.ts';
import './route.ts';
import { handleRequest } from './routes-simple/handle-request.ts';
import { port } from './modules/config.ts';
import { WssApp } from './modules/ws-proxy/index.ts';
import net from 'node:net';
// if (import.meta.url === `file://${process.argv[1]}`) {
app.listen(port, () => {
import { wssFun } from './modules/ws-proxy/index.ts';
console.log('Starting server...', port);
app.listen(port, '0.0.0.0', () => {
console.log(`server is running at http://localhost:${port}`);
});
app.server.on(handleRequest);
const wssApp = new WssApp();
const main = () => {
console.log('Upgrade initialization started');
app.server.server.on('upgrade', async (req, socket, head) => {
const isUpgrade = wssApp.upgrade(req, socket, head);
if (isUpgrade) {
console.log('WebSocket upgrade successful for path:', req.url);
return;
}
const proxyApiList = config?.apiList || [];
const proxyApi = proxyApiList.find((item) => req.url.startsWith(item.path));
if (proxyApi) {
const _u = new URL(req.url, `${proxyApi.target}`);
const options = {
hostname: _u.hostname,
port: Number(_u.port) || 80,
path: _u.pathname,
headers: req.headers,
};
const proxySocket = net.connect(options.port, options.hostname, () => {
proxySocket.write(
`GET ${options.path} HTTP/1.1\r\n` +
`Host: ${options.hostname}\r\n` +
`Connection: Upgrade\r\n` +
`Upgrade: websocket\r\n` +
`Sec-WebSocket-Key: ${req.headers['sec-websocket-key']}\r\n` +
`Sec-WebSocket-Version: ${req.headers['sec-websocket-version']}\r\n` +
`\r\n`,
);
proxySocket.pipe(socket);
socket.pipe(proxySocket);
});
proxySocket.on('error', (err) => {
console.error(`WebSocket proxy error: ${err.message}`);
socket.end();
});
} else {
socket.end();
}
});
};
// setTimeout(() => {
// main();
// }, 1200);
app.server.on([{
id: 'handle-all',
fun: handleRequest,
}, {
id: 'wss',
io: true,
path: '/ws/proxy',
fun: wssFun,
}]);