1
0
mirror of https://github.com/schollz/croc.git synced 2024-12-11 06:35:01 +03:00

Merge branch 'master' of github.com:schollz/croc

This commit is contained in:
Zack Scholl 2021-02-19 18:30:03 +00:00
commit b19a028aea

View File

@ -19,13 +19,13 @@ var (
func init() { func init() {
var err error var err error
DEFAULT_RELAY, err = lookupIP(DEFAULT_RELAY) DEFAULT_RELAY, err = lookupIPs(DEFAULT_RELAY)
if err == nil { if err == nil {
DEFAULT_RELAY += ":" + DEFAULT_PORT DEFAULT_RELAY += ":" + DEFAULT_PORT
} else { } else {
DEFAULT_RELAY = "" DEFAULT_RELAY = ""
} }
DEFAULT_RELAY6, err = lookupIP(DEFAULT_RELAY6) DEFAULT_RELAY6, err = lookupIPs(DEFAULT_RELAY6)
if err == nil { if err == nil {
DEFAULT_RELAY6 = "[" + DEFAULT_RELAY6 + "]:" + DEFAULT_PORT DEFAULT_RELAY6 = "[" + DEFAULT_RELAY6 + "]:" + DEFAULT_PORT
} else { } else {
@ -33,14 +33,32 @@ func init() {
} }
} }
func lookupIP(address string) (ipaddress string, err error) { func lookupIPs(address string) (ipaddress string, err error) {
var publicDns = []string{"1.1.1.1", "8.8.8.8", "8.8.4.4", "1.0.0.1", "8.26.56.26", "208.67.222.222", "208.67.220.220"}
result := make(chan string, len(publicDns))
for _, dns := range publicDns {
go func(dns string) {
s, _ := lookupIP(address, dns)
result <- s
}(dns)
}
for i := 0; i < len(publicDns); i++ {
ipaddress = <-result
if ipaddress != "" {
return
}
}
return
}
func lookupIP(address, dns string) (ipaddress string, err error) {
r := &net.Resolver{ r := &net.Resolver{
PreferGo: true, PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) { Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
d := net.Dialer{ d := net.Dialer{
Timeout: time.Millisecond * time.Duration(10000), Timeout: time.Millisecond * time.Duration(1000),
} }
return d.DialContext(ctx, "udp", "1.1.1.1:53") return d.DialContext(ctx, "udp", dns+":53")
}, },
} }
ip, err := r.LookupHost(context.Background(), address) ip, err := r.LookupHost(context.Background(), address)