|
|
@ -76,7 +76,7 @@ func NewLocalBackend(logf logger.Logf, logid string, store StateStore, e wgengin
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
portpoll, err := portlist.NewPoller()
|
|
|
|
portpoll, err := portlist.NewPoller()
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
logf("skipping portlist: %s\n", err)
|
|
|
|
logf("skipping portlist: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
b := &LocalBackend{
|
|
|
|
b := &LocalBackend{
|
|
|
@ -165,9 +165,9 @@ func (b *LocalBackend) Start(opts Options) error {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if opts.Prefs != nil {
|
|
|
|
if opts.Prefs != nil {
|
|
|
|
b.logf("Start: %v\n", opts.Prefs.Pretty())
|
|
|
|
b.logf("Start: %v", opts.Prefs.Pretty())
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
b.logf("Start\n")
|
|
|
|
b.logf("Start")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
hi := controlclient.NewHostinfo()
|
|
|
|
hi := controlclient.NewHostinfo()
|
|
|
@ -268,7 +268,7 @@ func (b *LocalBackend) Start(opts Options) error {
|
|
|
|
if strings.TrimSpace(diff) == "" {
|
|
|
|
if strings.TrimSpace(diff) == "" {
|
|
|
|
b.logf("netmap diff: (none)")
|
|
|
|
b.logf("netmap diff: (none)")
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
b.logf("netmap diff:\n%v\n", diff)
|
|
|
|
b.logf("netmap diff:\n%v", diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b.netMapCache = newSt.NetMap
|
|
|
|
b.netMapCache = newSt.NetMap
|
|
|
@ -278,7 +278,7 @@ func (b *LocalBackend) Start(opts Options) error {
|
|
|
|
b.updateFilter(newSt.NetMap)
|
|
|
|
b.updateFilter(newSt.NetMap)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if newSt.URL != "" {
|
|
|
|
if newSt.URL != "" {
|
|
|
|
b.logf("Received auth URL: %.20v...\n", newSt.URL)
|
|
|
|
b.logf("Received auth URL: %.20v...", newSt.URL)
|
|
|
|
|
|
|
|
|
|
|
|
b.mu.Lock()
|
|
|
|
b.mu.Lock()
|
|
|
|
interact := b.interact
|
|
|
|
interact := b.interact
|
|
|
@ -313,7 +313,7 @@ func (b *LocalBackend) Start(opts Options) error {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if s == nil {
|
|
|
|
if s == nil {
|
|
|
|
log.Fatalf("weird: non-error wgengine update with status=nil\n")
|
|
|
|
log.Fatalf("weird: non-error wgengine update with status=nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
es := b.parseWgStatus(s)
|
|
|
|
es := b.parseWgStatus(s)
|
|
|
@ -343,7 +343,7 @@ func (b *LocalBackend) Start(opts Options) error {
|
|
|
|
b.mu.Unlock()
|
|
|
|
b.mu.Unlock()
|
|
|
|
|
|
|
|
|
|
|
|
blid := b.backendLogID
|
|
|
|
blid := b.backendLogID
|
|
|
|
b.logf("Backend: logs: be:%v fe:%v\n", blid, opts.FrontendLogID)
|
|
|
|
b.logf("Backend: logs: be:%v fe:%v", blid, opts.FrontendLogID)
|
|
|
|
b.send(Notify{BackendLogID: &blid})
|
|
|
|
b.send(Notify{BackendLogID: &blid})
|
|
|
|
b.send(Notify{Prefs: prefs})
|
|
|
|
b.send(Notify{Prefs: prefs})
|
|
|
|
|
|
|
|
|
|
|
@ -362,10 +362,10 @@ func (b *LocalBackend) updateFilter(netMap *controlclient.NetworkMap) {
|
|
|
|
// TODO(apenwarr): print a diff instead of full filter.
|
|
|
|
// TODO(apenwarr): print a diff instead of full filter.
|
|
|
|
now := time.Now()
|
|
|
|
now := time.Now()
|
|
|
|
if now.Sub(b.lastFilterPrint) > 1*time.Minute {
|
|
|
|
if now.Sub(b.lastFilterPrint) > 1*time.Minute {
|
|
|
|
b.logf("netmap packet filter: %v\n", b.netMapCache.PacketFilter)
|
|
|
|
b.logf("netmap packet filter: %v", b.netMapCache.PacketFilter)
|
|
|
|
b.lastFilterPrint = now
|
|
|
|
b.lastFilterPrint = now
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
b.logf("netmap packet filter: (suppressed)\n")
|
|
|
|
b.logf("netmap packet filter: (suppressed)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b.e.SetFilter(filter.New(netMap.PacketFilter, b.e.GetFilter()))
|
|
|
|
b.e.SetFilter(filter.New(netMap.PacketFilter, b.e.GetFilter()))
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -426,7 +426,7 @@ func (b *LocalBackend) popBrowserAuthNow() {
|
|
|
|
b.authURL = ""
|
|
|
|
b.authURL = ""
|
|
|
|
b.mu.Unlock()
|
|
|
|
b.mu.Unlock()
|
|
|
|
|
|
|
|
|
|
|
|
b.logf("popBrowserAuthNow: url=%v\n", url != "")
|
|
|
|
b.logf("popBrowserAuthNow: url=%v", url != "")
|
|
|
|
|
|
|
|
|
|
|
|
b.blockEngineUpdates(true)
|
|
|
|
b.blockEngineUpdates(true)
|
|
|
|
b.stopEngineAndWait()
|
|
|
|
b.stopEngineAndWait()
|
|
|
@ -515,7 +515,7 @@ func (b *LocalBackend) StartLoginInteractive() {
|
|
|
|
url := b.authURL
|
|
|
|
url := b.authURL
|
|
|
|
c := b.c
|
|
|
|
c := b.c
|
|
|
|
b.mu.Unlock()
|
|
|
|
b.mu.Unlock()
|
|
|
|
b.logf("StartLoginInteractive: url=%v\n", url != "")
|
|
|
|
b.logf("StartLoginInteractive: url=%v", url != "")
|
|
|
|
|
|
|
|
|
|
|
|
if url != "" {
|
|
|
|
if url != "" {
|
|
|
|
b.popBrowserAuthNow()
|
|
|
|
b.popBrowserAuthNow()
|
|
|
@ -525,7 +525,7 @@ func (b *LocalBackend) StartLoginInteractive() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (b *LocalBackend) FakeExpireAfter(x time.Duration) {
|
|
|
|
func (b *LocalBackend) FakeExpireAfter(x time.Duration) {
|
|
|
|
b.logf("FakeExpireAfter: %v\n", x)
|
|
|
|
b.logf("FakeExpireAfter: %v", x)
|
|
|
|
if b.netMapCache != nil {
|
|
|
|
if b.netMapCache != nil {
|
|
|
|
e := b.netMapCache.Expiry
|
|
|
|
e := b.netMapCache.Expiry
|
|
|
|
if e.IsZero() || time.Until(e) > x {
|
|
|
|
if e.IsZero() || time.Until(e) > x {
|
|
|
@ -568,7 +568,7 @@ func (b *LocalBackend) parseWgStatus(s *wgengine.Status) EngineStatus {
|
|
|
|
rx += p.RxBytes
|
|
|
|
rx += p.RxBytes
|
|
|
|
tx += p.TxBytes
|
|
|
|
tx += p.TxBytes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b.logf("v%v peers: %v\n", version.LONG, strings.Join(ss, " "))
|
|
|
|
b.logf("v%v peers: %v", version.LONG, strings.Join(ss, " "))
|
|
|
|
return EngineStatus{
|
|
|
|
return EngineStatus{
|
|
|
|
RBytes: rx,
|
|
|
|
RBytes: rx,
|
|
|
|
WBytes: tx,
|
|
|
|
WBytes: tx,
|
|
|
@ -610,7 +610,7 @@ func (b *LocalBackend) SetPrefs(new *Prefs) {
|
|
|
|
cli := b.c
|
|
|
|
cli := b.c
|
|
|
|
b.mu.Unlock()
|
|
|
|
b.mu.Unlock()
|
|
|
|
|
|
|
|
|
|
|
|
b.logf("SetPrefs: %v\n", new.Pretty())
|
|
|
|
b.logf("SetPrefs: %v", new.Pretty())
|
|
|
|
|
|
|
|
|
|
|
|
if cli != nil && !oldHi.Equal(newHi) {
|
|
|
|
if cli != nil && !oldHi.Equal(newHi) {
|
|
|
|
cli.SetHostinfo(newHi)
|
|
|
|
cli.SetHostinfo(newHi)
|
|
|
@ -632,7 +632,7 @@ func (b *LocalBackend) NetMap() *controlclient.NetworkMap {
|
|
|
|
|
|
|
|
|
|
|
|
func (b *LocalBackend) blockEngineUpdates(block bool) {
|
|
|
|
func (b *LocalBackend) blockEngineUpdates(block bool) {
|
|
|
|
// TODO(apenwarr): probably need mutex here (and several other places)
|
|
|
|
// TODO(apenwarr): probably need mutex here (and several other places)
|
|
|
|
b.logf("blockEngineUpdates(%v)\n", block)
|
|
|
|
b.logf("blockEngineUpdates(%v)", block)
|
|
|
|
|
|
|
|
|
|
|
|
b.mu.Lock()
|
|
|
|
b.mu.Lock()
|
|
|
|
b.blocked = block
|
|
|
|
b.blocked = block
|
|
|
@ -647,15 +647,15 @@ func (b *LocalBackend) authReconfig() {
|
|
|
|
b.mu.Unlock()
|
|
|
|
b.mu.Unlock()
|
|
|
|
|
|
|
|
|
|
|
|
if blocked {
|
|
|
|
if blocked {
|
|
|
|
b.logf("authReconfig: blocked, skipping.\n")
|
|
|
|
b.logf("authReconfig: blocked, skipping.")
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if nm == nil {
|
|
|
|
if nm == nil {
|
|
|
|
b.logf("authReconfig: netmap not yet valid. Skipping.\n")
|
|
|
|
b.logf("authReconfig: netmap not yet valid. Skipping.")
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !uc.WantRunning {
|
|
|
|
if !uc.WantRunning {
|
|
|
|
b.logf("authReconfig: skipping because !WantRunning.\n")
|
|
|
|
b.logf("authReconfig: skipping because !WantRunning.")
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -683,14 +683,14 @@ func (b *LocalBackend) authReconfig() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cfg, err := nm.WGCfg(uflags, dns)
|
|
|
|
cfg, err := nm.WGCfg(uflags, dns)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("WGCfg: %v\n", err)
|
|
|
|
log.Fatalf("WGCfg: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
err = b.e.Reconfig(cfg, dom)
|
|
|
|
err = b.e.Reconfig(cfg, dom)
|
|
|
|
if err == wgengine.ErrNoChanges {
|
|
|
|
if err == wgengine.ErrNoChanges {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b.logf("authReconfig: ra=%v dns=%v 0x%02x: %v\n", uc.RouteAll, uc.CorpDNS, uflags, err)
|
|
|
|
b.logf("authReconfig: ra=%v dns=%v 0x%02x: %v", uc.RouteAll, uc.CorpDNS, uflags, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (b *LocalBackend) enterState(newState State) {
|
|
|
|
func (b *LocalBackend) enterState(newState State) {
|
|
|
@ -703,7 +703,7 @@ func (b *LocalBackend) enterState(newState State) {
|
|
|
|
if state == newState {
|
|
|
|
if state == newState {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b.logf("Switching ipn state %v -> %v (WantRunning=%v)\n",
|
|
|
|
b.logf("Switching ipn state %v -> %v (WantRunning=%v)",
|
|
|
|
state, newState, prefs.WantRunning)
|
|
|
|
state, newState, prefs.WantRunning)
|
|
|
|
if notify != nil {
|
|
|
|
if notify != nil {
|
|
|
|
b.send(Notify{State: &newState})
|
|
|
|
b.send(Notify{State: &newState})
|
|
|
@ -717,7 +717,7 @@ func (b *LocalBackend) enterState(newState State) {
|
|
|
|
case Stopped:
|
|
|
|
case Stopped:
|
|
|
|
err := b.e.Reconfig(&wgcfg.Config{}, nil)
|
|
|
|
err := b.e.Reconfig(&wgcfg.Config{}, nil)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
b.logf("Reconfig(down): %v\n", err)
|
|
|
|
b.logf("Reconfig(down): %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case Starting, NeedsMachineAuth:
|
|
|
|
case Starting, NeedsMachineAuth:
|
|
|
|
b.authReconfig()
|
|
|
|
b.authReconfig()
|
|
|
@ -726,7 +726,7 @@ func (b *LocalBackend) enterState(newState State) {
|
|
|
|
case Running:
|
|
|
|
case Running:
|
|
|
|
break
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
b.logf("Weird: unknown newState %#v\n", newState)
|
|
|
|
b.logf("[unexpected] unknown newState %#v", newState)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -790,22 +790,22 @@ func (b *LocalBackend) stateMachine() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (b *LocalBackend) stopEngineAndWait() {
|
|
|
|
func (b *LocalBackend) stopEngineAndWait() {
|
|
|
|
b.logf("stopEngineAndWait...\n")
|
|
|
|
b.logf("stopEngineAndWait...")
|
|
|
|
b.e.Reconfig(&wgcfg.Config{}, nil)
|
|
|
|
b.e.Reconfig(&wgcfg.Config{}, nil)
|
|
|
|
b.requestEngineStatusAndWait()
|
|
|
|
b.requestEngineStatusAndWait()
|
|
|
|
b.logf("stopEngineAndWait: done.\n")
|
|
|
|
b.logf("stopEngineAndWait: done.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Requests the wgengine status, and does not return until the status
|
|
|
|
// Requests the wgengine status, and does not return until the status
|
|
|
|
// was delivered (to the usual callback).
|
|
|
|
// was delivered (to the usual callback).
|
|
|
|
func (b *LocalBackend) requestEngineStatusAndWait() {
|
|
|
|
func (b *LocalBackend) requestEngineStatusAndWait() {
|
|
|
|
b.logf("requestEngineStatusAndWait\n")
|
|
|
|
b.logf("requestEngineStatusAndWait")
|
|
|
|
|
|
|
|
|
|
|
|
b.statusLock.Lock()
|
|
|
|
b.statusLock.Lock()
|
|
|
|
go b.e.RequestStatus()
|
|
|
|
go b.e.RequestStatus()
|
|
|
|
b.logf("requestEngineStatusAndWait: waiting...\n")
|
|
|
|
b.logf("requestEngineStatusAndWait: waiting...")
|
|
|
|
b.statusChanged.Wait() // temporarily releases lock while waiting
|
|
|
|
b.statusChanged.Wait() // temporarily releases lock while waiting
|
|
|
|
b.logf("requestEngineStatusAndWait: got status update.\n")
|
|
|
|
b.logf("requestEngineStatusAndWait: got status update.")
|
|
|
|
b.statusLock.Unlock()
|
|
|
|
b.statusLock.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|