mirror of
https://github.com/tailscale/tailscale.git
synced 2026-12-04 15:42:20 -08:00
a prototype for writing and reading the route info for current profile
Signed-off-by: Kevin Liang <[email protected]>
This commit is contained in:
1 parent
d0dd8fd38d
commit
9f0bb29912
3 files changed
+108
-7
No files matched your search
@@ -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 {
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
Reference in new issue
Block a user