all: use parallel dialer for bootstrapping ip

So we don't have to depend on network probing for checking ipv4/ipv6
enabled, making ctrld working more stably.
This commit is contained in:
Cuong Manh Le
2023-04-25 01:36:51 +07:00
committed by Cuong Manh Le
parent f73cbde7a5
commit 0af7f64bca
7 changed files with 108 additions and 95 deletions
+1 -14
View File
@@ -13,7 +13,6 @@ import (
const (
controldIPv6Test = "ipv6.controld.io"
controldIPv4Test = "ipv4.controld.io"
bootstrapDNS = "76.76.2.0:53"
)
@@ -38,7 +37,6 @@ var probeStackDialer = &net.Dialer{
var (
stackOnce atomic.Pointer[sync.Once]
ipv6Enabled bool
canListenIPv6Local bool
hasNetworkUp bool
)
@@ -47,13 +45,8 @@ func init() {
stackOnce.Store(new(sync.Once))
}
func supportIPv4() bool {
_, err := probeStackDialer.Dial("tcp4", net.JoinHostPort(controldIPv4Test, "80"))
return err == nil
}
func supportIPv6(ctx context.Context) bool {
_, err := probeStackDialer.DialContext(ctx, "tcp6", net.JoinHostPort(controldIPv6Test, "80"))
_, err := probeStackDialer.DialContext(ctx, "tcp6", net.JoinHostPort(controldIPv6Test, "443"))
return err == nil
}
@@ -75,7 +68,6 @@ func probeStack() {
b.BackOff(context.Background(), err)
}
}
ipv6Enabled = supportIPv6(context.Background())
canListenIPv6Local = supportListenIPv6Local()
}
@@ -84,11 +76,6 @@ func Up() bool {
return hasNetworkUp
}
func SupportsIPv6() bool {
stackOnce.Load().Do(probeStack)
return ipv6Enabled
}
func SupportsIPv6ListenLocal() bool {
stackOnce.Load().Do(probeStack)
return canListenIPv6Local