Debian Codegraph @

whois.go

raw symbols SCIP
1 package whois
2
3 import (
4 "errors"
5 "io"
6 "net"
7 "strings"
8 "time"
9 )
10
11 const (
12 ianaWHOISServerAddress = "whois.iana.org:43"
13 )
14
15 var tldWithoutExpirationDate = []string{"at", "be", "ch", "co.at", "de", "eu", "nl", "or.at"}
16
17 type Client struct {
18 whoisServerAddress string
19
20 isCachingReferralWHOISServers bool
21 referralWHOISServersCache map[string]string
22 }
23
24 func NewClient() *Client {
25 return &Client{
26 whoisServerAddress: ianaWHOISServerAddress,
27 referralWHOISServersCache: make(map[string]string),
28 }
29 }
30
31 // WithReferralCache allows you to enable or disable the referral WHOIS server cache.
32 // While ianaWHOISServerAddress is the "entry point" for WHOIS queries, it sometimes has
33 // availability issues. One way to mitigate this is to cache the referral WHOIS server.
34 //
35 // This is disabled by default
36 func (c *Client) WithReferralCache(enabled bool) *Client {
37 c.isCachingReferralWHOISServers = enabled
38 if enabled {
39 // We'll set a couple of common ones right away to avoid unnecessary queries
40 c.referralWHOISServersCache = map[string]string{
41 "com": "whois.verisign-grs.com",
42 "io": "whois.nic.io",
43 "net": "whois.verisign-grs.com",
44 "org": "whois.publicinterestregistry.org",
45 "sh": "whois.nic.sh",
46 "uk": "whois.nic.uk",
47 "mx": "whois.mx",
48 "ro": "whois.rotld.ro",
49 "kr": "whois.krnic.net",
50 }
51 }
52 return c
53 }
54
55 func doesTLDHaveExpirationDate(e string) bool {
56 for _, a := range tldWithoutExpirationDate {
57 if a == e {
58 return true
59 }
60 }
61 return false
62 }
63
64 func (c *Client) Query(domain string) (string, error) {
65 parts := strings.Split(domain, ".")
66 domainExtension := parts[len(parts)-1]
67 if doesTLDHaveExpirationDate(domainExtension) {
68 return "", errors.New("domain extension " + domainExtension + " does not have an expiration date")
69 }
70 if c.isCachingReferralWHOISServers {
71 if cachedWHOISServer, ok := c.referralWHOISServersCache[domainExtension]; ok {
72 return c.query(cachedWHOISServer+":43", domain)
73 }
74 }
75 var output string
76 var err error
77 switch domainExtension {
78 case "ua":
79 if len(parts) > 2 && len(parts[len(parts)-2]) < 4 {
80 domainExtension = parts[len(parts)-2] + "." + domainExtension
81 }
82 output, err = c.query("whois."+domainExtension+":43", domain)
83 default:
84 output, err = c.query(c.whoisServerAddress, domainExtension)
85 }
86 if err != nil {
87 return "", err
88 }
89 if strings.Contains(output, "whois:") {
90 startIndex := strings.Index(output, "whois:") + 6
91 endIndex := strings.Index(output[startIndex:], "\n") + startIndex
92 whoisServer := strings.TrimSpace(output[startIndex:endIndex])
93 if referOutput, err := c.query(whoisServer+":43", domain); err == nil {
94 if c.isCachingReferralWHOISServers {
95 c.referralWHOISServersCache[domainExtension] = whoisServer
96 }
97 return referOutput, nil
98 }
99 return "", err
100 }
101 return output, nil
102 }
103
104 func (c *Client) query(whoisServerAddress, domain string) (string, error) {
105 connection, err := net.DialTimeout("tcp", whoisServerAddress, 10*time.Second)
106 if err != nil {
107 return "", err
108 }
109 defer connection.Close()
110 _ = connection.SetDeadline(time.Now().Add(5 * time.Second))
111 _, err = connection.Write([]byte(domain + "\r\n"))
112 if err != nil {
113 return "", err
114 }
115 output, err := io.ReadAll(connection)
116 if err != nil {
117 return "", err
118 }
119 return string(output), nil
120 }
121
122 type Response struct {
123 ExpirationDate time.Time
124 DomainStatuses []string
125 NameServers []string
126 }
127
128 // QueryAndParse tries to parse the response from the WHOIS server
129 // There is no standardized format for WHOIS responses, so this is an attempt at best.
130 //
131 // Being the selfish person that I am, I also only parse the fields that I need.
132 // If you need more fields, please open an issue or pull request.
133 func (c *Client) QueryAndParse(domain string) (*Response, error) {
134 text, err := c.Query(domain)
135 if err != nil {
136 return nil, err
137 }
138 response := Response{}
139 for _, line := range strings.Split(text, "\n") {
140 line = strings.TrimSpace(line)
141 valueStartIndex := strings.Index(line, ":")
142 if valueStartIndex == -1 {
143 continue
144 }
145 key := strings.ToLower(strings.TrimSpace(line[:valueStartIndex]))
146 value := strings.TrimSpace(line[valueStartIndex+1:])
147 if strings.Contains(key, "expir") {
148 switch {
149 case strings.HasSuffix(domain, ".pp.ua"):
150 response.ExpirationDate, _ = time.Parse("02-Jan-2006 15:04:05 MST", strings.ToUpper(value))
151 case strings.HasSuffix(domain, ".ua"):
152 response.ExpirationDate, _ = time.Parse("2006-01-02 15:04:05Z07", strings.ToUpper(value))
153 case strings.HasSuffix(domain, ".uk"):
154 response.ExpirationDate, _ = time.Parse("02-Jan-2006", strings.ToUpper(value))
155 case strings.HasSuffix(domain, ".cz"):
156 response.ExpirationDate, _ = time.Parse("02.01.2006", strings.ToUpper(value))
157 case strings.HasSuffix(domain, ".im"):
158 response.ExpirationDate, _ = time.Parse("02/01/2006 15:04:05", strings.ToUpper(value))
159 case strings.HasSuffix(domain, ".scot"):
160 if !strings.Contains(key, "registrar") {
161 response.ExpirationDate, _ = time.Parse(time.RFC3339, strings.ToUpper(value))
162 }
163 case strings.HasSuffix(domain, ".br"):
164 response.ExpirationDate, _ = time.Parse("20060102", strings.ToUpper(value))
165 case strings.HasSuffix(domain, ".cn"):
166 response.ExpirationDate, _ = time.Parse("2006-01-02 15:04:05", strings.ToUpper(value))
167 case strings.HasSuffix(domain, ".mx"):
168 response.ExpirationDate, _ = time.Parse(time.DateOnly, strings.ToUpper(value))
169 case strings.HasSuffix(domain, ".lt"):
170 response.ExpirationDate, _ = time.Parse(time.DateOnly, strings.ToUpper(value))
171 case strings.HasSuffix(domain, ".ro"):
172 response.ExpirationDate, _ = time.Parse(time.DateOnly, strings.ToUpper(value))
173 case strings.HasSuffix(domain, ".kr"):
174 response.ExpirationDate, _ = time.Parse("2006. 01. 02.", strings.ToUpper(value))
175 default:
176 response.ExpirationDate, _ = time.Parse(time.RFC3339, strings.ToUpper(value))
177 }
178 } else if key == "paid-till" {
179 // example for ru/su domains -> paid-till: 2024-05-26T21:00:00Z
180 if strings.HasSuffix(domain, ".ru") || strings.HasSuffix(domain, ".su") {
181 response.ExpirationDate, _ = time.Parse(time.RFC3339, strings.ToUpper(value))
182 }
183 } else if strings.Contains(key, "status") {
184 response.DomainStatuses = append(response.DomainStatuses, value)
185 } else if key == "state" {
186 // example for ru/su domains -> state: DELEGATED, VERIFIED
187 if strings.HasSuffix(domain, ".ru") || strings.HasSuffix(domain, ".su") {
188 response.DomainStatuses = strings.Split(value, ", ")
189 }
190 } else if strings.Contains(key, "name server") || strings.Contains(key, "nserver") || strings.Contains(key, "nameserver") {
191 response.NameServers = append(response.NameServers, value)
192 }
193 }
194 return &response, nil
195 }