This repository was archived by the owner on Aug 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathisit.go
More file actions
218 lines (201 loc) · 5.31 KB
/
Copy pathisit.go
File metadata and controls
218 lines (201 loc) · 5.31 KB
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
package main
import (
"fmt"
"github.com/urfave/cli"
"io/ioutil"
"net"
"net/http"
"os"
"strings"
"time"
)
/*
Talking directly to whois can be interesting,
but why not just "proxy" the requests through
a (hopefully) trust worthy source?
One of the possible benefits of doing this could be
to help circumvent whois rate-limiting.
Note: in the future I would like to support different
whois backends -- via directly making a traditional whois
query, or through other sources like the whois.com with command-line
flags to be able to change up or just compare your findings with
other sources.
*/
const WhoisDotComLink = "https://www.whois.com/whois/"
const IsUpDotMeLink = "http://downforeveryoneorjustme.com/"
/*
To help avoid some common problems when working directly with
the native golang net/http package, we can customize the Trasport
a bit to provide a sane-ish timeout.
Note: in the future, custom transport options could be supported
at the command-line to fine-tune the requests happening under the hood.
*/
var netTransport = &http.Transport{
Dial: (&net.Dialer{
Timeout: 5 * time.Second,
}).Dial,
TLSHandshakeTimeout: 5 * time.Second,
}
// The client which will make the http requests.
var netClient = &http.Client{
Timeout: time.Second * 10,
Transport: netTransport,
}
// Check is a given domain has been registered.
func IsRegistered(domain string) bool {
resp, err := netClient.Get(WhoisDotComLink + domain)
if err != nil {
return false // possibly bad/false assumption?
//fmt.Printf("%s", err)
//os.Exit(1)
}
defer resp.Body.Close()
bodyBytes, _ := ioutil.ReadAll(resp.Body)
if strings.Contains(string(bodyBytes), "Registrar") {
return true
}
return false
}
// Check if a given domain is up or not
func IsUp(domain string) bool {
resp, err := netClient.Get(IsUpDotMeLink + domain)
if err != nil {
return false
}
defer resp.Body.Close()
bodyBytes, _ := ioutil.ReadAll(resp.Body)
if strings.Contains(string(bodyBytes), "is up") {
return true
}
return false
}
// Check is a given domain is available ( to buy ).
func IsAvailable(domain string) bool {
if IsRegistered(domain) {
return false
}
return true
}
// Check is a given domain is resolvable ( to an IP address ).
func IsResolvable(domain string) bool {
_, err := net.LookupHost(domain)
if err != nil {
return false
}
return true
}
// Failure function to fire off when there are now command-line arguments.
func noArgumentGiven() {
fmt.Println("no domain given!")
os.Exit(1)
}
// A struct to hold the results for a query of any type.
type Result struct {
domain string
value bool
}
func main() {
app := cli.NewApp()
app.Name = "isit"
app.Version = "1.0.0"
app.Usage = "domain availability command-line utility"
app.Commands = []cli.Command{
{
Name: "available",
Aliases: []string{"a"},
Usage: "check if the given domain(s) are available",
Action: func(c *cli.Context) error {
results := make(chan Result)
argumentCount := len(c.Args())
if argumentCount > 0 {
for i := 0; i < argumentCount; i++ {
go func(c *cli.Context, index int) {
domain := c.Args().Get(index)
results <- Result{domain: domain, value: IsAvailable(domain)}
}(c, i)
}
for i := 0; i < argumentCount; i++ {
result := <-results
fmt.Println(result.value, "\t", result.domain)
}
} else {
noArgumentGiven()
}
return nil
},
},
{
Name: "registered",
Aliases: []string{"r"},
Usage: "check if the given domain(s) are registered",
Action: func(c *cli.Context) error {
results := make(chan Result)
argumentCount := len(c.Args())
if argumentCount > 0 {
for i := 0; i < argumentCount; i++ {
go func(c *cli.Context, index int) {
domain := c.Args().Get(index)
results <- Result{domain: domain, value: IsRegistered(domain)}
}(c, i)
}
for i := 0; i < argumentCount; i++ {
result := <-results
fmt.Println(result.value, "\t", result.domain)
}
} else {
noArgumentGiven()
}
return nil
},
},
{
Name: "resolvable",
Aliases: []string{"R"},
Usage: "check if the given domain(s) are resolvable",
Action: func(c *cli.Context) error {
results := make(chan Result)
argumentCount := len(c.Args())
if argumentCount > 0 {
for i := 0; i < argumentCount; i++ {
go func(c *cli.Context, index int) {
domain := c.Args().Get(index)
results <- Result{domain: domain, value: IsResolvable(domain)}
}(c, i)
}
for i := 0; i < argumentCount; i++ {
result := <-results
fmt.Println(result.value, "\t", result.domain)
}
} else {
noArgumentGiven()
}
return nil
},
},
{
Name: "up",
Aliases: []string{"u"},
Usage: "check if the given domain(s) are up",
Action: func(c *cli.Context) error {
results := make(chan Result)
argumentCount := len(c.Args())
if argumentCount > 0 {
for i := 0; i < argumentCount; i++ {
go func(c *cli.Context, index int) {
domain := c.Args().Get(index)
results <- Result{domain: domain, value: IsUp(domain)}
}(c, i)
}
for i := 0; i < argumentCount; i++ {
result := <-results
fmt.Println(result.value, "\t", result.domain)
}
} else {
noArgumentGiven()
}
return nil
},
},
}
app.Run(os.Args)
}