V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
Lukedis
V2EX  ›  程序员

帮我分析下 shell 脚本有啥问题

  •  
  •   Lukedis ·
    Lukedis · 26 天前 · 510 次点击

    #!/bin/bash

    输出颜色

    GREEN='\033[0;32m' RED='\033[0;31m' NC='\033[0m' # No Color

    检查是否以 root 权限运行

    if [ "$(id -u)" -ne 0 ]; then echo "请以 root 用户运行此脚本!" exit 1 fi

    更新系统

    apt update && apt upgrade -y

    安装必要工具

    apt install -y curl wget unzip openssl

    变量定义

    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 << EOF { "inbounds": [ { "tag": "direct", "port": 443, "protocol": "vless", "settings": { "clients": [ { "id": "${UUID}", "flow": "" } ], "decryption": "none", "fallbacks": [] }, "streamSettings": { "network": "tcp", "security": "tls", "tlsSettings": { "serverNames": [ "" ], "alpn": [ "h2", "http/1.1" ], "certificates": [ { "certificateFile": "${CERT_DIR}/${DOMAIN}.crt", "keyFile": "${CERT_DIR}/${DOMAIN}.key" } ] } }, "sniffing": { "enabled": true, "destOverride": [ "http", "tls", "quic" ], "routeOnly": true } } ], "outbounds": [ { "tag": "direct", "protocol": "freedom" }, { "tag": "warp", "protocol": "socks", "settings": { "servers": [ { "address": "127.0.0.1", "port": 2333 } ] } } ], "routing": { "rules": [ { "type": "field", "outboundTag": "warp", "domain": [ "domain:openai.com", "domain:chatgpt.com", "domain:ai.com", "domain:chat.com", "domain:cloudflare.com", "domain:youtube.com", "domain:netflix.com" ] } ] } } EOF systemctl enable xray

    安装 Cloudflare WARP

    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 cloudflare-warp

    配置 Cloudflare WARP

    warp-cli mode proxy warp-cli proxy port 2333 warp-cli registration new warp-cli connect systemctl enable warp-svc

    获取公网 IP 地址

    get_public_ip() { # 使用主服务获取 IP IP=$(curl -s ifconfig.me) if [ -z "$IP" ]; then echo "主服务 ifconfig.me 不可用,尝试其他服务..." # 备选服务 1: ipinfo.io IP=$(curl -s ipinfo.io/ip) if [ -z "$IP" ]; then echo "备选服务 ipinfo.io 不可用,尝试其他服务..." # 备选服务 2: ip-api.com IP=$(curl -s http://ip-api.com/line?fields=query) if [ -z "$IP" ]; then echo "无法获取公网 IP 地址,请检查网络连接。" IP="未知" fi fi fi echo "$IP" }

    调用函数获取 IP 地址

    IP=$(get_public_ip)

    输出结果

    echo "可使用以下命令查看服务状态:" echo " systemctl status xray # 检查 Xray 服务状态" echo " warp-cli status # 检查 WARP 状态" echo -e "uuid: ${UUID}" echo -e "本机公网 IP 地址: ${IP}" echo "xray-core 配置文件路径: /usr/local/etc/xray/config.json"

    1 条回复
    NevadaLi
        1
    NevadaLi  
       25 天前 via iPhone
    不如…直接说遇到的问题?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1634 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 19ms · UTC 16:36 · PVG 00:36 · LAX 08:36 · JFK 11:36
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.