-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtower.go
More file actions
114 lines (106 loc) · 2.51 KB
/
tower.go
File metadata and controls
114 lines (106 loc) · 2.51 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
// tower
package main
import (
"fmt"
"log"
"os"
"github.com/dariubs/fmt2"
"github.com/mismatched/libtower"
"github.com/urfave/cli"
)
func main() {
app := cli.NewApp()
app.Name = "tower"
app.Usage = "network uptime and status checker"
app.Version = "0.0.1"
app.Authors = []cli.Author{
cli.Author{
Name: "Dariush Abbasi",
Email: "poshtehani@gmail.com",
},
cli.Author{
Name: "Hasan Aminfar",
Email: "aminfar69@gmail.com",
},
}
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "ping, p",
Usage: "ping a url. you must run it as root",
},
cli.StringFlag{
Name: "dns",
Usage: "dns resolve time of an address",
},
cli.StringFlag{
Name: "trace",
Usage: "http trace time",
},
cli.StringFlag{
Name: "http-status, s",
Usage: "http status time",
},
}
app.Action = ActionHandler
err := app.Run(os.Args)
if err != nil {
log.Fatalln(err)
}
}
// ActionHandler handle cli actions
func ActionHandler(c *cli.Context) error {
if c.String("ping") != "" {
r, d, err := libtower.Ping(c.String("ping"), 1)
if err != nil {
fmt.Printf("Error: %v\n", err)
return err
}
fmt.Printf("Ping %s in %v ms\n", r, d)
return nil
} else if c.String("dns") != "" {
r, d, err := libtower.DNSLookup(c.String("dns"))
if err != nil {
fmt.Printf("Error: %v\n", err)
return err
}
fmt.Printf("DNS of %s with %s ip resolves in %v ms\n", c.String("dns"), r, d)
return nil
} else if c.String("trace") != "" {
// TODO: get http method from user
// TODO: use http:// schema if for urls with no schemas
r := libtower.HTTPTrace{URL: c.String("trace"), Method: "GET"}
err := r.Trace()
if err != nil {
fmt.Printf("Error: %v\n", err)
return err
}
if r.DNS != 0 {
fmt.Printf("DNS Done: %v\n", r.DNS)
}
if r.TLSHandshake != 0 {
fmt.Printf("TLS Handshake: %v\n", r.TLSHandshake)
}
if r.Connect != 0 {
fmt.Printf("Connect time: %v\n", r.Connect)
}
if r.GotFirstResponseByte != 0 {
fmt.Printf("Time from start to first byte: %v\n", r.GotFirstResponseByte)
}
if r.Total != 0 {
fmt.Printf("Total time: %v\n", r.Total)
}
return nil
} else if c.String("http-status") != "" {
client := libtower.HTTP{URL: c.String("http-status"), Method: "GET"}
err := client.HTTPStatus()
if err != nil {
fmt2.Printlnf("Error: %v\n", err)
return err
}
fmt2.Printlnf("%s with status %s code %d in %v", c.String("http-status"), client.Status, client.StatusCode, client.Duration)
return nil
} else {
fmt.Println("Command not found in Tower")
}
return nil
}