-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
103 lines (95 loc) · 2.14 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
package main
import (
_ "embed"
"flag"
"fmt"
. "github.com/CursedHardware/go-ipv6-test/ipv6test"
"github.com/fatih/color"
"net/http"
"strings"
"sync"
)
var host string
var listAll bool
var testAll bool
func init() {
flag.StringVar(&host, "host", knownHosts[0], "Host")
flag.BoolVar(&testAll, "all", false, "Test All Hosts")
flag.BoolVar(&listAll, "hosts", false, "Available Hosts")
flag.Parse()
}
func main() {
tester := &Tester{
Client: http.DefaultClient,
MTU: 1600,
}
switch {
case listAll:
fmt.Println(strings.Join(knownHosts, "\n"))
case testAll:
batchTasks(tester, []Task{RecordIPv6}, knownHosts)
default:
fullTask(tester, host)
}
}
func invoke(tester *Tester, requests map[string][]Task) <-chan *Report {
var wg sync.WaitGroup
reports := make(chan *Report)
for taskHost, tasks := range requests {
wg.Add(len(tasks))
for _, task := range tasks {
go func(task Task, taskHost string) {
if task == RecordASN4 || task == RecordASN6 {
taskHost = "lookup.test-ipv6.com"
}
reports <- tester.Run(task, taskHost)
wg.Done()
}(task, taskHost)
}
}
go func() {
wg.Wait()
close(reports)
}()
return reports
}
func fullTask(tester *Tester, host string) {
requests := make(map[string][]Task)
requests[host] = []Task{
RecordIPv4, RecordIPv6, RecordDualStack, RecordDualStackMTU,
RecordIPv6MTU, RecordIPv6NS, RecordASN4, RecordASN6,
}
for report := range invoke(tester, requests) {
emitReport(report)
}
}
func batchTasks(tester *Tester, tasks []Task, hosts []string) {
requests := make(map[string][]Task)
for _, testHost := range hosts {
requests[testHost] = tasks
}
for report := range invoke(tester, requests) {
fmt.Printf("Test for %q\n", report.Host)
emitReport(report)
}
}
func emitReport(r *Report) {
var ok = color.GreenString("ok")
var bad = color.BlueString("bad")
fmt.Println(r.Task.Name())
if r.Failed {
fmt.Printf("%s (%s)\n", bad, r.Elapsed)
fmt.Println()
return
}
status := bad
if r.Task.Match(r.IPProto) {
status = ok
}
fmt.Printf("%s (%s) using %s", status, r.Elapsed, r.IPProto)
if r.ASN != "" {
fmt.Printf(" with ASN%s", r.ASN)
}
fmt.Println()
fmt.Println()
}