# 输出颜色
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m' # No Color
# 提示用户操作风险
confirm_action() {
echo -e "\033[1;33m 注意: 此脚本将安装多个工具和服务, 可能会修改系统配置, 带来潜在的安全风险!!!\033[0m"
read -p "是否继续执行? (y/n): " CONFIRM
if [[ "$CONFIRM" != "y" ]]; then
echo "操作已取消。"
exit 0
fi
}
# 检查是否以 root 权限运行
if [ "$(id -u)" -ne 0 ]; then
echo "请以 root 用户运行此脚本!!!"
exit 1
fi
# 检测操作系统和包管理器
detect_package_manager() {
if command -v apt &> /dev/null; then
PACKAGE_MANAGER="apt"
elif command -v yum &> /dev/null; then
PACKAGE_MANAGER="yum"
else
echo "无法检测到支持的包管理器 (apt 或 yum), 请手动安装必要的依赖。"
exit 1
fi
}
# 安装必要工具
install_packages() {
if [ "$PACKAGE_MANAGER" = "apt" ]; then
curl -fsSL https://pkg.cloudflareclient.com/pubkey.gpg | gpg --yes --dearmor --output /usr/share/keyrings/cloudflare-warp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/cloudflare-warp-archive-keyring.gpg] https://pkg.cloudflareclient.com/ $(lsb_release -cs) main" | tee /etc/apt/sources.list.d/cloudflare-client.list
apt update && apt install -y curl wget unzip openssl cloudflare-warp
elif [ "$PACKAGE_MANAGER" = "yum" ]; then
curl -fsSl https://pkg.cloudflareclient.com/cloudflare-warp-ascii.repo | tee /etc/yum.repos.d/cloudflare-warp.repo
yum install -y epel-release
yum install -y curl wget unzip openssl cloudflare-warp
fi
}
# 变量定义
DOMAIN=${1:-"example.com"} # 如果未提供域名参数,则默认使用 example.com
DAYS_VALID=365 # 证书有效期(天数)
CERT_DIR="/usr/local/etc/xray" # 证书存储目录
# 创建存储目录
mkdir -p ${CERT_DIR}
# 生成私钥
openssl genrsa -out ${CERT_DIR}/${DOMAIN}.key 2048
# 生成证书签名请求( CSR )
openssl req -new -key ${CERT_DIR}/${DOMAIN}.key -out ${CERT_DIR}/${DOMAIN}.csr -subj "/CN=${DOMAIN}"
# 生成自签名证书
openssl x509 -req -days ${DAYS_VALID} -in ${CERT_DIR}/${DOMAIN}.csr -signkey ${CERT_DIR}/${DOMAIN}.key -out ${CERT_DIR}/${DOMAIN}.crt
# 安装 Xray-core
bash -c "$(curl -L https://github.com/XTLS/Xray-install/raw/main/install-release.sh)" @ install
UUID=$(xray uuid)
cat >> /usr/local/etc/xray/config.json