chore: add setStatus support to nodeUpdater (#11568)

Add support for the wgengine Status callback to nodeUpdater
This commit is contained in:
Spike Curtis
2024-01-17 09:06:34 +04:00
committed by GitHub
parent f6dc707511
commit 38d9ce5267
2 changed files with 208 additions and 0 deletions
+30
View File
@@ -4,11 +4,13 @@ import (
"context"
"net/netip"
"sync"
"time"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
"tailscale.com/tailcfg"
"tailscale.com/types/key"
"tailscale.com/wgengine"
"cdr.dev/slog"
"github.com/coder/coder/v2/coderd/database/dbtime"
@@ -32,6 +34,7 @@ type nodeUpdater struct {
derpForcedWebsockets map[int]string
endpoints []string
addresses []netip.Prefix
lastStatus time.Time
}
// updateLoop waits until the config is dirty and then calls the callback with the newest node.
@@ -146,3 +149,30 @@ func (u *nodeUpdater) setDERPForcedWebsocket(region int, reason string) {
u.Broadcast()
}
}
// setStatus handles the status callback from the wireguard engine to learn about new endpoints
// (e.g. discovered by STUN)
func (u *nodeUpdater) setStatus(s *wgengine.Status, err error) {
u.logger.Debug(context.Background(), "wireguard status", slog.F("status", s), slog.Error(err))
if err != nil {
return
}
u.L.Lock()
defer u.L.Unlock()
if s.AsOf.Before(u.lastStatus) {
// Don't process outdated status!
return
}
u.lastStatus = s.AsOf
endpoints := make([]string, len(s.LocalAddrs))
for i, ep := range s.LocalAddrs {
endpoints[i] = ep.Addr.String()
}
if slices.Equal(endpoints, u.endpoints) {
// No need to update the node if nothing changed!
return
}
u.endpoints = endpoints
u.dirty = true
u.Broadcast()
}