#!/bin/bash

# 3xUI面板国内优化安装脚本
# 适用于CentOS 7+/Ubuntu 18+/Debian 10+

echo "正在安装3xUI面板（国内优化版）..."

# 检查系统类型
if [ -f /etc/redhat-release ]; then
    OS="centos"
    yum update -y
    yum install -y curl wget sudo
elif [ -f /etc/lsb-release ] || [ -f /etc/debian_version ]; then
    OS="debian"
    apt update -y
    apt install -y curl wget sudo
else
    echo "不支持的操作系统"
    exit 1
fi

# 设置国内镜像源（备用）
setup_china_mirror() {
    if [ "$OS" = "centos" ]; then
        if ! curl -I -m 10 -o /dev/null -s -w %{http_code} http://mirrors.aliyun.com | grep -q "200"; then
            echo "使用阿里云镜像源"
            sed -e 's|^mirrorlist=|#mirrorlist=|g' \
                -e 's|^#baseurl=http://mirror.centos.org|baseurl=https://mirrors.aliyun.com|g' \
                -i.bak /etc/yum.repos.d/CentOS-*.repo
        fi
    else
        if ! curl -I -m 10 -o /dev/null -s -w %{http_code} http://mirrors.aliyun.com | grep -q "200"; then
            echo "使用阿里云镜像源"
            sed -i 's/http:\/\/archive.ubuntu.com/http:\/\/mirrors.aliyun.com/g' /etc/apt/sources.list
            sed -i 's/http:\/\/deb.debian.org/http:\/\/mirrors.aliyun.com/g' /etc/apt/sources.list
        fi
    fi
}

# 安装依赖
install_dependencies() {
    echo "安装必要依赖..."
    
    if [ "$OS" = "centos" ]; then
        yum install -y curl wget socat cronie
        systemctl enable crond
        systemctl start crond
    else
        apt install -y curl wget socat cron
        systemctl enable cron
        systemctl start cron
    fi
}

# 下载并安装3xUI
install_3xui() {
    echo "下载3xUI安装脚本..."
    
    # 尝试多个下载源
    local download_success=false
    
    # 源1: 官方GitHub（可能较慢）
    if wget -O 3x-ui.sh https://raw.githubusercontent.com/mhsanaei/3x-ui/master/install.sh 2>/dev/null; then
        download_success=true
    # 源2: 使用ghproxy代理（推荐）
    elif wget -O 3x-ui.sh https://ghproxy.com/https://raw.githubusercontent.com/mhsanaei/3x-ui/master/install.sh 2>/dev/null; then
        download_success=true
    # 源3: 备用镜像
    elif wget -O 3x-ui.sh https://raw.githubusercontent.com.cnpmjs.org/mhsanaei/3x-ui/master/install.sh 2>/dev/null; then
        download_success=true
    else
        echo "下载失败，请检查网络连接"
        exit 1
    fi
    
    if [ "$download_success" = true ]; then
        chmod +x 3x-ui.sh
        echo "开始安装3xUI面板..."
        ./3x-ui.sh
    fi
}

# 防火墙配置
configure_firewall() {
    echo "配置防火墙..."
    
    if command -v ufw >/dev/null 2>&1; then
        ufw allow 2053/tcp
        ufw allow 2053/udp
        ufw allow 443/tcp
        ufw allow 80/tcp
        echo "UFW防火墙已配置"
    elif command -v firewall-cmd >/dev/null 2>&1; then
        firewall-cmd --permanent --add-port=2053/tcp
        firewall-cmd --permanent --add-port=2053/udp
        firewall-cmd --permanent --add-port=443/tcp
        firewall-cmd --permanent --add-port=80/tcp
        firewall-cmd --reload
        echo "FirewallD已配置"
    else
        echo "请手动开放2053、443端口"
    fi
}

# 优化系统配置
optimize_system() {
    echo "优化系统配置..."
    
    # 增加文件描述符限制
    echo "* soft nofile 51200" >> /etc/security/limits.conf
    echo "* hard nofile 51200" >> /etc/security/limits.conf
    
    # 优化内核参数
    cat >> /etc/sysctl.conf << EOF
net.core.default_qdisc=fq
net.ipv4.tcp_congestion_control=bbr
net.ipv4.tcp_fastopen=3
net.core.rmem_max=67108864
net.core.wmem_max=67108864
net.core.netdev_max_backlog=250000
net.core.somaxconn=4096
EOF
    
    sysctl -p
}

# 主安装流程
main() {
    echo "=== 3xUI面板国内优化安装脚本 ==="
    
    # 检查root权限
    if [ "$EUID" -ne 0 ]; then
        echo "请使用root权限运行此脚本"
        exit 1
    fi
    
    # 设置镜像源
    setup_china_mirror
    
    # 安装依赖
    install_dependencies
    
    # 系统优化
    optimize_system
    
    # 安装3xUI
    install_3xui
    
    # 配置防火墙
    configure_firewall
    
    echo "=== 安装完成 ==="
    echo "面板地址: http://你的服务器IP:2053"
    echo "默认用户名: admin"
    echo "默认密码: admin"
    echo ""
    echo "请及时修改默认密码！"
    echo "建议配置域名和SSL证书以增强安全性"
}

# 执行主函数
main