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(); };