mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-24 02:31:18 -08:00
This moves HiddenNetworkDvice policy out of ipnlocal and into an optional ipnext. This adds a per-session notification hook that uses each watcher's effective policy snapshot to populate NodeGUIVisibility. A full visibility snapshot is sent on initialization and policy changes, and on subsequent peer updates, only changed peers are evaluated. The extension populates Notify.NodeGUIVisibility with the hidden peers among the nodes the Notify covers: all peers when it carries Policy (initially and on policy change), or the PeersChanged peers otherwise. Absent means visible, so a Policy-bearing Notify replaces the client's visibility state and a peer-change Notify updates only the changed peers. No NodeGUIVisibilityChanges/Removed fields and no per-session node state are needed: removed peers simply leave the client's peer set, and full netmap updates already resend every peer alongside SelfChange. When the feature isn't linked, the hook slice is empty and the fast path in notifyForSessionLocked is unchanged. When the policy isn't configured, the hook costs a snapshot lookup per session per Notify and sends nothing. Updates tailscale/corp#44104 Signed-off-by: kari <[email protected]> Signed-off-by: Nick Khyl <[email protected]>
136 lines
3.6 KiB
Go
136 lines
3.6 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
// Package guivisibility interprets the HiddenNetworkDevices policy for IPN
|
|
// bus watchers and populates [ipn.Notify.NodeGUIVisibility] accordingly.
|
|
package guivisibility
|
|
|
|
import (
|
|
"tailscale.com/feature"
|
|
"tailscale.com/ipn"
|
|
"tailscale.com/ipn/ipnext"
|
|
"tailscale.com/tailcfg"
|
|
"tailscale.com/types/logger"
|
|
"tailscale.com/util/mak"
|
|
"tailscale.com/util/syspolicy/pkey"
|
|
"tailscale.com/util/syspolicy/policyclient"
|
|
)
|
|
|
|
func init() {
|
|
if feature.Register("guivisibility") {
|
|
ipnext.RegisterExtension("guivisibility", newExtension)
|
|
}
|
|
}
|
|
|
|
func newExtension(logger.Logf, ipnext.SafeBackend) (ipnext.Extension, error) {
|
|
return &extension{}, nil
|
|
}
|
|
|
|
type extension struct {
|
|
host ipnext.Host
|
|
}
|
|
|
|
func (e *extension) Name() string { return "guivisibility" }
|
|
|
|
func (e *extension) Init(h ipnext.Host) error {
|
|
e.host = h
|
|
h.Hooks().MutateNotifyForSessionLocked.Add(e.mutateNotifyForSession)
|
|
return nil
|
|
}
|
|
|
|
func (e *extension) Shutdown() error { return nil }
|
|
|
|
// mutateNotifyForSession populates n.NodeGUIVisibility with the peers that
|
|
// sess's HiddenNetworkDevices policy hides: all of them if n carries a policy
|
|
// snapshot, or those among n.PeersChanged otherwise.
|
|
func (e *extension) mutateNotifyForSession(sess ipnext.WatchSession, n *ipn.Notify) {
|
|
// Watchers requesting NotifySysPolicyChanges receive a Policy snapshot during
|
|
// initialization, so checking Policy covers the initial visibility calculation.
|
|
// Subsequent changes are handled via PeersChanged.
|
|
//
|
|
// TODO(kari-ts): consider handling InitialStatus directly if GUI visibility needs
|
|
// to support watchers that don't receive policy notifications.
|
|
if n.Policy == nil && len(n.PeersChanged) == 0 {
|
|
return
|
|
}
|
|
hidden := hiddenNetworkDevicesFromPolicy(sess.Policy())
|
|
if !hidden.hasAny() {
|
|
return
|
|
}
|
|
nb := e.host.NodeBackend()
|
|
self := nb.Self()
|
|
if !self.Valid() {
|
|
return
|
|
}
|
|
var visibility map[tailcfg.StableNodeID]ipn.NodeGUIVisibility
|
|
updateVisibility := func(peer tailcfg.NodeView) {
|
|
if peerHiddenInGUI(peer, self, hidden) {
|
|
mak.Set(&visibility, peer.StableID(), ipn.NodeGUIVisibility{HideInGUI: true})
|
|
}
|
|
}
|
|
if n.Policy != nil {
|
|
for _, peer := range nb.Peers() {
|
|
updateVisibility(peer)
|
|
}
|
|
} else {
|
|
for _, peer := range n.PeersChanged {
|
|
updateVisibility(peer.View())
|
|
}
|
|
}
|
|
n.NodeGUIVisibility = visibility
|
|
}
|
|
|
|
func (h hiddenNetworkDevices) hasAny() bool {
|
|
return h.currentUser || h.otherUsers || h.taggedDevices
|
|
}
|
|
|
|
const (
|
|
hiddenNetworkDevicesCurrentUser = "current-user"
|
|
hiddenNetworkDevicesOtherUsers = "other-users"
|
|
hiddenNetworkDevicesTaggedDevices = "tagged-devices"
|
|
)
|
|
|
|
// hiddenNetworkDevices is the parsed HiddenNetworkDevices policy for a watch
|
|
// session. Its zero value means no devices are hidden.
|
|
type hiddenNetworkDevices struct {
|
|
currentUser bool
|
|
otherUsers bool
|
|
taggedDevices bool
|
|
}
|
|
|
|
func hiddenNetworkDevicesFromPolicy(snapshot *policyclient.PolicySnapshot) hiddenNetworkDevices {
|
|
values := policyclient.GetStringArrayFromSnapshot(
|
|
snapshot,
|
|
pkey.HiddenNetworkDevices,
|
|
)
|
|
|
|
var hidden hiddenNetworkDevices
|
|
for _, value := range values {
|
|
switch value {
|
|
case hiddenNetworkDevicesCurrentUser:
|
|
hidden.currentUser = true
|
|
case hiddenNetworkDevicesOtherUsers:
|
|
hidden.otherUsers = true
|
|
case hiddenNetworkDevicesTaggedDevices:
|
|
hidden.taggedDevices = true
|
|
}
|
|
}
|
|
return hidden
|
|
}
|
|
|
|
func peerHiddenInGUI(peer, self tailcfg.NodeView, hidden hiddenNetworkDevices) bool {
|
|
if peer.ID() == self.ID() {
|
|
return false
|
|
}
|
|
|
|
if peer.IsTagged() {
|
|
return hidden.taggedDevices
|
|
}
|
|
|
|
if peer.SharerOrUser() == self.User() {
|
|
return hidden.currentUser
|
|
}
|
|
|
|
return hidden.otherUsers
|
|
}
|