Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/trayscale: migrate from manual polling to IPNBusWatcher API #62

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 27 additions & 37 deletions cmd/trayscale/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"net/netip"
"os"
"strconv"
"time"

"deedles.dev/mk"
Expand All @@ -21,7 +20,7 @@ import (
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
"golang.org/x/exp/slog"
"tailscale.com/ipn/ipnstate"
"tailscale.com/tailcfg"
"tailscale.com/types/key"
)

Expand Down Expand Up @@ -79,34 +78,34 @@ func (a *App) showAbout() {
a.app.AddWindow(&dialog.Window.Window)
}

func (a *App) updatePeerPage(page *peerPage, peer *ipnstate.PeerStatus, status tsutil.Status) {
func (a *App) updatePeerPage(page *peerPage, peer *tailcfg.Node, status tsutil.Status) {
page.page.SetIconName(peerIcon(peer))
page.page.SetTitle(peerName(status, peer, page.self))

page.container.SetTitle(peer.HostName)
page.container.SetDescription(peer.DNSName)
page.container.SetTitle(peer.Hostinfo.Hostname())
page.container.SetDescription(peer.Name)

slices.SortFunc(peer.TailscaleIPs, netip.Addr.Less)
page.addrRows.Update(peer.TailscaleIPs)
slices.SortFunc(peer.Addresses, netipPrefixLess)
page.addrRows.Update(peer.Addresses)

page.container.OptionsGroup.SetVisible(page.self)
if page.self {
page.container.AdvertiseExitNodeSwitch.SetState(status.Prefs.AdvertisesExitNode())
page.container.AdvertiseExitNodeSwitch.SetActive(status.Prefs.AdvertisesExitNode())
page.container.AllowLANAccessSwitch.SetState(status.Prefs.ExitNodeAllowLANAccess)
page.container.AllowLANAccessSwitch.SetActive(status.Prefs.ExitNodeAllowLANAccess)
page.container.AllowLANAccessSwitch.SetState(status.Prefs.ExitNodeAllowLANAccess())
page.container.AllowLANAccessSwitch.SetActive(status.Prefs.ExitNodeAllowLANAccess())
}

page.container.AdvertiseRouteButton.SetVisible(page.self)

switch {
case page.self:
page.routes = status.Prefs.AdvertiseRoutes
page.routes = status.Prefs.AdvertiseRoutes().AppendTo(page.routes[:0])
case peer.PrimaryRoutes != nil:
page.routes = peer.PrimaryRoutes.AsSlice()
page.routes = peer.PrimaryRoutes
}
page.routes = xslices.Filter(page.routes, func(p netip.Prefix) bool { return p.Bits() != 0 })
slices.SortFunc(page.routes, func(p1, p2 netip.Prefix) bool { return p1.Addr().Less(p2.Addr()) || p1.Bits() < p2.Bits() })
slices.SortFunc(page.routes, netipPrefixLess)
if len(page.routes) == 0 {
page.routes = append(page.routes, netip.Prefix{})
}
Expand All @@ -122,17 +121,17 @@ func (a *App) updatePeerPage(page *peerPage, peer *ipnstate.PeerStatus, status t
page.container.NetCheckGroup.SetVisible(page.self)

page.container.MiscGroup.SetVisible(!page.self)
page.container.ExitNodeRow.SetVisible(peer.ExitNodeOption)
page.container.ExitNodeSwitch.SetState(peer.ExitNode)
page.container.ExitNodeSwitch.SetActive(peer.ExitNode)
page.container.RxBytes.SetText(strconv.FormatInt(peer.RxBytes, 10))
page.container.TxBytes.SetText(strconv.FormatInt(peer.TxBytes, 10))
//page.container.ExitNodeRow.SetVisible(peer.ExitNodeOption)
//page.container.ExitNodeSwitch.SetState(peer.ExitNode)
//page.container.ExitNodeSwitch.SetActive(peer.ExitNode)
//page.container.RxBytes.SetText(strconv.FormatInt(peer.RxBytes, 10))
//page.container.TxBytes.SetText(strconv.FormatInt(peer.TxBytes, 10))
page.container.Created.SetText(formatTime(peer.Created))
page.container.LastSeen.SetText(formatTime(peer.LastSeen))
page.container.LastSeenRow.SetVisible(!peer.Online)
page.container.LastWrite.SetText(formatTime(peer.LastWrite))
page.container.LastHandshake.SetText(formatTime(peer.LastHandshake))
page.container.Online.SetFromIconName(boolIcon(peer.Online))
page.container.LastSeen.SetText(formatTime(*peer.LastSeen))
page.container.LastSeenRow.SetVisible((peer.Online == nil) || !*peer.Online)
//page.container.LastWrite.SetText(formatTime(peer.LastWrite))
//page.container.LastHandshake.SetText(formatTime(peer.LastHandshake))
page.container.Online.SetFromIconName(optBoolIcon(pbool(peer.Online)))
}

func (a *App) notify(status bool) {
Expand Down Expand Up @@ -164,7 +163,7 @@ func (a *App) updatePeers(status tsutil.Status) {

w := a.win.PeersStack

var peerMap map[key.NodePublic]*ipnstate.PeerStatus
var peerMap map[key.NodePublic]*tailcfg.Node
var peers []key.NodePublic

if status.Online() {
Expand Down Expand Up @@ -304,7 +303,6 @@ func (a *App) startTS(ctx context.Context) error {
if err != nil {
return err
}
a.poller.Poll() <- struct{}{}
return nil
}

Expand All @@ -313,7 +311,6 @@ func (a *App) stopTS(ctx context.Context) error {
if err != nil {
return err
}
a.poller.Poll() <- struct{}{}
return nil
}

Expand Down Expand Up @@ -382,7 +379,6 @@ func (a *App) onAppActivate(ctx context.Context) {
a.win = nil
return false
})
a.poller.Poll() <- struct{}{}
a.win.Show()
}

Expand Down Expand Up @@ -452,7 +448,7 @@ type peerPage struct {
self bool
routes []netip.Prefix

addrRows rowManager[netip.Addr]
addrRows rowManager[netip.Prefix]
routeRows rowManager[enum[netip.Prefix]]
}

Expand Down Expand Up @@ -496,15 +492,15 @@ func (row *routeRow) Widget() gtk.Widgetter {
return row.w
}

func (a *App) newPeerPage(status tsutil.Status, peer *ipnstate.PeerStatus) *peerPage {
func (a *App) newPeerPage(status tsutil.Status, peer *tailcfg.Node) *peerPage {
page := peerPage{
container: NewPeerPage(),
}

page.addrRows.Parent = page.container.IPGroup
page.addrRows.New = func(ip netip.Addr) row[netip.Addr] {
page.addrRows.New = func(ip netip.Prefix) row[netip.Prefix] {
row := addrRow{
ip: ip,
ip: ip.Addr(),

w: adw.NewActionRow(),
c: gtk.NewButtonFromIconName("edit-copy-symbolic"),
Expand Down Expand Up @@ -551,7 +547,6 @@ func (a *App) newPeerPage(status tsutil.Status, peer *ipnstate.PeerStatus) *peer
slog.Error("advertise routes", "err", err)
return
}
a.poller.Poll() <- struct{}{}
})

return &row
Expand All @@ -570,7 +565,7 @@ func (a *App) newPeerPage(status tsutil.Status, peer *ipnstate.PeerStatus) *peer
}
}

var node *ipnstate.PeerStatus
var node *tailcfg.Node
if s {
node = peer
}
Expand All @@ -580,7 +575,6 @@ func (a *App) newPeerPage(status tsutil.Status, peer *ipnstate.PeerStatus) *peer
page.container.ExitNodeSwitch.SetActive(!s)
return true
}
a.poller.Poll() <- struct{}{}
return true
})

