配置ubuntu的proxy脚本和config.json,深度学习加速可用
- 共 20 次检阅

{
  "inbounds": [
    {
      "port": 10808,
      "listen": "127.0.0.1",
      "protocol": "socks",
      "settings": {
        "udp": true
      }
    },
    {
      "port": 10809,
      "listen": "127.0.0.1",
      "protocol": "http"
    }
  ],
  "outbounds": [
    {
      "protocol": "vmess",
      "settings": {
        "vnext": [
          {
            "address": "0.0.0.0",
            "port": 52606,
            "users": [
              {
                "id": "2fb18ddd-ce42-48b2-8f1a-f89a8beeca45",
                "alterId": 0
              }
            ]
          }
        ]
      },
      "streamSettings": {
        "network": "tcp",
        "tcpSettings": {
          "header": {
            "type": "http"
          }
        }
      }
    }
  ]
}

 

#!/bin/bash

# 设置变量
V2RAY_SERVICE="v2ray"
PROXY_HOST="127.0.0.1"
CONFIG_FILE="/usr/local/etc/v2ray/config.json"
TEST_URL="https://www.google.com"
TIMEOUT=10  # 设置超时时间(秒)
HTTP_PORT="10809"
SOCKS_PORT="10808"

# 颜色代码
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 函数:检查 V2Ray 是否已安装
check_v2ray_installed() {
    if ! command -v v2ray &> /dev/null; then
        echo -e "${RED}错误:V2Ray 未安装。请先安装 V2Ray。${NC}"
        exit 1
    fi
}

# 函数:检查配置文件
check_config() {
    if [ ! -f "$CONFIG_FILE" ]; then
        echo -e "${RED}错误:V2Ray 配置文件不存在。请检查 $CONFIG_FILE${NC}"
        exit 1
    fi
}

# 函数:启动 v2ray 和设置代理
start_v2ray_and_proxy() {
    check_config
    if sudo systemctl start $V2RAY_SERVICE; then
        echo -e "${GREEN}V2Ray 已启动${NC}"
        set_proxy
    else
        echo -e "${RED}错误:无法启动 V2Ray 服务。${NC}"
        echo "检查 V2Ray 日志以获取更多信息:"
        sudo journalctl -u v2ray --no-pager | tail -n 20
    fi
}

