NanmiCoder/vpn_switch_tools

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).

★ 2Forks 1GoGitHub ↗Compare

README

VPN Switch

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).

Background

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!

Features

  • 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

Installation

From Source

# 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

Quick Start

1. Add VPN Configurations

# 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 7897

2. List Configured VPNs

vpn-switch list

Output:

Configured VPNs:
================

  v2rayN
  App Path: /Applications/v2rayN.app
  Port: 10808

● clashx
  App Path: /Applications/ClashX Meta.app
  Port: 7897
  Status: Active

3. Switch Between VPNs

vpn-switch switch clashx

This will:

  1. Stop the currently active VPN (if any)
  2. Update SSH config proxy port
  3. Update .zshrc proxy port
  4. Generate environment export script
  5. Start the target VPN application
  6. Mark the target VPN as active

4. Apply Proxy Settings

Option 1 - Quick Apply (Current Shell Only):

source ~/.vpn-switch/env.sh

Option 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.sh

Now all new terminals will automatically use the correct proxy settings!

Commands

add

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 10808

remove

Remove a VPN configuration.

vpn-switch remove <vpn-name>
vpn-switch rm <vpn-name>

Example:

vpn-switch remove v2rayN

list

List all configured VPNs.

vpn-switch list
vpn-switch ls

switch

Switch to a different VPN.

vpn-switch switch <vpn-name>
vpn-switch s <vpn-name>

Example:

vpn-switch switch clashx

Configuration

VPN Switch stores its configuration in ~/.vpn-switch/config.json.

Configuration File Structure

{
  "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
    }
  ]
}

What It Modifies

SSH Config (~/.ssh/config)

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

Zsh Configuration (~/.zshrc)

Updates or adds proxy environment variables:

export http_proxy=http://127.0.0.1:7897
export https_proxy=$http_proxy

Backup & Restore

VPN 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

Requirements

  • macOS (uses AppleScript for app control)
  • Go 1.21.0 or later (for building from source)
  • VPN applications installed in /Applications/

Supported VPN Applications

Any macOS application (.app) can be managed, including but not limited to:

  • v2rayN
  • ClashX / ClashX Meta
  • Shadowsocks
  • Surge
  • Quantumult X

Troubleshooting

VPN Application Won't Start

Make sure:

  1. The application path is correct
  2. The application is properly installed
  3. You have permission to run the application

SSH Config Not Updating

Check that:

  1. ~/.ssh/config exists and is readable
  2. The ProxyCommand format matches: nc -v -x 127.0.0.1:<port> %h %p

Proxy Not Working After Switch

You need to apply the proxy settings to your current shell:

source ~/.vpn-switch/env.sh

Or add auto-reload to ~/.zshrc (see Quick Start section 4).

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT License

Author

NanmiCoder

Contributors

NanmiCoder

Issues