-
Notifications
You must be signed in to change notification settings - Fork 1
/
http.go
152 lines (135 loc) · 4.42 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
package main
import (
"fmt"
"net"
"net/http"
"runtime"
"sort"
"time"
)
func header() string {
return `
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
</style>
</head>
<body>
`
}
func handler(w http.ResponseWriter, r *http.Request) {
hitmap := hits.GetMap()
if noop {
fmt.Fprintf(w, "<h1>In noop mode</h1>\n")
}
var m runtime.MemStats
runtime.ReadMemStats(&m)
fmt.Fprint(w, header())
rate := client.RateLimit()
fmt.Fprintf(w, "<p>Alloc: %d, HeapObjects: %d</p>", m.Alloc, m.HeapObjects)
fmt.Fprintf(w, "<p>Syslog Channel Buffer Size: %d</p>", len(syslogChannel))
fmt.Fprintf(w, "<p>Total IPs tracked: %d</p>", len(hits.m))
if rate != nil {
fmt.Fprintf(w, "<p>Fastly API ratelimit: %d calls remaining, reset in %v</p>", rate.Remaining, rate.Reset.Sub(time.Now()))
}
fmt.Fprint(w, "<h2>Limited IPs</h2>\n")
fmt.Fprint(w, "<table><th>IP</th><th>Hits</th><th>Limited</th><th>HPS</th><th>Strikes</th><th>Limit Time Remaining</th><th>List</th>\n")
var rates ipRates
for _, ipr := range hitmap {
rates = append(rates, ipr)
}
sort.Sort(sort.Reverse(hitsSortableIPRates{rates}))
for _, ipr := range rates {
hps := float64(ipr.Hits) / time.Now().Sub(ipr.FirstHit).Seconds()
if ipr.limited {
fmt.Fprintf(w, "<tr><td>%s</td><td>%d</td><td>%t</td><td>%0.2f</td><td>%d</td><td>%0.2fm</td><td>%s</td></tr>\n", ipr.ip.String(), ipr.Hits, ipr.limited, hps, ipr.Strikes, ipr.LimitExpire.Sub(time.Now()).Minutes(), ipr.list.name)
}
}
fmt.Fprint(w, "</table>")
fmt.Fprint(w, "<h2>Top IPs</h2>\n")
fmt.Fprint(w, "<table><th>IP</th><th>Hits</th><th>HPS</th><th>List</th>\n")
for _, ipr := range rates {
hps := float64(ipr.Hits) / time.Now().Sub(ipr.FirstHit).Seconds()
if hps > 0.5 && ipr.Hits > 50 {
fmt.Fprintf(w, "<tr><td>%s</td><td>%d</td><td>%0.2f</td><td>%s</td></tr>\n", ipr.ip.String(), ipr.Hits, hps, ipr.list.name)
}
}
fmt.Fprint(w, "</table>")
fmt.Fprintf(w, "<h2>Top /24s</h2>\n")
fmt.Fprint(w, "<table><th>Network</th><th>Hits</th><th>HPS</th><th>List</th>\n")
ipRatesByNetwork := make(map[string][]*ipRate)
mask := net.CIDRMask(24, 32)
for _, ipr := range rates {
iprIP := *ipr.ip
ip := net.IPv4(iprIP[12], iprIP[13], iprIP[14], byte(0))
network := net.IPNet{IP: ip, Mask: mask}
ipRatesByNetwork[network.IP.String()] = append(ipRatesByNetwork[network.IP.String()], ipr)
}
var networks networkHPSs
for network, rates := range ipRatesByNetwork {
var totalHPS float64
var totalHits int
list := rates[0].list
for _, ipr := range rates {
hps := float64(ipr.Hits) / time.Now().Sub(ipr.FirstHit).Seconds()
totalHPS += hps
totalHits += ipr.Hits
if ipr.list != list {
list = nil
}
}
if totalHits > 10000 || (totalHits > 100 && totalHPS > 1) {
networks = append(networks, networkHPS{network: network, hps: totalHPS, hits: totalHits, list: list})
}
}
sort.Sort(sort.Reverse(networks))
for _, network := range networks {
var list string
if network.list != nil {
list = network.list.name
}
fmt.Fprintf(w, "<tr><td><a href=\"https://stat.ripe.net/%s\">%s</a></td><td>%d</td><td>%0.2f</td><td>%s</td></tr>", network.network, network.network, network.hits, network.hps, list)
}
fmt.Fprint(w, "</table>")
fmt.Fprint(w, "<h2>Traffic by list</h2>\n")
fmt.Fprint(w, "<table><th>List</th><th>HPS</th><th>% of overall traffic</th>\n")
var total float64
l := make(map[*IPList]float64)
for _, ipr := range rates {
hps := float64(ipr.Hits) / time.Now().Sub(ipr.FirstHit).Seconds()
l[ipr.list] += hps
total += hps
}
var lists listHPSs
for iplist, hps := range l {
lists = append(lists, listHPS{list: iplist, hps: hps})
}
sort.Sort(sort.Reverse(lists))
for _, list := range lists {
fmt.Fprintf(w, "<tr><td>%s</td><td>%0.2f</td><td>%0.1f%%</tr>\n", list.list.name, list.hps, (list.hps/total)*100)
}
fmt.Fprint(w, "</table>")
}
type listHPS struct {
list *IPList
hps float64
}
type listHPSs []listHPS
func (l listHPSs) Len() int { return len(l) }
func (l listHPSs) Less(i, j int) bool { return l[i].hps < l[j].hps }
func (l listHPSs) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
type networkHPS struct {
network string
hps float64
hits int
list *IPList
}
type networkHPSs []networkHPS
func (l networkHPSs) Len() int { return len(l) }
func (l networkHPSs) Less(i, j int) bool { return l[i].hits < l[j].hits }
func (l networkHPSs) Swap(i, j int) { l[i], l[j] = l[j], l[i] }