From 4879fff29287853008451c94ba471783e98f3ba1 Mon Sep 17 00:00:00 2001 From: iBug Date: Wed, 11 Jan 2023 12:35:49 +0800 Subject: [PATCH] Use net.Dial to test if bind address is available --- cmd/cloudflared/tunnel/configuration.go | 32 ++++++++++++------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/cmd/cloudflared/tunnel/configuration.go b/cmd/cloudflared/tunnel/configuration.go index 0a2bb24b..ac2ff126 100644 --- a/cmd/cloudflared/tunnel/configuration.go +++ b/cmd/cloudflared/tunnel/configuration.go @@ -352,14 +352,8 @@ func prepareTunnelConfig( if err != nil { return nil, nil, err } - if ok, err := isIPHostLocal(edgeBindAddr); !ok { - if err != nil { - // There could be unforeseen reasons that net.InterfaceAddrs() may fail - // Better not to be fatal here, or it could be annoying for users - log.Warn().Msgf("Cannot determine if edge-bind-address is available: %v", err) - } else { - return nil, nil, fmt.Errorf("edge-bind-address is not local to this host: %s", edgeBindAddr) - } + if err := testIPBindable(edgeBindAddr); err != nil { + return nil, nil, fmt.Errorf("invalid edge-bind-address %s: %v", edgeBindAddr, err) } edgeIPVersion, err = adjustIPVersionByBindAddress(edgeIPVersion, edgeBindAddr) if err != nil { @@ -494,17 +488,21 @@ func parseConfigBindAddress(ipstr string) (net.IP, error) { return ip, nil } -func isIPHostLocal(ip net.IP) (bool, error) { - addrs, err := net.InterfaceAddrs() +func testIPBindable(ip net.IP) error { + var network, address string + if ip.To4() != nil { + network, address = "udp4", "127.0.0.1:4" + } else { + network, address = "udp6", "[::1]:4" + } + + dialer := net.Dialer{LocalAddr: &net.UDPAddr{IP: ip}} + conn, err := dialer.Dial(network, address) if err != nil { - return false, err + return err } - for _, addr := range addrs { - if ip.Equal(addr.(*net.IPNet).IP) { - return true, nil - } - } - return false, nil + conn.Close() + return nil } func adjustIPVersionByBindAddress(ipVersion allregions.ConfigIPVersion, ip net.IP) (allregions.ConfigIPVersion, error) {