From e1526b796e128b0b8232efb6d60d436cec9fd6ad Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Tue, 28 Apr 2020 19:20:02 -0700 Subject: [PATCH] ipn: don't listen on the unspecified address in test To avoid the Mac firewall dialog of (test) death. See https://github.com/tailscale/go/commit/4521a59f30cb4272f85bc05a38785624cad3e942 which I added to help debug this. --- ipn/e2e_test.go | 8 ++++++++ wgengine/magicsock/magicsock.go | 16 ++++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/ipn/e2e_test.go b/ipn/e2e_test.go index 7966f557e..63d74e661 100644 --- a/ipn/e2e_test.go +++ b/ipn/e2e_test.go @@ -13,6 +13,7 @@ import ( "net/http/cookiejar" "net/http/httptest" "net/url" + "os" "strings" "testing" "time" @@ -26,6 +27,13 @@ import ( "tailscale.io/control" // not yet released ) +func init() { + // Hacky way to signal to magicsock for now not to bind on the + // unspecified address. TODO(bradfitz): clean up wgengine's + // constructors. + os.Setenv("IN_TS_TEST", "1") +} + func TestIPN(t *testing.T) { testy.FixLogs(t) defer testy.UnfixLogs(t) diff --git a/wgengine/magicsock/magicsock.go b/wgengine/magicsock/magicsock.go index 6431bdcb9..0d95a00ba 100644 --- a/wgengine/magicsock/magicsock.go +++ b/wgengine/magicsock/magicsock.go @@ -1487,16 +1487,20 @@ func (c *Conn) initialBind() error { } func (c *Conn) bind1(ruc **RebindingUDPConn, which string) error { + host := "" + if v, _ := strconv.ParseBool(os.Getenv("IN_TS_TEST")); v { + host = "127.0.0.1" + } var pc net.PacketConn var err error if c.pconnPort == 0 && DefaultPort != 0 { - pc, err = net.ListenPacket(which, fmt.Sprintf(":%d", DefaultPort)) + pc, err = net.ListenPacket(which, fmt.Sprintf("%s:%d", host, DefaultPort)) if err != nil { c.logf("magicsock: bind: default port %s/%v unavailable; picking random", which, DefaultPort) } } if pc == nil { - pc, err = net.ListenPacket(which, fmt.Sprintf(":%d", c.pconnPort)) + pc, err = net.ListenPacket(which, fmt.Sprintf("%s:%d", host, c.pconnPort)) } if err != nil { c.logf("magicsock: bind(%s/%v): %v", which, c.pconnPort, err) @@ -1512,12 +1516,16 @@ func (c *Conn) bind1(ruc **RebindingUDPConn, which string) error { // Rebind closes and re-binds the UDP sockets. // It should be followed by a call to ReSTUN. func (c *Conn) Rebind() { + host := "" + if v, _ := strconv.ParseBool(os.Getenv("IN_TS_TEST")); v { + host = "127.0.0.1" + } if c.pconnPort != 0 { c.pconn4.mu.Lock() if err := c.pconn4.pconn.Close(); err != nil { c.logf("magicsock: link change close failed: %v", err) } - packetConn, err := net.ListenPacket("udp4", fmt.Sprintf(":%d", c.pconnPort)) + packetConn, err := net.ListenPacket("udp4", fmt.Sprintf("%s:%d", host, c.pconnPort)) if err == nil { c.logf("magicsock: link change rebound port: %d", c.pconnPort) c.pconn4.pconn = packetConn.(*net.UDPConn) @@ -1528,7 +1536,7 @@ func (c *Conn) Rebind() { c.pconn4.mu.Unlock() } c.logf("magicsock: link change, binding new port") - packetConn, err := net.ListenPacket("udp4", ":0") + packetConn, err := net.ListenPacket("udp4", host+":0") if err != nil { c.logf("magicsock: link change failed to bind new port: %v", err) return