forked from makazeu/AnotherSteamCommunityFix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlookup.go
More file actions
65 lines (56 loc) · 1.37 KB
/
Copy pathlookup.go
File metadata and controls
65 lines (56 loc) · 1.37 KB
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
package AnotherSteamCommunityFix
import (
"errors"
"log"
"math/rand"
"net"
"net/http"
"time"
"github.com/bitly/go-simplejson"
"github.com/miekg/dns"
)
const RETRY = 5
func init() {
rand.Seed(time.Now().UnixNano())
}
func DnsLookUp(domainName string, dnsList map[string]string) (net.IP, error) {
client := &dns.Client{Net: "tcp"}
msg := &dns.Msg{}
msg.SetQuestion(domainName+".", dns.TypeA)
for dnsName, dnsAddress := range dnsList {
for i := 1; i <= RETRY; i++ {
r, t, err := client.Exchange(msg, dnsAddress)
if err == nil && len(r.Answer) > 0 {
log.Printf("使用%s解析域名成功,耗时: %v\n", dnsName, t)
return r.Answer[rand.Int()%len(r.Answer)].(*dns.A).A, nil
}
}
log.Printf("使用%s解析域名失败...", dnsName)
}
return nil, errors.New("域名解析失败")
}
func HttpLookup(domain string) (net.IP, error) {
client := &http.Client{Timeout: 15 * time.Second}
r, err := client.Get("https://dns-api.org/A/" + domain)
if err != nil {
return nil, err
}
defer r.Body.Close()
var ip string
json, err := simplejson.NewFromReader(r.Body)
if err != nil {
return nil, err
}
arr, err := json.Array()
if err != nil {
return nil, err
}
for i := range arr {
ans := json.GetIndex(i)
ip, err = ans.Get("value").String()
if err == nil {
return net.ParseIP(ip), nil
}
}
return nil, errors.New("could not get the IP address")
}