# 函数:设置代理
set_proxy() {
    echo -e "${YELLOW}请选择代理模式:${NC}"
    echo "1. 仅终端代理"
    echo "2. 全局代理"
    read -p "请输入选择 (1-2): " proxy_choice

    case $proxy_choice in
        1)
            export http_proxy="http://$PROXY_HOST:$HTTP_PORT"
            export https_proxy="http://$PROXY_HOST:$HTTP_PORT"
            export all_proxy="socks5://$PROXY_HOST:$SOCKS_PORT"
            echo -e "${GREEN}终端代理已设置${NC}"
            ;;
        2)
            export http_proxy="http://$PROXY_HOST:$HTTP_PORT"
            export https_proxy="http://$PROXY_HOST:$HTTP_PORT"
            export all_proxy="socks5://$PROXY_HOST:$SOCKS_PORT"
            
            # 设置 GNOME 系统代理(如果使用 GNOME 桌面环境)
            if command -v gsettings &> /dev/null; then
                gsettings set org.gnome.system.proxy mode 'manual'
                gsettings set org.gnome.system.proxy.http host "$PROXY_HOST"
                gsettings set org.gnome.system.proxy.http port "$HTTP_PORT"
                gsettings set org.gnome.system.proxy.https host "$PROXY_HOST"
                gsettings set org.gnome.system.proxy.https port "$HTTP_PORT"
                gsettings set org.gnome.system.proxy.socks host "$PROXY_HOST"
                gsettings set org.gnome.system.proxy.socks port "$SOCKS_PORT"
                echo -e "${GREEN}GNOME 系统代理已设置${NC}"
            fi

            # 设置 KDE 系统代理(如果使用 KDE 桌面环境)
            if command -v kwriteconfig5 &> /dev/null; then
                kwriteconfig5 --file kioslaverc --group "Proxy Settings" --key ProxyType 1
                kwriteconfig5 --file kioslaverc --group "Proxy Settings" --key httpProxy "http://$PROXY_HOST:$HTTP_PORT"
                kwriteconfig5 --file kioslaverc --group "Proxy Settings" --key httpsProxy "http://$PROXY_HOST:$HTTP_PORT"
                kwriteconfig5 --file kioslaverc --group "Proxy Settings" --key socksProxy "socks://$PROXY_HOST:$SOCKS_PORT"
                echo -e "${GREEN}KDE 系统代理已设置${NC}"
            fi

            echo -e "${GREEN}全局代理已设置${NC}"
            ;;
        *)
            echo -e "${RED}无效选择,不设置代理。${NC}"
            return
            ;;
    esac
    
    echo -e "HTTP 代理: $PROXY_HOST:$HTTP_PORT"
    echo -e "SOCKS5 代理: $PROXY_HOST:$SOCKS_PORT"
    
    # 将代理设置写入 .bashrc 文件以持久化
    echo "export http_proxy=\"$http_proxy\"" >> ~/.bashrc
    echo "export https_proxy=\"$https_proxy\"" >> ~/.bashrc
    echo "export all_proxy=\"$all_proxy\"" >> ~/.bashrc
    echo -e "${YELLOW}代理设置已添加到 .bashrc 文件。请运行 'source ~/.bashrc' 或重新登录以应用更改。${NC}"
    
    verify_proxy
}

# 函数:停止 v2ray 和取消代理
stop_v2ray_and_proxy() {
    if sudo systemctl stop $V2RAY_SERVICE; then
        unset http_proxy https_proxy all_proxy
        echo -e "${GREEN}V2Ray 已停止,系统代理已取消。${NC}"
        # 从 .bashrc 文件中移除代理设置
        sed -i '/export http_proxy/d' ~/.bashrc
        sed -i '/export https_proxy/d' ~/.bashrc
        sed -i '/export all_proxy/d' ~/.bashrc
        echo -e "${YELLOW}代理设置已从 .bashrc 文件中移除。请运行 'source ~/.bashrc' 或重新登录以应用更改。${NC}"
        
        # 重置 GNOME 系统代理(如果使用 GNOME 桌面环境)
        if command -v gsettings &> /dev/null; then
            gsettings set org.gnome.system.proxy mode 'none'
            echo -e "${GREEN}GNOME 系统代理已重置${NC}"
        fi

        # 重置 KDE 系统代理(如果使用 KDE 桌面环境)
        if command -v kwriteconfig5 &> /dev/null; then
            kwriteconfig5 --file kioslaverc --group "Proxy Settings" --key ProxyType 0
            echo -e "${GREEN}KDE 系统代理已重置${NC}"
        fi
    else
        echo -e "${RED}错误:无法停止 V2Ray 服务。${NC}"
    fi
}

# 函数:检查 v2ray 状态
check_v2ray_status() {
    sudo systemctl status $V2RAY_SERVICE
}

