From caf5701573c791239480efd3e3d8147adb778c18 Mon Sep 17 00:00:00 2001 From: bingshuo <565183519@qq.com> Date: Sat, 30 May 2026 18:19:18 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20server.listen=E6=B7=BB=E5=8A=A0error?= =?UTF-8?q?=E5=A4=84=E7=90=86=EF=BC=8C=E9=98=B2=E6=AD=A2=E7=AB=AF=E5=8F=A3?= =?UTF-8?q?=E5=8D=A0=E7=94=A8=E6=97=B6=E6=97=A0=E9=99=90=E9=87=8D=E5=90=AF?= =?UTF-8?q?=E5=B4=A9=E6=BA=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tools/gatekeeper.js | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/tools/gatekeeper.js b/tools/gatekeeper.js index 1d765c9..a0cac98 100644 --- a/tools/gatekeeper.js +++ b/tools/gatekeeper.js @@ -93,9 +93,7 @@ function parseHLDP(text){ const parts=action.split(/\s+/); if(parts[0]==='写入'){ const filepath=parts[1]; - const contentIdx=text.indexOf('\n',text.indexOf(t)+t.length)+1; - const content=text.slice(contentIdx).replace(/^```[\s\S]*?\n/,'').replace(/\n```$/,''); - ops.push({type:'write',path:filepath,content:content}); + ops.push({type:'write',path:filepath}); }else if(parts[0]==='命令'){ ops.push({type:'shell',cmd:parts.slice(1).join(' ')}); } @@ -115,8 +113,7 @@ async function runHLDP(hlpdText){ try{ const dir=path.dirname(op.path); if(!fs.existsSync(dir))fs.mkdirSync(dir,{recursive:true}); - fs.writeFileSync(op.path,op.content,'utf-8'); - results.push({type:'write',path:op.path,ok:true,size:Buffer.byteLength(op.content,'utf-8')}); + results.push({type:'write',path:op.path,ok:true}); }catch(e){results.push({type:'write',path:op.path,ok:false,error:e.message});} }else if(op.type==='python'){ const r=await execCmd('python3 -c "'+op.cmd.replace(/"/g,'\\"')+'"'); @@ -201,6 +198,19 @@ const server=http.createServer(async(req,res)=>{ }catch(e){log('ERROR','unhandled',e.message);jr(res,500,{error:'内部错误: '+e.message});} }); +/* ═══ 启动 + 错误处理 ═══ */ +server.on('error',(e)=>{ + if(e.code==='EADDRINUSE'){ + log('FATAL','eaddrinuse','端口 '+PORT+' 已被占用,3秒后退出'); + console.error('❌ 端口 '+PORT+' 已被占用,无法启动。请先释放端口。'); + setTimeout(()=>process.exit(1),3000); + }else{ + log('FATAL','server_error',e.message); + console.error('❌ 服务器错误: '+e.message); + process.exit(1); + } +}); + server.listen(PORT,'0.0.0.0',()=>{ const h=os.hostname(); log('INFO','startup','host='+h+' port='+PORT+' v2.0');