From 91eaad04d7dd855bc6ddba6bbd9216a6a14a89c1 Mon Sep 17 00:00:00 2001 From: abearxiong Date: Tue, 10 Mar 2026 18:18:22 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9B=B4=E6=96=B0N5Proxy=E9=80=BB?= =?UTF-8?q?=E8=BE=91=EF=BC=8C=E4=BD=BF=E7=94=A8baseURL=E6=9E=84=E5=BB=BA?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E9=93=BE=E6=8E=A5=EF=BC=8C=E5=B9=B6=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86=E4=B8=8EHTML?= =?UTF-8?q?=E6=B8=B2=E6=9F=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/domain.ts | 3 +++ src/modules/n5/index.ts | 28 +++++++++++++++++++++------- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/modules/domain.ts b/src/modules/domain.ts index 42e78ef..e912a14 100644 --- a/src/modules/domain.ts +++ b/src/modules/domain.ts @@ -7,3 +7,6 @@ import { useKey } from "@kevisual/use-config"; export const proxyDomain = useKey('PROXY_DOMAIN') || ''; // 请在这里填写你的域名 export const baseProxyUrl = proxyDomain ? `https://${proxyDomain}` : 'https://kevisual.cn'; + + +export const baseURL = baseProxyUrl; \ No newline at end of file diff --git a/src/modules/n5/index.ts b/src/modules/n5/index.ts index 66623d1..d7a5806 100644 --- a/src/modules/n5/index.ts +++ b/src/modules/n5/index.ts @@ -1,6 +1,9 @@ import { convex, convexApi } from '@/app.ts'; import { User } from '@/models/user.ts'; +import { omit } from 'es-toolkit'; import { IncomingMessage, ServerResponse } from 'http'; +import { renderServerHtml } from '../html/render-server-html.ts'; +import { baseURL } from '../domain.ts'; type ProxyOptions = { createNotFoundPage: (msg?: string) => any; @@ -8,7 +11,7 @@ type ProxyOptions = { // /n5/:slug/ export const N5Proxy = async (req: IncomingMessage, res: ServerResponse, opts?: ProxyOptions) => { const { url } = req; - const _url = new URL(url || '', `http://localhost`); + const _url = new URL(url || '', baseURL); const { pathname, searchParams } = _url; let [user, app, userAppKey] = pathname.split('/').slice(1); if (!app) { @@ -23,7 +26,7 @@ export const N5Proxy = async (req: IncomingMessage, res: ServerResponse, opts?: const data = convexResult.data ?? {}; const link = data.link; if (!link) { - opts?.createNotFoundPage?.('应用未找到'); + opts?.createNotFoundPage?.('不存在对应的跳转的链接'); return false; } if (data?.useOwnerToken) { @@ -42,13 +45,24 @@ export const N5Proxy = async (req: IncomingMessage, res: ServerResponse, opts?: } catch (e) { console.error('Error fetching the link:', e); res.writeHead(500, { 'Content-Type': 'application/json' }); - res.end(JSON.stringify({ error: 'Failed to fetch the link' })); + res.end(JSON.stringify({ error: '请求服务区失败' })); } - return true; } - res.writeHead(302, { - Location: link, - }); + const type = data?.type; + if (type === 'html-render') { + const createJson = omit(data, ['useOwnerToken', 'permission']); + const html = await fetch(link, { method: 'GET' }).then(res => res.text()); + const renderedHtml = await renderServerHtml({ + data: createJson, + pathname: _url.toString(), + }, html); + res.writeHead(200, { 'Content-Type': 'text/html' }); + res.end(renderedHtml); + } else { + res.writeHead(302, { + Location: link, + }); + } res.end(); };