Created
September 9, 2026 18:03
-
-
Save wallentx/5cc85d0ef1a0711e2fcd4cc9f252d00f to your computer and use it in GitHub Desktop.
Tailscale panel script for wayle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| """Tailscale status and explicit menu actions for the desktop bar.""" | |
| import html | |
| import json | |
| from pathlib import Path | |
| import subprocess | |
| import sys | |
| def run(*args): | |
| return subprocess.run(args, check=True, text=True, capture_output=True, timeout=20).stdout | |
| def state(): | |
| return json.loads(run('tailscale', 'status', '--json')), json.loads(run('tailscale', 'debug', 'prefs')) | |
| def lan_rule(): | |
| return any(line.startswith('2500:') and 'to 192.168.1.0/24' in line and 'lookup main' in line | |
| for line in run('ip', 'rule', 'show').splitlines()) | |
| def status(): | |
| try: | |
| data, prefs = state() | |
| connected = data.get('BackendState') == 'Running' | |
| node = prefs.get('ExitNodeIP') or prefs.get('ExitNodeID') | |
| css = 'exitnode' if connected and node else 'connected' if connected else 'disconnected' | |
| address = next(iter(data.get('Self', {}).get('TailscaleIPs', [])), '') | |
| label = 'Exit node' if connected and node else 'Connected' if connected else 'Disconnected' | |
| print(json.dumps({'text': label, 'class': css, | |
| 'tooltip': html.escape(f'Tailscale: {label}\n{address}\nClick for connection, exit node and LAN controls')})) | |
| except (subprocess.SubprocessError, ValueError) as error: | |
| print(json.dumps({'text': 'Unavailable', 'class': 'disconnected', 'tooltip': html.escape(str(error))})) | |
| def choose(title, labels): | |
| result = subprocess.run(['rofi', '-theme', str(Path.home() / '.config/wayle/menus.rasi'), '-dmenu', '-i', '-no-custom', '-format', 'i', '-p', title], | |
| input='\n'.join(labels), text=True, capture_output=True) | |
| return int(result.stdout.strip()) if result.returncode == 0 and result.stdout.strip().isdigit() else None | |
| def menu(): | |
| data, prefs = state() | |
| running = data.get('BackendState') == 'Running' | |
| route = lan_rule() | |
| flag = lambda value: 'on' if value else 'off' | |
| labels = ['Disconnect' if running else 'Connect', | |
| f'Accept subnet routes: {flag(prefs.get("RouteAll"))}', | |
| f'Allow LAN with exit node: {flag(prefs.get("ExitNodeAllowLANAccess"))}', | |
| f'Prefer local 192.168.1.0/24 route: {flag(route)}', 'Choose exit node'] | |
| selection = choose('Tailscale', labels) | |
| if selection == 0: | |
| run('tailscale', 'down' if running else 'up') | |
| elif selection == 1: | |
| run('tailscale', 'set', '--accept-routes=' + str(not prefs.get('RouteAll', False)).lower()) | |
| elif selection == 2: | |
| run('tailscale', 'set', '--exit-node-allow-lan-access=' + str(not prefs.get('ExitNodeAllowLANAccess', False)).lower()) | |
| elif selection == 3: | |
| # Use the same existing sudo policy and exact rule as the previous panel. | |
| run('sudo', '-n', 'ip', 'rule', 'del' if route else 'add', 'to', '192.168.1.0/24', | |
| 'priority', '2500', 'lookup', 'main') | |
| elif selection == 4: | |
| peers = sorted((peer for peer in data.get('Peer', {}).values() | |
| if peer.get('ExitNodeOption') and peer.get('TailscaleIPs')), | |
| key=lambda peer: peer.get('HostName', '')) | |
| names = ['None'] + [f'{p.get("HostName", p["TailscaleIPs"][0])} ({"online" if p.get("Online") else "offline"})' for p in peers] | |
| selected = choose('Exit node', names) | |
| if selected is not None and 0 <= selected < len(names): | |
| address = '' if selected == 0 else peers[selected - 1]['TailscaleIPs'][0] | |
| run('tailscale', 'set', '--exit-node=' + address) | |
| if __name__ == '__main__': | |
| try: | |
| if sys.argv[1:] == ['menu']: | |
| menu() | |
| else: | |
| status() | |
| except (subprocess.SubprocessError, ValueError) as error: | |
| subprocess.run(['notify-send', 'Tailscale action failed', html.escape(str(error))]) | |
| sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment