Skip to content

Commit 0a3e81c

Browse files
committed
Export DNS resolver
1 parent 591bd43 commit 0a3e81c

File tree

5 files changed

+21
-2
lines changed

5 files changed

+21
-2
lines changed

control/controlclient/direct.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ type Options struct {
156156
// If we receive a new DialPlan from the server, this value will be
157157
// updated.
158158
DialPlan ControlDialPlanner
159+
160+
LookupHook dnscache.LookupHookFunc
159161
}
160162

161163
// ControlDialPlanner is the interface optionally supplied when creating a
@@ -251,6 +253,7 @@ func NewDirect(opts Options) (*Direct, error) {
251253
UseLastGood: true,
252254
LookupIPFallback: dnsfallback.MakeLookupFunc(opts.Logf, netMon),
253255
Logf: opts.Logf,
256+
LookupHook: opts.LookupHook,
254257
}
255258

256259
httpc := opts.HTTPTestClient

ipn/ipnlocal/local.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,8 @@ type LocalBackend struct {
385385
// backend is healthy and captive portal detection is not required
386386
// (sending false).
387387
needsCaptiveDetection chan bool
388+
389+
lookupHook dnscache.LookupHookFunc
388390
}
389391

390392
// HealthTracker returns the health tracker for the backend.
@@ -424,7 +426,7 @@ type clientGen func(controlclient.Options) (controlclient.Client, error)
424426
// but is not actually running.
425427
//
426428
// If dialer is nil, a new one is made.
427-
func NewLocalBackend(logf logger.Logf, logID logid.PublicID, sys *tsd.System, loginFlags controlclient.LoginFlags) (_ *LocalBackend, err error) {
429+
func NewLocalBackend(logf logger.Logf, logID logid.PublicID, sys *tsd.System, loginFlags controlclient.LoginFlags, lookupHook dnscache.LookupHookFunc) (_ *LocalBackend, err error) {
428430
e := sys.Engine.Get()
429431
store := sys.StateStore.Get()
430432
dialer := sys.Dialer.Get()
@@ -2300,6 +2302,7 @@ func (b *LocalBackend) Start(opts ipn.Options) error {
23002302
// Don't warn about broken Linux IP forwarding when
23012303
// netstack is being used.
23022304
SkipIPForwardingCheck: isNetstack,
2305+
LookupHook: b.lookupHook,
23032306
})
23042307
if err != nil {
23052308
return err

net/dnscache/dnscache.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ type Resolver struct {
6666
// to use if Forward returns an error or no results.
6767
LookupIPFallback func(ctx context.Context, host string) ([]netip.Addr, error)
6868

69+
LookupHook LookupHookFunc
70+
6971
// TTL is how long to keep entries cached
7072
//
7173
// If zero, a default (currently 10 minutes) is used.

net/dnscache/export.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package dnscache
2+
3+
import (
4+
"context"
5+
"net/netip"
6+
)
7+
8+
type LookupHookFunc func(ctx context.Context, host string) ([]netip.Addr, error)

tsnet/tsnet.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"encoding/hex"
1212
"errors"
1313
"fmt"
14+
"github.com/sagernet/tailscale/net/dnscache"
1415
"io"
1516
"log"
1617
"math"
@@ -121,6 +122,8 @@ type Server struct {
121122
// field at zero unless you know what you are doing.
122123
Port uint16
123124

125+
LookupHook dnscache.LookupHookFunc
126+
124127
getCertForTesting func(*tls.ClientHelloInfo) (*tls.Certificate, error)
125128

126129
initOnce sync.Once
@@ -628,7 +631,7 @@ func (s *Server) start() (reterr error) {
628631
if s.Ephemeral {
629632
loginFlags = controlclient.LoginEphemeral
630633
}
631-
lb, err := ipnlocal.NewLocalBackend(tsLogf, s.logid, sys, loginFlags|controlclient.LocalBackendStartKeyOSNeutral)
634+
lb, err := ipnlocal.NewLocalBackend(tsLogf, s.logid, sys, loginFlags|controlclient.LocalBackendStartKeyOSNeutral, s.LookupHook)
632635
if err != nil {
633636
return fmt.Errorf("NewLocalBackend: %v", err)
634637
}

0 commit comments

Comments
 (0)