将以下内容添加到您的 ~/.zshrc
文件中,以创建一个名为 proxy_switch
的函数:
function proxy_switch() { function enable_proxy() { export http_proxy="http://127.0.0.1:1087" export https_proxy="http://127.0.0.1:1087" export ALL_PROXY="socks5://127.0.0.1:1080" echo "Proxy enabled." } function disable_proxy() { unset http_proxy unset https_proxy unset ALL_PROXY echo "Proxy disabled." } function test_proxy() { if curl -s --head --fail "https://www.google.com" > /dev/null; then echo "Proxy is working." else echo "Proxy is not working." fi } function show_proxy() { echo "http_proxy: $http_proxy" echo "https_proxy: $https_proxy" echo "ALL_PROXY: $ALL_PROXY" } if [ "$1" == "on" ]; then enable_proxy elif [ "$1" == "off" ]; then disable_proxy else echo "Usage: proxy_switch [on|off]" return fi show_proxy test_proxy }
保存更改并重新启动 Zsh 会话,以应用更新。
现在,您可以在 Zsh 中使用 proxy_switch on
命令启用代理,使用 proxy_switch off
命令禁用代理。脚本将输出代理的启用/禁用状态,并通过 curl
请求 Google 来测试代理是否成功。