From dbc4a08ad078baec5d7c711999c2d20751f2c014 Mon Sep 17 00:00:00 2001 From: Zack Scholl Date: Tue, 17 Nov 2020 10:29:52 -0800 Subject: [PATCH] use google for lookup --- src/models/constants.go | 46 +++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/src/models/constants.go b/src/models/constants.go index 1c412190..dff1889e 100644 --- a/src/models/constants.go +++ b/src/models/constants.go @@ -1,6 +1,11 @@ package models -import "net" +import ( + "context" + "fmt" + "net" + "time" +) // TCP_BUFFER_SIZE is the maximum packet size const TCP_BUFFER_SIZE = 1024 * 64 @@ -14,12 +19,35 @@ var ( ) func init() { - iprecords, _ := net.LookupIP(DEFAULT_RELAY) - for _, ip := range iprecords { - DEFAULT_RELAY = ip.String() + ":" + DEFAULT_PORT - } - iprecords, _ = net.LookupIP(DEFAULT_RELAY6) - for _, ip := range iprecords { - DEFAULT_RELAY6 = "[" + ip.String() + "]:" + DEFAULT_PORT - } + DEFAULT_RELAY, _ = lookupIP(DEFAULT_RELAY) + DEFAULT_RELAY += ":" + DEFAULT_PORT + DEFAULT_RELAY6, _ = lookupIP(DEFAULT_RELAY6) + DEFAULT_RELAY6 += "[" + DEFAULT_RELAY6 + "]:" + DEFAULT_PORT + // iprecords, _ := lookupIP(DEFAULT_RELAY) + // for _, ip := range iprecords { + // DEFAULT_RELAY = ip.String() + ":" + DEFAULT_PORT + // } + // iprecords, _ = lookupIP(DEFAULT_RELAY6) + // for _, ip := range iprecords { + // DEFAULT_RELAY6 = "[" + ip.String() + "]:" + DEFAULT_PORT + // } +} + +func lookupIP(address 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), + } + return d.DialContext(ctx, "udp", "8.8.8.8:53") + }, + } + ip, err := r.LookupHost(context.Background(), address) + if err != nil { + fmt.Println(err) + return + } + ipaddress = ip[0] + return }