# 函数:显示当前代理设置
show_proxy_settings() {
    echo -e "${YELLOW}当前代理设置:${NC}"
    echo "HTTP_PROXY: $http_proxy"
    echo "HTTPS_PROXY: $https_proxy"
    echo "ALL_PROXY: $all_proxy"
    
    if command -v gsettings &> /dev/null; then
        echo -e "${YELLOW}GNOME 系统代理设置:${NC}"
        echo "模式: $(gsettings get org.gnome.system.proxy mode)"
        echo "HTTP 主机: $(gsettings get org.gnome.system.proxy.http host)"
        echo "HTTP 端口: $(gsettings get org.gnome.system.proxy.http port)"
        echo "HTTPS 主机: $(gsettings get org.gnome.system.proxy.https host)"
        echo "HTTPS 端口: $(gsettings get org.gnome.system.proxy.https port)"
        echo "SOCKS 主机: $(gsettings get org.gnome.system.proxy.socks host)"
        echo "SOCKS 端口: $(gsettings get org.gnome.system.proxy.socks port)"
    fi

    if command -v kreadconfig5 &> /dev/null; then
        echo -e "${YELLOW}KDE 系统代理设置:${NC}"
        echo "代理类型: $(kreadconfig5 --file kioslaverc --group 'Proxy Settings' --key ProxyType)"
        echo "HTTP 代理: $(kreadconfig5 --file kioslaverc --group 'Proxy Settings' --key httpProxy)"
        echo "HTTPS 代理: $(kreadconfig5 --file kioslaverc --group 'Proxy Settings' --key httpsProxy)"
        echo "SOCKS 代理: $(kreadconfig5 --file kioslaverc --group 'Proxy Settings' --key socksProxy)"
    fi
}

# 函数:检查 IP
check_ip() {
    echo -e "${YELLOW}正在检查 IP...${NC}"
    
    echo -e "${GREEN}不使用代理的 IP:${NC}"
    curl -s --max-time $TIMEOUT http://ip-api.com/json/ | jq -r '.query, .country, .city'
    
    echo -e "${GREEN}使用 HTTP 代理的 IP:${NC}"
    curl -s --max-time $TIMEOUT -x http://$PROXY_HOST:$HTTP_PORT http://ip-api.com/json/ | jq -r '.query, .country, .city'
    
    echo -e "${GREEN}使用 SOCKS5 代理的 IP:${NC}"
    curl -s --max-time $TIMEOUT -x socks5h://$PROXY_HOST:$SOCKS_PORT http://ip-api.com/json/ | jq -r '.query, .country, .city'
}

# 函数:验证代理
verify_proxy() {
    echo -e "${YELLOW}正在验证 HTTP 代理...${NC}"
    if curl -s --max-time $TIMEOUT -x http://$PROXY_HOST:$HTTP_PORT $TEST_URL > /dev/null; then
        echo -e "${GREEN}HTTP 代理验证成功:可以访问 $TEST_URL${NC}"
    else
        echo -e "${RED}HTTP 代理验证失败:无法访问 $TEST_URL${NC}"
        perform_diagnostics
    fi

    echo -e "${YELLOW}正在验证 SOCKS5 代理...${NC}"
    if curl -s --max-time $TIMEOUT -x socks5h://$PROXY_HOST:$SOCKS_PORT $TEST_URL > /dev/null; then
        echo -e "${GREEN}SOCKS5 代理验证成功:可以访问 $TEST_URL${NC}"
    else
        echo -e "${RED}SOCKS5 代理验证失败:无法访问 $TEST_URL${NC}"
        perform_diagnostics
    fi
}

