File size: 10,244 Bytes
b51f54a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0d3f8ee
 
b51f54a
0d3f8ee
 
b51f54a
0d3f8ee
 
b51f54a
0d3f8ee
 
 
 
 
 
 
b51f54a
0d3f8ee
 
b51f54a
 
 
 
 
 
 
 
 
 
 
0d3f8ee
 
 
 
 
b51f54a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0d3f8ee
 
 
b51f54a
 
0d3f8ee
 
 
b51f54a
 
0d3f8ee
 
 
 
 
 
 
 
b51f54a
 
0d3f8ee
 
 
b51f54a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0d3f8ee
 
 
 
 
b51f54a
 
0d3f8ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b51f54a
0d3f8ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b51f54a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0d3f8ee
 
 
 
b51f54a
0d3f8ee
 
 
 
b51f54a
 
 
 
 
 
 
 
 
 
 
 
 
 
0d3f8ee
 
 
 
 
 
 
b51f54a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0d3f8ee
 
 
 
 
 
b51f54a
0d3f8ee
 
 
 
 
 
 
 
 
 
b51f54a
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/usr/bin/env node

const fs = require('fs');
const path = require('path');
const readline = require('readline');
const dotenv = require('dotenv');

// 创建交互式命令行界面
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

// 配置模板
const ENV_TEMPLATE = `# 服务端口
PORT=3010

# 日志格式 (tiny, combined, common, dev, short)
MORGAN_FORMAT=tiny

# API Key与Cookie映射关系 (JSON格式)
# 格式: {"自定义API Key": "Cookie值"} 或 {"自定义API Key": ["Cookie值1", "Cookie值2"]}
API_KEYS={API_KEYS_PLACEHOLDER}

# 轮询策略 (random 或 round-robin 或 default)
ROTATION_STRATEGY=default

# 是否使用TLS代理 (true 或 false)
USE_TLS_PROXY={USE_TLS_PROXY_PLACEHOLDER}

# 是否使用辅助代理服务器 (true 或 false)
USE_OTHERS_PROXY={USE_OTHERS_PROXY_PLACEHOLDER}

# 代理服务器平台
# 可选值: auto, windows_x64, linux_x64, android_arm64
# auto: 自动检测平台
# windows_x64: Windows 64位
# linux_x64: Linux 64位
# android_arm64: 安卓ARM 64位
PROXY_PLATFORM={PROXY_PLATFORM_PLACEHOLDER}

# 是否使用其它接口 (true 或 false)
USE_OTHERS={USE_OTHERS_PLACEHOLDER}
`;

// 提示信息
console.log('===== Cursor-To-OpenAI 环境配置助手 =====');
console.log('这个脚本将帮助你配置必要的环境变量\n');

// 从现有.env文件加载配置
function loadExistingConfig() {
  const envPath = path.join(process.cwd(), '.env');
  let existingConfig = {
    apiKeys: {},
    useTlsProxy: true,
    useOthersProxy: true,
    proxyPlatform: 'auto',
    useOthers: true,
    rotationStrategy: 'default'
  };
  
  if (fs.existsSync(envPath)) {
    console.log('发现现有的.env配置文件,将加载现有设置作为默认值');
    console.log('提示: 直接按回车将保留现有设置不变\n');
    
    try {
      // 加载.env文件
      const envConfig = dotenv.parse(fs.readFileSync(envPath));
      
      // 提取API Keys
      if (envConfig.API_KEYS) {
        try {
          existingConfig.apiKeys = JSON.parse(envConfig.API_KEYS);
        } catch (e) {
          console.log('无法解析现有的API Keys配置,将使用默认设置');
        }
      }
      
      // 提取TLS代理配置
      if (envConfig.USE_TLS_PROXY !== undefined) {
        existingConfig.useTlsProxy = envConfig.USE_TLS_PROXY === 'true';
      }
      
      // 提取辅助代理服务器配置
      if (envConfig.USE_OTHERS_PROXY !== undefined) {
        existingConfig.useOthersProxy = envConfig.USE_OTHERS_PROXY === 'true';
      }
      
      // 提取代理服务器平台
      if (envConfig.PROXY_PLATFORM) {
        existingConfig.proxyPlatform = envConfig.PROXY_PLATFORM;
      }

      // 提取是否使用其它接口
      if (envConfig.USE_OTHERS !== undefined) {
        existingConfig.useOthers = envConfig.USE_OTHERS === 'true';
      }
      
      // 提取轮询策略
      if (envConfig.ROTATION_STRATEGY) {
        existingConfig.rotationStrategy = envConfig.ROTATION_STRATEGY;
      }
      
      console.log('成功加载现有配置');
    } catch (error) {
      console.error('加载现有配置时出错:', error.message);
      console.log('将使用默认设置');
    }
  } else {
    console.log('未找到现有的.env配置文件,将创建新的配置文件');
  }
  
  return existingConfig;
}

