aihezu 1.3.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/aihezu.js CHANGED
@@ -13,12 +13,12 @@ function showHelp() {
13
13
  console.log('使用方法:');
14
14
  console.log(' npx aihezu <command>\n');
15
15
  console.log('可用命令:');
16
- console.log(' install 配置 Claude Code API Key');
17
- console.log(' clear 清理 Claude Code 缓存和配置');
16
+ console.log(' ccinstall 配置 Claude Code API Key');
17
+ console.log(' ccclear 清理 Claude Code 缓存和配置');
18
18
  console.log(' help 显示帮助信息\n');
19
19
  console.log('示例:');
20
- console.log(' npx aihezu install # 配置 API Key');
21
- console.log(' npx aihezu clear # 清理缓存');
20
+ console.log(' npx aihezu ccinstall # 配置 API Key');
21
+ console.log(' npx aihezu ccclear # 清理缓存');
22
22
  }
23
23
 
24
24
  // 执行子命令
@@ -42,11 +42,11 @@ function runCommand(scriptName) {
42
42
 
43
43
  // 路由命令
44
44
  switch (command) {
45
- case 'install':
45
+ case 'ccinstall':
46
46
  runCommand('ccinstall.js');
47
47
  break;
48
48
 
49
- case 'clear':
49
+ case 'ccclear':
50
50
  runCommand('ccclear.js');
51
51
  break;
52
52
 
package/bin/ccclear.js CHANGED
@@ -4,6 +4,7 @@ const { execSync } = require('child_process');
4
4
  const path = require('path');
5
5
  const os = require('os');
6
6
  const fs = require('fs');
7
+ const { modifyHostsFile } = require('../lib/hosts');
7
8
 
8
9
  const homeDir = os.homedir();
9
10
 
@@ -24,104 +25,6 @@ const timestamp = getLocalTimestamp();
24
25
  console.log('🧹 Claude Code CLI 清理工具');
25
26
  console.log('🌐 Powered by https://aihezu.dev\n');
26
27
 
27
- // 修改 hosts 文件的函数
28
- function modifyHostsFile() {
29
- const isWindows = os.platform() === 'win32';
30
- const hostsPath = isWindows
31
- ? 'C:\\Windows\\System32\\drivers\\etc\\hosts'
32
- : '/etc/hosts';
33
-
34
- const domains = [
35
- 'statsig.anthropic.com',
36
- 'api.anthropic.com'
37
- ];
38
-
39
- try {
40
- console.log('🔧 开始修改 hosts 文件...');
41
-
42
- // 读取现有 hosts 文件内容
43
- let hostsContent = '';
44
- try {
45
- hostsContent = fs.readFileSync(hostsPath, 'utf8');
46
- } catch (error) {
47
- console.error('❌ 无法读取 hosts 文件,请确保以管理员/root权限运行');
48
- return false;
49
- }
50
-
51
- // 备份 hosts 文件
52
- const hostsBackup = `${hostsPath}.backup-${timestamp}`;
53
- try {
54
- fs.writeFileSync(hostsBackup, hostsContent);
55
- console.log(`📦 已备份 hosts 文件到 ${hostsBackup}`);
56
- } catch (error) {
57
- console.error('⚠️ 备份 hosts 文件失败:', error.message);
58
- }
59
-
60
- // 移除所有包含目标域名的现有条目(无论指向什么IP)
61
- const lines = hostsContent.split('\n');
62
- const filteredLines = lines.filter(line => {
63
- const trimmed = line.trim();
64
- // 跳过注释行(但保留其他注释)
65
- if (trimmed.startsWith('#')) {
66
- // 如果是之前添加的标记注释,也移除
67
- if (trimmed.includes('Added by aihezu ccclear tool')) {
68
- return false;
69
- }
70
- return true;
71
- }
72
- // 移除任何包含目标域名的行
73
- return !domains.some(domain => {
74
- const regex = new RegExp('\\s+' + domain.replace('.', '\\.') + '(\\s|$)', 'i');
75
- return regex.test(trimmed);
76
- });
77
- });
78
-
79
- // 添加新的条目
80
- const newEntries = [
81
- '',
82
- '# Added by aihezu ccclear tool',
83
- '127.0.0.1 statsig.anthropic.com',
84
- '127.0.0.1 api.anthropic.com'
85
- ];
86
-
87
- const newHostsContent = filteredLines.join('\n') + '\n' + newEntries.join('\n') + '\n';
88
-
89
- // 写入 hosts 文件
90
- try {
91
- fs.writeFileSync(hostsPath, newHostsContent);
92
- console.log('✅ hosts 文件修改成功!');
93
- console.log(' 已添加/更新以下域名解析:');
94
- console.log(' - statsig.anthropic.com -> 127.0.0.1');
95
- console.log(' - api.anthropic.com -> 127.0.0.1');
96
-
97
- // 刷新 DNS 缓存
98
- try {
99
- if (isWindows) {
100
- execSync('ipconfig /flushdns', { stdio: 'ignore' });
101
- } else if (os.platform() === 'darwin') {
102
- execSync('sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder', { stdio: 'ignore' });
103
- } else {
104
- // Linux
105
- execSync('sudo systemd-resolve --flush-caches 2>/dev/null || sudo service nscd restart 2>/dev/null || true', { stdio: 'ignore' });
106
- }
107
- console.log('🔄 DNS 缓存已刷新');
108
- } catch (e) {
109
- console.log('ℹ️ DNS 缓存刷新失败(可能需要手动刷新)');
110
- }
111
-
112
- return true;
113
- } catch (error) {
114
- console.error('❌ 无法写入 hosts 文件,请确保以管理员/root权限运行');
115
- console.error(' macOS/Linux: 使用 sudo 运行');
116
- console.error(' Windows: 以管理员身份运行命令提示符');
117
- return false;
118
- }
119
- } catch (error) {
120
- console.error('❌ 修改 hosts 文件失败:', error.message);
121
- return false;
122
- }
123
- }
124
-
125
28
  try {
126
29
  // 首先修改 hosts 文件
127
30
  console.log('=== 步骤 1: 修改 hosts 文件 ===\n');
package/bin/ccinstall.js CHANGED
@@ -4,6 +4,7 @@ const readline = require('readline');
4
4
  const path = require('path');
5
5
  const os = require('os');
6
6
  const fs = require('fs');
7
+ const { modifyHostsFile } = require('../lib/hosts');
7
8
 
8
9
  const homeDir = os.homedir();
9
10
  const settingsPath = path.join(homeDir, '.claude', 'settings.json');
@@ -64,12 +65,19 @@ rl.question('请输入您的 API Key: ', (apiKey) => {
64
65
  // 写入配置文件
65
66
  fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2), 'utf8');
66
67
 
67
- console.log('\n 配置成功!');
68
+ console.log('\n=== 步骤 1: API 配置 ===\n');
69
+ console.log('✅ 配置成功!');
68
70
  console.log('📝 已更新配置文件:', settingsPath);
69
71
  console.log('\n配置内容:');
70
72
  console.log(' ANTHROPIC_AUTH_TOKEN:', apiKey);
71
73
  console.log(' ANTHROPIC_BASE_URL:', 'https://cc.aihezu.dev/api');
72
- console.log('\n💡 您现在可以开始使用 Claude Code 了!');
74
+
75
+ // 修改 hosts 文件
76
+ console.log('\n=== 步骤 2: 修改 hosts 文件 ===\n');
77
+ modifyHostsFile();
78
+
79
+ console.log('\n=== 全部完成 ===');
80
+ console.log('💡 您现在可以开始使用 Claude Code 了!');
73
81
  console.log('🌐 更多服务请访问: https://aihezu.dev');
74
82
 
75
83
  } catch (error) {
package/lib/hosts.js ADDED
@@ -0,0 +1,118 @@
1
+ const { execSync } = require('child_process');
2
+ const os = require('os');
3
+ const fs = require('fs');
4
+
5
+ // 生成本地时间戳,格式:YYYYMMDDHHMMSS
6
+ function getLocalTimestamp() {
7
+ const now = new Date();
8
+ const year = now.getFullYear();
9
+ const month = String(now.getMonth() + 1).padStart(2, '0');
10
+ const day = String(now.getDate()).padStart(2, '0');
11
+ const hours = String(now.getHours()).padStart(2, '0');
12
+ const minutes = String(now.getMinutes()).padStart(2, '0');
13
+ const seconds = String(now.getSeconds()).padStart(2, '0');
14
+ return `${year}${month}${day}${hours}${minutes}${seconds}`;
15
+ }
16
+
17
+ // 修改 hosts 文件的函数
18
+ function modifyHostsFile() {
19
+ const isWindows = os.platform() === 'win32';
20
+ const hostsPath = isWindows
21
+ ? 'C:\\Windows\\System32\\drivers\\etc\\hosts'
22
+ : '/etc/hosts';
23
+
24
+ const domains = [
25
+ 'statsig.anthropic.com',
26
+ 'api.anthropic.com'
27
+ ];
28
+
29
+ try {
30
+ console.log('🔧 开始修改 hosts 文件...');
31
+
32
+ // 读取现有 hosts 文件内容
33
+ let hostsContent = '';
34
+ try {
35
+ hostsContent = fs.readFileSync(hostsPath, 'utf8');
36
+ } catch (error) {
37
+ console.error('❌ 无法读取 hosts 文件,请确保以管理员/root权限运行');
38
+ return false;
39
+ }
40
+
41
+ // 备份 hosts 文件
42
+ const timestamp = getLocalTimestamp();
43
+ const hostsBackup = `${hostsPath}.backup-${timestamp}`;
44
+ try {
45
+ fs.writeFileSync(hostsBackup, hostsContent);
46
+ console.log(`📦 已备份 hosts 文件到 ${hostsBackup}`);
47
+ } catch (error) {
48
+ console.error('⚠️ 备份 hosts 文件失败:', error.message);
49
+ }
50
+
51
+ // 移除所有包含目标域名的现有条目(无论指向什么IP)
52
+ const lines = hostsContent.split('\n');
53
+ const filteredLines = lines.filter(line => {
54
+ const trimmed = line.trim();
55
+ // 跳过注释行(但保留其他注释)
56
+ if (trimmed.startsWith('#')) {
57
+ // 如果是之前添加的标记注释,也移除
58
+ if (trimmed.includes('Added by aihezu ccclear tool')) {
59
+ return false;
60
+ }
61
+ return true;
62
+ }
63
+ // 移除任何包含目标域名的行
64
+ return !domains.some(domain => {
65
+ const regex = new RegExp('\\s+' + domain.replace('.', '\\.') + '(\\s|$)', 'i');
66
+ return regex.test(trimmed);
67
+ });
68
+ });
69
+
70
+ // 添加新的条目
71
+ const newEntries = [
72
+ '',
73
+ '# Added by aihezu ccclear tool',
74
+ '127.0.0.1 statsig.anthropic.com',
75
+ '127.0.0.1 api.anthropic.com'
76
+ ];
77
+
78
+ const newHostsContent = filteredLines.join('\n') + '\n' + newEntries.join('\n') + '\n';
79
+
80
+ // 写入 hosts 文件
81
+ try {
82
+ fs.writeFileSync(hostsPath, newHostsContent);
83
+ console.log('✅ hosts 文件修改成功!');
84
+ console.log(' 已添加/更新以下域名解析:');
85
+ console.log(' - statsig.anthropic.com -> 127.0.0.1');
86
+ console.log(' - api.anthropic.com -> 127.0.0.1');
87
+
88
+ // 刷新 DNS 缓存
89
+ try {
90
+ if (isWindows) {
91
+ execSync('ipconfig /flushdns', { stdio: 'ignore' });
92
+ } else if (os.platform() === 'darwin') {
93
+ execSync('sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder', { stdio: 'ignore' });
94
+ } else {
95
+ // Linux
96
+ execSync('sudo systemd-resolve --flush-caches 2>/dev/null || sudo service nscd restart 2>/dev/null || true', { stdio: 'ignore' });
97
+ }
98
+ console.log('🔄 DNS 缓存已刷新');
99
+ } catch (e) {
100
+ console.log('ℹ️ DNS 缓存刷新失败(可能需要手动刷新)');
101
+ }
102
+
103
+ return true;
104
+ } catch (error) {
105
+ console.error('❌ 无法写入 hosts 文件,请确保以管理员/root权限运行');
106
+ console.error(' macOS/Linux: 使用 sudo 运行');
107
+ console.error(' Windows: 以管理员身份运行命令提示符');
108
+ return false;
109
+ }
110
+ } catch (error) {
111
+ console.error('❌ 修改 hosts 文件失败:', error.message);
112
+ return false;
113
+ }
114
+ }
115
+
116
+ module.exports = {
117
+ modifyHostsFile
118
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aihezu",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "description": "Claude Code CLI 清理工具 - 快速备份和清理 Claude Code 的本地配置和缓存,同时修改 hosts 文件实现本地代理",
5
5
  "main": "bin/ccclear.js",
6
6
  "bin": {