forked from likexian/whois
-
Notifications
You must be signed in to change notification settings - Fork 0
/
whois.go
138 lines (117 loc) · 2.84 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
/*
* Copyright 2014-2019 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 info query
* https://www.likexian.com/
*/
package whois
import (
"fmt"
"io/ioutil"
"net"
"strings"
"time"
)
// Query server const
const (
IP_WHOIS_SERVER = "whois.iana.org"
DOMAIN_WHOIS_SERVER = "whois-servers.net"
WHOIS_PORT = "43"
)
// Version returns package version
func Version() string {
return "1.0.3"
}
// 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 info
func Whois(domain string, servers ...string) (result string, err error) {
domain = strings.Trim(strings.TrimSpace(domain), ".")
if domain == "" {
err = fmt.Errorf("Domain is empty")
return
}
result, err = query(domain, servers...)
if err != nil {
return
}
token := "Registrar WHOIS Server:"
if IsIP(domain) {
token = "whois:"
}
start := strings.Index(result, token)
if start == -1 {
return
}
start += len(token)
end := strings.Index(result[start:], "\n")
server := strings.TrimSpace(result[start : start+end])
if server == "" {
return
}
tmpResult, err := query(domain, server)
if err != nil {
return
}
result += tmpResult
return
}
// query do the query
func query(domain string, servers ...string) (result string, err error) {
var server string
if len(servers) == 0 || servers[0] == "" {
if IsIP(domain) {
server = IP_WHOIS_SERVER
} else {
domains := strings.Split(domain, ".")
if len(domains) < 2 {
err = fmt.Errorf("Domain %s is invalid", domain)
return
}
server = domains[len(domains)-1] + "." + DOMAIN_WHOIS_SERVER
}
} else {
server = strings.ToLower(servers[0])
if server == "whois.arin.net" {
domain = "n + " + domain
}
}
conn, e := net.DialTimeout("tcp", net.JoinHostPort(server, WHOIS_PORT), time.Second*3)
if e != nil {
err = e
return
}
defer conn.Close()
conn.Write([]byte(domain + "\r\n"))
conn.SetReadDeadline(time.Now().Add(time.Second * 30))
buffer, e := ioutil.ReadAll(conn)
if e != nil {
err = e
return
}
result = string(buffer)
return
}
// IsIP returns bool is an IP
func IsIP(ip string) bool {
i := net.ParseIP(ip)
return i != nil
}