feat: 多路径密钥扫描(.gatekeeper/.gk/.guanghu-engine),升级不再丢密钥

This commit is contained in:
bingshuo 2026-05-30 18:58:34 +08:00
parent bd3f58c18f
commit 614a8c789e

View File

@ -12,13 +12,21 @@ const CMD_TIMEOUT=30000,MAX_OUTPUT=100000,RATE_LIMIT=60;
if(!fs.existsSync(DATA_DIR))fs.mkdirSync(DATA_DIR,{recursive:true,mode:0o700});
/* ═══ 密钥 ═══ */
const SECRET_FILE=path.join(DATA_DIR,'secret');
/* ═══ 密钥 — 兼容所有历史数据目录 ═══ */
const SECRET_PATHS=[
path.join(os.homedir(),'.gatekeeper','secret'),
path.join(os.homedir(),'.gk','secret'),
path.join(os.homedir(),'.guanghu-engine','secret'),
];
let API_SECRET;
if(fs.existsSync(SECRET_FILE)){API_SECRET=fs.readFileSync(SECRET_FILE,'utf-8').trim();}
for(const sp of SECRET_PATHS){
if(fs.existsSync(sp)){const k=fs.readFileSync(sp,'utf-8').trim();if(k){API_SECRET=k;break;}}
}
if(!API_SECRET){
API_SECRET='zy_gtw_'+crypto.randomBytes(24).toString('hex');
fs.writeFileSync(SECRET_FILE,API_SECRET,{mode:0o600});
const dir=path.dirname(SECRET_PATHS[0]);
if(!fs.existsSync(dir))fs.mkdirSync(dir,{recursive:true,mode:0o700});
fs.writeFileSync(SECRET_PATHS[0],API_SECRET,{mode:0o600});
console.log('');
console.log('══════════════════════════════════════════════');
console.log(' 🔐 光湖驱动引擎 · 首次启动');
@ -27,7 +35,7 @@ if(!API_SECRET){
console.log(' ├─ '+API_SECRET);
console.log(' ╰─ 此密钥仅在此处显示一次');
console.log('');
console.log(' 已保存至: '+SECRET_FILE);
console.log(' 已保存至: '+SECRET_PATHS[0]);
console.log(' 监听端口: '+PORT);
console.log('══════════════════════════════════════════════');
console.log('');