mirror-switcher(镜像切换工具 / Mirror Switcher) 为什么需要它 / Why开发者在不同网络环境下经常需要在「官方源」与「国内镜像源」之间反复切换nrm 解决了 npm 的痛点但 pip、brew、docker、maven、go、cargo、yum、apt…… 每一个都要查一遍文档、改一遍配置文件。本套件把这些操作统一起来命令完全一致switch-xxx ls # 列出全部预设源 / list all preset sourcesswitch-xxx use tsinghua # 切到清华源 / switch to Tsinghua mirrorswitch-xxx test # 给所有源测速排序 / latency-test rank all sources二、工具清单共 20 个/ Tool list (20 in total)命令 / Command 用途 / Purpose 对标 / Mirrorsswitch-npm npm / pnpm / yarn 包源 / package registry nrmswitch-node Node.js 多版本管理 / version manager nvm自动识别 fnm / volta / nvmswitch-brew macOS Homebrew 镜像 / Homebrew mirror 需求① ibrewswitch-pip Python pipPyPI跨平台源 / cross-platform pip 需求②switch-conda Anaconda / Miniconda 通道 / conda channels 需求③switch-docker Docker registry 镜像加速器 / registry mirror 需求④switch-git Git 克隆加速GitHub 代理/ clone acceleration 需求⑤switch-yum CentOS / Rocky / Alma YUM 源 / YUM source 需求⑥switch-apt Ubuntu / Debian APT 源 / APT source Linux Debian 系switch-pacman Arch / Manjaro Pacman 源 / pacman mirrorlist Arch 系switch-dnf Fedora DNF 源 / DNF source Fedoraswitch-zypper openSUSE Zypper 源 / Zypper source SUSE 系switch-maven Maven 仓库镜像 / Maven repository Java 后端switch-gradle Gradle 依赖源 / Gradle source Gradle 项目switch-go Go GOPROXY 代理 / Go modules proxy Golangswitch-cargo Rust Cargo crates 源 / Cargo crates Rustswitch-composer PHP Composer 镜像 / Composer mirror PHPswitch-nuget .NET NuGet 程序包源 / NuGet feed C# / .NETswitch-pub Flutter / Dart Pub 镜像 / Pub mirror Flutterswitch-helm Kubernetes Helm Chart 仓库 / Helm repo 运维 K8s三、全局通用指令所有 switch-* 工具通用/ Shared commandsls 列出所有预设与自定义源* 标记当前生效源list all preset custom sources (* current)current 查看当前正在使用的源 / show current sourceuse 切换至指定命名源 / switch to a named sourceadd [home] 手动添加自定义私有源 / add a custom sourcedel 删除一条自定义源 / delete a custom sourcetest [name] 批量测速所有源并按延迟排序或测单个latency-test all sources (or a single one)home 在浏览器打开对应镜像源主页 / open a source’s homepagebackup 备份当前原有配置文件 / back up the original configrestore [stamp] 从最新或指定时间戳备份还原配置restore from latest (or given timestamp) backuphelp 查看帮助 / show help-v / --version 查看版本 / show versionswitch-node 命令语义按版本管理映射ls列版本、use 切换、add 安装、del 卸载、test验证每个已装版本可运行。switch-node maps commands to version management: lslist, useswitch, addinstall, deluninstall, testverify.保留的预设名不可 add/del/ Reserved preset names (cannot be added/deleted)official、tsinghua、aliyun、ustc部分系统源另有 163。各工具会按该生态实际可用的镜像补齐详见各工具 ls 输出。四、快速开始 / Quick start方式 A远程一键安装推荐/ Option A: Remote one-line installer (recommended)无需 git clone一行命令拉取并安装所有 20 个工具自动写入 PATH 并提示如何让当前 shell 立即生效Linux / macOSbash / zsh国内推荐Gitee 源速度快/ recommended in China (Gitee)bash (curl -fsSL https://gitee.com/eword/mirror-switcher/raw/main/bootstrap.sh)或管道模式无 TTY/ or: piped mode (no TTY)curl -fsSL https://gitee.com/eword/mirror-switcher/raw/main/bootstrap.sh | bash只安装指定工具逗号分隔/ install only specific toolsbash (curl -fsSL …/bootstrap.sh) --toolsnpm,pip,yum卸载 / uninstallbash (curl -fsSL …/bootstrap.sh) --uninstallWindowsPowerShell国内推荐 / recommended in Chinaiwr -useb https://gitee.com/eword/mirror-switcher/raw/main/bootstrap.ps1 | iex仅安装指定工具 / specific tools only ([scriptblock]::Create((iwr -useb https://gitee.com/eword/mirror-switcher/raw/main/bootstrap.ps1))) --toolsnpm,pip卸载 / uninstall ([scriptblock]::Create((iwr -useb https://gitee.com/eword/mirror-switcher/raw/main/bootstrap.ps1))) --uninstall远程安装策略 / Strategy优先 git clone可增量更新失败则下载 tarball / zip。国内环境自动选择 Gitee 源海外用 GitHub可设置环境变量 MS_SOURCEgitee|github|auto 强制选择。Prefers git clone (incremental), falls back to tarball/zip. Auto-picks Gitee in CN, GitHub abroad; override with MS_SOURCE.安装后让当前 shell 立即生效 / Make it effective in current shellsource ~/.zshrc # zshsource ~/.bashrc # bashsource ~/.config/fish/config.fish # fish或重开一个终端 / or open a new terminal方式 B从源码安装 / Option B: From sourcegit clone https://gitee.com/eword/mirror-switcher.gitcd mirror-switcher一键安装全部 20 个工具 自动 PATH / install all auto PATH./install.sh只装部分工具 / install only some./install.sh --toolsnpm,pip,yum卸载 / uninstall./install.sh --uninstall单个工具安装传统方式/ single tool (classic)cd switch-pip ./install-link.sh方式 C直接用 Python 运行 / Option C: Run with Python directly不想注册全局命令也行 / No global install neededpython3 switch-pip/switch-pip.py lspython3 switch-brew/switch-brew.py use tsinghuapython3 switch-npm/switch-npm.py testpython3 main.py # 交互式菜单 / interactive menupython3 main.py switch-go use goproxy.cn # 直接调度 / dispatch directly典型用法示例 / Examplesbrew 源操作语法和 nrm 一模一样/ brew (same syntax as nrm)switch-brew lsswitch-brew currentswitch-brew use tsinghuaswitch-brew use officialswitch-brew add mybrew https://my.internal/brewswitch-brew testpip 跨平台源 / cross-platform pipswitch-pip lsswitch-pip use aliyunswitch-pip backup # 备份 ~/.config/pip/pip.conf / back up pip.confswitch-pip restore # 还原 / restore多语言 / multi-language ecosystemsswitch-go use goproxy.cnswitch-cargo use rsproxyswitch-maven use aliyunswitch-docker use daocloudLinux 系统源需 root见下方安全说明/ Linux system sources (need root)switch-yum use aliyunswitch-apt use tsinghua五、架构设计 / Architecturemirror-switcher/├── main.py # 聚合入口交互菜单 直接调度├── common/ # 公共库一套契约所有工具共享│ ├── sources.py # Source 数据类│ ├── registry.py # SourceRegistryls/current/use/add/del/test/home/backup/restore│ ├── cli.py # 命令行解析与分发1:1 复刻 nrm│ ├── config.py # 用户自定义源持久化~/.mirror-switcher/│ ├── speedtest.py # 多线程并发测速│ ├── backup.py # 配置文件备份 / 还原│ ├── system.py # 跨平台系统识别、which│ ├── runner.py # 子进程执行、文件读写│ ├── browser.py # 打开主页│ └── logger.py # 彩色输出 双语辅助 bi()└── switch-/ # 每个工具一个目录薄实现预设 apply 逻辑├── switch-.py├── install-link.sh # macOS/Linux 一键软链└── install-link.ps1 # Windows PowerShell 注册设计要点把 nrm 的全部交互逻辑命令解析、测速、备份、自定义源存储、彩色输出下沉到 common/每个 switch-xxx 只需声明 预设源 如何读取/写入该工具的真实配置get_current / apply_source / config_files。这让新增一个工具通常只需要一个 ~80–150 行的文件。六、每个工具的内置镜像速查/ Built-in mirrors (cheat-sheet)镜像地址为编写时的常用值若上游调整可用 add 自行覆盖。所有工具均内置 official一键恢复官方源。URLs are common values at time of writing; override with add if upstream changes. Every tool ships official to restore the upstream registry in one step.switch-npm: official / npmmirror(淘宝) / ustc / huawei / tencentswitch-pip: official(PyPI) / tsinghua / aliyun / ustc / huawei / tencentswitch-conda: official / tsinghua / ustc / aliyun重写 ~/.condarcswitch-docker: official / daocloud / dockerproxy / ustc / 163 / baiduLinux 改 /etc/docker/daemon.jsonmacOS/Windows 改 Docker Desktop settings.jsonswitch-brew: official / tsinghua / ustc / aliyun写 HOMEBREW_* 环境变量并由 shell profile 加载switch-git: official / ghproxy / ghfast / kkgithub / gitclonegit config url..insteadOfswitch-yum: official / aliyun / tsinghua / ustc / 163按 centos/rocky/almalinux 自动选路径switch-apt: official / tsinghua / aliyun / ustc / 163自动区分 ubuntu / debian 家族switch-pacman: official / tsinghua / ustc / aliyun / 163置顶 Server 行switch-dnf: official / tsinghua / ustc / aliyun / 163switch-zypper: official / tsinghua / ustc / aliyun / sjtuswitch-maven: official / aliyun / huawei / tencent重写 ~/.m2/settings.xml 的 switch-gradle: official / aliyun / huawei / tencent写 ~/.gradle/init.gradleswitch-go: official / goproxy.cn / aliyun / goproxy.io / tencentgo env -w GOPROXY/GOSUMDBswitch-cargo: official / rsproxy / tsinghua / ustc / sjtug / aliyun写 ~/.cargo/config.tomlswitch-composer: official / aliyun / tencent / huaweicomposer config -g 或改 ~/.composer/config.jsonswitch-nuget: official / tsinghua / aliyun / huawei写 NuGet.Configswitch-pub: official / flutter-io.cn / tsinghua / ustcPUB_HOSTED_URL FLUTTER_STORAGE_BASE_URLswitch-helm: official / bitnami / aliyun / gitlab管理名为 switch-helm 的 repo七、跨平台与权限说明 / Cross-platform permissions零三方依赖仅使用 Python 3.7 标准库Windows / macOS / Linux 均可运行。Linux 系统源yum / apt / pacman / dnf / zypper与 Docker daemon.json 需要 root。工具会先尝试直接写入遇权限不足时自动写临时文件并通过 sudo cp 应用失败则打印精确命令由你手动执行。备份优先改动系统级配置前强烈建议先 switch-xxx backup。备份存放在 ~/.mirror-switcher/backups//时间戳/可用 switch-xxx restore 还原。macOS Homebrew / Flutter通过在 shell profile 中插入带标记的 source 行加载 mirror.sh多次 use 幂等不会污染 profile。七.5、全局 PATH 自动注入 / Auto PATH injectioninstall.sh / install-link.sh / bootstrap.sh以及 Windows 的 install-link.ps1 / bootstrap.ps1会自动把 ~/.local/binLinux/macOS或 %USERPROFILE%\binWindows幂等写入 rc 文件 / 注册表用 # mirror-switcher PATH / # mirror-switcher PATH 标记块包裹重复运行不会污染 rc 文件在结束时打印 source 命令让当前 shell 立即生效。支持的 rc 文件 / Supported rc filesLinux/macOS.zshrc、.bashrc、.bash_profile、.profile、.kshrc、.tcshrc、.cshrc、.config/fish/config.fish若以 root 运行额外写入 /etc/profile.d/mirror-switcher.sh系统级所有用户生效