66 lines
1.7 KiB
JavaScript
66 lines
1.7 KiB
JavaScript
'use strict';
|
||
|
||
const fs = require('fs');
|
||
const path = require('path');
|
||
const os = require('os');
|
||
|
||
// 生产环境用 /etc/clawd/,开发环境用 ~/.clawd/
|
||
const CONFIG_DIR = process.env.CLAWD_CONFIG_DIR
|
||
|| (process.getuid && process.getuid() === 0
|
||
? '/etc/clawd'
|
||
: path.join(os.homedir(), '.clawd'));
|
||
|
||
const CONFIG_FILE = path.join(CONFIG_DIR, 'config.json');
|
||
|
||
const DEFAULTS = {
|
||
server: 'wss://claw.cutos.ai/ws',
|
||
claw_id: null,
|
||
token: null,
|
||
heartbeat_interval: 30, // 秒
|
||
/** 云端已激活:用于启动/重连时立即点亮 alarm(pwr),不等首包 connected */
|
||
activated: false,
|
||
ssh_secret_key: null,
|
||
};
|
||
|
||
function _generateSshSecretKey() {
|
||
const bytes = require('crypto').randomBytes(16);
|
||
return 'sk-' + bytes.toString('hex');
|
||
}
|
||
|
||
function load() {
|
||
let cfg;
|
||
try {
|
||
if (fs.existsSync(CONFIG_FILE)) {
|
||
const raw = fs.readFileSync(CONFIG_FILE, 'utf8');
|
||
cfg = Object.assign({}, DEFAULTS, JSON.parse(raw));
|
||
}
|
||
} catch (e) {
|
||
const log = require('./logger');
|
||
log.error('config', '读取配置失败,使用默认值:', e.message);
|
||
}
|
||
if (!cfg) cfg = Object.assign({}, DEFAULTS);
|
||
|
||
if (!cfg.ssh_secret_key) {
|
||
cfg.ssh_secret_key = _generateSshSecretKey();
|
||
save(cfg);
|
||
}
|
||
|
||
return cfg;
|
||
}
|
||
|
||
function save(data) {
|
||
try {
|
||
fs.mkdirSync(CONFIG_DIR, { recursive: true });
|
||
fs.writeFileSync(CONFIG_FILE, JSON.stringify(data, null, 2), 'utf8');
|
||
} catch (e) {
|
||
const log = require('./logger');
|
||
log.error('config', '写入配置失败:', e.message);
|
||
}
|
||
}
|
||
|
||
function getConfigPath() {
|
||
return CONFIG_FILE;
|
||
}
|
||
|
||
module.exports = { load, save, getConfigPath };
|