-
Notifications
You must be signed in to change notification settings - Fork 1
/
whois.go
157 lines (136 loc) · 3.41 KB
/
whois.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"io/ioutil"
"net"
"strings"
"time"
)
const (
whoisPort = "43"
)
var (
rirs = []string{
"whois.ripe.net",
"whois.arin.net",
"whois.apnic.net",
"whois.lacnic.net",
"whois.afrinic.net",
}
badNetnames = []string{"NON-RIPE-NCC-MANAGED-ADDRESS-BLOCK", "IANA-NETBLOCK", "ERX-NETBLOCK", "CIDR-BLOCK"}
referField = []string{"refer: ", "ReferralServer: "}
netnameField = []string{"NetName: ", "netname: "}
originASField = []string{"OriginAS: ", "origin: ", "aut-num: "}
countryField = []string{"Country: ", "country: "}
)
// WhoisInfo contains information acquired from a whois request.
type WhoisInfo struct {
Address, Netname, AS, Country string
}
// GetWhoisInfo returns information acquired from a whois request.
func GetWhoisInfo(host string) (info WhoisInfo, err error) {
if isLocal(host) {
return WhoisInfo{Netname: "local"}, nil
}
replies := make(chan string, len(rirs))
for _, rir := range rirs {
go getRawInfo(host, rir, replies)
}
maxFound := 0
for range rirs {
reply := <-replies
result, found := parseInfo(host, reply)
if found > maxFound {
maxFound = found
info = result
}
}
return
}
func isLocal(host string) bool {
ip := net.ParseIP(host)
if ip == nil {
return false
}
if ip4 := ip.To4(); ip4 != nil {
return ip4[0] == 10 ||
(ip4[0] == 172 && ip4[1]&0xf0 == 16) ||
(ip4[0] == 192 && ip4[1] == 168)
}
return len(ip) == 16 && ip[0]&0xfe == 0xfc
}
func parseInfo(host, reply string) (info WhoisInfo, found int) {
var netname, as, country string
if netname = parseField(reply, netnameField); netname != "" {
found++
}
if as = parseField(reply, originASField); as != "" {
found++
}
if country = parseField(reply, countryField); country != "" {
found++
}
as = strings.TrimPrefix(as, "AS")
if _, pos := findAny(netname, badNetnames); pos != -1 {
return WhoisInfo{}, 0
}
return WhoisInfo{Address: host, Netname: netname, AS: as, Country: country}, found
}
func getRawInfo(host, server string, replies chan<- string) {
reply, err := followReferral(host, server)
if err != nil {
replies <- ""
}
replies <- reply
}
func followReferral(host string, referral string) (reply string, err error) {
reply, err = queryServer(host, referral)
if err != nil {
return "", err
}
foundServer := parseField(reply, referField)
if i := strings.LastIndex(foundServer, "/"); i != -1 {
foundServer = foundServer[i+1:]
}
if strings.Index(foundServer, "rwhois") != -1 {
foundServer = ""
}
if foundServer == "" {
return
}
return followReferral(host, foundServer)
}
func queryServer(host string, server string) (reply string, err error) {
conn, err := net.Dial("tcp", net.JoinHostPort(server, whoisPort))
if err != nil {
return "", err
}
defer conn.Close()
_, err = conn.Write([]byte(host + "\r\n"))
if err != nil {
return "", err
}
err = conn.SetReadDeadline(time.Now().Add(time.Second * 2))
buffer, err := ioutil.ReadAll(conn)
if err != nil {
return "", err
}
return string(buffer), nil
}
func findAny(str string, tokens []string) (token string, pos int) {
for _, token := range tokens {
pos = strings.Index(str, token)
if pos != -1 {
return token, pos
}
}
return "", -1
}
func parseField(reply string, fieldVariants []string) (value string) {
token, pos := findAny(reply, fieldVariants)
if pos == -1 {
return
}
pos += len(token) + 2
len := strings.Index(reply[pos:], "\n")
return strings.TrimSpace(reply[pos : pos+len])
}