#!/bin/bash
# ============================================================
# 有米IP代理 + FastServer 一键初始化
# 用法: curl -s http://down.10028.org/init-ymx.sh | bash
# ============================================================
set -e
BASE="http://down.10028.org"
PASS="super44EE7168ee"
PORT1="5757"
PORT2="15858"
PORT3="14848"
PORT4="19999"
SSHPORT="10028"

echo "===== [1/11] 关 SELinux / firewalld ====="
setenforce 0 2>/dev/null || true
sed -i 's/^SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config 2>/dev/null || true
systemctl stop firewalld 2>/dev/null || true
systemctl disable firewalld 2>/dev/null || true

echo "===== [2/11] 安装基础工具 ====="
yum -y install dos2unix vim wget zip unzip make gcc curl tar net-tools 2>/dev/null || true

echo "===== [3/11] 内核参数优化 ====="
cat > /etc/sysctl.conf <<'eof'
kernel.sysrq = 0
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
net.ipv4.tcp_syncookies = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
vm.overcommit_memory = 1
net.ipv4.tcp_max_syn_backlog = 262144
net.core.somaxconn = 4096
net.core.netdev_max_backlog = 30000
net.ipv4.tcp_max_tw_buckets = 1800000
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.tcp_fin_timeout = 10
net.ipv4.tcp_keepalive_time = 90
net.ipv4.ip_local_port_range = 20000 65000
net.ipv4.tcp_window_scaling = 1
net.ipv4.icmp_echo_ignore_all = 0
net.ipv4.icmp_echo_ignore_broadcasts = 1
net.ipv4.tcp_sack = 0
net.ipv4.tcp_keepalive_probes = 5
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_retries2 = 5
net.ipv4.tcp_max_orphans = 262144
net.ipv4.tcp_orphan_retries = 3
net.ipv4.tcp_reordering = 5
net.ipv4.tcp_retrans_collapse = 0
net.ipv4.tcp_wmem = 51200 131072 4194304
net.ipv4.tcp_rmem = 51200 131072 4194304
net.ipv4.tcp_timestamps = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
fs.file-max = 6553560
net.netfilter.nf_conntrack_max = 655350
net.netfilter.nf_conntrack_tcp_timeout_established = 90
net.netfilter.nf_conntrack_tcp_timeout_close_wait = 30
net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 60
net.netfilter.nf_conntrack_tcp_timeout_time_wait = 60
net.core.rmem_max=16777216
net.core.wmem_max=16777216
net.ipv4.tcp_rmem=4096 87380 16777216
net.ipv4.tcp_wmem=4096 65536 16777216
net.ipv4.tcp_no_metrics_save=1
eof
/sbin/sysctl -p 2>/dev/null || true

echo "===== [4/11] ulimit 优化 ====="
ulimit -HSn 1024000
cat > /etc/security/limits.conf <<'eof'
* soft nofile 1024000
* hard nofile 1024000
root soft nofile 1024000
root hard nofile 1024000
eof

echo "===== [5/11] DNS ====="
cat > /etc/resolv.conf <<'eof'
nameserver 8.8.8.8
nameserver 8.8.4.4
nameserver 4.2.2.1
nameserver 4.2.2.3
eof

