-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathwhois.go
48 lines (41 loc) · 1.02 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
package whois
import (
"fmt"
"net/url"
"strings"
"github.com/zonedb/zonedb"
)
// Fetch queries a whois server and returns a Response.
func Fetch(query string) (*Response, error) {
req, err := NewRequest(query)
if err != nil {
return nil, err
}
return DefaultClient.Fetch(req)
}
// Server returns the whois server and optional URL for a given query.
// Returns an error if it cannot resolve query to any known host.
func Server(query string) (string, string, error) {
// Queries on TLDs always against IANA
if strings.Index(query, ".") < 0 {
return IANA, "", nil
}
z := zonedb.PublicZone(query)
if z == nil {
return "", "", fmt.Errorf("no public zone found for %s", query)
}
// Try whois URL first (these are relatively rare)
wu := z.WhoisURL()
if wu != "" {
u, err := url.Parse(wu)
if err == nil && u.Host != "" {
return u.Host, wu, nil
}
}
// Then try host (more common)
h := z.WhoisServer()
if h != "" {
return h, "", nil
}
return "", "", fmt.Errorf("no whois server found for %s", query)
}