非常好,下面是一个`,用于在树莓派上:
• 安装并配置 Cloudflare Tunnel
• 启用 systemd 自动运行 tunnel
• 暴露多个本地服务(WebUI、SSH、NAS)
⸻
🧾 你将需要准备:
1. 已注册的域名(在 Cloudflare 上管理)
2. 已在树莓派中登录 Cloudflare(我们会自动处理)
⸻
🛠️ 脚本功能概览:
子域 绑定服务 本地端口
webui.example.com 树莓派的 Web 服务 localhost:8000
ssh.example.com SSH 远程连接 localhost:22
nas.example.com 家用 NAS 网页控制台 192.168.1.100:5000
你可根据需要修改这些地址。
⸻
🚀 一键部署脚本(cloudflare-tunnel-setup.sh)
#!/bin/bash
========== 参数设置 ==========
TUNNEL_NAME=“rpi-tunnel”
DOMAIN=“example.com”
CF_TUNNEL_DIR=“/etc/cloudflared”
映射的服务(子域名 -> 本地地址)
declare -A SERVICES=(
[“webui”]=“http://localhost:8000”
[“ssh”]=“ssh://localhost:22”
[“nas”]=“http://192.168.1.100:5000”
)
========== 安装 cloudflared ==========
echo “[1/5] 安装 cloudflared…”
sudo mkdir -p “$CF_TUNNEL_DIR”
sudo apt update && sudo apt install -y curl
curl -L https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-arm64.deb -o cloudflared.deb
sudo dpkg -i cloudflared.deb
rm cloudflared.deb
========== 登录 Cloudflare ==========
echo “[2/5] 登录 Cloudflare,请在浏览器中完成授权”
cloudflared tunnel login
========== 创建 tunnel ==========
echo “[3/5] 创建 tunnel:TUNNELNAME"cloudflaredtunnelcreate"TUNNEL_NAME" cloudflared tunnel create "TUNNELNAME"cloudflaredtunnelcreate"TUNNEL_NAME”
获取 tunnel ID
TUNNEL_ID=(cat"(cat "(cat"HOME/.cloudflared/$TUNNEL_NAME.json" | grep ““TunnelID”” | awk -F’"’ ‘{print $4}’)
echo “Tunnel ID: $TUNNEL_ID”
========== 写入 config.yml ==========
echo “[4/5] 写入配置文件…”
sudo tee “$CF_TUNNEL_DIR/config.yml” >/dev/null <<EOF
tunnel: $TUNNEL_ID
credentials-file: HOME/.cloudflared/HOME/.cloudflared/HOME/.cloudflared/{TUNNEL_NAME}.json
ingress:
EOF
for SUB in “${!SERVICES[@]}”; do
echo " - hostname: SUB.{SUB}.SUB.{DOMAIN}" | sudo tee -a “$CF_TUNNEL_DIR/config.yml”
echo " service: KaTeX parse error: Expected '}', got 'EOF' at end of input: {SERVICES[SUB]}" | sudo tee -a “$CF_TUNNEL_DIR/config.yml”
done
fallback
sudo tee -a “$CF_TUNNEL_DIR/config.yml” <<EOF
- service: http_status:404
EOF
========== 创建 systemd 服务 ==========
echo “[5/5] 配置 systemd 服务…”
sudo tee /etc/systemd/system/cloudflared.service >/dev/null <<EOF
[Unit]
Description=Cloudflare Tunnel
After=network.target
[Service]
TimeoutStartSec=0
Type=simple
ExecStart=/usr/bin/cloudflared tunnel --config $CF_TUNNEL_DIR/config.yml run
Restart=always
RestartSec=5s
[Install]
WantedBy=multi-user.target
EOF
启动服务
sudo systemctl daemon-reexec
sudo systemctl daemon-reload
sudo systemctl enable cloudflared
sudo systemctl start cloudflared
echo “✅ Cloudflare Tunnel 部署完成!”
echo “可访问如下地址:”
for SUB in “!SERVICES[@]";doecho"−https://{!SERVICES[@]}"; do
echo " - https://!SERVICES[@]";doecho"−https://{SUB}.${DOMAIN}”
done
⸻
📦 使用方式:
chmod +x cloudflare-tunnel-setup.sh
./cloudflare-tunnel-setup.sh
⚠️ 脚本中默认使用了 cloudflared-linux-arm64.deb 版本,如你是 32 位树莓派,请改成 armhf 版本。
⸻
💡 自定义建议
• WebUI 本地端口:可改为 localhost:80 或其他服务
• NAS IP:改为你实际 NAS 的局域网 IP 和端口
• SSH 映射:通过 ssh.example.com 可实现远程 ssh -p 22
⸻
如果你希望我进一步生成 systemd 启动失败自动恢复、或加上自动 Cloudflare DNS 记录注册功能,也可以继续帮你扩展。是否需要?