// Loopback static site for the native browser application; never forward logins, tokens or business APIs. import http from 'node:http' import path from 'node:path' import {fileURLToPath} from 'node:url' import {readFile} from 'node:fs/promises' const host='127.0.0.1',port=process.argv[2]==='--qa'?5181:5179,self=`http://${host}:${port}`,official='https://admin.zhenyangtang.com.cn' const candidate=port===5181 ? process.argv[3] : undefined if(candidate && !/^[a-z][a-z0-9-]{1,48}$/.test(candidate))throw Error('Invalid candidate') const base=fileURLToPath(new URL('../node_modules/.zyt-live-native/',import.meta.url)) const dist=candidate?path.join(base,'candidates',candidate):path.join(base,'dist'),entry=path.join(dist,'live-native/index.html') const types={js:'text/javascript',css:'text/css',svg:'image/svg+xml',png:'image/png',jpg:'image/jpeg',jpeg:'image/jpeg',webp:'image/webp',gif:'image/gif',ico:'image/x-icon',woff:'font/woff',woff2:'font/woff2',ttf:'font/ttf',otf:'font/otf',mp3:'audio/mpeg',mp4:'video/mp4',json:'application/json',wasm:'application/wasm'} // Same-origin program assets. API authorization, including write permissions, belongs to the official backend. // Media may be served by the storage provider configured by the business; never proxy it through localhost. const csp=`default-src 'self'; script-src 'self' 'wasm-unsafe-eval'; style-src 'self' 'unsafe-inline' https://web.sdk.qcloud.com; connect-src 'self' ${official} https://*.myqcloud.com https://*.aliyuncs.com https://*.tencentcloudapi.com https://*.rtc.qq.com https://*.my-imcloud.com wss://*.rtc.qq.com wss://*.my-imcloud.com; img-src 'self' https: data: blob:; font-src 'self' data:; media-src 'self' https: blob:; frame-src 'self' ${official} https://*.zhenyangtang.com.cn https://open.work.weixin.qq.com; form-action 'self' ${official}; frame-ancestors 'none'; base-uri 'none'; worker-src 'self' blob:; object-src 'none'` await readFile(entry) http.createServer(async(req,res)=>{ res.setHeader('Referrer-Policy','no-referrer');res.setHeader('Cache-Control','no-store');res.setHeader('Content-Security-Policy',csp);res.setHeader('X-Content-Type-Options','nosniff') const send=(s,t,b)=>{res.writeHead(s,{'Content-Type':t});res.end(req.method==='HEAD'?undefined:b)} if(req.headers.host!==`${host}:${port}` || (req.headers.origin && req.headers.origin!==self))return send(403,'text/plain','Forbidden') if(!['GET','HEAD'].includes(req.method))return send(405,'text/plain','Static site only') if(!req.url?.startsWith('/') || req.url.startsWith('//'))return send(400,'text/plain','Invalid path') try{ const url=new URL(req.url,self),name=decodeURIComponent(url.pathname) if(name==='/' || name==='/admin' || /^\/admin\/nav\/(?:m-\d+|p-[a-z0-9]+)(?:-\d+)?\/?$/.test(name)){res.writeHead(302,{Location:'/admin/'});return res.end()} if(!name.startsWith('/admin/') || /[\\\x00-\x1f]/.test(name))return send(404,'text/plain','Not found') const suffix=name.slice(7),parts=suffix.split('/');if(parts.some(p=>p==='..'||p.startsWith('.')))return send(404,'text/plain','Not found') const ext=path.extname(name).slice(1).toLowerCase() if(ext && Object.hasOwn(types,ext))return send(200,types[ext],await readFile(path.join(dist,suffix))) if(parts[0]==='assets' || /\.(?:map|env|php|ini|sh|pem|log|sql)$/i.test(name))return send(404,'text/plain','Not found') return send(200,'text/html; charset=utf-8',await readFile(entry)) }catch{return send(404,'text/plain','Not found')} }).listen(port,host,()=>console.log(`甄养堂管理后台 ${self}/admin/login`))