yichya / openwrt-cloudflared

Run argo tunnel client on your router
33 stars 6 forks source link

cloudflared configuration /etc/init.d/cloudflared #3

Open jirijanata opened 2 years ago

jirijanata commented 2 years ago

Hi, first of all, thank you very much for maintaining this OpenWrt package. The current ipk package 2022.3.4 has some modified /etc/init.d/cloudflared configuration file, which currently doesn't work properly (OpenWrt 22.03 r19235).

My cloudflared tunnel is working fine on the OpenWrt with this command /usr/bin/cloudflared tunnel --config /etc/cloudflared/config.yml run which is also used in the init.d config file. Unfortunately command /etc/init.d/cloudflare.d start does nothing and the status of the service is always inactive.

I also deleted the parameter with cert.pem from the init.d config, because I use credentials store inside the json file.

I also cannot find any logs, what is failing and which command was passed to the cloudflared.

Thank you very much!

yichya commented 2 years ago

try command logread and see if there's anything useful there

jirijanata commented 2 years ago

Thank you very much for your quick reply. Unfortunately there was nothing useful at all. No error, nothing.

I also tried modified version of the init.d file from BH4EHN repo and that worked quite good. Status (inactive, running), start and stop works also fine. The cloudflared init.d, which is from Cloudflare directly has some nice if function (service is starting, service is already running, but most of them did't worked of of the box and actually I don't need them on OpenWrt.

#!/bin/sh /etc/rc.common

USE_PROCD=1
START=95
STOP=01

name="cloudflared"
cmd="/usr/bin/cloudflared --pidfile /var/run/$name.pid --no-autoupdate --config /etc/cloudflared/config.yml tunnel run"

start_service() {
    procd_open_instance
    procd_set_param command $cmd
    procd_set_param stdout 1
    procd_set_param stderr 1
    procd_set_param respawn ${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5}
    procd_set_param user
    procd_close_instance
}

This is the init.d configuration, which is shipped with the 2022.3.4 ipk. It's quite complex and some parts are not needed, like cert.pem. The credentials should be stored in the json file and not in the cert.pem, because the json config is only for this one specific tunnel.

#!/bin/sh /etc/rc.common
# Copyright (C) 2021 Tianling Shen <cnsztl@immortalwrt.org>

USE_PROCD=1
START=99

CONF="cloudflared"
PROG="/usr/bin/cloudflared"

append_param_arg() {
        local value
        config_get value "config" "$1" $2
        [ -n "$value" ] && procd_append_param command "--$1" "$value"
}

start_service() {
        config_load "$CONF"

        local enabled
        config_get_bool enabled "config" "enabled"
        [ "$enabled" -eq "1" ] || exit 1

        procd_open_instance "$CONF"
        procd_set_param command "$PROG" "tunnel"
        procd_append_param command "--no-autoupdate"

        append_param_arg "config" "/etc/cloudflared/config.yml"
        append_param_arg "origincert" "/etc/cloudflared/cert.pem"
        append_param_arg "region"
        append_param_arg "loglevel"
        append_param_arg "logfile"

        procd_append_param command "run"

        procd_set_param respawn
        procd_set_param stderr 1

        procd_close_instance
}

reload_service() {
        stop
        start
}

service_triggers() {
        procd_add_reload_trigger "$CONF"
}