mirror of
https://github.com/tailscale/tailscale.git
synced 2025-04-16 03:31:39 +00:00
ipn/localapi: move EditPrefs to localapi
Follow-up/revision to recent 53cfff109b01baa3d219697a1a9e2ea16c4b0d3d which added EditPrefs. Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
parent
84430cdfa1
commit
00d641d9fc
@ -6,6 +6,7 @@
|
|||||||
package tailscale
|
package tailscale
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
@ -208,7 +209,23 @@ func GetPrefs(ctx context.Context) (*ipn.Prefs, error) {
|
|||||||
}
|
}
|
||||||
var p ipn.Prefs
|
var p ipn.Prefs
|
||||||
if err := json.Unmarshal(body, &p); err != nil {
|
if err := json.Unmarshal(body, &p); err != nil {
|
||||||
return nil, fmt.Errorf("invalid JSON from check-ip-forwarding: %w", err)
|
return nil, fmt.Errorf("invalid prefs JSON: %w", err)
|
||||||
|
}
|
||||||
|
return &p, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func EditPrefs(ctx context.Context, mp *ipn.MaskedPrefs) (*ipn.Prefs, error) {
|
||||||
|
mpj, err := json.Marshal(mp)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
body, err := send(ctx, "POST", "/localapi/v0/prefs", http.StatusOK, bytes.NewReader(mpj))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var p ipn.Prefs
|
||||||
|
if err := json.Unmarshal(body, &p); err != nil {
|
||||||
|
return nil, fmt.Errorf("invalid prefs JSON: %w", err)
|
||||||
}
|
}
|
||||||
return &p, nil
|
return &p, nil
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/peterbourgon/ff/v2/ffcli"
|
"github.com/peterbourgon/ff/v2/ffcli"
|
||||||
"tailscale.com/client/tailscale"
|
"tailscale.com/client/tailscale"
|
||||||
@ -37,34 +36,11 @@ func runDown(ctx context.Context, args []string) error {
|
|||||||
fmt.Fprintf(os.Stderr, "Tailscale was already stopped.\n")
|
fmt.Fprintf(os.Stderr, "Tailscale was already stopped.\n")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
_, err = tailscale.EditPrefs(ctx, &ipn.MaskedPrefs{
|
||||||
c, bc, ctx, cancel := connect(ctx)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
timer := time.AfterFunc(5*time.Second, func() {
|
|
||||||
log.Fatalf("timeout running stop")
|
|
||||||
})
|
|
||||||
defer timer.Stop()
|
|
||||||
|
|
||||||
bc.SetNotifyCallback(func(n ipn.Notify) {
|
|
||||||
if n.ErrMessage != nil {
|
|
||||||
log.Fatal(*n.ErrMessage)
|
|
||||||
}
|
|
||||||
if n.State != nil {
|
|
||||||
if *n.State == ipn.Stopped {
|
|
||||||
cancel()
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
bc.EditPrefs(&ipn.MaskedPrefs{
|
|
||||||
Prefs: ipn.Prefs{
|
Prefs: ipn.Prefs{
|
||||||
WantRunning: false,
|
WantRunning: false,
|
||||||
},
|
},
|
||||||
WantRunningSet: true,
|
WantRunningSet: true,
|
||||||
})
|
})
|
||||||
pump(ctx, bc, c)
|
return err
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
@ -158,8 +158,6 @@ type Backend interface {
|
|||||||
// WantRunning. This may cause the wireguard engine to
|
// WantRunning. This may cause the wireguard engine to
|
||||||
// reconfigure or stop.
|
// reconfigure or stop.
|
||||||
SetPrefs(*Prefs)
|
SetPrefs(*Prefs)
|
||||||
// EditPrefs is like SetPrefs but only sets the specified fields.
|
|
||||||
EditPrefs(*MaskedPrefs)
|
|
||||||
// RequestEngineStatus polls for an update from the wireguard
|
// RequestEngineStatus polls for an update from the wireguard
|
||||||
// engine. Only needed if you want to display byte
|
// engine. Only needed if you want to display byte
|
||||||
// counts. Connection events are emitted automatically without
|
// counts. Connection events are emitted automatically without
|
||||||
|
@ -96,13 +96,6 @@ func (b *FakeBackend) SetPrefs(new *Prefs) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *FakeBackend) EditPrefs(mp *MaskedPrefs) {
|
|
||||||
// This fake implementation only cares about this one pref.
|
|
||||||
if mp.WantRunningSet {
|
|
||||||
b.SetPrefs(&mp.Prefs)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *FakeBackend) RequestEngineStatus() {
|
func (b *FakeBackend) RequestEngineStatus() {
|
||||||
if b.notify != nil {
|
if b.notify != nil {
|
||||||
b.notify(Notify{Engine: &EngineStatus{}})
|
b.notify(Notify{Engine: &EngineStatus{}})
|
||||||
|
@ -1302,17 +1302,18 @@ func (b *LocalBackend) SetCurrentUserID(uid string) {
|
|||||||
b.mu.Unlock()
|
b.mu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *LocalBackend) EditPrefs(mp *ipn.MaskedPrefs) {
|
func (b *LocalBackend) EditPrefs(mp *ipn.MaskedPrefs) (*ipn.Prefs, error) {
|
||||||
b.mu.Lock()
|
b.mu.Lock()
|
||||||
p0 := b.prefs.Clone()
|
p0 := b.prefs.Clone()
|
||||||
p1 := b.prefs.Clone()
|
p1 := b.prefs.Clone()
|
||||||
p1.ApplyEdits(mp)
|
p1.ApplyEdits(mp)
|
||||||
if p1.Equals(p0) {
|
if p1.Equals(p0) {
|
||||||
b.mu.Unlock()
|
b.mu.Unlock()
|
||||||
return
|
return p1, nil
|
||||||
}
|
}
|
||||||
b.logf("EditPrefs: %v", mp.Pretty())
|
b.logf("EditPrefs: %v", mp.Pretty())
|
||||||
b.setPrefsLockedOnEntry("EditPrefs", p1)
|
b.setPrefsLockedOnEntry("EditPrefs", p1)
|
||||||
|
return p1, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetPrefs saves new user preferences and propagates them throughout
|
// SetPrefs saves new user preferences and propagates them throughout
|
||||||
|
@ -23,6 +23,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"inet.af/netaddr"
|
"inet.af/netaddr"
|
||||||
|
"tailscale.com/ipn"
|
||||||
"tailscale.com/ipn/ipnlocal"
|
"tailscale.com/ipn/ipnlocal"
|
||||||
"tailscale.com/ipn/ipnstate"
|
"tailscale.com/ipn/ipnstate"
|
||||||
"tailscale.com/tailcfg"
|
"tailscale.com/tailcfg"
|
||||||
@ -224,10 +225,26 @@ func (h *Handler) servePrefs(w http.ResponseWriter, r *http.Request) {
|
|||||||
http.Error(w, "prefs access denied", http.StatusForbidden)
|
http.Error(w, "prefs access denied", http.StatusForbidden)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
var prefs *ipn.Prefs
|
||||||
|
if r.Method == "POST" {
|
||||||
|
mp := new(ipn.MaskedPrefs)
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(mp); err != nil {
|
||||||
|
http.Error(w, err.Error(), 400)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var err error
|
||||||
|
prefs, err = h.b.EditPrefs(mp)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), 400)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
prefs = h.b.Prefs()
|
||||||
|
}
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
e := json.NewEncoder(w)
|
e := json.NewEncoder(w)
|
||||||
e.SetIndent("", "\t")
|
e.SetIndent("", "\t")
|
||||||
e.Encode(h.b.Prefs())
|
e.Encode(prefs)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) serveFiles(w http.ResponseWriter, r *http.Request) {
|
func (h *Handler) serveFiles(w http.ResponseWriter, r *http.Request) {
|
||||||
|
@ -80,7 +80,6 @@ type Command struct {
|
|||||||
Login *tailcfg.Oauth2Token
|
Login *tailcfg.Oauth2Token
|
||||||
Logout *NoArgs
|
Logout *NoArgs
|
||||||
SetPrefs *SetPrefsArgs
|
SetPrefs *SetPrefsArgs
|
||||||
EditPrefs *MaskedPrefs
|
|
||||||
RequestEngineStatus *NoArgs
|
RequestEngineStatus *NoArgs
|
||||||
RequestStatus *NoArgs
|
RequestStatus *NoArgs
|
||||||
FakeExpireAfter *FakeExpireAfterArgs
|
FakeExpireAfter *FakeExpireAfterArgs
|
||||||
@ -204,9 +203,6 @@ func (bs *BackendServer) GotCommand(ctx context.Context, cmd *Command) error {
|
|||||||
} else if c := cmd.SetPrefs; c != nil {
|
} else if c := cmd.SetPrefs; c != nil {
|
||||||
bs.b.SetPrefs(c.New)
|
bs.b.SetPrefs(c.New)
|
||||||
return nil
|
return nil
|
||||||
} else if c := cmd.EditPrefs; c != nil {
|
|
||||||
bs.b.EditPrefs(c)
|
|
||||||
return nil
|
|
||||||
} else if c := cmd.FakeExpireAfter; c != nil {
|
} else if c := cmd.FakeExpireAfter; c != nil {
|
||||||
bs.b.FakeExpireAfter(c.Duration)
|
bs.b.FakeExpireAfter(c.Duration)
|
||||||
return nil
|
return nil
|
||||||
@ -307,10 +303,6 @@ func (bc *BackendClient) SetPrefs(new *Prefs) {
|
|||||||
bc.send(Command{SetPrefs: &SetPrefsArgs{New: new}})
|
bc.send(Command{SetPrefs: &SetPrefsArgs{New: new}})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bc *BackendClient) EditPrefs(mp *MaskedPrefs) {
|
|
||||||
bc.send(Command{EditPrefs: mp})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (bc *BackendClient) RequestEngineStatus() {
|
func (bc *BackendClient) RequestEngineStatus() {
|
||||||
bc.send(Command{RequestEngineStatus: &NoArgs{}})
|
bc.send(Command{RequestEngineStatus: &NoArgs{}})
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user