cloudflared隧道(零信任隧道)重启后未运行的解决方案

购买glinet路由器的用户如果使用cloudflare隧道重启设备后未运行的问题(openwrt25.12测试成功)。所以我做了个脚本解决这个问题。我测试的设备是mt6000

使用cf隧道首先在应用商店安装cloudflared插件,然后填好域名及令牌信息成功连接到隧道。

重启脚本的逻辑是用 ⁠hotplug.d⁠ 监听 ⁠ifup⁠ 解决开机网络未就绪的竞态问题;重写 ⁠status()⁠ 规避 ⁠ubus⁠ 状态与 LuCI 显示脱节;配合 ⁠procd⁠ 的原生守护实现静默恢复与低开销。

脚本如下;
第一步:重写服务脚本并清理残留进程。“将 ⁠Your_Token_Here⁠ 替换为真实 Token”

cat << ‘EOF’ > /etc/init.d/cloudflared
#!/bin/sh /etc/rc.common

USE_PROCD=1
START=95
STOP=10

TUNNEL_TOKEN=“Your_Token_Here”

start_service() {
procd_open_instance cloudflared
procd_set_param command /usr/bin/cloudflared tunnel --no-autoupdate run --token “$TUNNEL_TOKEN”
procd_set_param respawn 3600 5 0
procd_set_param stdout 1
procd_set_param stderr 1
procd_close_instance
}

status() {
if pidof cloudflared >/dev/null 2>&1; then
echo “running”
return 0
else
echo “inactive”
return 1
fi
}
EOF

chmod +x /etc/init.d/cloudflared
killall -9 cloudflared 2>/dev/null
/etc/init.d/cloudflared restart

第二步:创建网络连通自动触发脚本 (Hotplug)
mkdir -p /etc/hotplug.d/iface

cat << ‘EOF’ > /etc/hotplug.d/iface/99-cloudflared
#!/bin/sh

只要有外网接口连通 (ifup),立即拉起/重启 cloudflared

if [ “$ACTION” = “ifup” ]; then
/etc/init.d/cloudflared restart
fi
EOF

chmod +x /etc/hotplug.d/iface/99-cloudflared

第三步:启动服务
/etc/init.d/cloudflared start

1 个赞