diff --git a/appc/conn25.go b/appc/conn25.go index 33a9552bb..0f08d2ac9 100644 --- a/appc/conn25.go +++ b/appc/conn25.go @@ -4,58 +4,17 @@ package appc import ( - "cmp" "fmt" - "slices" "strings" - "tailscale.com/ipn/ipnext" "tailscale.com/tailcfg" "tailscale.com/tailcfg/nodecap" "tailscale.com/types/appctype" "tailscale.com/types/dnstype" - "tailscale.com/util/set" ) const AppConnectorsExperimentalAttrName = "tailscale.com/app-connectors-experimental" -func isPeerEligibleConnector(peer tailcfg.NodeView) bool { - if !peer.Valid() || !peer.Hostinfo().Valid() { - return false - } - isConn, _ := peer.Hostinfo().AppConnector().Get() - return isConn -} - -func sortByPreference(ns []tailcfg.NodeView) { - // The ordering of the nodes is semantic (callers use the first node they can - // get a peer api url for). We don't (currently 2026-02-27) have any - // preference over which node is chosen as long as it's consistent. In the - // future we anticipate integrating with traffic steering. - slices.SortFunc(ns, func(a, b tailcfg.NodeView) int { - return cmp.Compare(a.ID(), b.ID()) - }) -} - -// PickConnector returns peers the backend knows about that match the app, in order of preference to use as -// a connector. -func PickConnector(nb ipnext.NodeBackend, app appctype.Conn25Attr) []tailcfg.NodeView { - appTagsSet := set.SetOf(app.Connectors) - matches := nb.AppendMatchingPeers(nil, func(n tailcfg.NodeView) bool { - if !isPeerEligibleConnector(n) { - return false - } - for _, t := range n.Tags().All() { - if appTagsSet.Contains(t) { - return true - } - } - return false - }) - sortByPreference(matches) - return matches -} - // DNSAddrScheme is the custom URI scheme used for conn25-managed split DNS // entries to determine the destination at query time rather than configuration // time. diff --git a/appc/conn25_test.go b/appc/conn25_test.go index 1cca32950..43123c28f 100644 --- a/appc/conn25_test.go +++ b/appc/conn25_test.go @@ -9,12 +9,10 @@ import ( "testing" "github.com/google/go-cmp/cmp" - "tailscale.com/ipn/ipnext" "tailscale.com/tailcfg" "tailscale.com/tailcfg/nodecap" "tailscale.com/types/appctype" "tailscale.com/types/dnstype" - "tailscale.com/types/opt" ) func TestAppDNSRoutes(t *testing.T) { @@ -153,157 +151,3 @@ func TestAppDNSRoutes(t *testing.T) { }) } } - -type testNodeBackend struct { - ipnext.NodeBackend - peers []tailcfg.NodeView -} - -func (nb *testNodeBackend) AppendMatchingPeers(base []tailcfg.NodeView, pred func(tailcfg.NodeView) bool) []tailcfg.NodeView { - for _, p := range nb.peers { - if pred(p) { - base = append(base, p) - } - } - return base -} - -func (nb *testNodeBackend) PeerHasPeerAPI(p tailcfg.NodeView) bool { - return true -} - -func TestPickConnector(t *testing.T) { - exampleApp := appctype.Conn25Attr{ - Name: "example", - Connectors: []string{"tag:example"}, - Domains: []string{"example.com"}, - } - - nvWithConnectorSet := func(id tailcfg.NodeID, isConnector bool, tags ...string) tailcfg.NodeView { - return (&tailcfg.Node{ - ID: id, - Tags: tags, - Hostinfo: (&tailcfg.Hostinfo{AppConnector: opt.NewBool(isConnector)}).View(), - }).View() - } - - nv := func(id tailcfg.NodeID, tags ...string) tailcfg.NodeView { - return nvWithConnectorSet(id, true, tags...) - } - - for _, tt := range []struct { - name string - candidates []tailcfg.NodeView - app appctype.Conn25Attr - want []tailcfg.NodeView - }{ - { - name: "empty-everything", - candidates: []tailcfg.NodeView{}, - app: appctype.Conn25Attr{}, - want: nil, - }, - { - name: "empty-candidates", - candidates: []tailcfg.NodeView{}, - app: exampleApp, - want: nil, - }, - { - name: "empty-app", - candidates: []tailcfg.NodeView{nv(1, "tag:example")}, - app: appctype.Conn25Attr{}, - want: nil, - }, - { - name: "one-matches", - candidates: []tailcfg.NodeView{nv(1, "tag:example")}, - app: exampleApp, - want: []tailcfg.NodeView{nv(1, "tag:example")}, - }, - { - name: "invalid-candidate", - candidates: []tailcfg.NodeView{ - {}, - nv(1, "tag:example"), - }, - app: exampleApp, - want: []tailcfg.NodeView{ - nv(1, "tag:example"), - }, - }, - { - name: "no-host-info", - candidates: []tailcfg.NodeView{ - (&tailcfg.Node{ - ID: 1, - Tags: []string{"tag:example"}, - }).View(), - nv(2, "tag:example"), - }, - app: exampleApp, - want: []tailcfg.NodeView{nv(2, "tag:example")}, - }, - { - name: "not-a-connector", - candidates: []tailcfg.NodeView{nvWithConnectorSet(1, false, "tag:example.com"), nv(2, "tag:example")}, - app: exampleApp, - want: []tailcfg.NodeView{nv(2, "tag:example")}, - }, - { - name: "without-matches", - candidates: []tailcfg.NodeView{nv(1, "tag:woo"), nv(2, "tag:example")}, - app: exampleApp, - want: []tailcfg.NodeView{nv(2, "tag:example")}, - }, - { - name: "multi-tags", - candidates: []tailcfg.NodeView{nv(1, "tag:woo", "tag:hoo"), nv(2, "tag:woo", "tag:example")}, - app: exampleApp, - want: []tailcfg.NodeView{nv(2, "tag:woo", "tag:example")}, - }, - { - name: "multi-matches", - candidates: []tailcfg.NodeView{nv(1, "tag:woo", "tag:hoo"), nv(2, "tag:woo", "tag:example"), nv(3, "tag:example1", "tag:example")}, - app: appctype.Conn25Attr{ - Name: "example2", - Connectors: []string{"tag:example1", "tag:example"}, - Domains: []string{"example.com"}, - }, - want: []tailcfg.NodeView{nv(2, "tag:woo", "tag:example"), nv(3, "tag:example1", "tag:example")}, - }, - { - name: "bit-of-everything", - candidates: []tailcfg.NodeView{ - nv(3, "tag:woo", "tag:hoo"), - {}, - nv(2, "tag:woo", "tag:example"), - nvWithConnectorSet(4, false, "tag:example"), - nv(1, "tag:example1", "tag:example"), - nv(7, "tag:example1", "tag:example"), - nvWithConnectorSet(5, false), - nv(6), - nvWithConnectorSet(8, false, "tag:example"), - nvWithConnectorSet(9, false), - nvWithConnectorSet(10, false), - }, - app: appctype.Conn25Attr{ - Name: "example2", - Connectors: []string{"tag:example1", "tag:example", "tag:example2"}, - Domains: []string{"example.com"}, - }, - want: []tailcfg.NodeView{ - nv(1, "tag:example1", "tag:example"), - nv(2, "tag:woo", "tag:example"), - nv(7, "tag:example1", "tag:example"), - }, - }, - } { - t.Run(tt.name, func(t *testing.T) { - got := PickConnector(&testNodeBackend{peers: tt.candidates}, tt.app) - if diff := cmp.Diff(tt.want, got); diff != "" { - t.Fatalf("PickConnectors (-want, +got):\n%s", diff) - } - }) - } -} diff --git a/cmd/tailscaled/depaware-min.txt b/cmd/tailscaled/depaware-min.txt index 147d79b6f..78f1c16aa 100644 --- a/cmd/tailscaled/depaware-min.txt +++ b/cmd/tailscaled/depaware-min.txt @@ -72,7 +72,7 @@ tailscale.com/cmd/tailscaled dependencies: (generated by github.com/tailscale/de tailscale.com/ipn from tailscale.com/cmd/tailscaled+ tailscale.com/ipn/conffile from tailscale.com/cmd/tailscaled+ tailscale.com/ipn/ipnauth from tailscale.com/ipn/ipnext+ - tailscale.com/ipn/ipnext from tailscale.com/ipn/ipnlocal+ + tailscale.com/ipn/ipnext from tailscale.com/ipn/ipnlocal tailscale.com/ipn/ipnlocal from tailscale.com/cmd/tailscaled+ tailscale.com/ipn/ipnlocal/netmapcache from tailscale.com/ipn/ipnlocal tailscale.com/ipn/ipnserver from tailscale.com/cmd/tailscaled diff --git a/cmd/tailscaled/depaware-minbox.txt b/cmd/tailscaled/depaware-minbox.txt index 7c035f101..e20a6e0ed 100644 --- a/cmd/tailscaled/depaware-minbox.txt +++ b/cmd/tailscaled/depaware-minbox.txt @@ -87,7 +87,7 @@ tailscale.com/cmd/tailscaled dependencies: (generated by github.com/tailscale/de tailscale.com/ipn from tailscale.com/cmd/tailscaled+ tailscale.com/ipn/conffile from tailscale.com/cmd/tailscaled+ tailscale.com/ipn/ipnauth from tailscale.com/ipn/ipnext+ - tailscale.com/ipn/ipnext from tailscale.com/ipn/ipnlocal+ + tailscale.com/ipn/ipnext from tailscale.com/ipn/ipnlocal tailscale.com/ipn/ipnlocal from tailscale.com/cmd/tailscaled+ tailscale.com/ipn/ipnlocal/netmapcache from tailscale.com/ipn/ipnlocal tailscale.com/ipn/ipnserver from tailscale.com/cmd/tailscaled diff --git a/feature/conn25/conn25.go b/feature/conn25/conn25.go index d06b31b83..7bf9a5763 100644 --- a/feature/conn25/conn25.go +++ b/feature/conn25/conn25.go @@ -9,6 +9,7 @@ package conn25 import ( "bytes" + "cmp" "container/list" "context" "encoding/json" @@ -1103,7 +1104,7 @@ func makePeerAPIReq(ctx context.Context, httpClient *http.Client, urlBase string func (e *extension) pickConnectorURLBase(app appctype.Conn25Attr) (tailcfg.NodeView, string) { nb := e.host.NodeBackend() - peers := appc.PickConnector(nb, app) + peers := PickConnector(nb, app) var urlBase string var conn tailcfg.NodeView for _, p := range peers { @@ -1670,3 +1671,40 @@ func (c *client) resendTransitIPMapping(transitIP netip.Addr) { c.logf("error enqueueing address assignment for resend: %v", err) } } + +func isPeerEligibleConnector(peer tailcfg.NodeView) bool { + if !peer.Valid() || !peer.Hostinfo().Valid() { + return false + } + isConn, _ := peer.Hostinfo().AppConnector().Get() + return isConn +} + +func sortByPreference(ns []tailcfg.NodeView) { + // The ordering of the nodes is semantic (callers use the first node they can + // get a peer api url for). We don't (currently 2026-02-27) have any + // preference over which node is chosen as long as it's consistent. In the + // future we anticipate integrating with traffic steering. + slices.SortFunc(ns, func(a, b tailcfg.NodeView) int { + return cmp.Compare(a.ID(), b.ID()) + }) +} + +// PickConnector returns peers the backend knows about that match the app, in order of preference to use as +// a connector. +func PickConnector(nb ipnext.NodeBackend, app appctype.Conn25Attr) []tailcfg.NodeView { + appTagsSet := set.SetOf(app.Connectors) + matches := nb.AppendMatchingPeers(nil, func(n tailcfg.NodeView) bool { + if !isPeerEligibleConnector(n) { + return false + } + for _, t := range n.Tags().All() { + if appTagsSet.Contains(t) { + return true + } + } + return false + }) + sortByPreference(matches) + return matches +} diff --git a/feature/conn25/conn25_test.go b/feature/conn25/conn25_test.go index 37bcf723b..6ccbefd52 100644 --- a/feature/conn25/conn25_test.go +++ b/feature/conn25/conn25_test.go @@ -3243,3 +3243,139 @@ func TestAddressExpiryDependsOnActiveFlows(t *testing.T) { }) } } + +func TestPickConnector(t *testing.T) { + exampleApp := appctype.Conn25Attr{ + Name: "example", + Connectors: []string{"tag:example"}, + Domains: []string{"example.com"}, + } + + nvWithConnectorSet := func(id tailcfg.NodeID, isConnector bool, tags ...string) tailcfg.NodeView { + return (&tailcfg.Node{ + ID: id, + Tags: tags, + Hostinfo: (&tailcfg.Hostinfo{AppConnector: opt.NewBool(isConnector)}).View(), + }).View() + } + + nv := func(id tailcfg.NodeID, tags ...string) tailcfg.NodeView { + return nvWithConnectorSet(id, true, tags...) + } + + for _, tt := range []struct { + name string + candidates []tailcfg.NodeView + app appctype.Conn25Attr + want []tailcfg.NodeView + }{ + { + name: "empty-everything", + candidates: []tailcfg.NodeView{}, + app: appctype.Conn25Attr{}, + want: nil, + }, + { + name: "empty-candidates", + candidates: []tailcfg.NodeView{}, + app: exampleApp, + want: nil, + }, + { + name: "empty-app", + candidates: []tailcfg.NodeView{nv(1, "tag:example")}, + app: appctype.Conn25Attr{}, + want: nil, + }, + { + name: "one-matches", + candidates: []tailcfg.NodeView{nv(1, "tag:example")}, + app: exampleApp, + want: []tailcfg.NodeView{nv(1, "tag:example")}, + }, + { + name: "invalid-candidate", + candidates: []tailcfg.NodeView{ + {}, + nv(1, "tag:example"), + }, + app: exampleApp, + want: []tailcfg.NodeView{ + nv(1, "tag:example"), + }, + }, + { + name: "no-host-info", + candidates: []tailcfg.NodeView{ + (&tailcfg.Node{ + ID: 1, + Tags: []string{"tag:example"}, + }).View(), + nv(2, "tag:example"), + }, + app: exampleApp, + want: []tailcfg.NodeView{nv(2, "tag:example")}, + }, + { + name: "not-a-connector", + candidates: []tailcfg.NodeView{nvWithConnectorSet(1, false, "tag:example.com"), nv(2, "tag:example")}, + app: exampleApp, + want: []tailcfg.NodeView{nv(2, "tag:example")}, + }, + { + name: "without-matches", + candidates: []tailcfg.NodeView{nv(1, "tag:woo"), nv(2, "tag:example")}, + app: exampleApp, + want: []tailcfg.NodeView{nv(2, "tag:example")}, + }, + { + name: "multi-tags", + candidates: []tailcfg.NodeView{nv(1, "tag:woo", "tag:hoo"), nv(2, "tag:woo", "tag:example")}, + app: exampleApp, + want: []tailcfg.NodeView{nv(2, "tag:woo", "tag:example")}, + }, + { + name: "multi-matches", + candidates: []tailcfg.NodeView{nv(1, "tag:woo", "tag:hoo"), nv(2, "tag:woo", "tag:example"), nv(3, "tag:example1", "tag:example")}, + app: appctype.Conn25Attr{ + Name: "example2", + Connectors: []string{"tag:example1", "tag:example"}, + Domains: []string{"example.com"}, + }, + want: []tailcfg.NodeView{nv(2, "tag:woo", "tag:example"), nv(3, "tag:example1", "tag:example")}, + }, + { + name: "bit-of-everything", + candidates: []tailcfg.NodeView{ + nv(3, "tag:woo", "tag:hoo"), + {}, + nv(2, "tag:woo", "tag:example"), + nvWithConnectorSet(4, false, "tag:example"), + nv(1, "tag:example1", "tag:example"), + nv(7, "tag:example1", "tag:example"), + nvWithConnectorSet(5, false), + nv(6), + nvWithConnectorSet(8, false, "tag:example"), + nvWithConnectorSet(9, false), + nvWithConnectorSet(10, false), + }, + app: appctype.Conn25Attr{ + Name: "example2", + Connectors: []string{"tag:example1", "tag:example", "tag:example2"}, + Domains: []string{"example.com"}, + }, + want: []tailcfg.NodeView{ + nv(1, "tag:example1", "tag:example"), + nv(2, "tag:woo", "tag:example"), + nv(7, "tag:example1", "tag:example"), + }, + }, + } { + t.Run(tt.name, func(t *testing.T) { + got := PickConnector(&testNodeBackend{peers: tt.candidates}, tt.app) + if diff := cmp.Diff(tt.want, got); diff != "" { + t.Fatalf("PickConnectors (-want, +got):\n%s", diff) + } + }) + } +}