faster dns resolution on startup

This commit is contained in:
Zack Scholl 2020-11-17 10:46:49 -08:00
parent 284af7fc7a
commit 1da2f58a53
1 changed files with 14 additions and 16 deletions

View File

@ -2,7 +2,6 @@ package models
import ( import (
"context" "context"
"fmt"
"net" "net"
"time" "time"
) )
@ -19,18 +18,19 @@ var (
) )
func init() { func init() {
DEFAULT_RELAY, _ = lookupIP(DEFAULT_RELAY) var err error
DEFAULT_RELAY += ":" + DEFAULT_PORT DEFAULT_RELAY, err = lookupIP(DEFAULT_RELAY)
DEFAULT_RELAY6, _ = lookupIP(DEFAULT_RELAY6) if err == nil {
DEFAULT_RELAY6 += "[" + DEFAULT_RELAY6 + "]:" + DEFAULT_PORT DEFAULT_RELAY += ":" + DEFAULT_PORT
// iprecords, _ := lookupIP(DEFAULT_RELAY) } else {
// for _, ip := range iprecords { DEFAULT_RELAY = ""
// DEFAULT_RELAY = ip.String() + ":" + DEFAULT_PORT }
// } DEFAULT_RELAY6, err = lookupIP(DEFAULT_RELAY6)
// iprecords, _ = lookupIP(DEFAULT_RELAY6) if err == nil {
// for _, ip := range iprecords { DEFAULT_RELAY6 = "[" + DEFAULT_RELAY6 + "]:" + DEFAULT_PORT
// DEFAULT_RELAY6 = "[" + ip.String() + "]:" + DEFAULT_PORT } else {
// } DEFAULT_RELAY6 = ""
}
} }
func lookupIP(address string) (ipaddress string, err error) { func lookupIP(address string) (ipaddress string, err error) {
@ -40,13 +40,11 @@ func lookupIP(address string) (ipaddress string, err error) {
d := net.Dialer{ d := net.Dialer{
Timeout: time.Millisecond * time.Duration(10000), Timeout: time.Millisecond * time.Duration(10000),
} }
return d.DialContext(ctx, "udp", "1.1.1.1") return d.DialContext(ctx, "udp", "1.1.1.1:53")
}, },
} }
ip, err := r.LookupHost(context.Background(), address) ip, err := r.LookupHost(context.Background(), address)
fmt.Println(ip)
if err != nil { if err != nil {
fmt.Println(err)
return return
} }
ipaddress = ip[0] ipaddress = ip[0]