跳转到正文
JayHai的小站
返回

开了VPN国内网站变卡,关了又上不了GitHub?5种方案帮你一劳永逸

VPN 按需代理的五种解决方案


开了VPN国内网站变卡,关了又上不了GitHub?5种方案帮你一劳永逸

作为开发者,你一定经历过这种抓狂时刻:开着VPN写代码,DeepSeek问个问题半天打不开;关掉VPN想push代码到GitHub,又死活连不上。每天在开关VPN之间反复横跳,烦不胜烦。

其实,这个问题的本质很简单——我们需要的不是全局代理,而是按需代理:访问GitHub、Google时自动走代理,打开淘宝、百度时直连,全程无感切换。

下面我整理了5种实测有效的方案,从浏览器到终端全覆盖,总有一款适合你。

还没有VPN? 建议试试我用了五年的这款: https://w1.soxo.top/auth/register?code=LahV

二、解决方案概览

方案适用场景复杂度推荐度
SwitchyOmega浏览器访问⭐⭐⭐⭐⭐
ProxyChains终端命令⭐⭐⭐⭐
Git单独配置Git操作⭐⭐⭐⭐⭐
自动化脚本全场景⭐⭐⭐⭐
Shell函数快速切换⭐⭐⭐⭐

三、方案一:SwitchyOmega(浏览器智能代理)

简介

SwitchyOmega 是最成熟的浏览器智能代理插件,支持自动切换规则。

安装

配置步骤

1. 创建代理情景模式

点击插件图标 → 选项 新建情景模式 → 选择”代理服务器” 填写VPN代理信息:

2. 配置自动切换模式

新建情景模式 → 选择”自动切换模式”

规则列表设置:

切换规则设置:

效果

四、方案二:ProxyChains(终端智能代理)

注:不要用,下面的proxy_on/proxy_off好用

简介

ProxyChains 可以让指定命令走代理,不影响其他程序。

安装

# macOS
brew install proxychains-ng

# Ubuntu/Debian
sudo apt install proxychains4

# Arch Linux
sudo pacman -S proxychains-ng

配置

编辑配置文件:

# macOS
vim /usr/local/etc/proxychains.conf

# Linux
sudo vim /etc/proxychains4.conf

在文件末尾添加代理:

[ProxyList]
socks5 127.0.0.1 7890

使用方式

# 单个命令走代理
proxychains4 git clone https://github.com/user/repo.git

# curl 走代理
proxychains4 curl https://github.com

# 整个shell会话走代理
proxychains4 bash

五、方案三:Git 单独配置代理

简介

仅让 GitHub 相关的 Git 操作走代理,不影响其他仓库。

配置命令

# 设置仅 github.com 走代理
git config --global http.https://github.com.proxy http://127.0.0.1:7890
git config --global https.https://github.com.proxy http://127.0.0.1:7890

# 验证配置
git config --global --get http.https://github.com.proxy

# 取消代理
git config --global --unset http.https://github.com.proxy
git config --global --unset https.https://github.com.proxy

效果

六、方案四:自动化脚本

智能代理开关脚本

创建文件 ~/bin/smart-proxy.sh

#!/bin/bash

PROXY_HOST="127.0.0.1"
PROXY_PORT="7890"  # 替换为你的VPN端口

# 检测 GitHub 是否可访问
check_github() {
    if curl -s --connect-timeout 3 https://github.com > /dev/null 2>&1; then
        return 0
    else
        return 1
    fi
}

# 设置代理
set_proxy() {
    export http_proxy="http://${PROXY_HOST}:${PROXY_PORT}"
    export https_proxy="http://${PROXY_HOST}:${PROXY_PORT}"
    export all_proxy="socks5://${PROXY_HOST}:${PROXY_PORT}"
    echo "✅ 代理已开启: ${PROXY_HOST}:${PROXY_PORT}"
}

# 取消代理
unset_proxy() {
    unset http_proxy https_proxy all_proxy
    echo "❌ 代理已关闭"
}

# 主逻辑
if check_github; then
    echo "🌐 GitHub 可直连,无需代理"
    unset_proxy
else
    echo "🔒 GitHub 不可访问,开启代理..."
    set_proxy
fi

使用方式

# 添加到 ~/.zshrc 或 ~/.bashrc
source ~/bin/smart-proxy.sh

# 或手动执行
~/bin/smart-proxy.sh

七、方案五:Shell 函数(快速切换)

配置

添加到 ~/.zshrc~/.bashrc

# 代理快捷命令
proxy_on() {
    export http_proxy="http://127.0.0.1:7890"
    export https_proxy="http://127.0.0.1:7890"
    export all_proxy="socks5://127.0.0.1:7890"
    echo "✅ 代理已开启"
}

proxy_off() {
    unset http_proxy https_proxy all_proxy
    echo "❌ 代理已关闭"
}

# 测试 GitHub 连接
test_github() {
    if curl -s --connect-timeout 3 https://github.com > /dev/null 2>&1; then
        echo "✅ GitHub 可访问"
    else
        echo "❌ GitHub 不可访问,请开启代理"
    fi
}

# Git 代理快捷命令
git_proxy_on() {
    git config --global http.https://github.com.proxy http://127.0.0.1:7890
    git config --global https.https://github.com.proxy http://127.0.0.1:7890
    echo "✅ Git GitHub 代理已开启"
}

git_proxy_off() {
    git config --global --unset http.https://github.com.proxy
    git config --global --unset https.https://github.com.proxy
    echo "❌ Git GitHub 代理已关闭"
}

使用方式

proxy_on       # 开启代理
proxy_off      # 关闭代理
test_github    # 测试连接
git_proxy_on   # Git 专用开启
git_proxy_off  # Git 专用关闭

八、推荐组合方案

使用场景推荐方案
浏览器访问 GitHubSwitchyOmega 自动切换
终端 Git 操作Git 单独配置代理
终端其他命令ProxyChains 或 Shell 函数
一键智能切换自动化脚本

九、快速配置清单

# 1. 浏览器:安装 SwitchyOmega,配置自动切换规则

# 2. Git:仅 GitHub 走代理
git config --global http.https://github.com.proxy http://127.0.0.1:7890
git config --global https.https://github.com.proxy http://127.0.0.1:7890

# 3. 终端:添加 Shell 函数到 ~/.zshrc

# 4. 测试
curl -I https://github.com
git clone https://github.com/user/repo.git

十、注意事项

  1. 代理端口:根据你的VPN软件确认代理端口(常见:7890、1080、10808)
  2. 代理协议:SOCKS5 通常比 HTTP 更快更稳定
  3. VPN设置:确保VPN开启了”允许局域网连接”(Allow LAN)
  4. 规则更新:SwitchyOmega 的规则列表需要定期更新

分享到:

上一篇
Claude'底裤'大泄露?这波神级操作,懂王看了都直呼内行!
下一篇
AIGC 视频平台调研报告:AI 漫剧方向