ipn/profiles: set default prefs based on Windows registry (#6803)

Co-authored-by: Maisem Ali maisem@tailscale.com
This commit is contained in:
Kristoffer Dalby 2023-01-04 18:34:31 +01:00 committed by GitHub
parent eafbf8886d
commit c4e262a0fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 12 deletions

View File

@ -9,6 +9,7 @@
"errors" "errors"
"fmt" "fmt"
"math/rand" "math/rand"
"net/netip"
"runtime" "runtime"
"time" "time"
@ -19,6 +20,7 @@
"tailscale.com/types/logger" "tailscale.com/types/logger"
"tailscale.com/util/clientmetric" "tailscale.com/util/clientmetric"
"tailscale.com/util/strs" "tailscale.com/util/strs"
"tailscale.com/util/winutil"
"tailscale.com/version" "tailscale.com/version"
) )
@ -322,7 +324,7 @@ func (pm *profileManager) setAsUserSelectedProfileLocked() error {
func (pm *profileManager) loadSavedPrefs(key ipn.StateKey) (ipn.PrefsView, error) { func (pm *profileManager) loadSavedPrefs(key ipn.StateKey) (ipn.PrefsView, error) {
bs, err := pm.store.ReadState(key) bs, err := pm.store.ReadState(key)
if err == ipn.ErrStateNotExist || len(bs) == 0 { if err == ipn.ErrStateNotExist || len(bs) == 0 {
return emptyPrefs, nil return defaultPrefs, nil
} }
if err != nil { if err != nil {
return ipn.PrefsView{}, err return ipn.PrefsView{}, err
@ -394,15 +396,29 @@ func (pm *profileManager) writeKnownProfiles() error {
func (pm *profileManager) NewProfile() { func (pm *profileManager) NewProfile() {
metricNewProfile.Add(1) metricNewProfile.Add(1)
pm.prefs = emptyPrefs pm.prefs = defaultPrefs
pm.isNewProfile = true pm.isNewProfile = true
pm.currentProfile = &ipn.LoginProfile{} pm.currentProfile = &ipn.LoginProfile{}
} }
// emptyPrefs is the default prefs for a new profile. // defaultPrefs is the default prefs for a new profile.
var emptyPrefs = func() ipn.PrefsView { var defaultPrefs = func() ipn.PrefsView {
prefs := ipn.NewPrefs() prefs := ipn.NewPrefs()
prefs.WantRunning = false prefs.WantRunning = false
prefs.ControlURL = winutil.GetPolicyString("LoginURL", "")
if exitNode := winutil.GetPolicyString("ExitNodeIP", ""); exitNode != "" {
if ip, err := netip.ParseAddr(exitNode); err == nil {
prefs.ExitNodeIP = ip
}
}
// Allow Incoming (used by the UI) is the negation of ShieldsUp (used by the
// backend), so this has to convert between the two conventions.
prefs.ShieldsUp = winutil.GetPolicyString("AllowIncomingConnections", "") == "never"
prefs.ForceDaemon = winutil.GetPolicyString("UnattendedMode", "") == "always"
return prefs.View() return prefs.View()
}() }()

View File

@ -53,7 +53,7 @@ func TestProfileCurrentUserSwitch(t *testing.T) {
} else if pm.currentProfile.ID != "" { } else if pm.currentProfile.ID != "" {
t.Fatalf("currentProfile.ID = %q, want empty", pm.currentProfile.ID) t.Fatalf("currentProfile.ID = %q, want empty", pm.currentProfile.ID)
} }
if !pm.CurrentPrefs().Equals(emptyPrefs) { if !pm.CurrentPrefs().Equals(defaultPrefs) {
t.Fatalf("CurrentPrefs() = %v, want emptyPrefs", pm.CurrentPrefs().Pretty()) t.Fatalf("CurrentPrefs() = %v, want emptyPrefs", pm.CurrentPrefs().Pretty())
} }
@ -67,7 +67,7 @@ func TestProfileCurrentUserSwitch(t *testing.T) {
} else if pm.currentProfile.ID != "" { } else if pm.currentProfile.ID != "" {
t.Fatalf("currentProfile.ID = %q, want empty", pm.currentProfile.ID) t.Fatalf("currentProfile.ID = %q, want empty", pm.currentProfile.ID)
} }
if !pm.CurrentPrefs().Equals(emptyPrefs) { if !pm.CurrentPrefs().Equals(defaultPrefs) {
t.Fatalf("CurrentPrefs() = %v, want emptyPrefs", pm.CurrentPrefs().Pretty()) t.Fatalf("CurrentPrefs() = %v, want emptyPrefs", pm.CurrentPrefs().Pretty())
} }
} }
@ -159,7 +159,7 @@ func TestProfileManagement(t *testing.T) {
} }
wantCurProfile := "" wantCurProfile := ""
wantProfiles := map[string]ipn.PrefsView{ wantProfiles := map[string]ipn.PrefsView{
"": emptyPrefs, "": defaultPrefs,
} }
checkProfiles := func(t *testing.T) { checkProfiles := func(t *testing.T) {
t.Helper() t.Helper()
@ -237,7 +237,7 @@ func TestProfileManagement(t *testing.T) {
t.Logf("Create new profile") t.Logf("Create new profile")
pm.NewProfile() pm.NewProfile()
wantCurProfile = "" wantCurProfile = ""
wantProfiles[""] = emptyPrefs wantProfiles[""] = defaultPrefs
checkProfiles(t) checkProfiles(t)
{ {
@ -276,7 +276,7 @@ func TestProfileManagement(t *testing.T) {
t.Logf("Create new profile - 2") t.Logf("Create new profile - 2")
pm.NewProfile() pm.NewProfile()
wantCurProfile = "" wantCurProfile = ""
wantProfiles[""] = emptyPrefs wantProfiles[""] = defaultPrefs
checkProfiles(t) checkProfiles(t)
t.Logf("Login with the existing profile") t.Logf("Login with the existing profile")
@ -310,7 +310,7 @@ func TestProfileManagementWindows(t *testing.T) {
} }
wantCurProfile := "" wantCurProfile := ""
wantProfiles := map[string]ipn.PrefsView{ wantProfiles := map[string]ipn.PrefsView{
"": emptyPrefs, "": defaultPrefs,
} }
checkProfiles := func(t *testing.T) { checkProfiles := func(t *testing.T) {
t.Helper() t.Helper()
@ -363,7 +363,7 @@ func TestProfileManagementWindows(t *testing.T) {
t.Logf("Create new profile") t.Logf("Create new profile")
pm.NewProfile() pm.NewProfile()
wantCurProfile = "" wantCurProfile = ""
wantProfiles[""] = emptyPrefs wantProfiles[""] = defaultPrefs
checkProfiles(t) checkProfiles(t)
t.Logf("Save as test profile") t.Logf("Save as test profile")
@ -380,7 +380,7 @@ func TestProfileManagementWindows(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
wantCurProfile = "" wantCurProfile = ""
wantProfiles[""] = emptyPrefs wantProfiles[""] = defaultPrefs
checkProfiles(t) checkProfiles(t)
{ {