diff --git a/src/modules/v1-ws-proxy/index.ts b/src/modules/v1-ws-proxy/index.ts index 033a537..f7fc998 100644 --- a/src/modules/v1-ws-proxy/index.ts +++ b/src/modules/v1-ws-proxy/index.ts @@ -40,7 +40,7 @@ export const wssFun: WebSocketListenerFun = async (req, res) => { } // @ts-ignore const userApp = ws.data.userApp; - logger.debug('message', data, ' userApp=', userApp); + // logger.debug('message', data, ' userApp=', userApp); const wsMessage = wsProxyManager.get(userApp); if (wsMessage) { wsMessage.sendResponse(data); diff --git a/src/modules/v1-ws-proxy/manager.ts b/src/modules/v1-ws-proxy/manager.ts index 4931a7e..1e694e6 100644 --- a/src/modules/v1-ws-proxy/manager.ts +++ b/src/modules/v1-ws-proxy/manager.ts @@ -84,23 +84,27 @@ class WsMessage { pathname: this.id ? `/${this.user}/v1/${shortAppId}` : '', }; } - async sendData(data: any, context?: any, opts?: { timeout?: number }) { + async sendData(data: any, context?: any, opts?: { timeout?: number }): Promise<{ code: number, data?: any, message?: string }> { if (this.ws.readyState !== WebSocket.OPEN) { return { code: 500, message: 'WebSocket is not open' }; } const timeout = opts?.timeout || 10 * 6 * 1000; // 10 minutes const id = nanoid(); + const msg = { path: data?.path, key: data?.key, id: data?.id }; + if (msg?.key?.includes('stream')) { + // @ts-ignore + msg.base64 = data?.base64 ?? true; + } const message = JSON.stringify({ id, type: 'proxy', data: { - message: data, + message: { ...data, ...msg }, context: context || {}, }, }); - logger.info('ws-proxy sendData', message); + // logger.info('ws-proxy sendData', message); this.ws.send(message); - const msg = { path: data?.path, key: data?.key, id: data?.id }; return new Promise((resolve) => { const timer = setTimeout(() => { console.log('ws-proxy sendData timeout', msg); diff --git a/src/modules/v1-ws-proxy/proxy.ts b/src/modules/v1-ws-proxy/proxy.ts index 83cde89..82bb119 100644 --- a/src/modules/v1-ws-proxy/proxy.ts +++ b/src/modules/v1-ws-proxy/proxy.ts @@ -76,6 +76,7 @@ export const UserV1Proxy = async (req: IncomingMessage, res: ServerResponse, opt return false; } const path = searchParams.get('path'); + const origin = searchParams.get('origin'); if (!path) { // 显示前端页面 const html = await fetch(`${baseProxyUrl}/root/router-studio/index.html`).then(res => res.text()); @@ -92,9 +93,31 @@ export const UserV1Proxy = async (req: IncomingMessage, res: ServerResponse, opt res.end(JSON.stringify({ message: '应用没有鉴权' })); return true; } + // console.log('proxy sendData', { path, key, data, userAppKey }); const value = await client.sendData(message, { state: { tokenUser: omit(loginUser.tokenUser, ['oauthExpand']) }, }); + if (value?.code === 200 && value?.data?.base64 && value?.data?.content) { + // 如果是文件流,同时没有 origin 参数,说明是查看文件的请求,直接返回文件内容 + if (!origin) { + // 如果是base64数据 + type Base64Data = { + filename: string; + base64: boolean; + dir?: string; + name: string; + contentType: string; + content: string; + } + const base64Data = value.data as Base64Data; + res.writeHead(200, { + 'Content-Type': base64Data.contentType || 'application/octet-stream' + }); + const buffer = Buffer.from(base64Data.content, 'base64'); + res.end(buffer); + return true; + } + } if (value) { res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify(value)); @@ -109,6 +132,7 @@ const handleRequest = async (req: IncomingMessage, res: ServerResponse, opts?: { const isGet = req.method === 'GET'; // 获取所有的管理员的应用列表 const { ids, infoList } = wsProxyManager.getIdsInfo(user + '--'); + const url = new URL(req.url || '', 'http://localhost'); if (isGet) { const html = await fetch(`${baseProxyUrl}/root/v1-manager/index.html`).then(res => res.text()); res.writeHead(200, { 'Content-Type': 'text/html' }); diff --git a/src/routes-simple/page-proxy.ts b/src/routes-simple/page-proxy.ts index 844ed2d..086ac63 100644 --- a/src/routes-simple/page-proxy.ts +++ b/src/routes-simple/page-proxy.ts @@ -308,7 +308,7 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR username: loginUser?.tokenUser?.username || '', password: password, }); - console.log('checkPermission', checkPermission, 'loginUser:', loginUser, password) + // console.log('checkPermission', checkPermission, 'loginUser:', loginUser, password) if (!checkPermission.success) { return createNotFoundPage('no permission'); } @@ -342,7 +342,7 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR }); return; } - console.log('appFile', appFile, appFileUrl, isExist); + // console.log('appFile', appFile, appFileUrl, isExist); // console.log('isExist', isExist); if (!appFile) { const [indexFilePath, etag] = indexFile.split('||');