Side Toggle Switch for Tailscale Exit Node

I’ve made an additional update. The newest command will find the location of places needing changed in the /usr/bin/gl_tailscale script and make the appropriate changes without overwriting the entire /usr/bin/gl_tailscale file. The purpose of this change is to make this command more likely to work on other gl-inet devices and continue to work in future firmware updates.

cp /usr/bin/gl_tailscale /usr/bin/gl_tailscale.bak && \
mkdir -p /etc/tailscale && \
sed -i '/^[[:space:]]*exit_node_ip=$(uci -q get tailscale\.settings\.exit_node_ip)/ {
    s/.*/exit_node_ip=$(uci -q get tailscale.settings.exit_node_ip)\n    if [ -n "$exit_node_ip" ]; then\n        echo "$exit_node_ip" > "\/etc\/tailscale\/last_exit_node_ip"\n    fi/
}' /usr/bin/gl_tailscale && \
sed -i '/^[[:space:]]*switch_status=$(get_switch_status)/,/^[[:space:]]*exit_node_ip=$(uci -q get tailscale\.settings\.exit_node_ip)/ {
    /switch_status=$(get_switch_status)/!d
    /exit_node_ip=$(uci -q get tailscale\.settings\.exit_node_ip)/!d
    s/.*/switch_status=$(get_switch_status)\n    if [ "$switch_status" = "on" ]; then\n        if [ -f "\/etc\/tailscale\/last_exit_node_ip" ]; then\n            exit_node_ip=$(cat "\/etc\/tailscale\/last_exit_node_ip")\n        else\n            exit_node_ip=""\n        fi\n    else\n        exit_node_ip=""\n    fi/
}' /usr/bin/gl_tailscale && \
if ! grep -q "get_switch_status()" /usr/bin/gl_tailscale; then
    sed -i '/action="$1"/a \
get_switch_status() \{\
    status=$(get_switch_button_status 2>/dev/null || echo "no support")\
    if [ "$status" = "no support" ] || [ "$status" = "" ]; then\
        echo "no_support"\
    elif [ "$status" = "on" ]; then\
        echo "on"\
    else\
        echo "off"\
    fi\
\}' /usr/bin/gl_tailscale
fi && \
cat << 'EOF' > /usr/bin/gl_tailscale_switch_monitor
#!/bin/sh

. /lib/functions/gl_util.sh

STATE_FILE="/tmp/gl_tailscale_switch_status"
POLL_INTERVAL=5
LOG_FILE="/tmp/gl_tailscale_switch_monitor.log"

log_message() {
    echo "$(date '+%Y-%m-%d %H:%M:%S'): $1" >> "$LOG_FILE"
}

get_switch_status() {
    status=$(get_switch_button_status 2>/dev/null || echo "no support")
    if [ "$status" = "no support" ] || [ "$status" = "" ]; then
        echo "no_support"
    elif [ "$status" = "on" ]; then
        echo "on"
    else
        echo "off"
    fi
}

if [ ! -f "$STATE_FILE" ]; then
    echo "unknown" > "$STATE_FILE"
    log_message "Initialized state file with 'unknown' status"
fi

while true; do
    current_status=$(get_switch_status)
    last_status=$(cat "$STATE_FILE")
    if [ "$current_status" != "$last_status" ]; then
        log_message "Switch status changed from '$last_status' to '$current_status'"
        echo "$current_status" > "$STATE_FILE"
        log_message "Executing /usr/bin/gl_tailscale restart"
        /usr/bin/gl_tailscale restart
        if [ $? -eq 0 ]; then
            log_message "Successfully executed gl_tailscale restart"
        else
            log_message "Failed to execute gl_tailscale restart"
        fi
    fi
    sleep "$POLL_INTERVAL"
done
EOF
chmod +x /usr/bin/gl_tailscale_switch_monitor && \
grep -q "/usr/bin/gl_tailscale_switch_monitor" /etc/rc.local || \
    sed -i '/exit 0/i \/usr/bin\/gl_tailscale_switch_monitor \&' /etc/rc.local && \
sleep 2 && /usr/bin/gl_tailscale_switch_monitor &