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

13 天前
 Lukedis

#!/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"

463 次点击
所在节点    程序员
1 条回复
NevadaLi
12 天前
不如…直接说遇到的问题?

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

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

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

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

© 2021 V2EX