forked from likexian/whois
-
Notifications
You must be signed in to change notification settings - Fork 0
/
whois.go
242 lines (200 loc) · 5.4 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
* Copyright 2014-2021 Li Kexian
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Go module for domain and ip whois information query
* https://www.likexian.com/
*/
package whois
import (
"fmt"
"io/ioutil"
"net"
"strconv"
"strings"
"time"
"golang.org/x/net/proxy"
)
const (
// defaultWhoisServer is iana whois server
defaultWhoisServer = "whois.iana.org"
// defaultWhoisPort is default whois port
defaultWhoisPort = "43"
// defaultTimeout is query default timeout
defaultTimeout = 30 * time.Second
// asnPrefix is asn prefix string
asnPrefix = "AS"
)
// DefaultClient is default whois client
var DefaultClient = NewClient()
// Client is whois client
type Client struct {
dialer proxy.Dialer
timeout time.Duration
elapsed time.Duration
}
// Version returns package version
func Version() string {
return "1.12.4"
}
// Author returns package author
func Author() string {
return "[Li Kexian](https://www.likexian.com/)"
}
// License returns package license
func License() string {
return "Licensed under the Apache License 2.0"
}
// Whois do the whois query and returns whois information
func Whois(domain string, servers ...string) (result string, err error) {
return DefaultClient.Whois(domain, servers...)
}
// New returns new whois client
func NewClient() *Client {
return &Client{
dialer: &net.Dialer{
Timeout: defaultTimeout,
},
timeout: defaultTimeout,
}
}
// SetDialer set query net dialer
func (c *Client) SetDialer(dialer proxy.Dialer) {
c.dialer = dialer
}
// SetTimeout set query timeout
func (c *Client) SetTimeout(timeout time.Duration) {
c.timeout = timeout
}
// Whois do the whois query and returns whois information
func (c *Client) Whois(domain string, servers ...string) (result string, err error) {
start := time.Now()
defer func() {
result = fmt.Sprintf("%s\n\n;; Query time: %d msec\n;; WHEN: %s\n",
strings.TrimRight(result, "\n"),
time.Since(start).Milliseconds(),
start.Format("Mon Jan 02 15:04:05 MST 2006"),
)
}()
domain = strings.Trim(strings.TrimSpace(domain), ".")
if domain == "" {
return "", ErrDomainEmpty
}
isASN := IsASN(domain)
if isASN {
if !strings.HasPrefix(strings.ToUpper(domain), asnPrefix) {
domain = asnPrefix + domain
}
}
if !strings.Contains(domain, ".") && !strings.Contains(domain, ":") && !isASN {
return c.rawQuery(domain, defaultWhoisServer)
}
var server string
if len(servers) > 0 && servers[0] != "" {
server = strings.ToLower(servers[0])
} else {
ext := getExtension(domain)
result, err := c.rawQuery(ext, defaultWhoisServer)
if err != nil {
return "", fmt.Errorf("whois: query for whois server failed: %w", err)
}
server = getServer(result)
if server == "" {
return "", fmt.Errorf("%w: %s", ErrWhoisServerNotFound, domain)
}
}
result, err = c.rawQuery(domain, server)
if err != nil {
return
}
refServer := getServer(result)
if refServer == "" || refServer == server {
return
}
data, err := c.rawQuery(domain, refServer)
if err == nil {
result += data
}
return
}
// rawQuery do raw query to the server
func (c *Client) rawQuery(domain, server string) (string, error) {
c.elapsed = 0
start := time.Now()
if server == "whois.arin.net" {
if IsASN(domain) {
domain = "a + " + domain
} else {
domain = "n + " + domain
}
}
// See: https://github.com/likexian/whois/issues/17
if server == "whois.godaddy" {
server = "whois.godaddy.com"
}
conn, err := c.dialer.Dial("tcp", net.JoinHostPort(server, defaultWhoisPort))
if err != nil {
return "", fmt.Errorf("whois: connect to whois server failed: %w", err)
}
defer conn.Close()
c.elapsed = time.Since(start)
_ = conn.SetWriteDeadline(time.Now().Add(c.timeout - c.elapsed))
_, err = conn.Write([]byte(domain + "\r\n"))
if err != nil {
return "", fmt.Errorf("whois: send to whois server failed: %w", err)
}
c.elapsed = time.Since(start)
_ = conn.SetReadDeadline(time.Now().Add(c.timeout - c.elapsed))
buffer, err := ioutil.ReadAll(conn)
if err != nil {
return "", fmt.Errorf("whois: read from whois server failed: %w", err)
}
c.elapsed = time.Since(start)
return string(buffer), nil
}
// getExtension returns extension of domain
func getExtension(domain string) string {
ext := domain
if net.ParseIP(domain) == nil {
domains := strings.Split(domain, ".")
ext = domains[len(domains)-1]
}
if strings.Contains(ext, "/") {
ext = strings.Split(ext, "/")[0]
}
return ext
}
// getServer returns server from whois data
func getServer(data string) string {
tokens := []string{
"Registrar WHOIS Server: ",
"whois: ",
}
for _, token := range tokens {
start := strings.Index(data, token)
if start != -1 {
start += len(token)
end := strings.Index(data[start:], "\n")
return strings.TrimSpace(data[start : start+end])
}
}
return ""
}
// IsASN returns if s is ASN
func IsASN(s string) bool {
s = strings.ToUpper(s)
s = strings.TrimPrefix(s, asnPrefix)
_, err := strconv.Atoi(s)
return err == nil
}