├── .github-readme.md ├── .gitignore ├── CHANGELOG.md ├── quick-start.md ├── LICENSE ├── CONTRIBUTING.md ├── test.sh ├── examples.md ├── README.md ├── install.sh ├── README_EN.md └── claude-proxy-manager.sh /.github-readme.md: -------------------------------------------------------------------------------- 1 | # Claude Proxy Manager 2 | 3 | 一个用于管理多个 Claude Code 代理商配置的命令行工具。 4 | 5 | ## 快速安装 6 | 7 | ```bash 8 | curl -fsSL https://raw.githubusercontent.com/greezi/claude-proxy-manager/main/install.sh | bash 9 | ``` 10 | 11 | 查看 [完整文档](README.md) 了解更多信息。 12 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # macOS 2 | .DS_Store 3 | .DS_Store? 4 | ._* 5 | .Spotlight-V100 6 | .Trashes 7 | ehthumbs.db 8 | Thumbs.db 9 | 10 | # Temporary files 11 | *.tmp 12 | *.bak 13 | *~ 14 | 15 | # Logs 16 | *.log 17 | 18 | # IDE 19 | .vscode/ 20 | .idea/ 21 | *.swp 22 | *.swo 23 | 24 | # Test files 25 | test_* 26 | tmp_* -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # 更新日志 2 | 3 | 所有重要的项目更改都将记录在此文件中。 4 | 5 | 格式基于 [Keep a Changelog](https://keepachangelog.com/zh-CN/1.0.0/), 6 | 并且本项目遵循 [语义化版本](https://semver.org/lang/zh-CN/)。 7 | 8 | ## [未发布] 9 | 10 | ### 计划功能 11 | - 支持配置文件导入/导出 12 | - 支持代理商配置验证 13 | - 添加配置加密功能 14 | - 支持批量操作 15 | 16 | ## [1.0.0] - 2025-07-17 17 | 18 | ### 新增 19 | - 初始版本发布 20 | - 代理商配置的增删改查功能 21 | - 快速切换代理商功能 22 | - 自动管理 shell 配置文件 23 | - 彩色命令行界面 24 | - 一键安装脚本 25 | - 完整的帮助文档 26 | - 错误处理和验证 27 | - 配置持久化存储 28 | 29 | ### 功能 30 | - `add` - 添加新的代理商配置 31 | - `remove` - 删除代理商配置 32 | - `list` - 列出所有配置的代理商 33 | - `switch` - 切换到指定代理商 34 | - `current` - 显示当前使用的代理商 35 | - `update` - 更新代理商信息 36 | - `help` - 显示帮助信息 37 | 38 | ### 技术特性 39 | - 支持 macOS 系统 40 | - 自动检测和安装依赖 (jq, Homebrew) 41 | - 支持多种 shell 配置文件 42 | - JSON 格式配置存储 43 | - 安全的本地存储 44 | - 完整的错误处理 45 | 46 | ### 文档 47 | - 详细的 README.md 48 | - 使用示例文档 49 | - 安装说明 50 | - 故障排除指南 51 | - MIT 许可证 -------------------------------------------------------------------------------- /quick-start.md: -------------------------------------------------------------------------------- 1 | # 快速开始 2 | 3 | ## 一键安装 4 | 5 | ```bash 6 | curl -fsSL https://raw.githubusercontent.com/greezi/claude-proxy-manager/main/install.sh | bash 7 | ``` 8 | 9 | ## 基本使用 10 | 11 | ### 1. 添加代理商 12 | 13 | ```bash 14 | # 添加官方 API 15 | claude-proxy add official sk-ant-api03-xxx https://api.anthropic.com 16 | 17 | # 添加代理商 18 | claude-proxy add proxy1 sk-ant-api03-yyy https://proxy1.example.com 19 | ``` 20 | 21 | ### 2. 切换代理商 22 | 23 | ```bash 24 | # 查看所有代理商 25 | claude-proxy list 26 | 27 | # 切换到指定代理商 28 | claude-proxy switch proxy1 29 | 30 | # 查看当前代理商 31 | claude-proxy current 32 | ``` 33 | 34 | ### 3. 使配置生效 35 | 36 | ```bash 37 | # 重新加载配置 38 | source ~/.zshrc 39 | 40 | # 或重启终端 41 | ``` 42 | 43 | ## 常用命令 44 | 45 | | 命令 | 说明 | 46 | |------|------| 47 | | `claude-proxy add ` | 添加代理商 | 48 | | `claude-proxy list` | 列出所有代理商 | 49 | | `claude-proxy switch ` | 切换代理商 | 50 | | `claude-proxy current` | 查看当前代理商 | 51 | | `claude-proxy update ` | 更新代理商 | 52 | | `claude-proxy remove ` | 删除代理商 | 53 | | `claude-proxy help` | 查看帮助 | 54 | 55 | 就是这么简单!🎉 -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2025 Claude Proxy Manager 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # 贡献指南 2 | 3 | 感谢您对 Claude Proxy Manager 项目的关注!我们欢迎各种形式的贡献。 4 | 5 | ## 如何贡献 6 | 7 | ### 报告 Bug 8 | 9 | 如果您发现了 bug,请通过 [GitHub Issues](https://github.com/greezi/claude-proxy-manager/issues) 报告: 10 | 11 | 1. 使用清晰、描述性的标题 12 | 2. 详细描述重现步骤 13 | 3. 提供预期行为和实际行为的对比 14 | 4. 包含系统信息(macOS 版本、shell 类型等) 15 | 5. 如果可能,提供错误日志 16 | 17 | ### 建议功能 18 | 19 | 我们欢迎新功能建议: 20 | 21 | 1. 检查是否已有相似的建议 22 | 2. 详细描述功能需求和使用场景 23 | 3. 解释为什么这个功能对用户有价值 24 | 25 | ### 提交代码 26 | 27 | #### 开发环境设置 28 | 29 | ```bash 30 | # 克隆仓库 31 | git clone https://github.com/greezi/claude-proxy-manager.git 32 | cd claude-proxy-manager 33 | 34 | # 安装依赖 35 | brew install jq 36 | 37 | # 运行测试 38 | ./test.sh 39 | ``` 40 | 41 | #### 代码规范 42 | 43 | - 使用 4 个空格缩进 44 | - 函数名使用下划线命名法 (snake_case) 45 | - 变量名使用下划线命名法 46 | - 添加适当的注释 47 | - 保持代码简洁易读 48 | 49 | #### 提交流程 50 | 51 | 1. Fork 本仓库 52 | 2. 创建功能分支 (`git checkout -b feature/amazing-feature`) 53 | 3. 进行更改 54 | 4. 运行测试确保通过 (`./test.sh`) 55 | 5. 提交更改 (`git commit -m 'Add amazing feature'`) 56 | 6. 推送到分支 (`git push origin feature/amazing-feature`) 57 | 7. 创建 Pull Request 58 | 59 | #### 提交信息规范 60 | 61 | 使用清晰的提交信息: 62 | 63 | ``` 64 | 类型(范围): 简短描述 65 | 66 | 详细描述(可选) 67 | 68 | - 相关 issue: #123 69 | ``` 70 | 71 | 类型: 72 | - `feat`: 新功能 73 | - `fix`: Bug 修复 74 | - `docs`: 文档更新 75 | - `style`: 代码格式调整 76 | - `refactor`: 代码重构 77 | - `test`: 测试相关 78 | - `chore`: 构建过程或辅助工具的变动 79 | 80 | ### 测试 81 | 82 | 在提交代码前,请确保: 83 | 84 | 1. 所有现有测试通过 85 | 2. 新功能包含相应测试 86 | 3. 代码覆盖率不降低 87 | 88 | 运行测试: 89 | ```bash 90 | ./test.sh 91 | ``` 92 | 93 | ### 文档 94 | 95 | 如果您的更改影响用户界面或功能: 96 | 97 | 1. 更新 README.md 98 | 2. 更新相关示例 99 | 3. 更新 CHANGELOG.md 100 | 101 | ## 代码审查 102 | 103 | 所有提交都需要经过代码审查: 104 | 105 | 1. 确保代码符合项目规范 106 | 2. 功能正确实现 107 | 3. 包含适当的测试 108 | 4. 文档完整 109 | 110 | ## 发布流程 111 | 112 | 维护者负责版本发布: 113 | 114 | 1. 更新版本号 115 | 2. 更新 CHANGELOG.md 116 | 3. 创建 Git 标签 117 | 4. 发布 GitHub Release 118 | 119 | ## 社区准则 120 | 121 | 请遵循以下准则: 122 | 123 | - 保持友善和专业 124 | - 尊重不同观点 125 | - 专注于建设性反馈 126 | - 帮助新贡献者 127 | 128 | ## 获得帮助 129 | 130 | 如果您需要帮助: 131 | 132 | 1. 查看现有文档 133 | 2. 搜索已有 Issues 134 | 3. 创建新 Issue 询问 135 | 136 | 感谢您的贡献! -------------------------------------------------------------------------------- /test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Claude Proxy Manager 测试脚本 4 | 5 | # 颜色定义 6 | RED='\033[0;31m' 7 | GREEN='\033[0;32m' 8 | YELLOW='\033[1;33m' 9 | BLUE='\033[0;34m' 10 | NC='\033[0m' # No Color 11 | 12 | SCRIPT="./claude-proxy-manager.sh" 13 | TEST_COUNT=0 14 | PASS_COUNT=0 15 | 16 | # 测试函数 17 | test_function() { 18 | local test_name="$1" 19 | local command="$2" 20 | local expected_exit_code="${3:-0}" 21 | 22 | TEST_COUNT=$((TEST_COUNT + 1)) 23 | echo -e "${BLUE}测试 $TEST_COUNT: $test_name${NC}" 24 | 25 | if eval "$command" > /dev/null 2>&1; then 26 | actual_exit_code=0 27 | else 28 | actual_exit_code=1 29 | fi 30 | 31 | if [ "$actual_exit_code" -eq "$expected_exit_code" ]; then 32 | echo -e "${GREEN}✓ 通过${NC}" 33 | PASS_COUNT=$((PASS_COUNT + 1)) 34 | else 35 | echo -e "${RED}✗ 失败 (期望退出码: $expected_exit_code, 实际: $actual_exit_code)${NC}" 36 | fi 37 | echo "" 38 | } 39 | 40 | # 清理函数 41 | cleanup() { 42 | echo -e "${YELLOW}清理测试环境...${NC}" 43 | rm -rf ~/.claude-proxy-manager 44 | 45 | # 清理测试添加的环境变量 46 | for config_file in ~/.bash_profile ~/.bashrc ~/.zshrc; do 47 | if [ -f "$config_file" ]; then 48 | sed -i.bak '/^export ANTHROPIC_AUTH_TOKEN=/d' "$config_file" 2>/dev/null || true 49 | sed -i.bak '/^export ANTHROPIC_BASE_URL=/d' "$config_file" 2>/dev/null || true 50 | rm -f "${config_file}.bak" 51 | fi 52 | done 53 | } 54 | 55 | # 开始测试 56 | echo -e "${BLUE}Claude Proxy Manager 功能测试${NC}" 57 | echo "==================================" 58 | echo "" 59 | 60 | # 清理之前的测试数据 61 | cleanup 62 | 63 | # 测试帮助命令 64 | test_function "显示帮助信息" "$SCRIPT help" 65 | 66 | # 测试添加代理商 67 | test_function "添加第一个代理商" "$SCRIPT add proxy1 sk-ant-token1 https://proxy1.com" 68 | test_function "添加第二个代理商" "$SCRIPT add proxy2 sk-ant-token2 https://proxy2.com" 69 | 70 | # 测试重复添加(应该失败) 71 | test_function "重复添加代理商(应该失败)" "$SCRIPT add proxy1 sk-ant-token1 https://proxy1.com" 1 72 | 73 | # 测试列出代理商 74 | test_function "列出所有代理商" "$SCRIPT list" 75 | 76 | # 测试切换代理商 77 | test_function "切换到 proxy1" "$SCRIPT switch proxy1" 78 | test_function "查看当前代理商" "$SCRIPT current" 79 | 80 | # 测试切换到不存在的代理商(应该失败) 81 | test_function "切换到不存在的代理商(应该失败)" "$SCRIPT switch nonexistent" 1 82 | 83 | # 测试更新代理商 84 | test_function "更新代理商信息" "$SCRIPT update proxy1 sk-ant-new-token https://new-proxy1.com" 85 | 86 | # 测试更新不存在的代理商(应该失败) 87 | test_function "更新不存在的代理商(应该失败)" "$SCRIPT update nonexistent token url" 1 88 | 89 | # 测试删除代理商 90 | test_function "删除 proxy2" "$SCRIPT remove proxy2" 91 | 92 | # 测试删除不存在的代理商(应该失败) 93 | test_function "删除不存在的代理商(应该失败)" "$SCRIPT remove nonexistent" 1 94 | 95 | # 测试参数不足的情况 96 | test_function "添加代理商参数不足(应该失败)" "$SCRIPT add proxy3" 1 97 | test_function "更新代理商参数不足(应该失败)" "$SCRIPT update proxy1" 1 98 | 99 | # 测试无效命令 100 | test_function "无效命令(应该失败)" "$SCRIPT invalid-command" 1 101 | 102 | # 显示测试结果 103 | echo "==================================" 104 | echo -e "${BLUE}测试完成${NC}" 105 | echo -e "总测试数: $TEST_COUNT" 106 | echo -e "${GREEN}通过: $PASS_COUNT${NC}" 107 | echo -e "${RED}失败: $((TEST_COUNT - PASS_COUNT))${NC}" 108 | 109 | if [ "$PASS_COUNT" -eq "$TEST_COUNT" ]; then 110 | echo -e "${GREEN}🎉 所有测试通过!${NC}" 111 | exit_code=0 112 | else 113 | echo -e "${RED}❌ 部分测试失败${NC}" 114 | exit_code=1 115 | fi 116 | 117 | # 清理测试环境 118 | cleanup 119 | 120 | exit $exit_code -------------------------------------------------------------------------------- /examples.md: -------------------------------------------------------------------------------- 1 | # 使用示例 2 | 3 | ## 快速开始 4 | 5 | ### 1. 安装工具 6 | 7 | ```bash 8 | curl -fsSL https://raw.githubusercontent.com/greezi/claude-proxy-manager/main/install.sh | bash 9 | ``` 10 | 11 | ### 2. 添加第一个代理商 12 | 13 | ```bash 14 | # 添加官方 API(如果您有官方访问权限) 15 | claude-proxy add official sk-ant-api03-your-official-token https://api.anthropic.com 16 | 17 | # 添加代理商(替换为您的实际配置) 18 | claude-proxy add proxy1 sk-ant-api03-your-proxy-token https://your-proxy-domain.com 19 | ``` 20 | 21 | ### 3. 查看配置 22 | 23 | ```bash 24 | # 列出所有代理商 25 | claude-proxy list 26 | 27 | # 输出示例: 28 | # 已配置的代理商: 29 | # 30 | # * official - https://api.anthropic.com - sk-ant-api03-your-of... 31 | # proxy1 - https://your-proxy-domain.com - sk-ant-api03-your-pr... 32 | ``` 33 | 34 | ### 4. 切换代理商 35 | 36 | ```bash 37 | # 切换到代理商 38 | claude-proxy switch proxy1 39 | 40 | # 查看当前使用的代理商 41 | claude-proxy current 42 | ``` 43 | 44 | ### 5. 使配置生效 45 | 46 | ```bash 47 | # 重新加载配置 48 | source ~/.zshrc 49 | 50 | # 或者重启终端 51 | ``` 52 | 53 | ## 常用场景 54 | 55 | ### 场景1: 多个代理商管理 56 | 57 | ```bash 58 | # 添加多个代理商 59 | claude-proxy add proxy-us sk-ant-xxx https://us.proxy.com 60 | claude-proxy add proxy-eu sk-ant-yyy https://eu.proxy.com 61 | claude-proxy add proxy-asia sk-ant-zzz https://asia.proxy.com 62 | 63 | # 根据需要切换 64 | claude-proxy switch proxy-us # 使用美国代理 65 | claude-proxy switch proxy-eu # 使用欧洲代理 66 | claude-proxy switch proxy-asia # 使用亚洲代理 67 | ``` 68 | 69 | ### 场景2: 开发和生产环境 70 | 71 | ```bash 72 | # 开发环境 73 | claude-proxy add dev sk-ant-dev-token https://dev-api.example.com 74 | 75 | # 生产环境 76 | claude-proxy add prod sk-ant-prod-token https://api.example.com 77 | 78 | # 切换环境 79 | claude-proxy switch dev # 开发环境 80 | claude-proxy switch prod # 生产环境 81 | ``` 82 | 83 | ### 场景3: 更新配置 84 | 85 | ```bash 86 | # 更新代理商的令牌或URL 87 | claude-proxy update proxy1 sk-ant-new-token https://new-url.com 88 | 89 | # 如果当前正在使用该代理商,配置会自动生效 90 | ``` 91 | 92 | ### 场景4: 清理不需要的配置 93 | 94 | ```bash 95 | # 删除不再使用的代理商 96 | claude-proxy remove old-proxy 97 | 98 | # 查看剩余配置 99 | claude-proxy list 100 | ``` 101 | 102 | ## 验证配置 103 | 104 | ### 检查环境变量 105 | 106 | ```bash 107 | # 查看当前环境变量 108 | echo $ANTHROPIC_AUTH_TOKEN 109 | echo $ANTHROPIC_BASE_URL 110 | ``` 111 | 112 | ### 测试 Claude Code 连接 113 | 114 | ```bash 115 | # 如果您安装了 claude-cli 或类似工具 116 | claude --version 117 | 118 | # 或者使用 curl 测试 API 连接 119 | curl -H "Authorization: Bearer $ANTHROPIC_AUTH_TOKEN" \ 120 | -H "Content-Type: application/json" \ 121 | "$ANTHROPIC_BASE_URL/v1/messages" \ 122 | -d '{"model":"claude-3-sonnet-20240229","max_tokens":10,"messages":[{"role":"user","content":"Hi"}]}' 123 | ``` 124 | 125 | ## 故障排除示例 126 | 127 | ### 问题1: 环境变量未生效 128 | 129 | ```bash 130 | # 检查当前 shell 131 | echo $SHELL 132 | 133 | # 手动加载配置 134 | source ~/.zshrc 135 | 136 | # 或者检查配置文件 137 | cat ~/.zshrc | grep ANTHROPIC 138 | ``` 139 | 140 | ### 问题2: 配置文件权限问题 141 | 142 | ```bash 143 | # 检查配置文件权限 144 | ls -la ~/.zshrc ~/.bash_profile ~/.bashrc 145 | 146 | # 修复权限(如果需要) 147 | chmod 644 ~/.zshrc 148 | ``` 149 | 150 | ### 问题3: jq 未安装 151 | 152 | ```bash 153 | # 手动安装 jq 154 | brew install jq 155 | 156 | # 验证安装 157 | jq --version 158 | ``` 159 | 160 | ## 高级用法 161 | 162 | ### 备份和恢复配置 163 | 164 | ```bash 165 | # 备份配置 166 | cp -r ~/.claude-proxy-manager ~/claude-proxy-backup 167 | 168 | # 恢复配置 169 | cp -r ~/claude-proxy-backup ~/.claude-proxy-manager 170 | ``` 171 | 172 | ### 查看配置文件 173 | 174 | ```bash 175 | # 查看代理商配置 176 | cat ~/.claude-proxy-manager/providers.json | jq . 177 | 178 | # 查看当前使用的代理商 179 | cat ~/.claude-proxy-manager/current 180 | ``` 181 | 182 | ### 批量操作 183 | 184 | ```bash 185 | # 批量添加代理商(示例脚本) 186 | #!/bin/bash 187 | claude-proxy add proxy1 token1 https://proxy1.com 188 | claude-proxy add proxy2 token2 https://proxy2.com 189 | claude-proxy add proxy3 token3 https://proxy3.com 190 | ``` -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Claude Code 代理商管理器 2 | 3 | > 🤖 **本项目完全由 Claude Code 自主开发** 4 | 5 | **中文** | [English](README_EN.md) 6 | 7 | 一个用于管理多个 Claude Code 代理商配置的命令行工具,支持快速切换不同的 API 服务提供商。 8 | 9 | ## 功能特性 10 | 11 | - 🚀 **快速切换**: 一键切换不同的 Claude Code 代理商 12 | - 📝 **配置管理**: 支持添加、删除、更新代理商配置 13 | - 🔧 **自动配置**: 自动更新 shell 配置文件 (`.bash_profile`, `.bashrc`, `.zshrc`) 14 | - 💾 **持久化存储**: 配置信息安全存储在本地 15 | - 🎨 **友好界面**: 彩色输出,清晰的状态提示 16 | - 🛡️ **安全可靠**: 本地存储,不上传任何敏感信息 17 | 18 | ## 系统要求 19 | 20 | - macOS 系统 21 | - Homebrew (安装脚本会自动安装) 22 | - jq (安装脚本会自动安装) 23 | 24 | ## 一键安装 25 | 26 | ```bash 27 | curl -fsSL https://raw.githubusercontent.com/greezi/claude-proxy-manager/main/install.sh | bash 28 | ``` 29 | 30 | 或者手动安装: 31 | 32 | ```bash 33 | # 克隆仓库 34 | git clone https://github.com/greezi/claude-proxy-manager.git 35 | cd claude-proxy-manager 36 | 37 | # 运行安装脚本 38 | ./install.sh 39 | ``` 40 | 41 | ## 使用方法 42 | 43 | ### 基本命令 44 | 45 | ```bash 46 | # 查看帮助 47 | claude-proxy help 48 | 49 | # 添加代理商 50 | claude-proxy add <名称> <令牌> <基础URL> 51 | 52 | # 列出所有代理商 53 | claude-proxy list 54 | 55 | # 切换代理商 56 | claude-proxy switch <名称> 57 | 58 | # 查看当前代理商 59 | claude-proxy current 60 | 61 | # 更新代理商信息 62 | claude-proxy update <名称> <新令牌> <新URL> 63 | 64 | # 删除代理商 65 | claude-proxy remove <名称> 66 | ``` 67 | 68 | ### 使用示例 69 | 70 | ```bash 71 | # 添加官方 API 72 | claude-proxy add official sk-ant-api03-xxx https://api.anthropic.com 73 | 74 | # 添加代理商1 75 | claude-proxy add proxy1 sk-ant-api03-yyy https://api.proxy1.com 76 | 77 | # 添加代理商2 78 | claude-proxy add proxy2 sk-ant-api03-zzz https://api.proxy2.com 79 | 80 | # 查看所有配置的代理商 81 | claude-proxy list 82 | 83 | # 切换到代理商1 84 | claude-proxy switch proxy1 85 | 86 | # 查看当前使用的代理商 87 | claude-proxy current 88 | 89 | # 更新代理商信息 90 | claude-proxy update proxy1 sk-ant-api03-new https://new.proxy1.com 91 | 92 | # 删除不需要的代理商 93 | claude-proxy remove proxy2 94 | ``` 95 | 96 | ## 配置文件 97 | 98 | 工具会自动管理以下配置文件中的环境变量: 99 | 100 | - `~/.bash_profile` 101 | - `~/.bashrc` 102 | - `~/.zshrc` 103 | 104 | 管理的环境变量: 105 | - `ANTHROPIC_AUTH_TOKEN`: Claude API 认证令牌 106 | - `ANTHROPIC_BASE_URL`: Claude API 基础URL 107 | 108 | ## 配置存储 109 | 110 | 所有配置信息存储在 `~/.claude-proxy-manager/` 目录下: 111 | 112 | - `providers.json`: 代理商配置信息 113 | - `current`: 当前使用的代理商名称 114 | 115 | ## 注意事项 116 | 117 | 1. **重启终端**: 切换代理商后需要重启终端或运行 `source ~/.zshrc` 使配置生效 118 | 2. **令牌安全**: 所有令牌信息仅存储在本地,请妥善保管 119 | 3. **备份配置**: 建议定期备份 `~/.claude-proxy-manager/` 目录 120 | 121 | ## 故障排除 122 | 123 | ### 常见问题 124 | 125 | **Q: 切换代理商后环境变量没有生效?** 126 | A: 请重启终端或运行 `source ~/.zshrc` 127 | 128 | **Q: 提示 jq 命令未找到?** 129 | A: 运行 `brew install jq` 安装 jq 130 | 131 | **Q: 无法写入配置文件?** 132 | A: 检查 shell 配置文件的权限,确保当前用户有写入权限 133 | 134 | **Q: 代理商列表为空?** 135 | A: 使用 `claude-proxy add` 命令添加第一个代理商配置 136 | 137 | ### 手动卸载 138 | 139 | ```bash 140 | # 删除命令 141 | sudo rm -f /usr/local/bin/claude-proxy 142 | 143 | # 删除配置目录 144 | rm -rf ~/.claude-proxy-manager 145 | 146 | # 手动清理环境变量(可选) 147 | # 编辑 ~/.zshrc 等文件,删除 ANTHROPIC_AUTH_TOKEN 和 ANTHROPIC_BASE_URL 相关行 148 | ``` 149 | 150 | ## 开发 151 | 152 | ### 本地开发 153 | 154 | ```bash 155 | # 克隆仓库 156 | git clone https://github.com/greezi/claude-proxy-manager.git 157 | cd claude-proxy-manager 158 | 159 | # 直接运行脚本 160 | ./claude-proxy-manager.sh help 161 | 162 | # 本地安装测试 163 | ./install.sh 164 | ``` 165 | 166 | ### 贡献 167 | 168 | 欢迎提交 Issue 和 Pull Request! 169 | 170 | 1. Fork 本仓库 171 | 2. 创建特性分支 (`git checkout -b feature/AmazingFeature`) 172 | 3. 提交更改 (`git commit -m 'Add some AmazingFeature'`) 173 | 4. 推送到分支 (`git push origin feature/AmazingFeature`) 174 | 5. 开启 Pull Request 175 | 176 | ## 许可证 177 | 178 | 本项目采用 MIT 许可证 - 查看 [LICENSE](LICENSE) 文件了解详情。 179 | 180 | ## 更新日志 181 | 182 | ### v1.0.0 (2025-07-17) 183 | - 初始版本发布 184 | - 支持代理商的增删改查 185 | - 支持快速切换功能 186 | - 一键安装脚本 187 | - 完整的文档 188 | 189 | ## 支持 190 | 191 | 如果您觉得这个工具有用,请给个 ⭐️ Star! 192 | 193 | 如有问题或建议,请提交 [Issue](https://github.com/greezi/claude-proxy-manager/issues)。 -------------------------------------------------------------------------------- /install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Claude Code 代理商管理器 - 一键安装脚本 4 | # 版本: 1.0.0 5 | 6 | # 颜色定义 7 | RED='\033[0;31m' 8 | GREEN='\033[0;32m' 9 | YELLOW='\033[1;33m' 10 | BLUE='\033[0;34m' 11 | NC='\033[0m' # No Color 12 | 13 | # 安装路径 14 | INSTALL_DIR="/usr/local/bin" 15 | SCRIPT_NAME="claude-proxy" 16 | SCRIPT_URL="https://raw.githubusercontent.com/greezi/claude-proxy-manager/main/claude-proxy-manager.sh" 17 | 18 | echo -e "${BLUE}Claude Code 代理商管理器 - 安装程序${NC}" 19 | echo "========================================" 20 | 21 | # 检查是否为 macOS 22 | if [[ "$OSTYPE" != "darwin"* ]]; then 23 | echo -e "${RED}错误: 此脚本仅支持 macOS 系统${NC}" 24 | exit 1 25 | fi 26 | 27 | # 检查是否安装了 jq 28 | check_jq() { 29 | if ! command -v jq &> /dev/null; then 30 | echo -e "${YELLOW}检测到未安装 jq,正在安装...${NC}" 31 | 32 | # 检查是否安装了 Homebrew 33 | if ! command -v brew &> /dev/null; then 34 | echo -e "${YELLOW}检测到未安装 Homebrew,正在安装...${NC}" 35 | /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" 36 | 37 | # 添加 Homebrew 到 PATH 38 | if [[ -f "/opt/homebrew/bin/brew" ]]; then 39 | echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zshrc 40 | eval "$(/opt/homebrew/bin/brew shellenv)" 41 | elif [[ -f "/usr/local/bin/brew" ]]; then 42 | echo 'eval "$(/usr/local/bin/brew shellenv)"' >> ~/.zshrc 43 | eval "$(/usr/local/bin/brew shellenv)" 44 | fi 45 | fi 46 | 47 | # 安装 jq 48 | brew install jq 49 | 50 | if ! command -v jq &> /dev/null; then 51 | echo -e "${RED}错误: jq 安装失败${NC}" 52 | exit 1 53 | fi 54 | 55 | echo -e "${GREEN}jq 安装成功${NC}" 56 | else 57 | echo -e "${GREEN}jq 已安装${NC}" 58 | fi 59 | } 60 | 61 | # 下载并安装脚本 62 | install_script() { 63 | echo -e "${YELLOW}正在下载脚本...${NC}" 64 | 65 | # 如果是从本地安装(开发模式) 66 | if [[ -f "claude-proxy-manager.sh" ]]; then 67 | echo -e "${YELLOW}检测到本地脚本文件,使用本地版本...${NC}" 68 | sudo cp claude-proxy-manager.sh "$INSTALL_DIR/$SCRIPT_NAME" 69 | else 70 | # 从 GitHub 下载 71 | if ! curl -fsSL "$SCRIPT_URL" -o "/tmp/$SCRIPT_NAME"; then 72 | echo -e "${RED}错误: 下载脚本失败${NC}" 73 | echo "请检查网络连接或手动下载安装" 74 | exit 1 75 | fi 76 | 77 | # 安装脚本 78 | sudo mv "/tmp/$SCRIPT_NAME" "$INSTALL_DIR/$SCRIPT_NAME" 79 | fi 80 | 81 | # 设置执行权限 82 | sudo chmod +x "$INSTALL_DIR/$SCRIPT_NAME" 83 | 84 | echo -e "${GREEN}脚本安装成功: $INSTALL_DIR/$SCRIPT_NAME${NC}" 85 | } 86 | 87 | # 验证安装 88 | verify_installation() { 89 | if command -v claude-proxy &> /dev/null; then 90 | echo -e "${GREEN}安装验证成功!${NC}" 91 | echo "" 92 | echo -e "${BLUE}使用方法:${NC}" 93 | echo " claude-proxy help # 查看帮助" 94 | echo " claude-proxy add # 添加代理商" 95 | echo " claude-proxy list # 列出所有代理商" 96 | echo " claude-proxy switch # 切换代理商" 97 | echo "" 98 | echo -e "${YELLOW}示例:${NC}" 99 | echo " claude-proxy add official sk-ant-xxx https://api.anthropic.com" 100 | echo " claude-proxy add proxy1 sk-ant-yyy https://proxy1.example.com" 101 | echo " claude-proxy switch proxy1" 102 | return 0 103 | else 104 | echo -e "${RED}安装验证失败${NC}" 105 | return 1 106 | fi 107 | } 108 | 109 | # 主安装流程 110 | main() { 111 | echo -e "${YELLOW}开始安装...${NC}" 112 | echo "" 113 | 114 | # 检查并安装依赖 115 | check_jq 116 | echo "" 117 | 118 | # 安装脚本 119 | install_script 120 | echo "" 121 | 122 | # 验证安装 123 | if verify_installation; then 124 | echo "" 125 | echo -e "${GREEN}🎉 Claude Code 代理商管理器安装完成!${NC}" 126 | echo "" 127 | echo -e "${BLUE}接下来您可以:${NC}" 128 | echo "1. 添加您的第一个代理商配置" 129 | echo "2. 使用 'claude-proxy help' 查看完整帮助" 130 | echo "" 131 | echo -e "${YELLOW}注意: 切换代理商后需要重启终端或运行 'source ~/.zshrc' 使配置生效${NC}" 132 | else 133 | echo -e "${RED}安装失败,请检查错误信息${NC}" 134 | exit 1 135 | fi 136 | } 137 | 138 | # 运行安装 139 | main "$@" -------------------------------------------------------------------------------- /README_EN.md: -------------------------------------------------------------------------------- 1 | # Claude Code Proxy Manager 2 | 3 | > 🤖 **This project is fully developed by Claude Code** 4 | 5 | [中文文档](README.md) | **English** 6 | 7 | A command-line tool for managing multiple Claude Code proxy configurations with quick switching capabilities. 8 | 9 | ## Features 10 | 11 | - 🚀 **Quick Switching**: One-click switching between different Claude Code proxies 12 | - 📝 **Configuration Management**: Add, remove, update proxy configurations 13 | - 🔧 **Auto Configuration**: Automatically updates shell config files (`.bash_profile`, `.bashrc`, `.zshrc`) 14 | - 💾 **Persistent Storage**: Secure local storage of configuration data 15 | - 🎨 **User-Friendly Interface**: Colorful output with clear status indicators 16 | - 🛡️ **Secure & Reliable**: Local storage only, no sensitive data uploaded 17 | 18 | ## System Requirements 19 | 20 | - macOS system 21 | - Homebrew (will be installed automatically) 22 | - jq (will be installed automatically) 23 | 24 | ## One-Click Installation 25 | 26 | ```bash 27 | curl -fsSL https://raw.githubusercontent.com/greezi/claude-proxy-manager/main/install.sh | bash 28 | ``` 29 | 30 | Or manual installation: 31 | 32 | ```bash 33 | # Clone repository 34 | git clone https://github.com/greezi/claude-proxy-manager.git 35 | cd claude-proxy-manager 36 | 37 | # Run installation script 38 | ./install.sh 39 | ``` 40 | 41 | ## Usage 42 | 43 | ### Basic Commands 44 | 45 | ```bash 46 | # Show help 47 | claude-proxy help 48 | 49 | # Add proxy provider 50 | claude-proxy add 51 | 52 | # List all providers 53 | claude-proxy list 54 | 55 | # Switch to provider 56 | claude-proxy switch 57 | 58 | # Show current provider 59 | claude-proxy current 60 | 61 | # Update provider info 62 | claude-proxy update 63 | 64 | # Remove provider 65 | claude-proxy remove 66 | ``` 67 | 68 | ### Usage Examples 69 | 70 | ```bash 71 | # Add official API 72 | claude-proxy add official sk-ant-api03-xxx https://api.anthropic.com 73 | 74 | # Add proxy provider 1 75 | claude-proxy add proxy1 sk-ant-api03-yyy https://api.proxy1.com 76 | 77 | # Add proxy provider 2 78 | claude-proxy add proxy2 sk-ant-api03-zzz https://api.proxy2.com 79 | 80 | # List all configured providers 81 | claude-proxy list 82 | 83 | # Switch to proxy1 84 | claude-proxy switch proxy1 85 | 86 | # Show current provider 87 | claude-proxy current 88 | 89 | # Update provider info 90 | claude-proxy update proxy1 sk-ant-api03-new https://new.proxy1.com 91 | 92 | # Remove unwanted provider 93 | claude-proxy remove proxy2 94 | ``` 95 | 96 | ## Configuration Files 97 | 98 | The tool automatically manages environment variables in these configuration files: 99 | 100 | - `~/.bash_profile` 101 | - `~/.bashrc` 102 | - `~/.zshrc` 103 | 104 | Managed environment variables: 105 | - `ANTHROPIC_AUTH_TOKEN`: Claude API authentication token 106 | - `ANTHROPIC_BASE_URL`: Claude API base URL 107 | 108 | ## Configuration Storage 109 | 110 | All configuration data is stored in the `~/.claude-proxy-manager/` directory: 111 | 112 | - `providers.json`: Provider configuration information 113 | - `current`: Current active provider name 114 | 115 | ## Important Notes 116 | 117 | 1. **Restart Terminal**: After switching providers, restart your terminal or run `source ~/.zshrc` to apply changes 118 | 2. **Token Security**: All token information is stored locally only, please keep it secure 119 | 3. **Backup Configuration**: Recommend regular backup of `~/.claude-proxy-manager/` directory 120 | 121 | ## Troubleshooting 122 | 123 | ### Common Issues 124 | 125 | **Q: Environment variables not taking effect after switching providers?** 126 | A: Please restart your terminal or run `source ~/.zshrc` 127 | 128 | **Q: jq command not found error?** 129 | A: Run `brew install jq` to install jq 130 | 131 | **Q: Cannot write to configuration files?** 132 | A: Check permissions of shell configuration files, ensure current user has write access 133 | 134 | **Q: Provider list is empty?** 135 | A: Use `claude-proxy add` command to add your first provider configuration 136 | 137 | ### Manual Uninstall 138 | 139 | ```bash 140 | # Remove command 141 | sudo rm -f /usr/local/bin/claude-proxy 142 | 143 | # Remove configuration directory 144 | rm -rf ~/.claude-proxy-manager 145 | 146 | # Manually clean environment variables (optional) 147 | # Edit ~/.zshrc etc., remove ANTHROPIC_AUTH_TOKEN and ANTHROPIC_BASE_URL related lines 148 | ``` 149 | 150 | ## Development 151 | 152 | ### Local Development 153 | 154 | ```bash 155 | # Clone repository 156 | git clone https://github.com/greezi/claude-proxy-manager.git 157 | cd claude-proxy-manager 158 | 159 | # Run script directly 160 | ./claude-proxy-manager.sh help 161 | 162 | # Local installation test 163 | ./install.sh 164 | ``` 165 | 166 | ### Contributing 167 | 168 | Contributions are welcome! Please submit Issues and Pull Requests. 169 | 170 | 1. Fork this repository 171 | 2. Create feature branch (`git checkout -b feature/AmazingFeature`) 172 | 3. Commit changes (`git commit -m 'Add some AmazingFeature'`) 173 | 4. Push to branch (`git push origin feature/AmazingFeature`) 174 | 5. Open Pull Request 175 | 176 | ## License 177 | 178 | This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. 179 | 180 | ## Changelog 181 | 182 | ### v1.0.0 (2025-07-17) 183 | - Initial release 184 | - Add/remove/update/list proxy providers 185 | - Quick switching functionality 186 | - Complete test suite 187 | - Comprehensive documentation 188 | 189 | ## Support 190 | 191 | If you find this tool useful, please give it a ⭐️ Star! 192 | 193 | For issues or suggestions, please submit an [Issue](https://github.com/greezi/claude-proxy-manager/issues). -------------------------------------------------------------------------------- /claude-proxy-manager.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Claude Code 代理商管理器 4 | # 版本: 1.0.0 5 | # 作者: Claude Proxy Manager 6 | 7 | # 配置文件路径 8 | CONFIG_DIR="$HOME/.claude-proxy-manager" 9 | PROVIDERS_FILE="$CONFIG_DIR/providers.json" 10 | CURRENT_FILE="$CONFIG_DIR/current" 11 | 12 | # Shell 配置文件 13 | SHELL_CONFIGS=("$HOME/.bash_profile" "$HOME/.bashrc" "$HOME/.zshrc") 14 | 15 | # 颜色定义 16 | RED='\033[0;31m' 17 | GREEN='\033[0;32m' 18 | YELLOW='\033[1;33m' 19 | BLUE='\033[0;34m' 20 | NC='\033[0m' # No Color 21 | 22 | # 初始化配置目录 23 | init_config() { 24 | if [ ! -d "$CONFIG_DIR" ]; then 25 | mkdir -p "$CONFIG_DIR" 26 | echo "[]" > "$PROVIDERS_FILE" 27 | echo -e "${GREEN}配置目录已创建: $CONFIG_DIR${NC}" 28 | fi 29 | } 30 | 31 | # 显示帮助信息 32 | show_help() { 33 | echo -e "${BLUE}Claude Code 代理商管理器${NC}" 34 | echo "" 35 | echo "用法: claude-proxy [命令] [参数]" 36 | echo "" 37 | echo "命令:" 38 | echo " add 添加新的代理商" 39 | echo " remove 删除代理商" 40 | echo " list 列出所有代理商" 41 | echo " switch 切换到指定代理商" 42 | echo " current 显示当前使用的代理商" 43 | echo " update 更新代理商信息" 44 | echo " help 显示此帮助信息" 45 | echo "" 46 | echo "示例:" 47 | echo " claude-proxy add official sk-ant-xxx https://api.anthropic.com" 48 | echo " claude-proxy add proxy1 sk-ant-yyy https://proxy1.example.com" 49 | echo " claude-proxy switch proxy1" 50 | echo " claude-proxy list" 51 | } 52 | 53 | # 检查 jq 是否安装 54 | check_jq() { 55 | if ! command -v jq &> /dev/null; then 56 | echo -e "${RED}错误: 需要安装 jq 来处理 JSON 数据${NC}" 57 | echo "请运行: brew install jq" 58 | exit 1 59 | fi 60 | } 61 | 62 | # 添加代理商 63 | add_provider() { 64 | local name="$1" 65 | local token="$2" 66 | local base_url="$3" 67 | 68 | if [ -z "$name" ] || [ -z "$token" ] || [ -z "$base_url" ]; then 69 | echo -e "${RED}错误: 请提供完整的参数 (名称 令牌 基础URL)${NC}" 70 | return 1 71 | fi 72 | 73 | # 检查是否已存在 74 | if jq -e ".[] | select(.name == \"$name\")" "$PROVIDERS_FILE" > /dev/null 2>&1; then 75 | echo -e "${YELLOW}警告: 代理商 '$name' 已存在,使用 update 命令来更新${NC}" 76 | return 1 77 | fi 78 | 79 | # 添加新代理商 80 | local new_provider=$(jq -n --arg name "$name" --arg token "$token" --arg url "$base_url" \ 81 | '{name: $name, token: $token, base_url: $url}') 82 | 83 | jq ". += [$new_provider]" "$PROVIDERS_FILE" > "${PROVIDERS_FILE}.tmp" && \ 84 | mv "${PROVIDERS_FILE}.tmp" "$PROVIDERS_FILE" 85 | 86 | echo -e "${GREEN}代理商 '$name' 已添加${NC}" 87 | } 88 | 89 | # 删除代理商 90 | remove_provider() { 91 | local name="$1" 92 | 93 | if [ -z "$name" ]; then 94 | echo -e "${RED}错误: 请提供代理商名称${NC}" 95 | return 1 96 | fi 97 | 98 | # 检查是否存在 99 | if ! jq -e ".[] | select(.name == \"$name\")" "$PROVIDERS_FILE" > /dev/null 2>&1; then 100 | echo -e "${RED}错误: 代理商 '$name' 不存在${NC}" 101 | return 1 102 | fi 103 | 104 | # 删除代理商 105 | jq "map(select(.name != \"$name\"))" "$PROVIDERS_FILE" > "${PROVIDERS_FILE}.tmp" && \ 106 | mv "${PROVIDERS_FILE}.tmp" "$PROVIDERS_FILE" 107 | 108 | # 如果删除的是当前使用的代理商,清除当前设置 109 | if [ -f "$CURRENT_FILE" ] && [ "$(cat "$CURRENT_FILE")" = "$name" ]; then 110 | rm -f "$CURRENT_FILE" 111 | clear_env_vars 112 | echo -e "${YELLOW}已清除当前代理商设置${NC}" 113 | fi 114 | 115 | echo -e "${GREEN}代理商 '$name' 已删除${NC}" 116 | } 117 | 118 | # 列出所有代理商 119 | list_providers() { 120 | local current_provider="" 121 | if [ -f "$CURRENT_FILE" ]; then 122 | current_provider=$(cat "$CURRENT_FILE") 123 | fi 124 | 125 | echo -e "${BLUE}已配置的代理商:${NC}" 126 | echo "" 127 | 128 | if [ "$(jq length "$PROVIDERS_FILE")" -eq 0 ]; then 129 | echo -e "${YELLOW}暂无配置的代理商${NC}" 130 | return 131 | fi 132 | 133 | jq -r '.[] | "\(.name)|\(.base_url)|\(.token[0:20])..."' "$PROVIDERS_FILE" | \ 134 | while IFS='|' read -r name url token; do 135 | if [ "$name" = "$current_provider" ]; then 136 | echo -e "${GREEN}* $name${NC} - $url - $token" 137 | else 138 | echo -e " $name - $url - $token" 139 | fi 140 | done 141 | } 142 | 143 | # 更新代理商 144 | update_provider() { 145 | local name="$1" 146 | local token="$2" 147 | local base_url="$3" 148 | 149 | if [ -z "$name" ] || [ -z "$token" ] || [ -z "$base_url" ]; then 150 | echo -e "${RED}错误: 请提供完整的参数 (名称 令牌 基础URL)${NC}" 151 | return 1 152 | fi 153 | 154 | # 检查是否存在 155 | if ! jq -e ".[] | select(.name == \"$name\")" "$PROVIDERS_FILE" > /dev/null 2>&1; then 156 | echo -e "${RED}错误: 代理商 '$name' 不存在${NC}" 157 | return 1 158 | fi 159 | 160 | # 更新代理商 161 | jq "map(if .name == \"$name\" then .token = \"$token\" | .base_url = \"$base_url\" else . end)" \ 162 | "$PROVIDERS_FILE" > "${PROVIDERS_FILE}.tmp" && \ 163 | mv "${PROVIDERS_FILE}.tmp" "$PROVIDERS_FILE" 164 | 165 | echo -e "${GREEN}代理商 '$name' 已更新${NC}" 166 | 167 | # 如果更新的是当前使用的代理商,重新应用设置 168 | if [ -f "$CURRENT_FILE" ] && [ "$(cat "$CURRENT_FILE")" = "$name" ]; then 169 | switch_provider "$name" 170 | fi 171 | } 172 | 173 | # 切换代理商 174 | switch_provider() { 175 | local name="$1" 176 | 177 | if [ -z "$name" ]; then 178 | echo -e "${RED}错误: 请提供代理商名称${NC}" 179 | return 1 180 | fi 181 | 182 | # 获取代理商信息 183 | local provider_info=$(jq -r ".[] | select(.name == \"$name\")" "$PROVIDERS_FILE") 184 | 185 | if [ -z "$provider_info" ]; then 186 | echo -e "${RED}错误: 代理商 '$name' 不存在${NC}" 187 | return 1 188 | fi 189 | 190 | local token=$(echo "$provider_info" | jq -r '.token') 191 | local base_url=$(echo "$provider_info" | jq -r '.base_url') 192 | 193 | # 更新环境变量 194 | update_env_vars "$token" "$base_url" 195 | 196 | # 记录当前代理商 197 | echo "$name" > "$CURRENT_FILE" 198 | 199 | echo -e "${GREEN}已切换到代理商: $name${NC}" 200 | echo -e "${BLUE}BASE_URL: $base_url${NC}" 201 | echo -e "${YELLOW}请重新启动终端或运行 'source ~/.zshrc' 使配置生效${NC}" 202 | } 203 | 204 | # 显示当前代理商 205 | show_current() { 206 | if [ ! -f "$CURRENT_FILE" ]; then 207 | echo -e "${YELLOW}当前未设置任何代理商${NC}" 208 | return 209 | fi 210 | 211 | local current_name=$(cat "$CURRENT_FILE") 212 | local provider_info=$(jq -r ".[] | select(.name == \"$current_name\")" "$PROVIDERS_FILE") 213 | 214 | if [ -z "$provider_info" ]; then 215 | echo -e "${RED}错误: 当前代理商配置已损坏${NC}" 216 | rm -f "$CURRENT_FILE" 217 | return 1 218 | fi 219 | 220 | local base_url=$(echo "$provider_info" | jq -r '.base_url') 221 | 222 | echo -e "${GREEN}当前代理商: $current_name${NC}" 223 | echo -e "${BLUE}BASE_URL: $base_url${NC}" 224 | } 225 | 226 | # 更新环境变量到配置文件 227 | update_env_vars() { 228 | local token="$1" 229 | local base_url="$2" 230 | 231 | for config_file in "${SHELL_CONFIGS[@]}"; do 232 | # 创建文件如果不存在 233 | if [ ! -f "$config_file" ]; then 234 | touch "$config_file" 235 | fi 236 | 237 | # 移除旧的配置 238 | sed -i.bak '/^export ANTHROPIC_AUTH_TOKEN=/d' "$config_file" 2>/dev/null || true 239 | sed -i.bak '/^export ANTHROPIC_BASE_URL=/d' "$config_file" 2>/dev/null || true 240 | 241 | # 添加新的配置 242 | echo "export ANTHROPIC_AUTH_TOKEN=\"$token\"" >> "$config_file" 243 | echo "export ANTHROPIC_BASE_URL=\"$base_url\"" >> "$config_file" 244 | 245 | # 删除备份文件 246 | rm -f "${config_file}.bak" 247 | done 248 | } 249 | 250 | # 清除环境变量 251 | clear_env_vars() { 252 | for config_file in "${SHELL_CONFIGS[@]}"; do 253 | if [ -f "$config_file" ]; then 254 | sed -i.bak '/^export ANTHROPIC_AUTH_TOKEN=/d' "$config_file" 2>/dev/null || true 255 | sed -i.bak '/^export ANTHROPIC_BASE_URL=/d' "$config_file" 2>/dev/null || true 256 | rm -f "${config_file}.bak" 257 | fi 258 | done 259 | } 260 | 261 | # 主函数 262 | main() { 263 | init_config 264 | check_jq 265 | 266 | case "$1" in 267 | "add") 268 | add_provider "$2" "$3" "$4" 269 | ;; 270 | "remove") 271 | remove_provider "$2" 272 | ;; 273 | "list") 274 | list_providers 275 | ;; 276 | "switch") 277 | switch_provider "$2" 278 | ;; 279 | "current") 280 | show_current 281 | ;; 282 | "update") 283 | update_provider "$2" "$3" "$4" 284 | ;; 285 | "help"|"--help"|"-h"|"") 286 | show_help 287 | ;; 288 | *) 289 | echo -e "${RED}错误: 未知命令 '$1'${NC}" 290 | echo "使用 'claude-proxy help' 查看帮助信息" 291 | exit 1 292 | ;; 293 | esac 294 | } 295 | 296 | # 运行主函数 297 | main "$@" --------------------------------------------------------------------------------