Expand All @@ -603,7 +597,6 @@ func (a *App) newPeerPage(status tsutil.Status, peer *ipnstate.PeerStatus) *peer
page.container.AdvertiseExitNodeSwitch.SetActive(!s)
return true
}
a.poller.Poll() <- struct{}{}
return true
})

Expand All @@ -618,7 +611,6 @@ func (a *App) newPeerPage(status tsutil.Status, peer *ipnstate.PeerStatus) *peer
page.container.AllowLANAccessSwitch.SetActive(!s)
return true
}
a.poller.Poll() <- struct{}{}
return true
})

Expand All @@ -644,8 +636,6 @@ func (a *App) newPeerPage(status tsutil.Status, peer *ipnstate.PeerStatus) *peer
slog.Error("advertise routes", "err", err)
return
}

a.poller.Poll() <- struct{}{}
})
})

Expand Down
27 changes: 23 additions & 4 deletions cmd/trayscale/trayscale.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import (
"context"
_ "embed"
"io"
"net/netip"
"os"
"os/signal"
"strconv"
"strings"
"time"

"deedles.dev/trayscale"
"deedles.dev/trayscale/internal/tsutil"
"tailscale.com/ipn/ipnstate"
"tailscale.com/tailcfg"
"tailscale.com/types/opt"
)

