-
Notifications
You must be signed in to change notification settings - Fork 0
/
cznic-checker.go
238 lines (189 loc) · 4.52 KB
/
cznic-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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package main
import (
"bufio"
"bytes"
"errors"
"flag"
"fmt"
"log"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
)
// BaseURL Url to send queries to.
const BaseURL = "https://www.nic.cz/whois/domain/"
// Politeness factor. Don't be evil.
const Politeness = 1 * time.Second
// HaystackCaptcha means the captcha is displayed.
const HaystackCaptcha = "Kontrolní kód"
// HaystackFree means the domain is free to register.
const HaystackFree = "nebyla nalezena"
// HaystackExpiration is used to find the expiration date offset.
const HaystackExpiration = "Datum expirace"
// ExpirationOffset = (the start of the date) - HaystackExpiration
const ExpirationOffset = 72
// ExpirationLength is length of the expiration date format.
const ExpirationLength = 10
// CheckResult holds the result of a domain check.
type CheckResult struct {
URL string
IsFree bool
Expiration time.Time
}
func getPageContent(url string) (string, error) {
response, e := http.Get(url)
if e != nil {
return "", e
}
if response.StatusCode != 200 {
return "", errors.New("Returned code " + strconv.Itoa(response.StatusCode))
}
defer response.Body.Close()
buf := new(bytes.Buffer)
buf.ReadFrom(response.Body)
return buf.String(), nil
}
func waitForUser() {
reader := bufio.NewReader(os.Stdin)
reader.ReadString('\n')
}
func strToDate(date string) (time.Time, error) {
str := fmt.Sprintf("%s-%s-%sT00:00:00.000Z", date[6:], date[3:5], date[0:2])
return time.Parse(time.RFC3339, str)
}
func reportDay(expiration int) string {
if expiration < 0 {
expiration = -expiration
}
switch {
case expiration == 0:
return "today"
case expiration == 1:
return "1 day"
default:
return strconv.Itoa(expiration) + " days"
}
}
func report(result *CheckResult) {
res := ""
if result.IsFree {
res = "Free"
} else {
exp := int((result.Expiration.Sub(time.Now())).Hours() / 24)
day := reportDay(exp)
switch {
case exp == 0:
res = fmt.Sprintf("Expires %s", day)
break
case exp < 0:
res = fmt.Sprintf("Expired %s ago", day)
break
default:
res = fmt.Sprintf("Expires in %s", day)
}
}
log.Printf("%s\t%s\n", result.URL, res)
}
func processURLResult(url, content string) (*CheckResult, error) {
ret := new(CheckResult)
ret.URL = url
ret.IsFree = strings.Contains(content, HaystackFree)
if ret.IsFree {
return ret, nil
}
index := strings.Index(content, HaystackExpiration)
sub := content[index+ExpirationOffset : index+ExpirationOffset+ExpirationLength]
var err error
ret.Expiration, err = strToDate(sub)
if err != nil {
return nil, err
}
return ret, nil
}
func normalizeCzURL(urlAddr string) (string, error) {
urlAddr = strings.TrimSpace(urlAddr)
if !strings.HasPrefix(urlAddr, "https://") {
urlAddr = "https://" + urlAddr
}
if !strings.HasSuffix(urlAddr, ".cz") {
urlAddr = urlAddr + ".cz"
}
parsed, e := url.Parse(urlAddr)
if e != nil {
return "", e
}
if strings.Count(parsed.Host, ".") > 1 {
return "", errors.New("You can check only second-level .cz domains")
}
return parsed.Host, nil
}
// CheckURL checks if a domain (url) is free to register.
func CheckURL(url string) (*CheckResult, error) {
content := ""
normalizedURL, err := normalizeCzURL(url)
if err != nil {
return nil, err
}
for {
query := BaseURL + normalizedURL
pageContent, err := getPageContent(query)
if err != nil {
return nil, err
}
if strings.Contains(pageContent, HaystackCaptcha) {
fmt.Printf("Go to %s and check the captcha.\nPress enter to continue.", query)
waitForUser()
} else {
content = pageContent
break
}
}
return processURLResult(normalizedURL, content)
}
func processURL(url string) {
result, err := CheckURL(url)
if err != nil {
log.Fatalf("%s\t%s", url, err)
} else {
report(result)
time.Sleep(Politeness)
}
}
func getUserURL() string {
reader := bufio.NewReader(os.Stdin)
fmt.Print("\nEnter domain: ")
domain, _ := reader.ReadString('\n')
return strings.Replace(domain, "\n", "", -1)
}
func startArgLoop(urls []string) {
for _, url := range urls {
processURL(url)
}
}
func startInteractiveLoop() {
for {
processURL(getUserURL())
}
}
func printUsage() {
fmt.Printf("Usage: %s domain1[.cz][ domain2[ domain3]...]\n", os.Args[0])
fmt.Println("Available arguments:")
flag.PrintDefaults()
}
func main() {
interactive := flag.Bool("i", false, "Interactive mode")
flag.Parse()
if *interactive {
fmt.Println("Press CTRL-C to quit.")
startInteractiveLoop()
} else {
if len(flag.Args()) > 0 {
startArgLoop(flag.Args())
} else {
printUsage()
}
}
}