-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
122 lines (102 loc) · 3.94 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
package main
import (
"errors"
"flag"
"fmt"
"os"
"strconv"
"strings"
"github.com/azurejelly/checkhost/client"
"github.com/azurejelly/checkhost/utils"
)
var (
mode = flag.String("mode", "http", "Type of check to request (http, tcp, udp, dns)")
maxNodes = flag.Int("max-nodes", 100, "Maximum amount of nodes to check from")
nodeList = flag.String("nodes", "", "Comma-separated list of nodes to use")
fullNodeList = flag.Bool("display-all-nodes", false, "Whether to display more than 5 nodes on the final output")
help = flag.Bool("help", false, "Shows a list of available options")
yes = flag.Bool("open-report", false, "Whether to automatically open the report in your web browser")
)
func init() {
flag.StringVar(mode, "m", "http", "Type of check to request (http, tcp, udp, dns)")
flag.IntVar(maxNodes, "M", 100, "Maximum amount of nodes to check from")
flag.StringVar(nodeList, "n", "", "Comma-separated list of nodes to use")
flag.BoolVar(yes, "y", false, "Whether to automatically open the report in your web browser")
flag.BoolVar(fullNodeList, "a", false, "Whether to display more than 5 nodes on the final output")
flag.BoolVar(help, "h", false, "Shows a list of available options")
flag.Usage = func() {
fmt.Println(utils.ToolSymbol, " Usage: checkhost [options] [target]")
fmt.Printf("\n")
fmt.Println("Options:")
fmt.Println(" -m, --mode string Type of check to request (http,")
fmt.Println(" tcp, udp, dns)")
fmt.Println(" -M, --max-nodes int Maximum amount of nodes to check from")
fmt.Println(" -n, --nodes string Comma-separated list of nodes to use")
fmt.Println(" -a, --display-all-nodes boolean Whether to display more than 5 nodes")
fmt.Println(" on the final output")
fmt.Println(" -y, --open-report boolean Whether to automatically open the")
fmt.Println(" report in your web browser")
fmt.Println(" -h, --help boolean Shows a list of available options")
}
}
func main() {
flag.Parse()
if *help {
flag.Usage()
os.Exit(0) // they asked for it
}
err := utils.ValidateMode(mode)
if err != nil {
utils.PrintFatalError("Invalid '--mode' parameter:", err)
}
err = utils.ValidateMaxNodes(maxNodes)
if err != nil {
utils.PrintFatalError("Invalid '--max-nodes' parameter:", err)
}
nodes, err := utils.ParseNodeList(nodeList, maxNodes)
if err != nil {
utils.PrintFatalError("Invalid '--nodes' parameter:", err)
}
host, err := utils.GetTarget()
if err != nil {
flag.Usage()
os.Exit(-1)
}
url, err := client.BuildURL(mode, &host, maxNodes, &nodes)
if err != nil {
utils.PrintFatalError("Failed to build check-host.net URL. This might be a bug!", err)
}
c, r, err := client.MakeRequest(url)
if err != nil {
utils.PrintFatalError("Failed to make request:", err)
}
if c != 200 {
utils.PrintFatalError("Failed to make request:", fmt.Errorf("server replied with unsuccessful status code '%s'", strconv.Itoa(c)))
}
if r.Ok != 1 {
utils.PrintFatalError("Failed to generate the report:", errors.New("server did not reply with the expected 'ok' parameter"))
}
utils.PrintSuccess(fmt.Sprint("Successfully requested a ", strings.ToUpper(*mode), " check!"))
fmt.Println(" • Report:", r.PermanentLink)
fmt.Println(" • Target:", host)
fmt.Println(" • Nodes:")
count := 0
for n, v := range r.Nodes {
if !*fullNodeList && len(n) > 5 && count == 5 {
fmt.Printf(" - ... and %s more node(s)\n", strconv.Itoa(len(n)-count))
fmt.Print(" (use '--display-all-nodes' for a full list)\n")
break
}
fmt.Printf(" - %s @ %s, %s (%s)\n", n, v.City, v.Country, v.ASNumber)
count++
}
fmt.Printf("\n")
if *yes {
utils.OpenURL(r.PermanentLink)
} else {
open := utils.Ask("Do you want to open the report on your web browser?")
if open {
utils.OpenURL(r.PermanentLink)
}
}
}