Install Cloudflared on OpenWRT

VERSION="2023.5.1"

For ARM CPU:

curl -O -L \
https://github.com/cloudflare/cloudflared/releases/download/${VERSION}/cloudflared-linux-arm \
&& chmod +x cloudflared-linux-arm \
&& mv cloudflared-linux-arm /usr/bin/cloudflared

For AMD64 CPU:

curl -O -L \
https://github.com/cloudflare/cloudflared/releases/download/${VERSION}/cloudflared-linux-amd64 \
&& chmod +x cloudflared-linux-amd64 \
&& mv cloudflared-linux-amd64 /usr/bin/cloudflared
  • Create Cloudflared service file
touch /etc/init.d/cloudflared
chmod +x /etc/init.d/cloudflared
nano /etc/init.d/cloudflared
  • Paste the content. Fill in cfd_token
#!/bin/sh /etc/rc.common

USE_PROCD=1
START=95
STOP=01

cfd_init="/etc/init.d/cloudflared"
cfd_token=""

boot()
{
    ubus -t 30 wait_for network.interface network.loopback 2>/dev/null
    rc_procd start_service
}

start_service() {
    if [ $("${cfd_init}" enabled; printf "%u" ${?}) -eq 0 ]
    then
        procd_open_instance
        procd_set_param command /usr/bin/cloudflared --no-autoupdate tunnel run --token ${cfd_token}
        procd_set_param stdout 1
        procd_set_param stderr 1
        procd_set_param respawn ${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5}
        procd_close_instance
    fi
}

stop_service() {
    pidof cloudflared && kill -SIGINT `pidof cloudflared`
}
  • Enable and start the service
/etc/init.d/cloudflared enable
/etc/init.d/cloudflared start

Source:
https://hmolina.dev/p/cloudflared-tunnel-in-openwrt/