-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
158 lines (149 loc) · 3.65 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
package main
import (
"fmt"
"os"
"runtime"
"sort"
"strings"
"sync"
"time"
"github.com/alexflint/go-arg"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/jedib0t/go-pretty/v6/text"
)
var args config
func main() {
arg.MustParse(&args)
args.Provider = strings.ToLower(args.Provider)
semaphore := make(chan struct{}, args.Concurrent)
var wg sync.WaitGroup
for key, provider := range providers {
if args.All || key == args.Provider {
for _, r := range provider.regions {
if (len(args.FilterRegion) > 0 && !isFiltered(r.name, args.FilterRegion)) ||
(len(args.FilterLocation) > 0 && !isFiltered(r.location, args.FilterLocation)) {
continue
}
if r.endpoint == "" {
code := r.name
if r.code != "" {
code = r.code
}
r.endpoint = fmt.Sprintf(provider.hostTemplate, code)
}
semaphore <- struct{}{}
wg.Add(1)
go func(region *region) {
defer wg.Done()
defer func() { <-semaphore }()
region.rtt, region.err = endpointPing(region.endpoint)
if region.rtt == 0 && region.err == nil {
region.err = fmt.Errorf("timeout")
}
if region.err != nil {
fmt.Print(".")
} else {
fmt.Print("!")
}
}(r)
time.Sleep(time.Millisecond)
}
}
}
wg.Wait()
clearLine := "\033[2K\r"
if runtime.GOOS == "windows" {
clearLine = "\r"
}
fmt.Print(clearLine)
results := []*region{}
for key, provider := range providers {
if args.All || key == args.Provider {
for _, region := range provider.regions {
region.code = provider.name
results = append(results, region)
}
}
}
if len(results) == 0 {
fmt.Printf("Provider %s not found\n", args.Provider)
os.Exit(1)
}
sort.Slice(results, func(i, j int) bool {
if results[i].err != nil && results[j].err == nil {
return false
}
if results[i].err == nil && results[j].err != nil {
return true
}
if results[i].rtt == 0 && results[j].rtt != 0 {
return false
}
if results[i].rtt != 0 && results[j].rtt == 0 {
return true
}
return results[i].rtt < results[j].rtt
})
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.SetAutoIndex(true)
if args.Provider == "dns" && !args.All {
t.SetTitle("Public DNS Servers")
t.Style().Title = table.TitleOptions{Align: text.AlignCenter}
t.AppendHeader(table.Row{"Provider", "Type", "Address", "RTT", "Status"})
} else {
t.AppendHeader(table.Row{"Provider", "Region", "Location", "RTT", "Status"})
}
var minRTT time.Duration
for _, region := range results {
status := text.FgGreen.Sprint("OK")
if region.err != nil {
if args.HideErrors {
continue
}
status = text.FgRed.Sprintf("ERROR: %s", region.err)
} else if region.rtt == 0 {
continue
}
rtt := region.rtt.Round(time.Microsecond)
if rtt > time.Second {
rtt = rtt.Round(time.Millisecond)
}
if rtt != 0 && (rtt < minRTT || minRTT == 0) {
minRTT = rtt
}
if args.Provider == "dns" && !args.All {
t.AppendRow(table.Row{region.name, region.location, region.endpoint, rtt, status})
} else {
t.AppendRow(table.Row{region.code, region.name, region.location, rtt, status})
}
}
t.SetColumnConfigs([]table.ColumnConfig{
{
Name: "RTT",
Align: text.AlignRight,
Transformer: func(val interface{}) string {
if val.(time.Duration) == 0 {
return text.FgRed.Sprintf("N/A")
}
if val.(time.Duration) > time.Second {
return text.FgRed.Sprintf("%s", val)
}
if val.(time.Duration) == minRTT {
return text.Bold.Sprintf("%s", val)
}
return val.(time.Duration).String()
},
},
{
Name: "Status",
WidthMax: 55,
},
})
if t.Length() > 0 {
t.Render()
} else {
fmt.Println("No results found")
os.Exit(2)
}
}