mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-21 20:22:41 +09:00
tsnet: add Server.SystemDial
Lets an embedder pick the dialer for non-tailnet connections, such as to control. When control runs in the same process, the node can then reach it in memory (e.g. memnet) instead of via loopback. Updates tailscale/corp#27636 Signed-off-by: Kristoffer Dalby <kristoffer@dalby.cc>
This commit is contained in:
1 parent
7b2c32b913
commit
a2da5fbcaa
3 files changed
+65
-6
No files matched your search
+15
-6
@@ -55,7 +55,7 @@ func NewDialer(netMon *netmon.Monitor) *Dialer {
|
||||
// and no netMon. It's meant exclusively for the "tailscale debug ts2021"
|
||||
// debug command, and perhaps tests.
|
||||
func NewFromFuncForDebug(logf logger.Logf, dial netx.DialFunc) *Dialer {
|
||||
return &Dialer{sysDialForTest: dial, Logf: logf}
|
||||
return &Dialer{sysDial: dial, Logf: logf}
|
||||
}
|
||||
|
||||
// Dialer dials out of tailscaled, while taking care of details while
|
||||
@@ -86,7 +86,7 @@ type Dialer struct {
|
||||
|
||||
netnsDialerOnce sync.Once
|
||||
netnsDialer netns.Dialer
|
||||
sysDialForTest netx.DialFunc // or nil
|
||||
sysDial netx.DialFunc // or nil
|
||||
|
||||
routes atomic.Pointer[bart.Table[bool]] // or nil if UserDial should not use routes. `true` indicates routes that point into the Tailscale interface
|
||||
|
||||
@@ -553,11 +553,20 @@ func (d *Dialer) logf(format string, args ...any) {
|
||||
}
|
||||
}
|
||||
|
||||
// SetSystemDialer sets an alternate function to use for SystemDial.
|
||||
// It must be called before d is first used. If it is not set, host
|
||||
// network dials use a [netns.Dialer] constructed from the network monitor.
|
||||
func (d *Dialer) SetSystemDialer(fn netx.DialFunc) {
|
||||
d.sysDial = fn
|
||||
}
|
||||
|
||||
// SetSystemDialerForTest sets an alternate function to use for SystemDial
|
||||
// instead of netns.Dialer. This is intended for use with nettest.MemoryNetwork.
|
||||
//
|
||||
// Deprecated: Use SetSystemDialer instead.
|
||||
func (d *Dialer) SetSystemDialerForTest(fn netx.DialFunc) {
|
||||
testenv.AssertInTest()
|
||||
d.sysDialForTest = fn
|
||||
d.sysDial = fn
|
||||
}
|
||||
|
||||
// SystemDial connects to the provided network address without going over
|
||||
@@ -566,7 +575,7 @@ func (d *Dialer) SetSystemDialerForTest(fn netx.DialFunc) {
|
||||
// Control and (in the future, as of 2022-04-27) DERPs..
|
||||
func (d *Dialer) SystemDial(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||
d.mu.Lock()
|
||||
if d.netMon == nil && d.sysDialForTest == nil {
|
||||
if d.netMon == nil && d.sysDial == nil {
|
||||
d.mu.Unlock()
|
||||
if testenv.InTest() {
|
||||
panic("SystemDial requires a netmon.Monitor; call SetNetMon first")
|
||||
@@ -581,8 +590,8 @@ func (d *Dialer) SystemDial(ctx context.Context, network, addr string) (net.Conn
|
||||
|
||||
var c net.Conn
|
||||
var err error
|
||||
if d.sysDialForTest != nil {
|
||||
c, err = d.sysDialForTest(ctx, network, addr)
|
||||
if d.sysDial != nil {
|
||||
c, err = d.sysDial(ctx, network, addr)
|
||||
} else {
|
||||
d.netnsDialerOnce.Do(func() {
|
||||
d.netnsDialer = netns.NewDialer(d.logf, d.netMon)
|
||||
|
||||
@@ -208,6 +208,7 @@ import (
|
||||
"tailscale.com/logtail/filch"
|
||||
"tailscale.com/net/memnet"
|
||||
"tailscale.com/net/netmon"
|
||||
"tailscale.com/net/netx"
|
||||
"tailscale.com/net/proxymux"
|
||||
"tailscale.com/net/socks5"
|
||||
"tailscale.com/net/tsdial"
|
||||
@@ -325,6 +326,17 @@ type Server struct {
|
||||
// If that is also empty, the Tailscale default is used.
|
||||
ControlURL string
|
||||
|
||||
// SystemDial, if non-nil, replaces the default dialer for
|
||||
// connections that go over the host network rather than the
|
||||
// tailnet, such as those to the control server. It is not used for
|
||||
// DERP or WireGuard traffic. Hostnames may already be resolved to
|
||||
// IP addresses before SystemDial is called.
|
||||
//
|
||||
// This field must be set before calling Start. If it is not set,
|
||||
// host network dials use a [netns.Dialer] constructed from the server's
|
||||
// network monitor, if available; otherwise host network dials fail.
|
||||
SystemDial netx.DialFunc
|
||||
|
||||
// RunWebClient, if true, runs a client for managing this node over
|
||||
// its Tailscale interface on port 5252.
|
||||
RunWebClient bool
|
||||
@@ -907,6 +919,7 @@ func (s *Server) start() (reterr error) {
|
||||
|
||||
s.dialer = &tsdial.Dialer{Logf: tsLogf} // mutated below (before used)
|
||||
s.dialer.SetBus(sys.Bus.Get())
|
||||
s.dialer.SetSystemDialer(s.SystemDial)
|
||||
// LookupIP handles names not resolved by the MagicDNS hook.
|
||||
if buildfeatures.HasDNS {
|
||||
s.dialer.SetUserDialResolver(s.userDialResolve)
|
||||
|
||||
@@ -53,6 +53,7 @@ import (
|
||||
"tailscale.com/ipn"
|
||||
"tailscale.com/ipn/ipnlocal"
|
||||
"tailscale.com/ipn/store/mem"
|
||||
"tailscale.com/net/memnet"
|
||||
"tailscale.com/net/netns"
|
||||
"tailscale.com/net/packet"
|
||||
"tailscale.com/net/tsaddr"
|
||||
@@ -837,6 +838,42 @@ func TestTailscaleIPs(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestSystemDial verifies that Server.SystemDial carries the control
|
||||
// connection, by serving control only on an in-memory listener.
|
||||
func TestSystemDial(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(t.Context(), 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
_, control := startControl(t)
|
||||
|
||||
// Nothing listens on this host port, so control is only reachable
|
||||
// via SystemDial.
|
||||
const controlAddr = "127.0.0.1:1"
|
||||
ln := memnet.Listen(controlAddr)
|
||||
defer ln.Close()
|
||||
go http.Serve(ln, control)
|
||||
|
||||
s := &Server{
|
||||
Dir: t.TempDir(),
|
||||
ControlURL: "http://" + controlAddr,
|
||||
Hostname: "s1",
|
||||
Store: new(mem.Store),
|
||||
Ephemeral: true,
|
||||
SystemDial: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||
if addr == controlAddr {
|
||||
return ln.Dial(ctx, network, addr)
|
||||
}
|
||||
var d net.Dialer
|
||||
return d.DialContext(ctx, network, addr)
|
||||
},
|
||||
}
|
||||
defer s.Close()
|
||||
|
||||
if _, err := s.Up(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestListenerCleanup is a regression test to verify that s.Close doesn't
|
||||
// deadlock if a listener is still open.
|
||||
func TestListenerCleanup(t *testing.T) {
|
||||
|
||||
Reference in new issue
Block a user