Skip to content

Commit

Permalink
Add reconfig listener
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Dec 3, 2024
1 parent 97a7019 commit 8b68177
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
8 changes: 0 additions & 8 deletions ipn/ipnlocal/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,10 +370,6 @@ type LocalBackend struct {
// backend is healthy and captive portal detection is not required
// (sending false).
needsCaptiveDetection chan bool

cfg *wgcfg.Config
rcfg *router.Config
dcfg *dns.Config
}

// HealthTracker returns the health tracker for the backend.
Expand Down Expand Up @@ -4264,10 +4260,6 @@ func (b *LocalBackend) authReconfig() {
}

b.initPeerAPIListener()

b.cfg = cfg
b.rcfg = rcfg
b.dcfg = dcfg
}

// shouldUseOneCGNATRoute reports whether we should prefer to make one big
Expand Down
8 changes: 3 additions & 5 deletions ipn/ipnlocal/local_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ package ipnlocal
import (
"sync/atomic"

"github.com/sagernet/tailscale/net/dns"
"github.com/sagernet/tailscale/wgengine"
"github.com/sagernet/tailscale/wgengine/filter"
"github.com/sagernet/tailscale/wgengine/router"
"github.com/sagernet/tailscale/wgengine/wgcfg"
)

func (b *LocalBackend) ExportFilter() *atomic.Pointer[filter.Filter] {
return &b.filterAtomic
}

func (b *LocalBackend) ExportConfig() (*wgcfg.Config, *dns.Config, *router.Config) {
return b.cfg, b.dcfg, b.rcfg
func (b *LocalBackend) ExportEngine() wgengine.Engine {
return b.e
}
10 changes: 8 additions & 2 deletions wgengine/userspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,10 @@ const statusPollInterval = 1 * time.Minute
const networkLoggerUploadTimeout = 5 * time.Second

type userspaceEngine struct {
ctx context.Context
workers int
ctx context.Context
workers int
onReconfig ReconfigListener

logf logger.Logf
wgLogger *wglog.Logger //a wireguard-go logging wrapper
reqCh chan struct{}
Expand Down Expand Up @@ -1051,6 +1053,10 @@ func (e *userspaceEngine) Reconfig(cfg *wgcfg.Config, routerCfg *router.Config,
}
}

if routerChanged && e.onReconfig != nil {
e.onReconfig(cfg, routerCfg, dnsCfg)
}

e.logf("[v1] wgengine: Reconfig done")
return nil
}
Expand Down
19 changes: 19 additions & 0 deletions wgengine/userspace_export.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package wgengine

import (
"github.com/sagernet/tailscale/net/dns"
"github.com/sagernet/tailscale/wgengine/router"
"github.com/sagernet/tailscale/wgengine/wgcfg"
)

type ExportedUserspaceEngine interface {
SetOnReconfigListener(listener ReconfigListener)
}

type ReconfigListener = func(cfg *wgcfg.Config, routerCfg *router.Config, dnsCfg *dns.Config)

func (e *userspaceEngine) SetOnReconfigListener(listener ReconfigListener) {
e.mu.Lock()
defer e.mu.Unlock()
e.onReconfig = listener
}

0 comments on commit 8b68177

Please sign in to comment.