-
Notifications
You must be signed in to change notification settings - Fork 13
/
check.go
143 lines (114 loc) · 2.67 KB
/
check.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
package available
import (
"fmt"
"strings"
"github.com/domainr/whois"
"golang.org/x/net/publicsuffix"
)
/* SafeDomain utilizes the badtld checklist as a triage step,
then returns whether the domain is available and/or if the
domain has a "bad" tld.
*/
func SafeDomain(domain string) (available, badtld bool) {
available = false
domain = setDomain(domain)
tld, icann := publicsuffix.PublicSuffix(domain)
badtld = badTLD(tld)
if icann == true {
query, err := publicsuffix.EffectiveTLDPlusOne(domain)
if err != nil {
return
}
if !badtld {
available = match(tld, getWhois(tld, query))
}
}
return available, badtld
}
/* Domain returns if the domain is available or not.
This function does not have a triage step, and will
result in testing each TLD with one of the default
responses.
*/
func Domain(domain string) (available bool) {
available = false
domain = setDomain(domain)
tld, icann := publicsuffix.PublicSuffix(domain)
if icann == true {
query, err := publicsuffix.EffectiveTLDPlusOne(domain)
if err != nil {
return
}
available = match(tld, getWhois(tld, query))
}
return available
}
func setDomain(domain string) string {
if strings.Contains(domain, "://") {
domain = strings.Split(domain, "://")[1]
}
if len(domain) > 1 {
if domain[len(domain)-1:] == "." {
domain = domain[:len(domain)-1]
}
}
return strings.ToLower(domain)
}
func match(tld, resp string) (available bool) {
available = false
fp := fingerprints()
// .ca & .lt have opposite fingerprints
if tld == "ca" || tld == "lt" {
if !strings.Contains(resp, fp[tld]) {
available = true
}
}
/* Checks if the .tld is in our fingerprint list
Then checks if the fingerprint is in the whois
response data */
if fp[tld] != "" {
if strings.Contains(resp, fp[tld]) {
available = true
}
} else {
/* If the .tld isn't in our fingerprint list,
this is the last resort options to check a
list of possible responses.*/
afp := all_fingerprints()
for _, f := range afp {
if strings.Contains(resp, f) {
available = true
break
}
}
}
return available
}
// Makes whois request, returns resposne as string
func getWhois(tld string, domain string) (response string) {
req, err := whois.NewRequest(domain)
if err != nil {
return
}
if tld == "tv" {
req.Host = "whois.nic.tv"
req.Body = []byte(fmt.Sprintf("%s\r\n", req.Query))
}
resp, err := whois.DefaultClient.Fetch(req)
if err != nil {
return
}
return fmt.Sprintf("%s", resp)
}
// Checks if the TLD is apart of our "bad tld" list
func badTLD(tld string) (bad bool) {
bad = false
tlds := badtlds()
for _, badtld := range tlds {
if tld == badtld {
bad = true
break
}
}
return bad
}