diff --git a/ipn/ipnlocal/profiles.go b/ipn/ipnlocal/profiles.go index e82945f2f..1467f9b46 100644 --- a/ipn/ipnlocal/profiles.go +++ b/ipn/ipnlocal/profiles.go @@ -36,6 +36,8 @@ type profileManager struct { knownProfiles map[ipn.ProfileID]*ipn.LoginProfile // always non-nil currentProfile *ipn.LoginProfile // always non-nil prefs ipn.PrefsView // always Valid. + + currentRoutes *ipn.RouteInfo } func (pm *profileManager) dlogf(format string, args ...any) { @@ -49,16 +51,35 @@ func (pm *profileManager) WriteState(id ipn.StateKey, val []byte) error { return ipn.WriteState(pm.store, id, val) } -func (pm *profileManager) WriteStateForCurrentProfile(id ipn.StateKey, val []byte) error { +func (pm *profileManager) WriteRoutesForCurrentProfile() error { + routeInfoInBytes, err := json.Marshal(*pm.currentRoutes) + if err != nil { + return err + } + + return pm.writeStateForCurrentProfile("_routes", routeInfoInBytes) +} + +func (pm *profileManager) ReadRoutesForCurrentProfile() error { + routeInfoInBytes, err := pm.readStateForCurrentProfile("_routes") + if err != nil { + return err + } + + return json.Unmarshal(routeInfoInBytes, pm.currentRoutes) +} + +func (pm *profileManager) writeStateForCurrentProfile(id ipn.StateKey, val []byte) error { var currentProfileKey ipn.StateKey // TODO(fran) maybe we should error if the current profile is nil? if pm.currentProfile != nil { currentProfileKey = pm.currentProfile.Key } + return ipn.WriteState(pm.store, currentProfileKey+"||"+id, val) } -func (pm *profileManager) ReadStateForCurrentProfile(id ipn.StateKey) ([]byte, error) { +func (pm *profileManager) readStateForCurrentProfile(id ipn.StateKey) ([]byte, error) { var currentProfileKey ipn.StateKey // TODO(fran) maybe we should error if the current profile is nil? if pm.currentProfile != nil { diff --git a/ipn/ipnlocal/profiles_test.go b/ipn/ipnlocal/profiles_test.go index 815b46af5..bae081d59 100644 --- a/ipn/ipnlocal/profiles_test.go +++ b/ipn/ipnlocal/profiles_test.go @@ -5,9 +5,11 @@ package ipnlocal import ( "fmt" + "net/netip" "os/user" "strconv" "testing" + "time" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" @@ -585,11 +587,11 @@ func TestFran(t *testing.T) { t.Fatal(err) } - err = pm.WriteStateForCurrentProfile("x", []byte("hello")) + err = pm.writeStateForCurrentProfile("x", []byte("hello")) if err != nil { t.Fatal(err) } - bs, err := pm.ReadStateForCurrentProfile("x") + bs, err := pm.readStateForCurrentProfile("x") if err != nil { t.Fatal(err) } @@ -623,11 +625,11 @@ func TestFran(t *testing.T) { pm.SetCurrentUserID("user1") newProfile(t, "user1") - err = pm.WriteStateForCurrentProfile("x", []byte("hello user1")) + err = pm.writeStateForCurrentProfile("x", []byte("hello user1")) if err != nil { t.Fatal(err) } - bs, err = pm.ReadStateForCurrentProfile("x") + bs, err = pm.readStateForCurrentProfile("x") if err != nil { t.Fatal(err) } @@ -638,7 +640,7 @@ func TestFran(t *testing.T) { } pm.SetCurrentUserID("") - bs, err = pm.ReadStateForCurrentProfile("x") + bs, err = pm.readStateForCurrentProfile("x") if err != nil { t.Fatal(err) } @@ -648,3 +650,69 @@ func TestFran(t *testing.T) { t.Fatal("oh") } } + +func TestKevin(t *testing.T) { + store := new(mem.Store) + + pm, err := newProfileManagerWithGOOS(store, logger.Discard, "linux") + if err != nil { + t.Fatal(err) + } + + var id int + newProfile := func(t *testing.T, loginName string) ipn.PrefsView { + id++ + t.Helper() + pm.NewProfile() + p := pm.CurrentPrefs().AsStruct() + p.Persist = &persist.Persist{ + NodeID: tailcfg.StableNodeID(fmt.Sprint(id)), + PrivateNodeKey: key.NewNode(), + UserProfile: tailcfg.UserProfile{ + ID: tailcfg.UserID(id), + LoginName: loginName, + }, + } + if err := pm.SetPrefs(p.View(), ipn.NetworkProfile{}); err != nil { + t.Fatal(err) + } + return p.View() + } + + pm.SetCurrentUserID("user1") + newProfile(t, "user1") + + Prefix_1 := netip.MustParsePrefix("1.2.3.0/24") + Prefix_2 := netip.MustParsePrefix("2.3.4.5/32") + Prefix_3 := netip.MustParsePrefix("192.16.0.0/16") + + fakeRouteInfo1 := ipn.RouteInfo{ + Local: []netip.Prefix{Prefix_2}, + Corp: []netip.Prefix{Prefix_1}, + Discovered: map[string]ipn.DatedRoute{ + "Home.dom": {Route: Prefix_3, LastSeen: time.Now()}, + }, + } + + fakeRouteInfo2 := ipn.RouteInfo{ + Local: []netip.Prefix{Prefix_1}, + Corp: []netip.Prefix{Prefix_3}, + Discovered: map[string]ipn.DatedRoute{ + "Home.dom": {Route: Prefix_2, LastSeen: time.Now()}, + }, + } + + pm.currentRoutes = &fakeRouteInfo1 + if err := pm.WriteRoutesForCurrentProfile(); err != nil { + t.Fatal(err) + } + + pm.currentRoutes = &fakeRouteInfo2 + if err := pm.ReadRoutesForCurrentProfile(); err != nil { + t.Fatal(err) + } + + if pm.currentRoutes.Corp[0] != Prefix_1 { + t.Fatal("That's not right") + } +} diff --git a/ipn/prefs.go b/ipn/prefs.go index ab0bf2cf7..e6ecc7cb2 100644 --- a/ipn/prefs.go +++ b/ipn/prefs.go @@ -16,6 +16,7 @@ import ( "runtime" "slices" "strings" + "time" "tailscale.com/atomicfile" "tailscale.com/ipn/ipnstate" @@ -952,3 +953,14 @@ type LoginProfile struct { // into. ControlURL string } + +type RouteInfo struct { + Local []netip.Prefix + Corp []netip.Prefix + Discovered map[string]DatedRoute +} + +type DatedRoute struct { + Route netip.Prefix + LastSeen time.Time +}