-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchecker.go
57 lines (46 loc) · 1004 Bytes
/
checker.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
package domaincheck
import (
"context"
"github.com/twiny/whois/v2"
)
type DomainStatus string
// String
func (s DomainStatus) String() string {
return string(s)
}
const (
NotApplicable DomainStatus = "N/A"
Available DomainStatus = "available"
Registered DomainStatus = "registered"
Premium DomainStatus = "premium"
)
// Checker
type Checker struct {
client *whois.Client
matcher *matcher
}
// NewChecker
func NewChecker() (*Checker, error) {
client, err := whois.NewClient(whois.Localhost)
if err != nil {
return nil, err
}
return &Checker{
client: client,
matcher: newMatcher(),
}, nil
}
// Check
func (c *Checker) Check(ctx context.Context, domain string) (DomainStatus, error) {
// get whois server of domain
server, err := c.client.WHOISHost(domain)
if err != nil {
return NotApplicable, err
}
// get whois response
resp, err := c.client.Lookup(ctx, domain, server)
if err != nil {
return NotApplicable, err
}
return c.matcher.match(resp)
}