ipn/localapi: plumb an event bus through the localapi.Handler (#16892)

Some of the operations of the local API need an event bus to correctly
instantiate other components (notably including the portmapper).

This commit adds that, and as the parameter list is starting to get a bit long
and hard to read, I took the opportunity to move the arguments to a config
type. Only a few call sites needed to be updated and this API is not intended
for general use, so I did not bother to stage the change.

Updates #15160
Updates #16842

Change-Id: I7b057d71161bd859f5acb96e2f878a34c85be0ef
Signed-off-by: M. J. Fromberger <fromberger@tailscale.com>
This commit is contained in:
M. J. Fromberger
2025-08-18 10:56:17 -07:00
committed by GitHub
parent 02f6030dbd
commit 55698c8511
4 changed files with 45 additions and 11 deletions

View File

@@ -172,9 +172,26 @@ var (
metrics = map[string]*clientmetric.Metric{}
)
// NewHandler creates a new LocalAPI HTTP handler. All parameters are required.
func NewHandler(actor ipnauth.Actor, b *ipnlocal.LocalBackend, logf logger.Logf, logID logid.PublicID) *Handler {
return &Handler{Actor: actor, b: b, logf: logf, backendLogID: logID, clock: tstime.StdClock{}}
// NewHandler creates a new LocalAPI HTTP handler from the given config.
func NewHandler(cfg HandlerConfig) *Handler {
return &Handler{
Actor: cfg.Actor,
b: cfg.Backend,
logf: cfg.Logf,
backendLogID: cfg.LogID,
clock: tstime.StdClock{},
eventBus: cfg.EventBus,
}
}
// HandlerConfig carries the settings for a local API handler.
// All fields are required.
type HandlerConfig struct {
Actor ipnauth.Actor
Backend *ipnlocal.LocalBackend
Logf logger.Logf
LogID logid.PublicID
EventBus *eventbus.Bus
}
type Handler struct {
@@ -203,6 +220,7 @@ type Handler struct {
logf logger.Logf
backendLogID logid.PublicID
clock tstime.Clock
eventBus *eventbus.Bus // read-only after initialization
}
func (h *Handler) Logf(format string, args ...any) {
@@ -850,6 +868,7 @@ func (h *Handler) serveDebugPortmap(w http.ResponseWriter, r *http.Request) {
NetMon: h.b.NetMon(),
DebugKnobs: debugKnobs,
ControlKnobs: h.b.ControlKnobs(),
EventBus: h.eventBus,
OnChange: func() {
logf("portmapping changed.")
logf("have mapping: %v", c.HaveMapping())