-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.go
168 lines (136 loc) · 3.23 KB
/
client.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
package whois
import (
"bytes"
"context"
_ "embed"
"fmt"
"io"
"net"
"regexp"
"strings"
"time"
"github.com/goccy/go-yaml"
"golang.org/x/net/proxy"
"golang.org/x/net/publicsuffix"
)
var (
//go:embed db.yaml
whoisdb []byte
domainRegExp = regexp.MustCompile(`^(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,24})$`)
defaultDialer = &net.Dialer{
Timeout: 15 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}
)
type (
Client struct {
dialer proxy.Dialer
db map[string]string
}
Response struct {
Domain string
Name string
TLD string
WHOISHost string
WHOISRaw string
}
)
// NewClient: create a new WHOIS client, if dialer is nil, it will use the default dialer
func NewClient(dialer proxy.Dialer) (*Client, error) {
var db = map[string]string{}
if err := yaml.Unmarshal(whoisdb, &db); err != nil {
return nil, err
}
if dialer == nil {
dialer = defaultDialer
}
return &Client{
db: db,
dialer: dialer,
}, nil
}
// Query: return raw WHOIS information for a domain
func (c *Client) Query(ctx context.Context, domain string) (Response, error) {
var response Response
name, tld, whoisHost, err := c.split(domain)
if err != nil {
return response, err
}
domain = strings.Join([]string{name, tld}, ".")
var (
text string
lookupErr error
result = make(chan string, 1)
)
go func() {
defer close(result)
text, lookupErr = c.lookup(domain, whoisHost)
result <- text
}()
for {
select {
case <-ctx.Done():
return response, ctx.Err()
case text := <-result:
if lookupErr != nil {
return response, err
}
response.Domain = domain
response.Name = name
response.TLD = tld
response.WHOISHost = whoisHost
response.WHOISRaw = text
return response, nil
}
}
}
func (c *Client) lookup(domain, whoisHost string) (string, error) {
addr := net.JoinHostPort(whoisHost, "43") // WHOIS server address at port 43
conn, err := c.dialer.Dial("tcp", addr)
if err != nil {
return "", err
}
defer conn.Close()
var buff bytes.Buffer
buff.WriteString(domain)
buff.WriteString("\r\n")
if _, err := conn.Write(buff.Bytes()); err != nil {
return "", err
}
resp, err := io.ReadAll(conn)
if err != nil {
return "", err
}
if len(resp) == 0 {
return "", fmt.Errorf("empty reponse")
}
return strings.Replace(string(resp), "\r", "", -1), nil
}
// split: validate and split domain: name, tld, server or error
func (c *Client) split(raw string) (string, string, string, error) {
if !domainRegExp.MatchString(raw) {
return "", "", "", fmt.Errorf("invalid domain: %s", raw)
}
tld, icann := publicsuffix.PublicSuffix(raw)
if !icann {
return "", "", "", fmt.Errorf("unsupported TLD: %s", tld)
}
server, found := c.db[tld]
if !found {
return "", "", "", fmt.Errorf("unsupported TLD: %s", tld)
}
trimmedDomain := strings.TrimSuffix(raw, "."+tld)
// Split the raw domain into parts.
// e.g. www.google.com -> www.google -> google
// If no parts, the trimmed domain itself is the name.
// Otherwise, the name is the last part in array.
parts := strings.Split(trimmedDomain, ".")
var name string
if len(parts) == 0 {
name = trimmedDomain
} else {
name = parts[len(parts)-1]
}
return name, tld, server, nil
}