Files
luci-app-tailscale/root/etc/init.d/tailscale
2024-01-28 14:11:45 +08:00

90 lines
1.8 KiB
Bash
Executable File

#!/bin/sh /etc/rc.common
START=90
USE_PROCD=1
PROG=/usr/sbin/tailscaled
CONFIG_PATH=/var/lib/tailscale
service_triggers() {
procd_add_reload_trigger "tailscale"
procd_add_interface_trigger "interface.*.up" wan /etc/init.d/tailscale reload
}
section_enabled() {
config_get_bool enabled "$1" 'enabled' 0
[ $enabled -gt 0 ]
}
start_instance() {
local cfg="$1"
local port std_err std_out config_path
local ARGS=""
if ! section_enabled "$cfg"; then
echo "disabled in config"
return 1
fi
[ -d /etc/tailscale ] || mkdir -p /etc/tailscale
config_path=/etc/tailscale
config_get_bool std_out $cfg log_stdout 1
config_get_bool std_err $cfg log_stderr 1
config_get port $cfg port 41641
config_get state_file $cfg state_file /etc/tailscale/tailscaled.state
config_get fw_mode $cfg fw_mode nftables
/usr/sbin/tailscaled --cleanup
# Remove existing link or folder
rm -rf $CONFIG_PATH
# Create link from CONFIG_PATH to config_path
if [ -n "$config_path" -a "$config_path" != $CONFIG_PATH ]; then
if [ ! -d "$config_path" ]; then
echo "Tailscale config_path does not exist: $config_path"
return
fi
ln -s $config_path $CONFIG_PATH
fi
[ -n "$port" ] && ARGS="$ARGS --port $port"
[ -n "$state_file" ] && ARGS="$ARGS --state $state_file"
procd_open_instance
procd_set_param command $PROG $ARGS
procd_set_param env TS_DEBUG_FIREWALL_MODE="$fw_mode"
procd_set_param respawn
procd_set_param stdout "$std_out"
procd_set_param stderr "$std_err"
procd_close_instance
}
start_service() {
config_load 'tailscale'
config_foreach start_instance 'settings'
}
stop_instance() {
local cfg="$1"
/usr/sbin/tailscaled --cleanup
# Remove existing link or folder
rm -rf $CONFIG_PATH/*.log*
rm -rf $CONFIG_PATH
}
stop_service() {
config_load 'tailscale'
config_foreach stop_instance 'settings'
}
reload_service() {
stop
start
}