#!/bin/bash

echo "此脚本将安装配置Caddy服务器并部署NaiveProxy服务。"
read -p "按下回车键开始运行，或按Ctrl+C取消..." dummy

# 检查是否为 root 用户
if [ "$(id -u)" != "0" ]; then
   echo "此脚本需要以 root 用户运行，请切换到 root 用户后重试。"
   exit 1
fi

# 定义变量
CADDY_URL="https://depot.gggu.eu.org/caddy/caddy"
HTML_TAR_URL="https://depot.gggu.eu.org/caddy/html.tar.gz"
INSTALL_DIR="/root/caddy"
HTML_DIR="/root/caddy/html"

# 创建安装目录和 HTML 目录
mkdir -p "$INSTALL_DIR"
mkdir -p "$HTML_DIR"

# 下载 Caddy
echo "正在下载 Caddy单文件..."
curl -L "$CADDY_URL" -o "$INSTALL_DIR/caddy"
if [ $? -ne 0 ]; then
    echo "下载 Caddy 失败，请检查链接是否正确或网络是否正常。"
    exit 1
fi

# 设置 Caddy 可执行权限
chmod +x "$INSTALL_DIR/caddy"

# 下载 HTML 压缩包并解压到指定目录
echo "正在下载 默认网站HTML文件..."
curl -L "$HTML_TAR_URL" -o "$INSTALL_DIR/html.tar.gz"
if [ $? -ne 0 ]; then
    echo "下载 HTML 文件失败，请检查链接或网络。"
    exit 1
fi

tar -xzf "$INSTALL_DIR/html.tar.gz" -C "$HTML_DIR"
if [ $? -ne 0 ]; then
    echo "解压 HTML 文件失败。"
    exit 1
fi


# 设置 HTML 目录权限为 644
chmod -R 644 "$HTML_DIR"

# 提示用户输入网站域名和端口
read -p "请输入网站域名(示例：vps012.example.com 请确保域名已解析到此机IP): " DOMAIN
read -p "请输入申请TLS证书邮箱（回车自动生成）: " RANDOMEMAIL
read -p "确认你的80，443端口没有被占用（回车确认）: " PORT
read -p "请输入Naive用户名（回车随机生成）: " NaiveUser
read -p "请输入Naive密码（回车随机生成）: " NaivePassword

if [ -z "$RANDOMEMAIL" ]; then
    RANDOMEMAIL="$(date +%s%N | md5sum | head -c 9)@qq.com"
fi
if [ -z "$PORT" ]; then
    PORT=443
fi
if [ -z "$NaiveUser" ]; then
    NaiveUser="$(date +%s%N | md5sum | head -c 5)U"
fi
if [ -z "$NaivePassword" ]; then
    NaivePassword="$(date +%s%N | md5sum | head -c 11)P"
fi

# 生成 Caddyfile
echo "正在生成 Caddyfile..."
cat > "$INSTALL_DIR/Caddyfile" << EOF
{
  order forward_proxy before file_server
}
:$PORT, $DOMAIN {
  tls $RANDOMEMAIL
  forward_proxy {
    basic_auth $NaiveUser $NaivePassword
    hide_ip
    hide_via
    probe_resistance
  }
  file_server {
    root /root/caddy/html
  }
}
EOF

# 设置 cap_net_bind_service 能力
echo "设置 cap_net_bind_service 能力..."
setcap cap_net_bind_service=+ep /root/caddy/caddy
if [ $? -ne 0 ]; then
    echo "设置能力失败，请检查系统是否支持 setcap 命令。"
    exit 1
fi

# 创建 systemd 服务文件
echo "创建 Caddy systemd 服务..."
cat <<EOF > /etc/systemd/system/caddy.service
[Unit]
Description=Caddy
After=network.target syslog.target

[Service]
User=root
Group=root
LimitNOFILE=64000
ExecStart=/root/caddy/caddy run --environ --config /root/caddy/Caddyfile
# 如果你的 Caddy 版本支持 systemd 通知，则：
# Type=notify
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF

# 检查服务文件是否创建成功
if [ $? -ne 0 ]; then
    echo "创建服务文件失败。"
    exit 1
fi

# 重载 systemd 配置
echo "应用新的服务配置..."
systemctl daemon-reload
if [ $? -ne 0 ]; then
    echo "配置重载失败。"
    exit 1
fi

# 启用开机启动
echo "设置开机启动..."
systemctl enable caddy
if [ $? -ne 0 ]; then
    echo "启用开机启动失败。"
    exit 1
fi

# 启动服务
echo "启动 Caddy 服务..."
systemctl start caddy
if [ $? -ne 0 ]; then
    echo "服务启动失败。"
    exit 1
fi

# 验证服务状态
echo "检查服务状态："
systemctl status caddy --no-pager

echo "清理临时文件..."
rm -f "$INSTALL_DIR/caddy.log"
rm -f "$INSTALL_DIR/html.tar.gz"

echo "NaiveProxy安装完成"
echo "域名：$DOMAIN:$PORT"
echo "Naive用户名：$NaiveUser"
echo "Naive密码：$NaivePassword"
echo "分享链接：naive+https://$NaiveUser:$NaivePassword@$DOMAIN:$PORT#NaiveProxy"
echo "可自定义替换默认网站文件，目录为/root/caddy/html。"
