diff --git a/wgengine/magicsock/magicsock.go b/wgengine/magicsock/magicsock.go index d20389a10..d12f2cf08 100644 --- a/wgengine/magicsock/magicsock.go +++ b/wgengine/magicsock/magicsock.go @@ -113,6 +113,7 @@ type Conn struct { pconn4 *RebindingUDPConn pconn6 *RebindingUDPConn // non-nil if IPv6 available epFunc func(endpoints []string) + derpActiveFunc func() logf logger.Logf sendLogLimit *rate.Limiter netChecker *netcheck.Client @@ -281,6 +282,10 @@ type Options struct { // endpoints change. The called func does not own the slice. EndpointsFunc func(endpoint []string) + // DERPActiveFunc optionally provides a func to be called when + // a connection is made to a DERP server. + DERPActiveFunc func() + // IdleFunc optionally provides a func to return how long // it's been since a TUN packet was sent or received. IdleFunc func() time.Duration @@ -317,6 +322,13 @@ func (o *Options) endpointsFunc() func([]string) { return o.EndpointsFunc } +func (o *Options) derpActiveFunc() func() { + if o == nil || o.DERPActiveFunc == nil { + return func() {} + } + return o.DERPActiveFunc +} + // newConn is the error-free, network-listening-side-effect-free based // of NewConn. Mostly for tests. func newConn() *Conn { @@ -346,6 +358,7 @@ func NewConn(opts Options) (*Conn, error) { c.pconnPort = opts.Port c.logf = opts.logf() c.epFunc = opts.endpointsFunc() + c.derpActiveFunc = opts.derpActiveFunc() c.idleFunc = opts.IdleFunc c.packetListener = opts.PacketListener c.noteRecvActivity = opts.NoteRecvActivity @@ -1206,6 +1219,7 @@ func (c *Conn) derpWriteChanOfAddr(addr netaddr.IPPort, peer key.Public) chan<- go c.runDerpReader(ctx, addr, dc, wg, startGate) go c.runDerpWriter(ctx, dc, ch, wg, startGate) + go c.derpActiveFunc() return ad.writeCh } diff --git a/wgengine/userspace.go b/wgengine/userspace.go index b5c0790af..7f7070b35 100644 --- a/wgengine/userspace.go +++ b/wgengine/userspace.go @@ -239,6 +239,7 @@ func newUserspaceEngineAdvanced(conf EngineConfig) (_ Engine, reterr error) { Logf: logf, Port: conf.ListenPort, EndpointsFunc: endpointsFn, + DERPActiveFunc: e.RequestStatus, IdleFunc: e.tundev.IdleDuration, NoteRecvActivity: e.noteReceiveActivity, }