51 lines
3.6 KiB
JavaScript
51 lines
3.6 KiB
JavaScript
import {createServer,request} from 'node:http'
|
|
import {stat,readFile,realpath} from 'node:fs/promises'
|
|
import {resolve,sep,extname} from 'node:path'
|
|
import {fileURLToPath} from 'node:url'
|
|
|
|
// Local preview only. Production should use the project's existing reverse proxy.
|
|
const root=resolve(process.env.KEFU_PREVIEW_DIST || fileURLToPath(new URL('../.test-runtime/kefu-admin-web/apps/web-antd/dist/',import.meta.url)))
|
|
const mime={'.html':'text/html; charset=utf-8','.js':'text/javascript; charset=utf-8','.mjs':'text/javascript; charset=utf-8','.css':'text/css; charset=utf-8','.json':'application/json; charset=utf-8','.svg':'image/svg+xml','.png':'image/png','.jpg':'image/jpeg','.jpeg':'image/jpeg','.gif':'image/gif','.webp':'image/webp','.ico':'image/x-icon','.woff2':'font/woff2','.woff':'font/woff','.ttf':'font/ttf'}
|
|
const server=createServer(async(req,res)=>{
|
|
res.setHeader('Referrer-Policy','no-referrer')
|
|
res.setHeader('X-Content-Type-Options','nosniff')
|
|
const fail=(status,message)=>{if(!res.headersSent){res.writeHead(status,{'Content-Type':'text/plain; charset=utf-8','Cache-Control':'no-store'});res.end(message)}}
|
|
let url
|
|
try{url=new URL(req.url,'http://127.0.0.1:5666')}catch{return fail(400,'无效请求')}
|
|
if(url.pathname.startsWith('/api/')){
|
|
if(!['GET','HEAD','POST','PUT','PATCH','DELETE','OPTIONS'].includes(req.method))return fail(405,'请求方法不支持')
|
|
if(Number(req.headers['content-length']||0)>64*1024*1024)return fail(413,'上传内容超过本地预览限制')
|
|
const headers={...req.headers,host:'127.0.0.1:8766'}
|
|
for(const key of Object.keys(headers))if(key.startsWith('x-forwarded-')||['forwarded','proxy-authorization','proxy-connection'].includes(key))delete headers[key]
|
|
const apiPath=url.pathname.startsWith('/api/v2/')?url.pathname:'/api/v2/'+url.pathname.slice(5)
|
|
const target=request({hostname:'127.0.0.1',port:8766,path:apiPath+url.search,method:req.method,headers,timeout:20000},response=>{
|
|
res.writeHead(response.statusCode,{...response.headers,'Referrer-Policy':'no-referrer'});response.pipe(res)
|
|
})
|
|
target.on('timeout',()=>target.destroy());target.on('error',()=>fail(502,'客服接口暂时不可用'))
|
|
let bytes=0;req.on('data',chunk=>{bytes+=chunk.length;if(bytes>64*1024*1024){target.destroy();fail(413,'上传内容超过本地预览限制')}})
|
|
req.on('aborted',()=>target.destroy());req.pipe(target);return
|
|
}
|
|
if(!['GET','HEAD'].includes(req.method))return fail(405,'请求方法不支持')
|
|
try{
|
|
const pathname=decodeURIComponent(url.pathname)
|
|
if(pathname.includes('\0')||pathname.includes('\\'))return fail(400,'无效路径')
|
|
let path=resolve(root,'.'+pathname)
|
|
if(path!==root&&!path.startsWith(root+sep))return fail(403,'无效路径')
|
|
let info=await stat(path).catch(()=>null)
|
|
if(!info?.isFile()){
|
|
if(extname(pathname)&&!pathname.endsWith('.html'))return fail(404,'资源不存在')
|
|
path=resolve(root,'index.html');info=await stat(path).catch(()=>null)
|
|
}
|
|
if(!info?.isFile())return fail(503,'客服前端正在构建,请稍后刷新')
|
|
const actual=await realpath(path),actualRoot=await realpath(root)
|
|
if(!actual.startsWith(actualRoot+sep))return fail(403,'无效路径')
|
|
res.setHeader('Content-Type',mime[extname(path)]||'application/octet-stream')
|
|
res.setHeader('Cache-Control',extname(path)==='.html'?'no-store':'public,max-age=300')
|
|
res.setHeader('Content-Length',info.size)
|
|
if(req.method==='HEAD'){res.end();return}
|
|
res.end(await readFile(path))
|
|
}catch{fail(500,'客服预览暂时不可用')}
|
|
})
|
|
server.listen(5666,'127.0.0.1',()=>console.log('客服本机预览:http://127.0.0.1:5666'))
|
|
process.on('SIGTERM',()=>server.close())
|