#!/bin/bash

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 代理配置信息
PROXY_URL="vless://30229969-1519-436e-9241-a8ef433ded01@172.98.22.69:443?type=tcp&encryption=none&security=reality&pbk=auuE2QmWskEyhrTsO307CpLzrJAaDvPaDYrmyiu1ehA&fp=chrome&sni=30229969.com&sid=8b&spx=%2F#proxy"

# 检查系统类型
check_system() {
    if [[ "$OSTYPE" == "linux-gnu"* ]]; then
        echo "linux"
    elif [[ "$OSTYPE" == "darwin"* ]]; then
        echo "macos"
    else
        echo "unknown"
    fi
}

# 安装必要的工具
install_tools() {
    local system=$(check_system)
    
    if [[ "$system" == "linux" ]]; then
        # 检测包管理器
        if command -v apt &> /dev/null; then
            sudo apt update
            sudo apt install -y curl wget
        elif command -v yum &> /dev/null; then
            sudo yum install -y curl wget
        elif command -v dnf &> /dev/null; then
            sudo dnf install -y curl wget
        fi
    elif [[ "$system" == "macos" ]]; then
        if ! command -v brew &> /dev/null; then
            echo -e "${YELLOW}请先安装Homebrew: https://brew.sh/${NC}"
            exit 1
        fi
        brew install curl wget
    fi
}

# 显示代理信息
show_proxy_info() {
    echo -e "${GREEN}代理配置信息:${NC}"
    echo -e "${BLUE}协议:${NC} VLESS+REALITY"
    echo -e "${BLUE}服务器:${NC} 172.98.22.69:443"
    echo -e "${BLUE}SNI:${NC} 30229969.com"
    echo -e "${BLUE}指纹:${NC} chrome"
    echo -e "${BLUE}安全:${NC} reality"
    echo
}

# 方法1: 使用curl设置代理（临时）
setup_curl_proxy() {
    echo -e "${GREEN}设置curl使用代理...${NC}"
    export http_proxy="socks5://127.0.0.1:1080"
    export https_proxy="socks5://127.0.0.1:1080"
    echo "代理已设置为SOCKS5://127.0.0.1:1080"
    echo
}

# 方法2: 生成客户端配置文件
generate_config() {
    echo -e "${GREEN}生成客户端配置文件...${NC}"
    
    cat > vless-config.json << EOF
{
    "inbounds": [
        {
            "port": 1080,
            "protocol": "socks",
            "settings": {
                "auth": "noauth",
                "udp": true
            },
            "tag": "socks-in"
        },
        {
            "port": 1081,
            "protocol": "http",
            "settings": {
                "allowTransparent": false
            },
            "tag": "http-in"
        }
    ],
    "outbounds": [
        {
            "protocol": "vless",
            "settings": {
                "vnext": [
                    {
                        "address": "172.98.22.69",
                        "port": 443,
                        "users": [
                            {
                                "id": "30229969-1519-436e-9241-a8ef433ded01",
                                "encryption": "none",
                                "flow": ""
                            }
                        ]
                    }
                ]
            },
            "streamSettings": {
                "network": "tcp",
                "security": "reality",
                "realitySettings": {
                    "show": false,
                    "fingerprint": "chrome",
                    "serverName": "30229969.com",
                    "publicKey": "auuE2QmWskEyhrTsO307CpLzrJAaDvPaDYrmyiu1ehA",
                    "shortId": "8b",
                    "spiderX": "/"
                }
            },
            "tag": "proxy"
        }
    ]
}
EOF
    echo -e "${GREEN}配置文件已生成: vless-config.json${NC}"
    echo
}

# 方法3: 下载和使用客户端
setup_client() {
    local system=$(check_system)
    
    echo -e "${GREEN}设置代理客户端...${NC}"
    
    if [[ "$system" == "linux" ]]; then
        # 下载Xray核心
        if [ ! -f "xray" ]; then
            echo "下载Xray核心..."
            wget -O xray.zip https://github.com/XTLS/Xray-core/releases/latest/download/Xray-linux-64.zip
            unzip -o xray.zip xray
            rm xray.zip
            chmod +x xray
        fi
        
        # 启动代理
        if [ -f "xray" ]; then
            generate_config
            echo -e "${GREEN}启动Xray代理...${NC}"
            ./xray run -config vless-config.json &
            XRAY_PID=$!
            echo -e "${GREEN}Xray已启动 (PID: $XRAY_PID)${NC}"
            echo -e "${YELLOW}SOCKS5代理: 127.0.0.1:1080${NC}"
            echo -e "${YELLOW}HTTP代理: 127.0.0.1:1081${NC}"
        fi
        
    elif [[ "$system" == "macos" ]]; then
        echo -e "${YELLOW}macOS用户请手动安装客户端:${NC}"
        echo "1. 安装ClashX: https://github.com/yichengchen/clashX/releases"
        echo "2. 或安装V2RayX: https://github.com/Cenmrev/V2RayX/releases"
        echo "3. 使用上面的代理链接导入配置"
    fi
    echo
}

# 方法4: 测试代理连接
test_proxy() {
    echo -e "${GREEN}测试代理连接...${NC}"
    
    # 测试直接连接
    echo -e "${BLUE}直接连接测试:${NC}"
    curl -s --connect-timeout 5 https://httpbin.org/ip || echo -e "${RED}直接连接失败${NC}"
    
    echo
    
    # 测试代理连接
    echo -e "${BLUE}代理连接测试:${NC}"
    curl -s --connect-timeout 5 --socks5 127.0.0.1:1080 https://httpbin.org/ip && echo -e "${GREEN}代理连接成功${NC}" || echo -e "${RED}代理连接失败${NC}"
    echo
}

# 显示使用说明
show_usage() {
    echo -e "${GREEN}使用方法:${NC}"
    echo -e "  ${BLUE}1. 临时设置环境变量${NC}"
    echo -e "     export http_proxy=socks5://127.0.0.1:1080"
    echo -e "     export https_proxy=socks5://127.0.0.1:1080"
    echo
    echo -e "  ${BLUE}2. 使用代理测试网络${NC}"
    echo -e "     curl --socks5 127.0.0.1:1080 https://httpbin.org/ip"
    echo
    echo -e "  ${BLUE}3. 配置应用程序使用代理${NC}"
    echo -e "     SOCKS5: 127.0.0.1:1080"
    echo -e "     HTTP: 127.0.0.1:1081"
    echo
}

# 清理函数
cleanup() {
    if [ ! -z "$XRAY_PID" ]; then
        kill $XRAY_PID 2>/dev/null
        echo -e "${GREEN}已停止代理服务${NC}"
    fi
}

# 主函数
main() {
    echo -e "${GREEN}=== VLESS代理一键设置脚本 ===${NC}"
    echo
    
    # 注册清理函数
    trap cleanup EXIT
    
    show_proxy_info
    
    # 安装必要工具
    install_tools
    
    # 设置客户端
    setup_client
    
    # 设置curl代理
    setup_curl_proxy
    
    # 等待服务启动
    echo -e "${YELLOW}等待代理服务启动...${NC}"
    sleep 3
    
    # 测试连接
    test_proxy
    
    # 显示使用说明
    show_usage
    
    echo -e "${GREEN}设置完成! 按Ctrl+C退出脚本并停止代理服务${NC}"
    
    # 等待用户中断
    wait
}

# 运行主函数
main