# 函数:执行诊断
perform_diagnostics() {
    echo "正在进行进一步诊断..."
    
    # 检查 V2Ray 是否正在运行
    if ! systemctl is-active --quiet v2ray; then
        echo -e "${RED}V2Ray 服务未运行。${NC}"
        return
    fi
    
    # 检查本地端口是否开放
    if ! nc -z localhost $HTTP_PORT; then
        echo -e "${RED}本地 HTTP 代理端口 $HTTP_PORT 未开放。${NC}"
    fi
    if ! nc -z localhost $SOCKS_PORT; then
        echo -e "${RED}本地 SOCKS5 代理端口 $SOCKS_PORT 未开放。${NC}"
    fi
    
    # 检查到 V2Ray 服务器的连接
    OUTBOUND_ADDRESS=$(grep -oP '"address": "\K[^"]+' "$CONFIG_FILE")
    OUTBOUND_PORT=$(grep -oP '"port": \K\d+' "$CONFIG_FILE" | tail -1)
    echo -e "${YELLOW}正在检查到 V2Ray 服务器 ($OUTBOUND_ADDRESS:$OUTBOUND_PORT) 的连接...${NC}"
    if nc -z -w5 $OUTBOUND_ADDRESS $OUTBOUND_PORT; then
        echo -e "${GREEN}可以连接到 V2Ray 服务器。${NC}"
    else
        echo -e "${RED}无法连接到 V2Ray 服务器。请检查服务器地址和端口是否正确,以及服务器是否在线。${NC}"
    fi
    
    # 尝试不使用代理访问测试 URL
    if curl -s --max-time $TIMEOUT $TEST_URL > /dev/null; then
        echo -e "${YELLOW}不使用代理可以访问 $TEST_URL,但使用代理无法访问。请检查 V2Ray 配置。${NC}"
    else
        echo -e "${YELLOW}无法访问 $TEST_URL,可能是网络问题或目标网站暂时不可用。${NC}"
    fi
    
    # 显示 V2Ray 错误日志
    echo -e "${YELLOW}V2Ray 错误日志:${NC}"
    sudo journalctl -u v2ray --no-pager | tail -n 20
    
    # 检查 SELinux 状态
    if command -v getenforce &> /dev/null; then
        SELINUX_STATUS=$(getenforce)
        echo -e "${YELLOW}SELinux 状态: $SELINUX_STATUS${NC}"
        if [ "$SELINUX_STATUS" == "Enforcing" ]; then
            echo -e "${RED}SELinux 可能阻止 V2Ray 正常工作。考虑临时禁用 SELinux 或为 V2Ray 创建适当的 SELinux 策略。${NC}"
        fi
    fi
    
    # 检查防火墙状态
    if command -v ufw &> /dev/null; then
        UFW_STATUS=$(sudo ufw status | grep "Status: " | cut -d' ' -f2)
        echo -e "${YELLOW}UFW 防火墙状态: $UFW_STATUS${NC}"
        if [ "$UFW_STATUS" == "active" ]; then
            echo -e "${YELLOW}请确保 UFW 允许 V2Ray 的出站连接。${NC}"
        fi
    fi
}

# 函数:显示 V2Ray 配置
show_v2ray_config() {
    echo -e "${YELLOW}V2Ray 配置文件内容:${NC}"
    sudo cat $CONFIG_FILE
}

# 主菜单
show_menu() {
    echo -e "${YELLOW}==== V2Ray VMess 和代理管理 ====${NC}"
    echo "1. 启动 V2Ray 和设置代理"
    echo "2. 停止 V2Ray 和取消代理"
    echo "3. 检查 V2Ray 状态"
    echo "4. 显示当前代理设置"
    echo "5. 检查 IP"
    echo "6. 验证代理"
    echo "7. 显示 V2Ray 配置"
    echo "8. 切换代理模式"
    echo "9. 退出"
    echo -e "${YELLOW}===========================${NC}"
}

# 函数:切换代理模式
switch_proxy_mode() {
    if [ -z "$http_proxy" ]; then
        echo -e "${YELLOW}当前未设置代理,请先启动 V2Ray 和设置代理。${NC}"
        return
    fi
    
    set_proxy
}

# 主函数
main() {
    check_v2ray_installed

    while true; do
        show_menu
        read -p "请选择操作 (1-9): " choice
        case $choice in
            1) start_v2ray_and_proxy ;;
            2) stop_v2ray_and_proxy ;;
            3) check_v2ray_status ;;
            4) show_proxy_settings ;;
            5) check_ip ;;
            6) verify_proxy ;;
            7) show_v2ray_config ;;
            8) switch_proxy_mode ;;
            9) echo -e "${GREEN}再见!${NC}"; exit 0 ;;
            *) echo -e "${RED}无效选项,请重新选择。${NC}" ;;
        esac
        echo
        read -p "按回车键继续..."
        clear
    done
}

# 运行主函数
main

 

分享到:

这篇文章还没有评论

发表评论