-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
185 lines (153 loc) · 4.07 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
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
package main
import (
"encoding/json"
"flag"
"fmt"
"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"`
}
type Config struct {
Token string `json:"token"`
}
var token string
var configFilePath string
func init() {
// Define the path to the configuration file in the user's home directory
configFilePath = getHomeDir() + "/.indiwtf/config.json"
// Load the API token from the configuration file, if available.
config := loadConfig()
token = config.Token
}
// getHomeDir returns the user's home directory
func getHomeDir() string {
home, err := os.UserHomeDir()
if err != nil {
fmt.Println("Error getting user's home directory:", err)
os.Exit(1)
}
return home
}
// loadConfig loads the API token from a configuration file.
func loadConfig() Config {
config := Config{}
file, err := os.Open(configFilePath)
if err != nil {
// If the file doesn't exist, return an empty configuration.
return config
}
defer file.Close()
decoder := json.NewDecoder(file)
err = decoder.Decode(&config)
if err != nil {
// If there is an error decoding the file, return an empty configuration.
return config
}
return config
}
// saveConfig saves the API token to a configuration file.
func saveConfig(config Config) error {
// Ensure the directory exists
configDir := getHomeDir() + "/.indiwtf"
err := os.MkdirAll(configDir, os.ModePerm)
if err != nil {
return err
}
file, err := os.Create(configFilePath)
if err != nil {
return err
}
defer file.Close()
encoder := json.NewEncoder(file)
return encoder.Encode(config)
}
// checkDomain sends an HTTP GET request to the API endpoint with the token and returns the status and IP of the domain.
func checkDomain(domain string) (*DomainStatus, error) {
if token == "" {
fmt.Println("API token is required. Please enter your API token (https://indiwtf.com/pricing):")
fmt.Scanln(&token)
config := Config{
Token: token,
}
err := saveConfig(config)
if err != nil {
fmt.Printf("Error saving the API token to the configuration file: %v\n", err)
}
}
apiURL := fmt.Sprintf("https://indiwtf.com/api/check?domain=%s&token=%s", url.QueryEscape(domain), token)
// 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()
var domainStatus DomainStatus
err = json.NewDecoder(resp.Body).Decode(&domainStatus)
if err != nil {
return nil, err
}
return &domainStatus, nil
}
func main() {
// Instructions for running the program.
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [domain1] [domain2] ...\n", os.Args[0])
flag.PrintDefaults()
}
// Parse command-line flags.
flag.Parse()
domains := flag.Args()
// 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)
}
}