-
Notifications
You must be signed in to change notification settings - Fork 13
/
dns.go
69 lines (65 loc) · 1.5 KB
/
dns.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"errors"
"net"
"strconv"
"github.com/tidwall/gjson"
"github.com/valyala/fasthttp"
)
var errPrivateIP = errors.New("Private IP")
var errNoSuitableIP = errors.New("No suitable IP")
var errNoAnswer = errors.New("No IP")
var dnsEndpointQs string
func getUsableIP(hostname, portStr string) (*net.TCPAddr, error) {
port, err := strconv.Atoi(portStr)
if err != nil {
return nil, err
}
ipp := net.ParseIP(hostname)
if ipp != nil {
if isPrivateIP(ipp) {
return nil, errPrivateIP
}
return &net.TCPAddr{
IP: ipp,
Port: port,
}, nil
}
req := fasthttp.AcquireRequest()
req.Header.Set("Accept-Encoding", "gzip, deflate")
req.SetRequestURI(dnsEndpointQs)
req.URI().QueryArgs().Set("name", hostname)
resp := fasthttp.AcquireResponse()
err = httpClient.DoTimeout(req, resp, httpClientTimeout)
fasthttp.ReleaseRequest(req)
if err != nil {
fasthttp.ReleaseResponse(resp)
return nil, err
}
body, err := getResponseBody(resp)
fasthttp.ReleaseResponse(resp)
if err != nil {
return nil, err
}
answers := gjson.GetBytes(body, "Answer")
if answers.Exists() == false || answers.IsArray() == false {
return nil, errNoAnswer
}
for _, answer := range answers.Array() {
ip := answer.Get("data").String()
ipp = net.ParseIP(ip)
if ipp == nil || isPrivateIP(ipp) {
continue
}
tcpAddr := &net.TCPAddr{
IP: ipp,
Port: port,
}
conn, err := net.DialTCP("tcp4", nil, tcpAddr)
if err == nil {
conn.Close()
return tcpAddr, nil
}
}
return nil, errNoSuitableIP
}