Widget for my desktop bar - tiling window manager

I’d like to add tailscale status to my awesomewm system bar. Although I’m guessing it’s the same routine if using xmonad, i3wm, dwm, or whatever

I need to determine is-active and which exit node I’m connected to, so with a bit of string wrangling I can display:

TS: Off
TS: hostname

Is there a more efficient way than running tailscale status every 10 seconds, I tried to poke around in /sys/class/net/tailscale0 but I cant see where I can determine the exit node, perhaps there is also a dbus event?

So from stracing tailscale status, it initially seems to sends something to the socket /var/run/tailscale/tailscaled.sock and gets back a payload?

Poking around in github code I can issue a http query to the socket

echo -e “GET /localapi/v0/status HTTP/1.0\r\n” | sudo nc -U /run/tailscale/tailscaled.sock

Okay, great, now I can code this up in awesomewm/lua

Just going to leave a few more points for others.

< 			"ExitNode": true,
---
> 			"ExitNode": false,

This is property looking for to find the Peer that is acting as exit node. Be careful though as tailscale persists exit node settings, which confused me, so you have to be aware of --reset on tailscale up. Also need to check BackendState for Running

Decide to just do this in shell so compatible if I switch WMs. You can probably optimize this I imagine, jq could probably test the first condition rather than two seperate jq invocations…

#!/bin/bash

DATA=$(echo -e "GET /localapi/v0/status HTTP/1.0\r\n" | nc -U /run/tailscale/tailscaled.sock | tail -n +4)

BACKENDSTATE=$(echo "$DATA" | jq -r .BackendState)

ACTIVENODE=$(echo "$DATA" | jq -r '.Peer[] | select(.ExitNode == true) | .HostName')

if [ "$BACKENDSTATE" = "Running" -a -n "$ACTIVENODE" ]; then
  echo "TS: $ACTIVENODE"
else
  echo "TS: off"
fi

This is really neat! You can probably also make it a bit more clean with the use of curl --unix-socket /run/tailscale/tailscaled.sock http://tailscale/localapi/v0/status instead of having to assume details about how the HTTP protocol works with tail like that. I may end up using something like that for my own tiling window manager setup.

1 Like

@within much cleaner using curl, will adopt thanks

1 Like