ZhaoShanGeng commited on
Commit
e6c8f63
·
1 Parent(s): f076c2a

fix: 安全增强 - 移除默认凭据,使用完全随机生成

Browse files
Files changed (4) hide show
  1. .env.example +6 -5
  2. config.json +1 -1
  3. scripts/build.js +8 -11
  4. src/config/config.js +6 -8
.env.example CHANGED
@@ -1,10 +1,11 @@
1
  # 敏感配置(只在 .env 中配置)
2
- API_KEY=sk-text
3
- ADMIN_USERNAME=admin
4
- ADMIN_PASSWORD=admin123
5
- JWT_SECRET=your-jwt-secret-key-change-this-in-production
 
6
 
7
  # 可选配置
8
  # PROXY=http://127.0.0.1:7890
9
- SYSTEM_INSTRUCTION=你是聊天机器人,名字叫萌萌,如同名字这般,你的性格是软软糯糯萌萌哒的,专门为用户提供聊天和情绪价值,协助进行小说创作或者角色扮演
10
  # IMAGE_BASE_URL=http://your-domain.com
 
1
  # 敏感配置(只在 .env 中配置)
2
+ # 如果不配置以下三项,系统会自动生成随机凭据并在启动时显示
3
+ # API_KEY=your-api-key
4
+ # ADMIN_USERNAME=your-username
5
+ # ADMIN_PASSWORD=your-password
6
+ # JWT_SECRET=your-jwt-secret
7
 
8
  # 可选配置
9
  # PROXY=http://127.0.0.1:7890
10
+ # SYSTEM_INSTRUCTION=你的系统提示词
11
  # IMAGE_BASE_URL=http://your-domain.com
config.json CHANGED
@@ -32,7 +32,7 @@
32
  "retryTimes": 3,
33
  "skipProjectIdFetch": false,
34
  "useNativeAxios": false,
35
- "useContextSystemPrompt": false,
36
  "passSignatureToClient": false
37
  }
38
  }
 
32
  "retryTimes": 3,
33
  "skipProjectIdFetch": false,
34
  "useNativeAxios": false,
35
+ "useContextSystemPrompt": true,
36
  "passSignatureToClient": false
37
  }
38
  }
scripts/build.js CHANGED
@@ -240,15 +240,12 @@ try {
240
  }
241
  }
242
 
243
- // 复制配置文件模板
244
- const configFiles = ['.env.example', 'config.json'];
245
- for (const file of configFiles) {
246
- const srcPath = path.join(rootDir, file);
247
- const destPath = path.join(distDir, file);
248
- if (fs.existsSync(srcPath)) {
249
- fs.copyFileSync(srcPath, destPath);
250
- console.log(` ✓ Copied ${file}`);
251
- }
252
  }
253
 
254
  console.log('');
@@ -256,8 +253,8 @@ try {
256
  console.log('');
257
  console.log('📋 Usage:');
258
  console.log(' 1. Copy the dist folder to your target machine');
259
- console.log(' 2. Rename .env.example to .env and configure it');
260
- console.log(' 3. Run the executable');
261
  console.log('');
262
 
263
  } catch (error) {
 
240
  }
241
  }
242
 
243
+ // 复制配置文件模板(只复制 config.json)
244
+ const configSrcPath = path.join(rootDir, 'config.json');
245
+ const configDestPath = path.join(distDir, 'config.json');
246
+ if (fs.existsSync(configSrcPath)) {
247
+ fs.copyFileSync(configSrcPath, configDestPath);
248
+ console.log(' ✓ Copied config.json');
 
 
 
249
  }
250
 
251
  console.log('');
 
253
  console.log('');
254
  console.log('📋 Usage:');
255
  console.log(' 1. Copy the dist folder to your target machine');
256
+ console.log(' 2. Run the executable (will auto-generate random credentials if not configured)');
257
+ console.log(' 3. Optionally create .env file to customize settings');
258
  console.log('');
259
 
260
  } catch (error) {
src/config/config.js CHANGED
@@ -36,8 +36,8 @@ function getAdminCredentials() {
36
  // 生成随机凭据(只生成一次)
37
  if (!generatedCredentials) {
38
  generatedCredentials = {
39
- username: username || `admin_${crypto.randomBytes(4).toString('hex')}`,
40
- password: password || crypto.randomBytes(12).toString('base64').replace(/[+/=]/g, ''),
41
  jwtSecret: jwtSecret || crypto.randomBytes(32).toString('hex')
42
  };
43
 
@@ -61,14 +61,12 @@ function getAdminCredentials() {
61
  return generatedCredentials;
62
  }
63
 
64
- const { envPath, configJsonPath, examplePath } = getConfigPaths();
65
 
66
- // 确保 .env 存在(如果缺失则从 .env.example 复制一份)
67
  if (!fs.existsSync(envPath)) {
68
- if (fs.existsSync(examplePath)) {
69
- fs.copyFileSync(examplePath, envPath);
70
- log.info('✓ 已从 .env.example 创建 .env 文件');
71
- }
72
  }
73
 
74
  // 加载 config.json
 
36
  // 生成随机凭据(只生成一次)
37
  if (!generatedCredentials) {
38
  generatedCredentials = {
39
+ username: username || crypto.randomBytes(8).toString('hex'),
40
+ password: password || crypto.randomBytes(16).toString('base64').replace(/[+/=]/g, ''),
41
  jwtSecret: jwtSecret || crypto.randomBytes(32).toString('hex')
42
  };
43
 
 
61
  return generatedCredentials;
62
  }
63
 
64
+ const { envPath, configJsonPath } = getConfigPaths();
65
 
66
+ // 确保 .env 存在(如果缺失则创建空白文件,方便用户后续配置)
67
  if (!fs.existsSync(envPath)) {
68
+ fs.writeFileSync(envPath, '# 环境变量配置文件\n# 参考 .env.example 了解可用配置项\n', 'utf8');
69
+ log.info('✓ 已创建空白 .env 文件,请根据需要配置环境变量');
 
 
70
  }
71
 
72
  // 加载 config.json