ipn: don't listen on the unspecified address in test

To avoid the Mac firewall dialog of (test) death.

See 4521a59f30
which I added to help debug this.
pull/342/head
Brad Fitzpatrick 4 years ago
parent d2b7cb1e45
commit e1526b796e

@ -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)

@ -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

Loading…
Cancel
Save