#!/bin/bash # WSL 代理管理脚本 # 用于快速开启和关闭代理设置 # 获取 Windows 主机 IP get_windows_ip() { local wsl_ip=$(ip addr show eth1 | grep 'inet ' | awk '{print $2}' | cut -d'/' -f1 2>/dev/null) if [ -z "$wsl_ip" ]; then wsl_ip=$(hostname -I | awk '{print $1}') fi # WSL2 共享 IP 的情况下,按优先级尝试不同地址 local test_ips=("127.0.0.1" "$wsl_ip") for ip in "${test_ips[@]}"; do if timeout 3 bash -c "/dev/null; then echo "$ip" return fi done # 如果都失败了,返回 localhost echo "127.0.0.1" } # Clash 端口配置 HTTP_PORT="7897" SOCKS_PORT="7897" # 代理配置函数 set_proxy() { local windows_ip=$(get_windows_ip) echo "正在设置代理..." echo "Windows IP: $windows_ip" # 设置 HTTP/HTTPS 代理 export http_proxy="http://${windows_ip}:${HTTP_PORT}" export https_proxy="http://${windows_ip}:${HTTP_PORT}" export HTTP_PROXY="http://${windows_ip}:${HTTP_PORT}" export HTTPS_PROXY="http://${windows_ip}:${HTTP_PORT}" # 设置 SOCKS 代理 export all_proxy="socks5://${windows_ip}:${SOCKS_PORT}" export ALL_PROXY="socks5://${windows_ip}:${SOCKS_PORT}" # 设置不走代理的地址 export no_proxy="localhost,127.0.0.1,::1,*.local" export NO_PROXY="localhost,127.0.0.1,::1,*.local" # 写入到当前 shell 配置文件 echo "# Proxy settings - Auto generated" >> ~/.bashrc echo "export http_proxy=\"http://${windows_ip}:${HTTP_PORT}\"" >> ~/.bashrc echo "export https_proxy=\"http://${windows_ip}:${HTTP_PORT}\"" >> ~/.bashrc echo "export HTTP_PROXY=\"http://${windows_ip}:${HTTP_PORT}\"" >> ~/.bashrc echo "export HTTPS_PROXY=\"http://${windows_ip}:${HTTP_PORT}\"" >> ~/.bashrc echo "export all_proxy=\"socks5://${windows_ip}:${SOCKS_PORT}\"" >> ~/.bashrc echo "export ALL_PROXY=\"socks5://${windows_ip}:${SOCKS_PORT}\"" >> ~/.bashrc echo "export no_proxy=\"localhost,127.0.0.1,::1,*.local\"" >> ~/.bashrc echo "export NO_PROXY=\"localhost,127.0.0.1,::1,*.local\"" >> ~/.bashrc echo "✅ 代理已开启" echo "HTTP/HTTPS 代理: http://${windows_ip}:${HTTP_PORT}" echo "SOCKS 代理: socks5://${windows_ip}:${SOCKS_PORT}" } # 取消代理配置函数 unset_proxy() { echo "正在关闭代理..." # 取消当前会话的代理环境变量 unset http_proxy unset https_proxy unset HTTP_PROXY unset HTTPS_PROXY unset all_proxy unset ALL_PROXY unset no_proxy unset NO_PROXY # 从 .bashrc 中移除代理配置 sed -i '/# Proxy settings - Auto generated/,+7d' ~/.bashrc echo "✅ 代理已关闭" } # 检查代理状态 check_proxy() { echo "当前代理状态:" if [ -n "$http_proxy" ]; then echo "✅ HTTP 代理: $http_proxy" else echo "❌ HTTP 代理: 未设置" fi if [ -n "$all_proxy" ]; then echo "✅ SOCKS 代理: $all_proxy" else echo "❌ SOCKS 代理: 未设置" fi echo "" echo "测试连接..." if curl -s --connect-timeout 5 --max-time 10 http://www.google.com > /dev/null 2>&1; then echo "✅ 网络连接正常" else echo "❌ 网络连接失败" fi } # 测试代理连接 test_proxy() { local windows_ip=$(get_windows_ip) echo "测试代理连接..." echo "使用地址: $windows_ip:${HTTP_PORT}" # 测试 HTTP 代理 if curl -s --connect-timeout 5 --proxy "http://${windows_ip}:${HTTP_PORT}" http://www.google.com > /dev/null 2>&1; then echo "✅ HTTP 代理连接正常" else echo "❌ HTTP 代理连接失败" fi # 测试 SOCKS 代理 if curl -s --connect-timeout 5 --socks5 "${windows_ip}:${SOCKS_PORT}" http://www.google.com > /dev/null 2>&1; then echo "✅ SOCKS 代理连接正常" else echo "❌ SOCKS 代理连接失败" fi } # Git 代理设置 set_git_proxy() { local windows_ip=$(get_windows_ip) echo "设置 Git 代理..." git config --global http.proxy "http://${windows_ip}:${HTTP_PORT}" git config --global https.proxy "http://${windows_ip}:${HTTP_PORT}" echo "✅ Git 代理已设置为: http://${windows_ip}:${HTTP_PORT}" } # 取消 Git 代理 unset_git_proxy() { echo "取消 Git 代理..." git config --global --unset http.proxy git config --global --unset https.proxy echo "✅ Git 代理已取消" } # 显示帮助信息 show_help() { echo "WSL 代理管理脚本" echo "" echo "用法: $0 [选项]" echo "" echo "选项:" echo " on 开启代理" echo " off 关闭代理" echo " status 检查代理状态" echo " test 测试代理连接" echo " git-on 开启 Git 代理" echo " git-off 关闭 Git 代理" echo " help 显示此帮助信息" echo "" echo "注意: 请确保 Windows 上的 Clash 正在运行" echo "默认端口: HTTP=${HTTP_PORT}, SOCKS=${SOCKS_PORT}" } # 主菜单 show_menu() { echo "==================================" echo " WSL 代理管理工具" echo "==================================" echo "1. 开启代理" echo "2. 关闭代理" echo "3. 检查代理状态" echo "4. 测试代理连接" echo "5. 开启 Git 代理" echo "6. 关闭 Git 代理" echo "7. 退出" echo "==================================" echo -n "请选择操作 (1-7): " } # 主程序逻辑 main() { case "$1" in "on") set_proxy ;; "off") unset_proxy ;; "status") check_proxy ;; "test") test_proxy ;; "git-on") set_git_proxy ;; "git-off") unset_git_proxy ;; "help") show_help ;; "") # 交互式菜单 while true; do show_menu read choice echo "" case $choice in 1) set_proxy ;; 2) unset_proxy ;; 3) check_proxy ;; 4) test_proxy ;; 5) set_git_proxy ;; 6) unset_git_proxy ;; 7) echo "再见!" break ;; *) echo "❌ 无效选择,请重新输入" ;; esac echo "" echo "按任意键继续..." read -n 1 clear done ;; *) echo "❌ 无效参数: $1" show_help ;; esac } # 运行主程序 main "$@"