// 提示用户输入,带有默认值
function promptWithDefault(question, defaultValue) {
  return new Promise((resolve) => {
    const defaultText = defaultValue ? ` [${defaultValue}]` : '';
    rl.question(`${question}${defaultText}: `, (answer) => {
      // 如果用户只按了回车,使用默认值
      resolve(answer.trim() || defaultValue || '');
    });
  });
}

// 收集配置信息
async function collectConfig() {
  // 加载现有配置
  const existingConfig = loadExistingConfig();
  
  const config = {
    apiKeys: {},
    useTlsProxy: existingConfig.useTlsProxy,
    useOthersProxy: existingConfig.useOthersProxy,
    proxyPlatform: existingConfig.proxyPlatform,
    useOthers: existingConfig.useOthers,
    rotationStrategy: existingConfig.rotationStrategy
  };

  // 询问是否使用TLS代理
  const useTlsProxyPrompt = `是否使用TLS代理服务器? (y/n)`;
  const defaultUseTlsProxy = existingConfig.useTlsProxy ? 'y' : 'n';
  const useTlsProxyAnswer = await promptWithDefault(useTlsProxyPrompt, defaultUseTlsProxy);
  config.useTlsProxy = useTlsProxyAnswer.toLowerCase() === 'y';

  if (config.useTlsProxy) {
    // 询问是否使用辅助代理服务器
    const useOthersProxyPrompt = `是否使用辅助代理服务器(port 10654)? (y/n)`;
    const defaultUseOthersProxy = existingConfig.useOthersProxy ? 'y' : 'n';
    const useOthersProxyAnswer = await promptWithDefault(useOthersProxyPrompt, defaultUseOthersProxy);
    config.useOthersProxy = useOthersProxyAnswer.toLowerCase() === 'y';
    
    // 询问代理服务器平台
    console.log('\n代理服务器平台选项:');
    console.log('- auto: 自动检测当前系统平台');
    console.log('- windows_x64: Windows 64位');
    console.log('- linux_x64: Linux 64位');
    console.log('- android_arm64: 安卓ARM 64位');
    
    const proxyPlatformPrompt = `选择代理服务器平台`;
    const defaultProxyPlatform = existingConfig.proxyPlatform || 'auto';
    config.proxyPlatform = await promptWithDefault(proxyPlatformPrompt, defaultProxyPlatform);
  }

  // 询问是否使用其它接口
  const useOthersPrompt = `是否使用其它接口? (y/n)`;
  const defaultUseOthers = existingConfig.useOthers ? 'y' : 'n';
  const useOthersAnswer = await promptWithDefault(useOthersPrompt, defaultUseOthers);
  config.useOthers = useOthersAnswer.toLowerCase() === 'y';

  // 询问轮询策略
  console.log('\n轮询策略选项:');
  console.log('- default: 默认策略');
  console.log('- random: 随机策略');
  console.log('- round-robin: 轮询策略');
  
  const rotationStrategyPrompt = `选择轮询策略`;
  const defaultRotationStrategy = existingConfig.rotationStrategy || 'default';
  config.rotationStrategy = await promptWithDefault(rotationStrategyPrompt, defaultRotationStrategy);

  // 处理API Keys
  const existingApiKeys = Object.keys(existingConfig.apiKeys);
  if (existingApiKeys.length > 0) {
    console.log('\n现有的API Keys:');
    existingApiKeys.forEach(key => console.log(`- ${key}`));
    
    const keepExistingApiKeys = await promptWithDefault('是否保留现有的API Keys? (y/n)', 'y');
    if (keepExistingApiKeys.toLowerCase() === 'y') {
      config.apiKeys = { ...existingConfig.apiKeys };
    }
  }

  // 询问是否添加新的API Key
  const addNewApiKey = await promptWithDefault('是否添加新的API Key? (y/n)', existingApiKeys.length === 0 ? 'y' : 'n');
  if (addNewApiKey.toLowerCase() === 'y') {
    const apiKey = await promptWithDefault('请输入自定义的API Key (不含sk-前缀,将自动添加)', '');
    if (apiKey) {
      const fullApiKey = apiKey.startsWith('sk-') ? apiKey : `sk-${apiKey}`;
      config.apiKeys[fullApiKey] = [];
    } else {
      // 如果用户直接回车跳过,默认添加 sk-text
      config.apiKeys['sk-text'] = [];
      console.log('已默认添加API Key: sk-text');
    }
  } else if (Object.keys(config.apiKeys).length === 0) {
    // 如果没有任何API Key,默认添加 sk-text
    config.apiKeys['sk-text'] = [];
    console.log('已默认添加API Key: sk-text');
  }

  return config;
}

