-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
92 lines (81 loc) · 1.79 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
package main
import (
"os"
"strings"
"github.com/docopt/docopt-go"
"github.com/gookit/color"
)
const usage = `Print the route packets trace to network host showing AS information
Usage:
tracert-as <host>
Options:
-h --help Show this message`
func main() {
if len(os.Args) == 1 {
os.Args = append(os.Args, "-h")
}
arguments, _ := docopt.ParseDoc(usage)
host, _ := arguments.String("<host>")
fi, _ := os.Stdout.Stat()
if fi.Mode()&os.ModeCharDevice == 0 {
color.Disable()
}
hops, errs := TraceRoute(host)
for {
select {
case err, ok := <-errs:
if !ok {
return
}
printErr(err)
case hop, ok := <-hops:
if !ok {
return
}
printHop(hop)
if hop.Success {
info, err := GetWhoisInfo(hop.Addr.String())
if err == nil {
printWhoisInfo(info)
}
}
color.Normal.Println()
}
}
}
func printHop(hop Hop) {
color.Normal.Printf("%2d ", hop.Number)
if hop.Success {
color.Yellow.Println(hop.Addr.String())
} else {
color.Red.Println("*")
}
}
func printErr(err error) {
if strings.Contains(err.Error(), "operation not permitted") {
color.LightRed.Println("Could not get network permissions")
color.Normal.Println("Try running the program as root, or give it permission to use the network like this:")
color.Normal.Println("$ sudo setcap cap_net_raw+ep tracert-as")
} else {
color.LightRed.Println(err)
}
}
func printWhoisInfo(info WhoisInfo) {
color.Normal.Print(" ")
if info.Netname != "" {
color.Green.Print(info.Netname)
}
if info.Netname != "" && info.AS != "" {
color.Normal.Print(", ")
}
if info.AS != "" {
color.Magenta.Print(info.AS)
}
if (info.AS != "" || info.Netname != "") && info.Country != "" {
color.Normal.Print(", ")
}
if info.Country != "" {
color.Cyan.Print(info.Country)
}
color.Normal.Println()
}