Expand Down Expand Up @@ -69,9 +71,13 @@ func readAssetString(file string) string {
return str.String()
}

func peerName(status tsutil.Status, peer *ipnstate.PeerStatus, self bool) string {
func peerName(status tsutil.Status, peer *tailcfg.Node, self bool) string {
if peer.ComputedName == "" {
peer.InitDisplayNames("")
}

const maxNameLength = 30
name := tsutil.DNSOrQuoteHostname(status.Status, peer)
name := peer.DisplayName(true)
if len(name) > maxNameLength {
name = name[:maxNameLength-3] + "..."
}
Expand All @@ -88,7 +94,7 @@ func peerName(status tsutil.Status, peer *ipnstate.PeerStatus, self bool) string
return name
}

func peerIcon(peer *ipnstate.PeerStatus) string {
func peerIcon(peer *tailcfg.Node) string {
if peer.ExitNode {
return "network-workgroup-symbolic"
}
Expand All @@ -114,6 +120,19 @@ func optBoolIcon(v opt.Bool) string {
return boolIcon(b)
}

func netipPrefixLess(p1, p2 netip.Prefix) bool {
return p1.Addr().Less(p2.Addr()) || p1.Bits() < p2.Bits()
}

func pbool(v *bool) opt.Bool {
if v == nil {
return ""
}

s := strconv.FormatBool(*v)
return opt.Bool(s)
}

func main() {
pprof()

Expand Down
9 changes: 7 additions & 2 deletions internal/tsutil/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ func (c *Client) run(ctx context.Context, args ...string) (string, error) {
return out.String(), err
}

// Watch returns an IPNBusWatcher to get notifications from the Tailscale daemon.
func (c *Client) Watch(ctx context.Context) (*tailscale.IPNBusWatcher, error) {
return localClient.WatchIPNBus(ctx, ipn.NotifyInitialState|ipn.NotifyInitialPrefs)
}

// Status returns the status of the connection to the Tailscale
// network. If the network is not currently connected, it returns
// nil, nil.
Expand Down Expand Up @@ -81,7 +86,7 @@ func (c *Client) Stop(ctx context.Context) error {

// ExitNode uses the specified peer as an exit node, or unsets
// an existing exit node if peer is nil.
func (c *Client) ExitNode(ctx context.Context, peer *ipnstate.PeerStatus) error {
func (c *Client) ExitNode(ctx context.Context, peer *tailcfg.Node) error {
if peer == nil {
var prefs ipn.Prefs
prefs.ClearExitNode()
Expand All @@ -102,7 +107,7 @@ func (c *Client) ExitNode(ctx context.Context, peer *ipnstate.PeerStatus) error
}

var prefs ipn.Prefs
prefs.SetExitNodeIP(peer.TailscaleIPs[0].String(), status)
prefs.SetExitNodeIP(peer.Addresses[0].String(), status)
_, err = localClient.EditPrefs(ctx, &ipn.MaskedPrefs{
Prefs: prefs,
ExitNodeIDSet: true,
Expand Down
Loading