Skip to content

Commit d1a4196

Browse files
committed
Export DNS resolver
1 parent 257fa79 commit d1a4196

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
@@ -377,6 +377,8 @@ type LocalBackend struct {
377377
// backend is healthy and captive portal detection is not required
378378
// (sending false).
379379
needsCaptiveDetection chan bool
380+
381+
lookupHook dnscache.LookupHookFunc
380382
}
381383

382384
// HealthTracker returns the health tracker for the backend.
@@ -416,7 +418,7 @@ type clientGen func(controlclient.Options) (controlclient.Client, error)
416418
// but is not actually running.
417419
//
418420
// If dialer is nil, a new one is made.
419-
func NewLocalBackend(logf logger.Logf, logID logid.PublicID, sys *tsd.System, loginFlags controlclient.LoginFlags) (_ *LocalBackend, err error) {
421+
func NewLocalBackend(logf logger.Logf, logID logid.PublicID, sys *tsd.System, loginFlags controlclient.LoginFlags, lookupHook dnscache.LookupHookFunc) (_ *LocalBackend, err error) {
420422
e := sys.Engine.Get()
421423
store := sys.StateStore.Get()
422424
dialer := sys.Dialer.Get()
@@ -2196,6 +2198,7 @@ func (b *LocalBackend) Start(opts ipn.Options) error {
21962198
// Don't warn about broken Linux IP forwarding when
21972199
// netstack is being used.
21982200
SkipIPForwardingCheck: isNetstack,
2201+
LookupHook: b.lookupHook,
21992202
})
22002203
if err != nil {
22012204
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"
@@ -120,6 +121,8 @@ type Server struct {
120121
// field at zero unless you know what you are doing.
121122
Port uint16
122123

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

125128
initOnce sync.Once
@@ -595,7 +598,7 @@ func (s *Server) start() (reterr error) {
595598
if s.Ephemeral {
596599
loginFlags = controlclient.LoginEphemeral
597600
}
598-
lb, err := ipnlocal.NewLocalBackend(tsLogf, s.logid, sys, loginFlags|controlclient.LocalBackendStartKeyOSNeutral)
601+
lb, err := ipnlocal.NewLocalBackend(tsLogf, s.logid, sys, loginFlags|controlclient.LocalBackendStartKeyOSNeutral, s.LookupHook)
599602
if err != nil {
600603
return fmt.Errorf("NewLocalBackend: %v", err)
601604
}

0 commit comments

Comments
 (0)