English | 简体中文
A command-line tool for macOS to easily switch between different VPN applications. It automatically manages VPN app lifecycle and updates proxy configurations (SSH config and .zshrc).
Switching between different VPN applications has always been a pain point in my daily workflow. Different VPN protocols use different ports, which means every time I switch from one VPN to another, I have to manually update multiple configuration files - SSH config for GitHub proxy, HTTP/HTTPS proxy settings in .zshrc, and more. Opening vim to edit these files every single time became frustrating and time-consuming.
This tool was born from that frustration. What makes it special? It was entirely developed using Claude 4.5 Sonnet - not a single line of code was written manually. The AI handled everything from architecture design, test-driven development, debugging, to documentation. It's amazing how comfortable the AI era has become for developers!
- VPN Management: Add, remove, and list VPN configurations
- Automatic Switching: One command to switch between VPNs
- Proxy Auto-Configuration: Automatically updates SSH config and .zshrc proxy settings
- Backup & Restore: Creates backups before modifying configurations
- Safe Operations: Validates all operations and provides detailed error messages
# Clone the repository
git clone https://github.com/NanmiCoder/vpn_switch_tools
cd vpn_switch_tools
# Build the binary
go build -o vpn-switch
# Move to a directory in your PATH
sudo mv vpn-switch /usr/local/bin/
# Verify installation
vpn-switch --help# Add v2rayN
vpn-switch add --name v2rayN --app /Applications/v2rayN.app --port 10808
# Add ClashX
vpn-switch add --name clashx --app "/Applications/ClashX Meta.app" --port 7897vpn-switch listOutput:
Configured VPNs:
================
v2rayN
App Path: /Applications/v2rayN.app
Port: 10808
● clashx
App Path: /Applications/ClashX Meta.app
Port: 7897
Status: Active
vpn-switch switch clashxThis will:
- Stop the currently active VPN (if any)
- Update SSH config proxy port
- Update .zshrc proxy port
- Generate environment export script
- Start the target VPN application
- Mark the target VPN as active
Option 1 - Quick Apply (Current Shell Only):
source ~/.vpn-switch/env.shOption 2 - Auto-Apply (Recommended):
Add this to the end of your ~/.zshrc:
# Auto-load VPN proxy settings
[ -f ~/.vpn-switch/env.sh ] && source ~/.vpn-switch/env.shNow all new terminals will automatically use the correct proxy settings!
Add a new VPN configuration.
vpn-switch add --name <name> --app <app-path> --port <port>
vpn-switch add -n <name> -a <app-path> -p <port>Options:
--name, -n: VPN name (required)--app, -a: Application path (required)--port, -p: Proxy port number (required)
Example:
vpn-switch add --name v2rayN --app /Applications/v2rayN.app --port 10808Remove a VPN configuration.
vpn-switch remove <vpn-name>
vpn-switch rm <vpn-name>Example:
vpn-switch remove v2rayNList all configured VPNs.
vpn-switch list
vpn-switch lsSwitch to a different VPN.
vpn-switch switch <vpn-name>
vpn-switch s <vpn-name>Example:
vpn-switch switch clashxVPN Switch stores its configuration in ~/.vpn-switch/config.json.
{
"vpns": [
{
"name": "v2rayN",
"app_path": "/Applications/v2rayN.app",
"port": 10808,
"is_active": false
},
{
"name": "clashx",
"app_path": "/Applications/ClashX Meta.app",
"port": 7897,
"is_active": true
}
]
}VPN Switch updates the ProxyCommand lines in your SSH config to use the correct port:
Before:
Host github.com
ProxyCommand nc -v -x 127.0.0.1:10808 %h %p
After switching to ClashX (port 7897):
Host github.com
ProxyCommand nc -v -x 127.0.0.1:7897 %h %p
Updates or adds proxy environment variables:
export http_proxy=http://127.0.0.1:7897
export https_proxy=$http_proxyVPN Switch automatically creates backups before modifying configuration files. Only the 5 most recent backups are kept.
If needed, manually restore from backup:
# Find latest backup
ls -lt ~/.ssh/config.backup.* | head -1
# Restore
cp ~/.ssh/config.backup.<timestamp> ~/.ssh/config- macOS (uses AppleScript for app control)
- Go 1.21.0 or later (for building from source)
- VPN applications installed in
/Applications/
Any macOS application (.app) can be managed, including but not limited to:
- v2rayN
- ClashX / ClashX Meta
- Shadowsocks
- Surge
- Quantumult X
Make sure:
- The application path is correct
- The application is properly installed
- You have permission to run the application
Check that:
~/.ssh/configexists and is readable- The ProxyCommand format matches:
nc -v -x 127.0.0.1:<port> %h %p
You need to apply the proxy settings to your current shell:
source ~/.vpn-switch/env.shOr add auto-reload to ~/.zshrc (see Quick Start section 4).
Contributions are welcome! Please feel free to submit a Pull Request.
MIT License
NanmiCoder