// 生成配置文件
function generateEnvFile(config) {
  try {
    // 准备API Keys
    const apiKeysJson = JSON.stringify(config.apiKeys);
    
    // 替换模板中的占位符
    let envContent = ENV_TEMPLATE
      .replace('{API_KEYS_PLACEHOLDER}', apiKeysJson)
      .replace('{USE_TLS_PROXY_PLACEHOLDER}', config.useTlsProxy)
      .replace('{USE_OTHERS_PROXY_PLACEHOLDER}', config.useOthersProxy)
      .replace('{PROXY_PLATFORM_PLACEHOLDER}', config.proxyPlatform)
      .replace('{USE_OTHERS_PLACEHOLDER}', config.useOthers);
    
    // 更新轮询策略
    envContent = envContent.replace('ROTATION_STRATEGY=default', `ROTATION_STRATEGY=${config.rotationStrategy}`);
    
    // 写入.env文件
    const envPath = path.join(process.cwd(), '.env');
    
    // 检查是否存在备份文件
    const backupPath = path.join(process.cwd(), '.env.backup');
    if (fs.existsSync(envPath)) {
      // 创建备份
      fs.copyFileSync(envPath, backupPath);
      console.log(`\n✅ 已创建原配置文件备份: ${backupPath}`);
    }
    
    fs.writeFileSync(envPath, envContent, 'utf8');
    console.log(`\n✅ 配置文件已生成: ${envPath}`);
    
    // 检查data目录
    const dataDir = path.join(process.cwd(), 'data');
    if (!fs.existsSync(dataDir)) {
      fs.mkdirSync(dataDir, { recursive: true });
      console.log(`✅ 创建数据目录: ${dataDir}`);
    }
    
    return true;
  } catch (error) {
    console.error('\n❌ 生成配置文件时出错:', error.message);
    return false;
  }
}

// 主函数
async function main() {
  try {
    const config = await collectConfig();
    
    if (generateEnvFile(config)) {
      console.log('\n===== 配置完成 =====');
      console.log('你可以使用以下命令启动服务:');
      console.log('  npm start');
      
      // 显示TLS代理配置信息
      console.log(`\n当前TLS代理配置:`);
      console.log(`- 是否启用TLS代理: ${config.useTlsProxy ? '是' : '否'}`);
      if (config.useTlsProxy) {
        console.log(`- 是否启用辅助代理服务器: ${config.useOthersProxy ? '是' : '否'}`);
        console.log(`- 代理服务器平台: ${config.proxyPlatform}`);
      }

      // 显示是否使用其它接口配置信息
      console.log(`\n当前是否使用其它接口: ${config.useOthers ? '是' : '否'}`);
      
      // 显示轮询策略
      console.log(`\n当前轮询策略: ${config.rotationStrategy}`);
      
      // 显示API Keys
      console.log('\n当前配置的API Keys:');
      Object.keys(config.apiKeys).forEach(key => console.log(`- ${key}`));
    }
  } catch (error) {
    console.error('\n❌ 配置过程中出错:', error.message);
  } finally {
    rl.close();
  }
}

// 运行主函数
main();