-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
135 lines (108 loc) · 3.15 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
package main
import (
"fmt"
"log"
"os/exec"
"os"
"net/http"
"net"
"net/url"
"strings"
"io/ioutil"
"flag"
)
//Let's Troubleshoot some endpoints!
func main() {
verbose := flag.Bool("v", false, "Turn on verbose output")
flag.Parse()
if flag.Arg(0) == "" {
fmt.Println("ERROR! Need to provide an endpoint to test. IE: https://google.com")
flag.PrintDefaults()
os.Exit(1)
}
var url_endpoint string = flag.Arg(0)
parsedUrl, err := url.Parse(url_endpoint)
if err != nil {
log.Fatal(err, "ERROR! Check primary URL!")
}
if strings.Contains(url_endpoint, "://") {
fmt.Println("NetTroubleshoot Starting......")
} else {
log.Fatal("The URL must contain a scheme! IE: http://, https://, or tcp://")
}
parsedUrlHost, parsedUrlPort, _ := net.SplitHostPort(parsedUrl.Host)
if parsedUrlPort == "" {
if parsedUrl.Scheme == "https" {
parsedUrlPort = "443"
} else if parsedUrl.Scheme == "http" {
parsedUrlPort = "80"
} else {
fmt.Println("No port provided, defaulting to 80")
parsedUrlPort = "80"
}
}
if parsedUrlHost == "" {
parsedUrlHost = parsedUrl.Host
}
if parsedUrl.Scheme == "" {
parsedUrl.Scheme = "http"
}
fmt.Println("Troubleshooting connection to", parsedUrlHost)
fmt.Println("=================================================")
fmt.Println(dnsLookup(parsedUrlHost))
fmt.Println("=================================================")
fmt.Println(dnsLookupPublic(parsedUrlHost))
fmt.Println("=================================================")
fmt.Println(pingEndpoint(parsedUrlHost))
fmt.Println("=================================================")
fmt.Println("HTTP Request to,", url_endpoint, "HTTP Status Code: ", checkHTTPResponse(url_endpoint))
if *verbose == true {
fmt.Println("Verbose HTTP Request Output: ", checkHTTTPResponseVerbose(url_endpoint))
}
}
func checkHTTPResponse(host string) int {
fmt.Println("Calling endpoint, ", host)
response, err := http.Get(host)
if err != nil {
log.Fatal("An error has occurred!")
}
defer response.Body.Close()
return response.StatusCode
}
func checkHTTTPResponseVerbose(host string) string {
fmt.Println("Calling endpoint, ", host)
response, err := http.Get(host)
if err != nil {
log.Fatal("An error has occurred!")
}
defer response.Body.Close()
body, _ := ioutil.ReadAll(response.Body)
return string(body)
}
func dnsLookup(endpoint string) string {
fmt.Println(" Performing DNS Lookup on ", endpoint)
dns, err := exec.Command("nslookup", endpoint).Output()
if err != nil {
log.Fatal(err)
}
fmt.Println("DNS lookup successfully completed on ", endpoint)
return string(dns)
}
func dnsLookupPublic(endpoint string) string {
fmt.Println("Performing Public DNS Lookup on ", endpoint, "using 1.1.1.1......")
dns, err := exec.Command("nslookup", endpoint, "1.1.1.1").Output()
if err != nil {
log.Fatal(err)
}
fmt.Println("DNS Lookup successfully completed on", endpoint)
return string(dns)
}
func pingEndpoint(endpoint string) string {
fmt.Println("Performing Ping on", endpoint)
ping, err := exec.Command("ping", "-c", "5", endpoint).Output()
if err != nil {
log.Fatal(err)
}
fmt.Println("Ping Successfully Completed")
return string(ping)
}