-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp.go
347 lines (308 loc) · 9.72 KB
/
http.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
Copyright 2009-2013,2018 Phil Pennock
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.
*/
package sks_spider
import (
"encoding/json"
"expvar"
"fmt"
"html/template"
"net/http"
_ "net/http/pprof"
"sort"
"strings"
"time"
)
const SERVE_PREFIX = "/sks-peers"
const (
kHTML_FAVICON = "/favicon.ico"
kBUCKET_SIZE = 3000
)
const (
ContentTypeTextPlain = "text/plain; charset=UTF-8"
ContentTypeJson = "application/json"
)
var (
statsCollectionTimestamp *expvar.Int
statsServersTotal *expvar.Int
statsServersHostnamesSeen *expvar.Int
statsServersHaveData *expvar.Int
statsServersBadDNS *expvar.Int
statsServersBadData *expvar.Int
)
func init() {
statsCollectionTimestamp = expvar.NewInt("collection.timestamp.activated")
statsServersTotal = expvar.NewInt("collection.servers.total")
statsServersHostnamesSeen = expvar.NewInt("collection.servers.hostnamesseen")
statsServersHaveData = expvar.NewInt("collection.servers.havedata")
statsServersBadDNS = expvar.NewInt("collection.servers.baddns")
statsServersBadData = expvar.NewInt("collection.servers.baddata")
}
func setupHttpServer(listen string) *http.Server {
s := &http.Server{
Addr: listen,
Handler: http.DefaultServeMux,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 14, // we don't POST, so 16kB should be plenty (famous last words)
}
http.HandleFunc(SERVE_PREFIX, apiPeersPage)
http.HandleFunc(SERVE_PREFIX+"/peer-info", apiPeerInfoPage)
http.HandleFunc(SERVE_PREFIX+"/ip-valid", apiIpValidPage)
http.HandleFunc(SERVE_PREFIX+"/ip-valid-stats", apiIpValidStatsPage)
http.HandleFunc(SERVE_PREFIX+"/hostnames-json", apiHostnamesJsonPage)
http.HandleFunc(SERVE_PREFIX+"/graph-dot", apiGraphDot)
http.HandleFunc("/helpz", apiHelpz)
http.HandleFunc("/scanstatusz", apiScanStatusz)
// net/http/pprof provides /debug/pprof with threads and profiling information
// expvar provides /debug/vars (JSON)
// MISSING: environz rescanz (internalz) quitz
// leave quitz out?
http.HandleFunc("/", apiOops)
return s
}
func apiOops(w http.ResponseWriter, req *http.Request) {
if len(req.RequestURI) > 1 {
http.NotFound(w, req)
return
}
w.Header().Set("Content-Type", ContentTypeTextPlain)
fmt.Fprintf(w, "You shouldn't see this top level. Err, oops?\n")
}
func apiHelpz(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", ContentTypeTextPlain)
fmt.Fprintf(w, "Some help here one day, maybe.\n")
//TODO: write
}
func apiScanStatusz(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", ContentTypeTextPlain)
SpiderDiagnostics(w)
fmt.Fprintf(w, "\nDone.\n")
}
func apiPeersPage(w http.ResponseWriter, req *http.Request) {
//TODO: restore "in progress" reporting
//TODO: restore this as trigger for rescan if membership file has changed?
//TODO: restore distance
//TODO: restore entries which are missing DNS but are configured
persisted := GetCurrentPersisted()
var warning string
var display_order = []string{}
if persisted == nil {
warning = "Still awaiting data collection"
} else {
display_order = persisted.DepthSorted
}
namespace := genNamespace()
namespace["Scanning_active"] = ""
// IsZero will hold if persisted loaded from JSON which predates change
// that adds the timestamp.
if persisted != nil && !persisted.Timestamp.IsZero() {
namespace["LastScanTime"] = persisted.Timestamp.UTC().Format("20060102_150405") + "Z"
}
namespace["Mesh_count"] = len(display_order)
if len(display_order) > 0 {
pc := 0
for _, name := range display_order {
d := persisted.HostMap[name].Distance
if d == 1 {
pc += 1
} else if d > 1 {
break
}
}
namespace["Peer_count"] = pc
} else {
namespace["Peer_count"] = 0
}
if warning != "" {
namespace["warning"] = warning
}
serveTemplates["head"].Execute(w, namespace)
for index, host := range display_order {
node := persisted.HostMap[host]
attributes := make(map[string]interface{}, 11)
if len(node.IpList) > 1 {
attributes["Rowspan"] = template.HTMLAttr(fmt.Sprintf(" rowspan=\"%d\"", len(node.IpList)))
} else {
attributes["Rowspan"] = template.HTMLAttr("")
}
if index%2 == 0 {
attributes["Rowclass"] = "even"
} else {
attributes["Rowclass"] = "odd"
}
attributes["Hostname"] = host
attributes["Sks_info"] = NodeUrl(host, node)
attributes["Info_page"] = fmt.Sprintf(SERVE_PREFIX+"/peer-info?peer=%s", host)
attributes["Distance"] = node.Distance
if node.AnalyzeError != "" {
attributes["Error"] = node.AnalyzeError
serveTemplates["hosterr"].Execute(w, attributes)
continue
}
switch node.Distance {
case 0:
attributes["Mutual"] = "n/a"
case 1:
attributes["Mutual"] = persisted.Graph.LabelMutualWithBase(host)
default:
attributes["Mutual"] = "-"
}
if len(node.Aliases) > 0 {
attributes["Host_aliases_text"] = template.HTML(fmt.Sprintf(" <span class=\"host_aliases\">%s</span>", node.Aliases))
} else {
attributes["Host_aliases_text"] = ""
}
attributes["Version"] = node.Version
attributes["Keycount"] = node.Keycount
attributes["Web_server"] = node.ServerHeader
if node.ViaHeader != "" {
attributes["Via_info"] = fmt.Sprintf("✓ [%s]", node.ViaHeader)
} else {
attributes["Via_info"] = "✗"
}
for n, ip := range node.IpList {
attributes["Ip"] = ip
attributes["Geo"] = persisted.IPCountryMap[ip]
if n == 0 {
serveTemplates["host"].Execute(w, attributes)
} else {
serveTemplates["hostmore"].Execute(w, attributes)
}
}
}
serveTemplates["foot"].Execute(w, namespace)
}
func apiPeerInfoPage(w http.ResponseWriter, req *http.Request) {
var err error
if err = req.ParseForm(); err != nil {
http.Error(w, "Failed to parse form information", http.StatusBadRequest)
return
}
peer := req.Form.Get("peer")
if peer == "" {
http.Error(w, "Missing 'peer' parameter to query", http.StatusBadRequest)
return
}
namespace := genNamespace()
namespace["Peername"] = peer
persisted := GetCurrentPersisted()
var warning string
var node *SksNode
var ok bool
if persisted == nil {
warning = "Still awaiting data collection"
} else if node, ok = persisted.HostMap[peer]; !ok {
warning = fmt.Sprintf("Peer \"%s\" not found", peer)
}
if warning != "" {
namespace["Warning"] = warning
serveTemplates["pi_head"].Execute(w, namespace)
serveTemplates["pi_foot"].Execute(w, namespace)
return
}
namespace["Keycount"] = node.Keycount
namespace["Version"] = node.Version
if node.Software != "" {
namespace["Software"] = node.Software
} else {
namespace["Software"] = defaultSoftware
}
namespace["Ips"] = "[" + strings.Join(node.IpList, "], [") + "]"
namespace["Mailsync"] = node.MailsyncPeers
namespace["Mailsync_count"] = len(node.MailsyncPeers)
namespace["Web_server"] = node.ServerHeader
namespace["Via_info"] = node.ViaHeader
namespace["Peer_statsurl"] = node.Url()
peer_list := persisted.Graph.AllPeersOf(node.Hostname)
serveTemplates["pi_head"].Execute(w, namespace)
serveTemplates["pi_main"].Execute(w, namespace)
serveTemplates["pi_peers_start"].Execute(w, namespace)
for _, other := range peer_list {
attributes := make(map[string]interface{}, 5)
attributes["Name"] = other
attributes["Ref_url"] = NodeUrl(other, persisted.HostMap[other])
out := persisted.Graph.ExistsLink(peer, other)
in := persisted.Graph.ExistsLink(other, peer)
common := out && in
attributes["Out"] = out
if _, ok := persisted.AliasMap[other]; !ok {
// peer not successfully polled
attributes["In"] = "?"
attributes["Common"] = "?"
} else {
attributes["In"] = in
attributes["Common"] = common
}
serveTemplates["pi_peers"].Execute(w, attributes)
}
serveTemplates["pi_peers_end"].Execute(w, namespace)
serveTemplates["pi_foot"].Execute(w, namespace)
}
func apiHostnamesJsonPage(w http.ResponseWriter, req *http.Request) {
var err error
if err = req.ParseForm(); err != nil {
http.Error(w, "Failed to parse form information", http.StatusBadRequest)
return
}
// we want to allow empty value to just mean 'yes',
// so the .Get() API doesn't work for us
all := false
if _, ok := req.Form["all"]; ok {
all = true
} else if _, ok := req.Form["mesh"]; ok {
all = true
}
alphaSort := false
if _, ok := req.Form["sort"]; ok {
alphaSort = true
}
var hostList []string
if all {
hosts := GetCurrentHosts()
if hosts == nil || len(hosts) == 0 {
Log.Printf("Request for current hosts, none loaded yet")
http.Error(w, "Still waiting for data collection", http.StatusServiceUnavailable)
return
}
hostList = make([]string, len(hosts))
i := 0
for k := range hosts {
hostList[i] = k
i += 1
}
} else {
hostList, err = GetMembershipHosts()
if err != nil {
Log.Printf("Failed to load membership: %s", err)
http.Error(w, "Problem loading membership file", http.StatusServiceUnavailable)
return
}
}
if alphaSort {
sort.Strings(hostList)
} else {
HostSort(hostList)
}
b, err := json.Marshal(hostList)
if err != nil {
Log.Printf("Failed to marshal hostlist to JSON: %s", err)
http.Error(w, "JSON encoding glitch", http.StatusInternalServerError)
return
}
contentType := ContentTypeJson
if _, ok := req.Form["textplain"]; ok {
contentType = ContentTypeTextPlain
}
w.Header().Set("Content-Type", contentType)
fmt.Fprintf(w, "{ \"hostnames\": %s }\n", b)
}