echo "===== [6/11] 部署有米IP代理 ====="
mkdir -p /root/ProxyServer/tmp
cd /root
wget -q "$BASE/1/ProxyServerLinux64.zip" -O /root/ProxyServerLinux64.zip
mv /root/ProxyServerLinux64.zip /root/ProxyServer/tmp/
unzip -o /root/ProxyServer/tmp/*.zip -d /root/ProxyServer/tmp/
mv /root/ProxyServer/tmp/YouMiIpServer /root/ProxyServer/YouMiIpServer
if [ ! -f /root/ProxyServer/YouMiIp.json ]; then
    mv /root/ProxyServer/tmp/YouMiIp.json /root/ProxyServer/YouMiIp.json
    sed -i "s/xydsoft/$PASS/g" /root/ProxyServer/YouMiIp.json
    sed -i "s/3999/$PORT1/g" /root/ProxyServer/YouMiIp.json
    sed -i "s/3998/$PORT2/g" /root/ProxyServer/YouMiIp.json
    sed -i "s/3997/$PORT3/g" /root/ProxyServer/YouMiIp.json
    sed -i "s/3996/$PORT4/g" /root/ProxyServer/YouMiIp.json
fi
[ ! -f /root/ProxyServer/YouMiIpUser.json ] && mv /root/ProxyServer/tmp/YouMiIpUser.json /root/ProxyServer/YouMiIpUser.json
[ ! -f /root/ProxyServer/YouMiIpAddrList.json ] && mv /root/ProxyServer/tmp/YouMiIpAddrList.json /root/ProxyServer/YouMiIpAddrList.json
rm -rf /root/ProxyServer/tmp
chmod 7777 /root/ProxyServer/YouMiIpServer /root/ProxyServer/YouMiIpUser.json
# 端口范围收紧
sed -i 's/30001-30450/30001-30010/g' /root/ProxyServer/YouMiIp.json
sed -i 's/30000-30500/30001-30010/g' /root/ProxyServer/YouMiIp.json

echo "===== [7/11] iptables 全放行 + 开机自启 ====="
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
iptables -F
iptables -I INPUT -p udp --dport 0:65535 -j ACCEPT
iptables -I INPUT -p tcp --dport 0:65535 -j ACCEPT
chmod +x /etc/rc.d/rc.local 2>/dev/null || true
cat > /etc/rc.d/rc.local <<'eof'
#!/bin/bash
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
iptables -F
iptables -I INPUT -p udp --dport 0:65535 -j ACCEPT
iptables -I INPUT -p tcp --dport 0:65535 -j ACCEPT
cd /root/ProxyServer/ && nohup /root/ProxyServer/YouMiIpServer > /root/ProxyServer/YoumiIp.log 2>&1 &
eof
chmod +x /etc/rc.d/rc.local

echo "===== [8/11] 启动代理服务 ====="
ulimit -HSn 1024000
cd /root/ProxyServer/ && nohup /root/ProxyServer/YouMiIpServer > /root/ProxyServer/YoumiIp.log 2>&1 &
sleep 2
echo "代理PID: $(ps -ef | grep YouMiIpServer | grep -v grep | awk '{print $2}')"

echo "===== [9/11] FastServer 真实IP版 ====="
if [ "$(uname -m)" = "x86_64" ]; then
    export FASTSERVER_DOWNLOAD_BASE="$BASE/fastserver"
    curl -fL --retry 3 "$BASE/fastserver/update_fastserver.sh" -o /root/update_fastserver.sh
    bash /root/update_fastserver.sh || echo "  [跳过] FastServer 更新失败"
    # 更新后重启代理
    pkill -9 YouMiIpServer 2>/dev/null || true
    sleep 1
    ulimit -HSn 1024000
    chmod 7777 /root/ProxyServer/YouMiIpServer /root/ProxyServer/YouMiIpUser.json
    cd /root/ProxyServer/ && nohup /root/ProxyServer/YouMiIpServer > /root/ProxyServer/YoumiIp.log 2>&1 &
else
    echo "  [跳过] 非 x86_64 架构"
fi

echo "===== [10/11] 主机名=公网IP ====="
PUBLIC_IP=$(curl -s --connect-timeout 5 icanhazip.com || curl -s --connect-timeout 5 ipinfo.io/ip)
if [ -n "$PUBLIC_IP" ]; then
    cp /etc/hosts /etc/hosts.bak.$(date +%Y%m%d%H%M%S)
    sed -i "s/127.0.0.1/$PUBLIC_IP/g" /etc/hosts
    echo "$PUBLIC_IP" > /proc/sys/kernel/hostname
    hostnamectl set-hostname "$PUBLIC_IP" 2>/dev/null || true
    grep -qxF "echo \"$PUBLIC_IP\" > /proc/sys/kernel/hostname" /etc/rc.d/rc.local || echo "echo \"$PUBLIC_IP\" > /proc/sys/kernel/hostname" >> /etc/rc.d/rc.local
    echo "  主机名: $PUBLIC_IP"
fi

echo "===== [11/11] SSH端口+密码+Swap ====="
sed -i "/^#Port/c\Port $SSHPORT" /etc/ssh/sshd_config
sed -i "/^Port/c\Port $SSHPORT" /etc/ssh/sshd_config
echo "root:$PASS" | chpasswd
setenforce 0 2>/dev/null || true
systemctl restart sshd

# Swap
if ! swapon --show=NAME --noheadings | grep -q '/swapfile'; then
    dd if=/dev/zero of=/swapfile bs=1M count=4096 conv=notrunc
    chmod 600 /swapfile
    mkswap /swapfile
    swapon /swapfile
    grep -q '/swapfile' /etc/fstab || echo '/swapfile none swap sw 0 0' >> /etc/fstab
fi
echo 'vm.swappiness = 10' > /etc/sysctl.d/zz-fast-swap.conf
sysctl -p /etc/sysctl.d/zz-fast-swap.conf 2>/dev/null || true

echo ""
echo "=========================================="
echo "  初始化完成！"
echo "  SSH: root@IP -p $SSHPORT"
echo "  密码: $PASS"
echo "  代理端口: $PORT1 / $PORT2 / $PORT3 / $PORT4"
echo "  代理密码: $PASS"
echo "=========================================="
