forked from indiwtf/indiwtf-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
124 lines (100 loc) · 2.73 KB
/
main.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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
)
type stringSliceFlag []string
func (f *stringSliceFlag) String() string {
return strings.Join(*f, ", ")
}
func (f *stringSliceFlag) Set(value string) error {
*f = append(*f, value)
return nil
}
type DomainStatus struct {
Domain string `json:"domain"`
Status string `json:"status"`
IP string `json:"ip"`
}
// checkDomain sends an HTTP GET request to the API endpoint and returns the status and IP of the domain.
func checkDomain(domain string) (*DomainStatus, error) {
apiURL := fmt.Sprintf("https://indiwtf.upset.dev/api/check?domain=%s", url.QueryEscape(domain))
// Create an HTTP client with a custom User-Agent string
client := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
},
}
req, err := http.NewRequest("GET", apiURL, nil)
if err != nil {
return nil, err
}
// Set a custom User-Agent string
req.Header.Set("User-Agent", "indiwtf-cli/1.0")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var domainStatus DomainStatus
err = json.Unmarshal(body, &domainStatus)
if err != nil {
return nil, err
}
return &domainStatus, nil
}
func main() {
var domainFlags stringSliceFlag
// Customize usage message to provide instructions for running the program.
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [flags] [domain1] [domain2] ...\n", os.Args[0])
flag.PrintDefaults()
}
// Define a custom flag for specifying domain names.
flag.Var(&domainFlags, "d", "Domain names")
// Parse command-line flags.
flag.Parse()
domains := flag.Args()
// If domain names are provided using the -d flag, add them to the list of domains.
if len(domainFlags) > 0 {
domains = append(domains, domainFlags...)
}
// If no domain names are provided, show the usage and exit.
if len(domains) == 0 {
flag.Usage()
return
}
// Iterate over the domain names and perform the necessary checks.
for _, rawURL := range domains {
parsedURL, err := url.Parse(rawURL)
if err != nil {
fmt.Printf("Error parsing URL: %v\n", err)
continue
}
// If the scheme is empty, assume HTTPS as the default scheme.
if parsedURL.Scheme == "" {
rawURL = "https://" + rawURL
parsedURL, err = url.Parse(rawURL)
if err != nil {
fmt.Printf("Error parsing URL: %v\n", err)
continue
}
}
domainStatus, err := checkDomain(parsedURL.Hostname())
if err != nil {
fmt.Printf("Error checking domain %s: %v\n", parsedURL.Hostname(), err)
continue
}
fmt.Printf("Domain: %s | Status: %s | IP: %s\n", domainStatus.Domain, domainStatus.Status, domainStatus.IP)
}
}