forked from gcash/dnsseeder
-
Notifications
You must be signed in to change notification settings - Fork 4
/
http.go
601 lines (525 loc) · 15.5 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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
package main
import (
"fmt"
"html"
"log"
"net/http"
"text/template"
"time"
)
// startHTTP runs in a goroutine and provides the web interface
// to the dnsseeder
func startHTTP(port string) {
http.HandleFunc("/dns", dnsWebHandler)
http.HandleFunc("/node", nodeHandler)
http.HandleFunc("/statusRG", statusRGHandler)
http.HandleFunc("/statusCG", statusCGHandler)
http.HandleFunc("/statusWG", statusWGHandler)
http.HandleFunc("/statusNG", statusNGHandler)
http.HandleFunc("/summary", summaryHandler)
http.HandleFunc("/", emptyHandler)
// listen only on 0.0.0.0 for easy ui viewing
err := http.ListenAndServe("0.0.0.0:"+port, nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
// dnsWebHandler processes all requests and returns output in the requested format
func dnsWebHandler(w http.ResponseWriter, r *http.Request) {
st := time.Now()
// skip the s= from the raw query
n := r.FormValue("s")
s := getSeederByName(n)
if s == nil {
writeHeader(w, r)
fmt.Fprintf(w, "<b>No seeder found: %s</b>", html.EscapeString(n))
writeFooter(w, r, st)
return
}
// FIXME - This is ugly code and needs to be cleaned up a lot
config.dnsmtx.RLock()
// if the dns map does not have a key for the request it will return an empty slice
v4std := config.dns[s.dnsHost+".A"]
v4non := config.dns["nonstd."+s.dnsHost+".A"]
v6std := config.dns[s.dnsHost+".AAAA"]
v6non := config.dns["nonstd."+s.dnsHost+".AAAA"]
config.dnsmtx.RUnlock()
var v4stdstr, v4nonstr []string
var v6stdstr, v6nonstr []string
if x := len(v4std); x > 0 {
v4stdstr = make([]string, x)
for k, v := range v4std {
v4stdstr[k] = v.String()
}
} else {
v4stdstr = []string{"No records Available"}
}
if x := len(v4non); x > 0 {
v4nonstr = make([]string, x)
for k, v := range v4non {
v4nonstr[k] = v.String()
}
} else {
v4nonstr = []string{"No records Available"}
}
// ipv6
if x := len(v6std); x > 0 {
v6stdstr = make([]string, x)
for k, v := range v6std {
v6stdstr[k] = v.String()
}
} else {
v6stdstr = []string{"No records Available"}
}
if x := len(v6non); x > 0 {
v6nonstr = make([]string, x)
for k, v := range v6non {
v6nonstr[k] = v.String()
}
} else {
v6nonstr = []string{"No records Available"}
}
t1 := `
<center>
<table class="table table-hover table-dark">
<tr>
<th>Standard Ports</th>
<th>Non Standard Ports</th>
</tr>
<tr>
<td>
`
t2 := ` {{range .}}
{{.}}<br>
{{end}}
`
t3 := `
</td>
<td>
`
t4 := `
</td>
</tr>
</table>
</center>
`
writeHeader(w, r)
fmt.Fprintf(w, "<b>Currently serving the following DNS records</b>")
fmt.Fprintf(w, "<p><center><b>IPv4</b></center></p>")
fmt.Fprintf(w, t1)
t := template.New("v4 template")
t, err := t.Parse(t2)
if err != nil {
log.Printf("error parsing template v4 %v\n", err)
}
err = t.Execute(w, v4stdstr)
if err != nil {
log.Printf("error executing template v4 %v\n", err)
}
fmt.Fprintf(w, t3)
err = t.Execute(w, v4nonstr)
if err != nil {
log.Printf("error executing template v4 non %v\n", err)
}
fmt.Fprintf(w, t4)
// ipv6 records
fmt.Fprintf(w, "<p><center><b>IPv6</b></center></p>")
fmt.Fprintf(w, t1)
err = t.Execute(w, v6stdstr)
if err != nil {
log.Printf("error executing template v6 %v\n", err)
}
fmt.Fprintf(w, t3)
err = t.Execute(w, v6nonstr)
if err != nil {
log.Printf("error executing template v6 non %v\n", err)
}
fmt.Fprintf(w, t4)
writeFooter(w, r, st)
}
// emptyHandler processes all requests for non-existent urls
func emptyHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintf(w, "Nothing to see here. Move along please\n")
}
func statusRGHandler(w http.ResponseWriter, r *http.Request) {
statusHandler(w, r, statusRG)
}
func statusCGHandler(w http.ResponseWriter, r *http.Request) {
statusHandler(w, r, statusCG)
}
func statusWGHandler(w http.ResponseWriter, r *http.Request) {
statusHandler(w, r, statusWG)
}
func statusNGHandler(w http.ResponseWriter, r *http.Request) {
statusHandler(w, r, statusNG)
}
type webstatus struct {
Key string
Value string
Seeder string
}
func statusHandler(w http.ResponseWriter, r *http.Request, status uint32) {
startT := time.Now()
// read the seeder name
n := r.FormValue("s")
s := getSeederByName(n)
if s == nil {
writeHeader(w, r)
fmt.Fprintf(w, "<b>No seeder found called %s</b>", html.EscapeString(n))
writeFooter(w, r, startT)
return
}
// gather all the info before writing anything to the remote browser
ws := generateWebStatus(s, status)
st := `
<center>
<table class="table table-hover table-dark">
<tr>
<th>Node</th>
<th>Summary</th>
</tr>
{{range .}}
<tr>
<td>
<a href="/node?s={{.Seeder}}&nd={{.Key}}">{{.Key}}</a>
</td>
<td>
{{.Value}}
</td>
</tr>
{{end}}
</table>
</center>
`
writeHeader(w, r)
if len(ws) == 0 {
fmt.Fprintf(w, "<b>No Nodes found with this status</b>")
} else {
switch status {
case statusRG:
fmt.Fprintf(w, "<center><b>Node Status: statusRG - (Reported Good) Have not been able to get addresses yet</b></center>")
case statusCG:
fmt.Fprintf(w, "<center><b>Node Status: statusCG - (Currently Good) Able to connect and get addresses</b></center>")
case statusWG:
fmt.Fprintf(w, "<center><b>Node Status: statusWG - (Was Good) Was Ok but now can not get addresses</b></center>")
case statusNG:
fmt.Fprintf(w, "<center><b>Node Status: statusNG - (No Good) Unable to get addresses</b></center>")
}
t := template.New("Status template")
t, err := t.Parse(st)
if err != nil {
log.Printf("error parsing status template %v\n", err)
}
err = t.Execute(w, ws)
if err != nil {
log.Printf("error executing status template %v\n", err)
}
}
writeFooter(w, r, startT)
}
// generateWebStatus is given a node status and returns a slice of webstatus structures
// ready to be ranged over by an html/template
func generateWebStatus(s *dnsseeder, status uint32) (ws []webstatus) {
s.mtx.RLock()
defer s.mtx.RUnlock()
var valueStr string
for k, v := range s.theList {
if v.Status != status {
continue
}
switch status {
case statusRG:
valueStr = fmt.Sprintf("<b>Fail Count:</b> %v <b>DNS Type:</b> %s",
v.ConnectFails,
v.dns2str())
case statusCG:
valueStr = fmt.Sprintf("<b>Remote Version:</b> %v%s <b>Last Block:</b> %v <b>DNS Type:</b> %s",
v.Version,
v.StrVersion,
v.LastBlock,
v.dns2str())
case statusWG:
valueStr = fmt.Sprintf("<b>Last Try:</b> %s ago <b>Last Status:</b> %s\n",
time.Since(v.LastTry).String(),
v.StatusStr)
case statusNG:
valueStr = fmt.Sprintf("<b>Fail Count:</b> %v <b>Last Try:</b> %s ago <b>Last Status:</b> %s\n",
v.ConnectFails,
time.Since(v.LastTry).String(),
v.StatusStr)
default:
valueStr = ""
}
ows := webstatus{
Key: k,
Value: valueStr,
Seeder: s.name,
}
ws = append(ws, ows)
}
return ws
}
// copy Node details into a template friendly struct
type webtemplate struct {
Key string
IP string
Port uint16
Statusstr string
Rating string
DNStype string
Lastconnect string
Lastconnectago string
Lasttry string
Lasttryago string
Crawlstart string
Crawlstartago string
Crawlactive bool
Connectfails uint32
Version int32
Strversion string
Services string
Lastblock int32
Nonstdip string
}
// nodeHandler displays details about one node
func nodeHandler(w http.ResponseWriter, r *http.Request) {
st := time.Now()
ndt := `
<center>
<table class="table table-hover table-dark">
<tr>
<th>Node {{.Key}}</th><th>Details</th>
</tr>
<tr><td>IP Address</td><td>{{.IP}}</td></tr>
<tr><td>Port</td><td>{{.Port}}</td></tr>
<tr><td>DNS Type</td><td>{{.DNStype}}</td></tr>
<tr><td>Non Standard IP</td><td>{{.Nonstdip}}</td></tr>
<tr><td>Last Connect</td><td>{{.Lastconnect}}<br>{{.Lastconnectago}} ago</td></tr>
<tr><td>Last Connect Status</td><td>{{.Statusstr}}</td></tr>
<tr><td>Last Try</td><td>{{.Lasttry}}<br>{{.Lasttryago}} ago</td></tr>
<tr><td>Crawl Start</td><td>{{.Crawlstart}}<br>{{.Crawlstartago}} ago</td></tr>
<tr><td>Crawl Active</td><td>{{.Crawlactive}}</td></tr>
<tr><td>Connection Fails</td><td>{{.Connectfails}}</td></tr>
<tr><td>Remote Version</td><td>{{.Version}}</td></tr>
<tr><td>Remote SubVersion</td><td>{{.Strversion}}</td></tr>
<tr><td>Remote Services</td><td>{{.Services}}</td></tr>
<tr><td>Remote Last Block</td><td>{{.Lastblock}}</td></tr>
</table>
</center>
`
// read the seeder name
n := r.FormValue("s")
s := getSeederByName(n)
if s == nil {
writeHeader(w, r)
fmt.Fprintf(w, "<b>No seeder found called %s</b>", html.EscapeString(n))
writeFooter(w, r, st)
return
}
s.mtx.RLock()
defer s.mtx.RUnlock()
k := r.FormValue("nd")
writeHeader(w, r)
if _, ok := s.theList[k]; !ok {
fmt.Fprintf(w, "<b>Sorry there is no Node with those details</b>\n")
} else {
nd := s.theList[k]
wt := webtemplate{
IP: nd.NA.IP.String(),
Port: nd.NA.Port,
DNStype: nd.dns2str(),
Nonstdip: nd.NonstdIP.String(),
Statusstr: nd.StatusStr,
Lastconnect: nd.LastConnect.String(),
Lastconnectago: time.Since(nd.LastConnect).String(),
Lasttry: nd.LastTry.String(),
Lasttryago: time.Since(nd.LastTry).String(),
Crawlstart: nd.CrawlStart.String(),
Crawlstartago: time.Since(nd.CrawlStart).String(),
Connectfails: nd.ConnectFails,
Crawlactive: nd.CrawlActive,
Version: nd.Version,
Strversion: nd.StrVersion,
Services: nd.Services.String(),
Lastblock: nd.LastBlock,
}
// display details for the Node
t := template.New("Node template")
t, err := t.Parse(ndt)
if err != nil {
log.Printf("error parsing Node template %v\n", err)
}
err = t.Execute(w, wt)
if err != nil {
log.Printf("error executing Node template %v\n", err)
}
}
writeFooter(w, r, st)
}
// summaryHandler displays details about one node
func summaryHandler(w http.ResponseWriter, r *http.Request) {
st := time.Now()
var hc struct {
Name string
RG uint32
RGS uint32
CG uint32
CGS uint32
WG uint32
WGS uint32
NG uint32
NGS uint32
Total uint32
V4Std uint32
V4Non uint32
V6Std uint32
V6Non uint32
DNSTotal uint32
}
writeHeader(w, r)
// loop through each of the seeder name from a slice so they are always returned in
// the same order then get a pointer to the seeder struct
for _, n := range config.order {
s := config.seeders[n]
hc.Name = s.name
// fill the structs so they can be displayed via the template
s.counts.mtx.RLock()
hc.RG = s.counts.NdStatus[statusRG]
hc.RGS = s.counts.NdStarts[statusRG]
hc.CG = s.counts.NdStatus[statusCG]
hc.CGS = s.counts.NdStarts[statusCG]
hc.WG = s.counts.NdStatus[statusWG]
hc.WGS = s.counts.NdStarts[statusWG]
hc.NG = s.counts.NdStatus[statusNG]
hc.NGS = s.counts.NdStarts[statusNG]
hc.Total = hc.RG + hc.CG + hc.WG + hc.NG
hc.V4Std = s.counts.DNSCounts[dnsV4Std]
hc.V4Non = s.counts.DNSCounts[dnsV4Non]
hc.V6Std = s.counts.DNSCounts[dnsV6Std]
hc.V6Non = s.counts.DNSCounts[dnsV6Non]
hc.DNSTotal = hc.V4Std + hc.V4Non + hc.V6Std + hc.V6Non
s.counts.mtx.RUnlock()
// we are using basic and simple html here. No fancy graphics or css
sp := `
<b>Stats for seeder: {{.Name}}</b>
<center>
<table class="table-dark"><tr><td>
Node Stats (count/started)<br>
<table class="table table-hover table-dark"><tr>
<td><a href="/statusRG?s={{.Name}}">RG: {{.RG}}/{{.RGS}}</a></td>
<td><a href="/statusCG?s={{.Name}}">CG: {{.CG}}/{{.CGS}}</a></td>
<td><a href="/statusWG?s={{.Name}}">WG: {{.WG}}/{{.WGS}}</a></td>
<td><a href="/statusNG?s={{.Name}}">NG: {{.NG}}/{{.NGS}}</a></td>
<td>Total: {{.Total}}</td>
</tr></table>
</td><td>
DNS Requests<br>
<table class="table table-hover table-dark"><tr>
<td>V4 Std: {{.V4Std}}</td>
<td>V4 Non: {{.V4Non}}</td>
<td>V6 Std: {{.V6Std}}</td>
<td>V6 Non: {{.V6Non}}</td>
<td><a href="/dns?s={{.Name}}">Total: {{.DNSTotal}}</a></td>
</tr></table>
</td></tr></table>
</center>
`
t := template.New("Header template")
t, err := t.Parse(sp)
if err != nil {
log.Printf("error parsing summary template %v\n", err)
}
err = t.Execute(w, hc)
if err != nil {
log.Printf("error executing summary template %v\n", err)
}
}
writeFooter(w, r, st)
}
// writeHeader will output the standard header
func writeHeader(w http.ResponseWriter, r *http.Request) {
// we are using basic and simple html here. No fancy graphics or css
h1 := `
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head><title>Quantis Go Seeder</title><link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/css/bootstrap-material-design.min.css" integrity="sha384-wXznGJNEXNG1NFsbm0ugrLFMQPWswR3lds2VeinahP8N0zJw9VWSopbjv2x7WCvX" crossorigin="anonymous">
<style>
body {
background-color: #1e1e1e;
body: white;
}
b{
color: white;
}
</style>
</head><body>
<center>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://unpkg.com/[email protected]/dist/umd/popper.js" integrity="sha384-fA23ZRQ3G/J53mElWqVJEGJzU0sTs+SvzG8fXVWP+kJQ1lwFAOkcUOysnlKJC33U" crossorigin="anonymous"></script>
<script src="https://unpkg.com/[email protected]/dist/js/bootstrap-material-design.js" integrity="sha384-CauSuKpEqAFajSpkdjv3z9t8E7RlpJ1UP0lKM/+NdtSarroVKu069AlsRPKkFBz9" crossorigin="anonymous"></script>
<script>$(document).ready(function() { $('body').bootstrapMaterialDesign(); });</script>
<nav class="navbar navbar-expand-lg navbar-dark" style="background-color: #388E3C;">
<a class="navbar-brand" href="#">Quantis SeederX</a>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" href="/summary">Summary <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="/dns?s=Mainnet All">DNS</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/statusCG?s=Mainnet All">CGNodes</a>
</li>
</ul>
</div>
</nav>
`
fmt.Fprintf(w, h1)
// read the seeder name
n := r.FormValue("s")
if n != "" {
s := getSeederByName(n)
if s != nil {
fmt.Fprintf(w, "<br><b>Seeder: %s</b>", html.EscapeString(s.name))
}
}
fmt.Fprintf(w, "</center><hr><br>")
}
// writeFooter will output the standard footer
func writeFooter(w http.ResponseWriter, r *http.Request, st time.Time) {
// Footer needs to be exported for template processing to work
var Footer struct {
Uptime string
Version string
Rt string
}
f := `
<hr>
<center>
<b>Version:{{.Version}}</b>
<b>Uptime:{{.Uptime}}</b>
<b>Request Time:{{.Rt}}</b>
</center>
</body></html>
`
Footer.Uptime = time.Since(config.uptime).String()
Footer.Version = config.version
Footer.Rt = time.Since(st).String()
t := template.New("Footer template")
t, err := t.Parse(f)
if err != nil {
log.Printf("error parsing template %v\n", err)
}
err = t.Execute(w, Footer)
if err != nil {
log.Printf("error executing template %v\n", err)
}
if config.verbose {
log.Printf("status - processed web request: %s %s\n",
r.RemoteAddr,
r.RequestURI)
}
}
/*
*/