1
0
mirror of https://github.com/schollz/croc.git synced 2024-12-02 23:22:23 +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() {
var err error
DEFAULT_RELAY, err = lookupIP(DEFAULT_RELAY)
DEFAULT_RELAY, err = lookupIPs(DEFAULT_RELAY)
if err == nil {
DEFAULT_RELAY += ":" + DEFAULT_PORT
} else {
DEFAULT_RELAY = ""
}
DEFAULT_RELAY6, err = lookupIP(DEFAULT_RELAY6)
DEFAULT_RELAY6, err = lookupIPs(DEFAULT_RELAY6)
if err == nil {
DEFAULT_RELAY6 = "[" + DEFAULT_RELAY6 + "]:" + DEFAULT_PORT
} 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{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
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)