使用 ChatGPT 编写的 shell 脚本,想问下这个脚本有没有啥问题

288 天前
 Lukedis
#!/bin/bash

# 输出颜色
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 << 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 
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"
3487 次点击
所在节点    程序员
27 条回复
Asakijz
287 天前
你这个脚本是想实现啥?
henix
287 天前
用 shellcheck 静态检查一下
renmu
287 天前
搁这找喷呢
aegon466
287 天前
可以用自己熟悉的语言写逻辑然后脚本调用
iv8d
287 天前
你再问一下 gpt 该 shell 是否有问题
m1nm13
287 天前
好好好.这回把 V 友当 GPT 来用了
gerefoxing
287 天前
太长不看

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1095067

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX