This commit is contained in:
2025-04-24 14:12:08 +08:00
parent 313f9eb93c
commit 14bf9aa187
19 changed files with 459 additions and 393 deletions

View File

@@ -44,6 +44,6 @@
},
"dependencies": {
"send": "^1.1.0",
"ws": "^8.18.1"
"ws": "npm:@kevisual/ws"
}
}

View File

@@ -1,5 +1,5 @@
import path from 'path';
import fs from 'fs';
import path from 'node:path';
import fs from 'node:fs';
type DownloadTask = {
downloadPath: string;

View File

@@ -1,5 +1,5 @@
import http from 'http';
import https from 'https';
import http from 'node:http';
import https from 'node:https';
import { rewriteCookieDomain } from '../https/cookie-rewrite.ts';
import { ProxyInfo } from './proxy.ts';
export const defaultApiProxy = [

View File

@@ -1,6 +1,6 @@
import http from 'http';
import http from 'node:http';
import send from 'send';
import fs from 'fs';
import fs from 'node:fs';
import path from 'path';
import { ProxyInfo } from './proxy.ts';
import { checkFileExists } from '@/file/index.ts';

View File

@@ -2,4 +2,4 @@ export * from './proxy.ts';
export * from './file-proxy.ts';
export { default as send } from 'send';
export * from './api-proxy.ts';
export * from './wx-proxy.ts';
export * from './ws-proxy.ts';

View File

@@ -8,6 +8,11 @@ export type ProxyInfo = {
* 类型
*/
type?: 'static' | 'dynamic' | 'minio';
/**
* 是否使用websocket
* @default false
*/
ws?: boolean;
/**
* 首要文件比如index.html 设置了首要文件,如果文件不存在,则访问首要文件
*/
@@ -23,6 +28,10 @@ export type ApiList = {
* url或者相对路径
*/
target: string;
/**
* 目标地址
*/
ws?: boolean;
/**
* 类型
*/

View File

@@ -1,19 +1,20 @@
import { Server } from 'http';
import WebSocket from 'ws';
import { ProxyInfo } from './proxy.ts';
/**
* websocket代理
* apiList: [{ path: '/api/router', target: 'https://kevisual.xiongxiao.me' }]
* @param server
* @param config
*/
export const wsProxy = (server: Server, config: { apiList: any[] }) => {
export const wsProxy = (server: Server, config: { apiList: ProxyInfo[] }) => {
console.log('Upgrade initialization started');
server.on('upgrade', (req, socket, head) => {
const proxyApiList = config?.apiList || [];
const proxyApiList: ProxyInfo[] = config?.apiList || [];
const proxyApi = proxyApiList.find((item) => req.url.startsWith(item.path));
if (proxyApi) {
if (proxyApi && proxyApi.ws) {
const _u = new URL(req.url, `${proxyApi.target}`);
const isHttps = _u.protocol === 'https:';
const wsProtocol = isHttps ? 'wss' : 'ws';