-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebserver.go
279 lines (261 loc) · 6.71 KB
/
webserver.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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"os/signal"
"reflect"
"strconv"
"strings"
"sync"
"sync/atomic"
"syscall"
)
var httpListenPort = flag.String("listenport", ":8888", "what the webserver listens to")
type CollectedData struct {
Type string `json:"type,omitempty"`
Count int `json:"count,omitempty"`
}
// Key is used during parsing to keep name and type
type Key struct {
Name string
Type string
}
// WebCollector contains the main structure for the web server
type WebCollector struct {
FailedHttp uint64 `json:"failed_http,omitempty"` // number of times an error occured on web parsing
SuccessHttp uint64 `json:"success_http,omitempty"` // successful HTTP calls received
TotalKeys uint64 `json:"total_keys,omitempty"` // total number of keys found
TotalAdd uint64 `json:"total_add,omitempty"` // total number of times Add was called
mtx sync.Mutex `json:"-"` // mutex for Data
Data map[string]CollectedData `json:"data,omitempty"`
}
func NewWebCollector() *WebCollector {
return &WebCollector{
Data: make(map[string]CollectedData),
}
}
// Stop stops the web server and prints the output before returning
func (wc *WebCollector) Stop() {
result := ConfigFile{WebCollector: wc}
// process data
result.Input = wc.GetResult()
// save to output file
outBytes, err := json.Marshal(&result)
if err != nil {
fmt.Println(err)
return
}
if len(*outputFile) > 0 {
err = ioutil.WriteFile(*outputFile, outBytes, 0644)
if err != nil {
fmt.Println(err)
return
}
} else {
fmt.Println(string(outBytes))
}
}
// fieldTypePriorityList is priorities of types, meaning a field can be "upgraded" to the left on the list
var fieldTypePriorityList = []string{"text", "ip", "float", "integer", "bool"}
// upgradeFieldType checks if the field type have to change to a better matching type
// based on detection of types.
func upgradeFieldType(currentKey, newKey string) string {
// if no change
if currentKey == newKey {
return currentKey
}
// if empty newKey
if len(newKey) == 0 {
return currentKey
}
const notFound = -1
// func to find the position in the list, or notFound if not found
getPos := func(name string) int {
x := 0
for _, v := range fieldTypePriorityList {
if v == name {
return x
}
x++
}
return notFound
}
// see if field is in list
cu := getPos(currentKey)
ne := getPos(newKey)
// if one of the keys are not in the list return the current key
if cu == notFound || ne == notFound {
return currentKey
}
if cu < ne {
return currentKey
} else {
return newKey
}
}
// Add increases the number for all the given keys
func (wc *WebCollector) Add(keys []Key) {
atomic.AddUint64(&wc.TotalKeys, uint64(len(keys)))
if len(keys) == 0 {
return
}
atomic.AddUint64(&wc.TotalAdd, 1)
wc.mtx.Lock()
for _, key := range keys {
if keyData, ok := wc.Data[key.Name]; ok {
keyData.Count++
keyData.Type = upgradeFieldType(keyData.Type, key.Type)
wc.Data[key.Name] = keyData
} else {
d := CollectedData{Count: 1, Type: key.Type}
wc.Data[key.Name] = d
}
}
wc.mtx.Unlock()
}
// webPing responds OK with a short text
func webPing(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("OK"))
}
// RunWebServer starts the webserver. This method does not return.
func RunWebServer() {
wc := NewWebCollector()
// start ctrl+c handler
ctrlc := make(chan os.Signal)
signal.Notify(ctrlc, os.Interrupt, syscall.SIGTERM)
go func() {
<-ctrlc
fmt.Println("\rCtrl+C pressed in Terminal, stopping")
wc.Stop()
os.Exit(0)
}()
// webserver handler
webserverFunc := func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
atomic.AddUint64(&wc.FailedHttp, 1)
w.WriteHeader(http.StatusBadRequest)
return
}
if len(body) == 0 || r.Method != http.MethodPost {
atomic.AddUint64(&wc.FailedHttp, 1)
w.WriteHeader(http.StatusBadRequest)
return
}
// go through body
var inputBody map[string]interface{}
err = json.Unmarshal(body, &inputBody)
if err != nil {
atomic.AddUint64(&wc.FailedHttp, 1)
w.WriteHeader(http.StatusConflict)
return
}
// return
keys := findKeywords("", inputBody)
wc.Add(keys)
atomic.AddUint64(&wc.SuccessHttp, 1)
w.WriteHeader(http.StatusOK)
}
// setup webserver
http.HandleFunc("/post", webserverFunc)
http.HandleFunc("/ping", webPing)
fmt.Println("Webserver started")
http.ListenAndServe(*httpListenPort, nil)
}
// findKeywords returns a list of keywords
func findKeywords(srcParent string, input map[string]interface{}) []Key {
var result []Key
var parent string
for k, v := range input {
key := Key{Name: srcParent + k}
switch t := v.(type) {
case map[string]interface{}:
if len(srcParent) > 0 {
parent = srcParent + k + "."
} else {
parent = k + "."
}
res := findKeywords(parent, t)
result = append(result, res...)
default:
key.Type = GuessTypeOf(t)
result = append(result, key)
}
}
return result
}
// GuessTypeOf tries to guess the type of input, returns type if not sure
func GuessTypeOf(input interface{}) (result string) {
switch t := input.(type) {
case []interface{}:
if len(t) > 0 {
return GuessTypeOf(t[0])
}
case float64, float32, []float32, []float64:
// float can be returned on integers, check content
str := fmt.Sprintf("%v", t)
_, err := strconv.Atoi(str)
if err == nil {
result = "integer"
} else {
result = "float"
}
case int, uint, int32, int64, uint64, uint32, []int, []uint, []int32, []int64:
result = "integer"
case bool, []bool:
result = "boolean"
case string:
if net.ParseIP(t) != nil {
result = "ip"
} else {
result = "text"
}
case []string:
result = "text"
default:
result = "unknown-" + reflect.TypeOf(input).String()
}
return
}
// addKey creates the struct based on name
func (i InputStruct) addKey(name, fieldType string) {
splits := strings.Split(name, ".")
// simple case - no . in name
if len(splits) < 2 {
i[name] = fieldType
return
}
endName := splits[len(splits)-1]
// we have a dot
var ptr interface{}
ptr = i
for x := 0; x < len(splits)-1; x++ {
name := splits[x]
if ptr2, ok := ptr.(InputStruct)[name]; !ok {
n := make(InputStruct)
ptr.(InputStruct)[name] = n
ptr = n
} else {
ptr = ptr2.(InputStruct)
}
}
ptr.(InputStruct)[endName] = fieldType
}
// GetResult returns an InputStruct with the parsed data
func (wc *WebCollector) GetResult() InputStruct {
result := make(InputStruct)
wc.mtx.Lock()
myData := wc.Data
wc.mtx.Unlock()
for k, v := range myData {
result.addKey(k, v.Type)
}
return result
}