/
diagd.go
156 lines (142 loc) · 3.6 KB
/
diagd.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
// Copyright 2018 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Binary diagd provides automated network diagnostics.
package main
import (
"encoding/json"
"flag"
"fmt"
"html"
"io"
"log"
"net"
"net/http"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"github.com/gokrazy/gokrazy"
"github.com/rtr7/router7/internal/diag"
"github.com/rtr7/router7/internal/multilisten"
_ "net/http/pprof"
)
var httpListeners = multilisten.NewPool()
func updateListeners() error {
hosts, err := gokrazy.PrivateInterfaceAddrs()
if err != nil {
return err
}
httpListeners.ListenAndServe(hosts, func(host string) multilisten.Listener {
return &http.Server{Addr: net.JoinHostPort(host, "7733")}
})
return nil
}
func dump(indent int, w io.Writer, re *diag.EvalResult) {
symbol := "✔"
if re.Error {
symbol = "✘"
}
fmt.Fprintf(w, "<li>\n%s%s %s: %s<ul>",
strings.Repeat(" ", indent),
symbol,
html.EscapeString(re.Name),
html.EscapeString(re.Status))
for _, ch := range re.Children {
dump(indent+1, w, ch)
}
fmt.Fprintf(w, "</ul></li>")
}
func firstError(re *diag.EvalResult) string {
if re.Error {
return fmt.Sprintf("%s: %s", re.Name, re.Status)
}
for _, ch := range re.Children {
if msg := firstError(ch); msg != "" {
return msg
}
}
return ""
}
func graph(uplink string, ipv6 bool) *diag.Monitor {
const ip6allrouters = "ff02::2" // no /etc/hosts on gokrazy
graph := diag.Link(uplink).
Then(diag.DHCPv4().
Then(diag.Ping4Gateway().
Then(diag.TCP4("www.google.ch:80"))))
if ipv6 {
graph = graph.
Then(diag.DHCPv6().
Then(diag.TCP6("lan0", "www.google.ch:80"))).
Then(diag.RouterAdvertisments(uplink).
Then(diag.Ping6Gateway().
Then(diag.TCP6(uplink, "www.google.ch:80")))).
Then(diag.Ping6("", ip6allrouters+"%"+uplink))
}
return diag.NewMonitor(graph)
}
func logic() error {
var (
ifname = flag.String("interface",
"uplink0",
"interface name to query")
ipv6 = flag.Bool("ipv6",
true,
"whether to expect IPv6 connectivity in health.json")
)
flag.Parse()
uplink := *ifname
mHumanReadable := graph(uplink, true) // for display only
mJSON := graph(uplink, *ipv6) // for updates
var mu sync.Mutex
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
re := mHumanReadable.Evaluate()
mu.Unlock()
fmt.Fprintf(w, `<!DOCTYPE html><style type="text/css">ul { list-style-type: none; }</style><ul>`)
dump(0, w, re)
})
http.HandleFunc("/health.json", func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
re := mJSON.Evaluate()
mu.Unlock()
reply := struct {
FirstError string `json:"first_error"`
}{
FirstError: firstError(re),
}
b, err := json.Marshal(&reply)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(b)
})
if err := updateListeners(); err != nil {
return err
}
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGUSR1)
for range ch {
if err := updateListeners(); err != nil {
log.Printf("updateListeners: %v", err)
}
}
return nil
}
func main() {
if err := logic(); err != nil {
log.Fatal(err)
}
}