-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
465 lines (395 loc) · 12.1 KB
/
main.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
package main
import (
"encoding/json"
"encoding/xml"
"fmt"
"io"
"net"
"net/http"
"os"
"time"
"github.com/google/uuid"
"github.com/gorilla/mux"
"github.com/ilyakaznacheev/cleanenv"
log "github.com/sirupsen/logrus"
)
var cfg ConfigOptions
type ConfigOptions struct {
SnfPort string `env:"SNFPORT" env-default:"9001"`
SnfHost string `env:"SNFHOST" env-default:"127.0.0.1"`
SnfTimeout float32 `env:"SNFTIMEOUT" env-default:"5"`
HttpPort string `env:"HTTPPORT" env-default:"8080"`
WorkingDir string `env:"WORKINGDIR" env-default:"/usr/share/snf-server/storage/"`
LogLevel string `env:"LOGLEVEL" env-default:"info"`
}
type XciResult struct {
IpResult struct {
Ip string `xml:"ip,attr"`
Type string `xml:"type,attr"`
Range string `xml:"range,attr"`
Code int `xml:"code,attr"`
Probability float32 `xml:"p,attr"`
Confidence float32 `xml:"c,attr"`
Bad int `xml:"b,attr"`
Good int `xml:"g,attr"`
} `xml:"xci>gbudb>result"`
ScanResult struct {
Code int `xml:"code,attr"`
Xhdr string `xml:"xhdr"`
Log struct {
Scan struct {
TimeStamp int `xml:"u,attr"`
Message string `xml:"m,attr"`
Result int `xml:"s,attr"`
RuleId int `xml:"r,attr"`
Performance struct {
SetupTime int `xml:"s,attr"`
ScanTime int `xml:"t,attr"`
Bytes int `xml:"l,attr"`
Evaluators int `xml:"d,attr"`
} `xml:"p"`
Gbudb struct {
Ordinal int `xml:"o,attr"`
Ip string `xml:"i,attr"`
Flag string `xml:"t,attr"`
Confidence float32 `xml:"c,attr"`
Probability float32 `xml:"p,attr"`
Result string `xml:"r,attr"`
} `xml:"g"`
} `xml:"s"`
} `xml:"log"`
} `xml:"xci>scanner>result"`
ReportResult struct {
Stats struct {
NodeId string `xml:"nodeid,attr"`
Basetime int `xml:"basetime,attr"`
Elapsed int `xml:"elapsed,attr"`
Class string `xml:"class,attr"`
Version struct {
Engine string `xml:"engine"`
Platform string `xml:"platform"`
} `xml:"version"`
Timers struct {
Run struct {
Started int `xml:"started,attr"`
Elapsed int `xml:"elapsed,attr"`
} `xml:"run"`
Sync struct {
Latest int `xml:"latest,attr"`
Elapsed int `xml:"elapsed,attr"`
} `xml:"sync"`
Save struct {
Latest int `xml:"latest,attr"`
Elapsed int `xml:"elapsed,attr"`
} `xml:"save"`
Condense struct {
Latest int `xml:"latest,attr"`
Elapsed int `xml:"elapsed,attr"`
} `xml:"condense"`
} `xml:"timers"`
Gbudb struct {
Size struct {
Bytes int `xml:"bytes,attr"`
} `xml:"size"`
Records struct {
Count int `xml:"count,attr"`
} `xml:"records"`
Utilization struct {
Percent float32 `xml:"percent,attr"`
} `xml:"utilization"`
} `xml:"gbudb"`
Rules struct {
Rulesbase struct {
Utc int `xml:"utc,attr"`
} `xml:"rulesbase"`
Active struct {
Utc int `xml:"utc,attr"`
} `xml:"active"`
Update struct {
Ready string `xml:"ready,attr"`
Utc int `xml:"utc,attr"`
} `xml:"update"`
Latest struct {
Rule string `xml:"rule,attr"`
} `xml:"latest"`
} `xml:"rules"`
} `xml:"stats"`
} `xml:"xci>report>response"`
}
func httpHealth(w http.ResponseWriter, r *http.Request) {
uuid := uuid.New()
reqId := uuid.String()
_, err := snifferReport(reqId, "5")
if err != nil {
writeLogLine("error", "httpHealth", reqId, err.Error())
writeHttpError(reqId, w, fmt.Sprintf("%v", err))
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
result := "{\"result\": alive}"
writeLogLine("debug", "httpHealth", reqId, result)
io.WriteString(w, result)
}
func httpScan(w http.ResponseWriter, r *http.Request) {
uuid := uuid.New()
reqId := uuid.String()
l := r.Header.Get("logEnable")
x := r.Header.Get("xhdrEnable")
ip := r.Header.Get("ip")
id := r.Header.Get("requestId")
l = setDefault(l, "yes")
x = setDefault(x, "no")
if id != "" {
reqId = id
}
reqBody, err := io.ReadAll(r.Body)
if err != nil {
writeLogLine("error", "httpScan", reqId, err.Error())
writeHttpError(reqId, w, fmt.Sprintf("%v", err.Error()))
return
}
tmpFile, err := os.CreateTemp(cfg.WorkingDir, "*")
if err != nil {
writeLogLine("error", "httpScan", reqId, err.Error())
writeHttpError(reqId, w, fmt.Sprintf("%v", err.Error()))
return
}
tmpFile.Write(reqBody)
tmpFile.Close()
writeLogLine("debug", "httpScan", reqId, fmt.Sprintf("{\"file_write\":\"%v\"}", tmpFile.Name()))
writeLogLine("debug", "httpScan", reqId, fmt.Sprintf("{\"ip\":\"%v\"}", ip))
result, err := snifferScan(reqId, tmpFile.Name(), ip, l, x)
writeLogLine("debug", "httpScan", reqId, fmt.Sprintf("{\"file_delete\":\"%v\"}", tmpFile.Name()))
os.Remove(tmpFile.Name())
if result != "" {
writeLogLine("info", "httpScan", reqId, result)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
io.WriteString(w, result)
return
} else {
writeLogLine("error", "httpScan", reqId, err.Error())
writeHttpError(reqId, w, fmt.Sprintf("%v", err.Error()))
}
}
func httpTestIp(w http.ResponseWriter, r *http.Request) {
uuid := uuid.New()
reqId := uuid.String()
ip := r.Header.Get("ip")
id := r.Header.Get("requestId")
if id != "" {
reqId = id
}
if ip == "" {
writeLogLine("error", "httpTestIp", reqId, "must include ip to search")
writeHttpError(reqId, w, fmt.Sprintf("must include ip to search"))
return
} else {
writeLogLine("debug", "httpTestIp", reqId, fmt.Sprintf("{\"ip\":\"%v\"}", ip))
}
result, err := snifferTestIp(reqId, ip)
if err != nil {
writeLogLine("error", "httpTestIp", reqId, err.Error())
writeHttpError(reqId, w, fmt.Sprintf("%v", err.Error()))
return
}
writeLogLine("info", "httpTestIp", reqId, result)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
io.WriteString(w, result)
}
func httpStatus(w http.ResponseWriter, r *http.Request) {
uuid := uuid.New()
reqId := uuid.String()
interval := r.Header.Get("interval")
if (interval != "second") && (interval != "minute") && (interval != "hour") {
writeHttpError(reqId, w, fmt.Sprintf("must include interval"))
writeLogLine("error", "httpStatus", reqId, "must include interval")
return
}
result, err := snifferReport(reqId, interval)
if err != nil {
writeLogLine("error", "httpStatus", reqId, err.Error())
writeHttpError(reqId, w, fmt.Sprintf("%v", err))
return
}
writeLogLine("info", "httpStatus", reqId, result)
io.WriteString(w, result)
}
func snifferTestIp(reqId string, ip string) (string, error) {
xci := fmt.Sprintf("<snf><xci><gbudb><test ip=\"%v\"/></gbudb></xci></snf>", ip)
writeLogLine("debug", "snifferTestIp", reqId, fmt.Sprintf("{\"command\":\"%v\"}", xci))
r, err := sendXci(reqId, xci)
if err != nil {
writeLogLine("error", "snifferTestIp", reqId, err.Error())
return "", err
}
writeLogLine("debug", "snifferTestIp", reqId, fmt.Sprintf("{\"response\":\"%v\"}", string(r)))
result := XciToJson(reqId, r, "testip")
return result, nil
}
func snifferScan(reqId string, file string, ip string, l string, x string) (string, error) {
var xci string
if ip == "" {
xci = fmt.Sprintf("<snf><xci><scanner><scan xhdr=\"%v\" log=\"%v\" file=\"%v\"/></scanner></xci></snf>", x, l, file)
} else {
xci = fmt.Sprintf("<snf><xci><scanner><scan xhdr=\"%v\" log=\"%v\" file=\"%v\" ip=\"%v\"/></scanner></xci></snf>", x, l, file, ip)
}
writeLogLine("debug", "snifferScan", reqId, fmt.Sprintf("{\"command\":\"%v\"}", xci))
r, err := sendXci(reqId, xci)
if err != nil {
writeLogLine("error", "snifferScan", reqId, err.Error())
return "", err
}
writeLogLine("debug", "snifferScan", reqId, fmt.Sprintf("{\"response\":\"%v\"}", string(r)))
result := XciToJson(reqId, r, "scan")
return result, nil
}
func snifferReport(reqId string, interval string) (string, error) {
xci := fmt.Sprintf("<snf><xci><report><request><status class=\"%v\"/></request></report></xci></snf>", interval)
writeLogLine("debug", "snifferReport", reqId, fmt.Sprintf("{\"command\":\"%v\"}", xci))
r, err := sendXci(reqId, xci)
if err != nil {
writeLogLine("error", "snifferReport", reqId, err.Error())
return "", err
}
writeLogLine("debug", "snifferReport", reqId, fmt.Sprintf("{\"response\":\"%v\"}", string(r)))
result := XciToJson(reqId, r, "report")
return result, nil
}
func XciToJson(reqId string, xci []byte, reqType string) string {
var data XciResult
err := xml.Unmarshal(xci, &data)
if err != nil {
writeLogLine("error", "XciToJson", reqId, err.Error())
}
writeLogLine("debug", "XciToJson", reqId, fmt.Sprintf("{\"unmarshaled_xml\":\"%v\"}", data))
switch reqType {
case "scan":
json, _ := json.Marshal(data.ScanResult)
return fmt.Sprintf("%s", string(json))
case "testip":
json, _ := json.Marshal(data.IpResult)
return fmt.Sprintf("%s", string(json))
case "report":
json, _ := json.Marshal(data.ReportResult)
return fmt.Sprintf("%s", string(json))
default:
return "no response"
}
}
func sendXci(reqId string, cmd string) ([]byte, error) {
conn, err := connInit(reqId)
if err != nil {
writeLogLine("error", "sendXci", reqId, err.Error())
return nil, err
}
writeLogLine("debug", "sendXci", reqId, fmt.Sprintf("{\"command\":\"%v\"}", cmd))
connWrite(reqId, cmd, conn)
resp := connRead(reqId, conn)
defer conn.Close()
return resp, nil
}
func connInit(reqId string) (net.Conn, error) {
writeLogLine("debug", "connInit", reqId, cfg.SnfHost+":"+cfg.SnfPort)
d := net.Dialer{Timeout: time.Second * 5}
c, err := d.Dial("tcp", cfg.SnfHost+":"+cfg.SnfPort)
if err != nil {
return nil, err
}
return c, nil
}
func connWrite(reqId string, xci string, c net.Conn) {
_, err := c.Write([]byte(xci))
if err != nil {
writeLogLine("error", "connWrite", reqId, err.Error())
}
}
func connRead(reqId string, c net.Conn) []byte {
resp, err := io.ReadAll(c)
if err != nil {
writeLogLine("error", "connRead", reqId, err.Error())
}
writeLogLine("debug", "connRead", reqId, fmt.Sprintf("{\"response\":\"%v\"}", resp))
return resp
}
func writeHttpError(reqId string, w http.ResponseWriter, msg string) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, fmt.Sprintf("{\"level\":\"error\",\"msg\":\"%v\"}", msg))
writeLogLine("error", "writeHttpError", reqId, msg)
}
func notFound(w http.ResponseWriter, r *http.Request) {
uuid := uuid.New()
reqId := uuid.String()
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusNotFound)
io.WriteString(w, "{\"level\":\"error\",\"msg\":\"endpoint not found\"}")
writeLogLine("info", "notFound", reqId, "endpoint not found")
}
func setDefault(v string, d string) string {
if v == "" {
return d
} else {
return v
}
}
func setupLogging(logLevel string) {
log.SetFormatter(&log.JSONFormatter{})
log.SetOutput(os.Stdout)
if logLevel == "info" {
log.SetLevel(log.InfoLevel)
} else if logLevel == "debug" {
log.SetLevel(log.DebugLevel)
}
}
func writeLogLine(level string, function string, reqId string, msg string) {
if level == "info" {
log.WithFields(log.Fields{
"function": function,
"request_id": reqId,
}).Info(msg)
} else if level == "error" {
log.WithFields(log.Fields{
"function": function,
"request_id": reqId,
}).Error(msg)
} else if level == "debug" {
log.WithFields(log.Fields{
"function": function,
"request_id": reqId,
}).Debug(msg)
}
}
func setupRoutes() {
r := mux.NewRouter()
r.NotFoundHandler = http.HandlerFunc(notFound)
r.HandleFunc("/health", httpHealth)
r.HandleFunc("/scan", httpScan)
r.HandleFunc("/testip", httpTestIp)
r.HandleFunc("/status", httpStatus)
writeLogLine("info", "setupRoutes", "0", fmt.Sprintf("{\"initialized snifferfy on port %v\"}", cfg.HttpPort))
srv := &http.Server{
Addr: "0.0.0.0:" + cfg.HttpPort,
WriteTimeout: time.Second * 15,
ReadTimeout: time.Second * 15,
IdleTimeout: time.Second * 60,
Handler: r,
}
if err := srv.ListenAndServe(); err != nil {
log.Fatal(err)
}
}
func init() {
// config
err := cleanenv.ReadEnv(&cfg)
if err != nil {
log.Panic(err)
}
// logging
setupLogging(cfg.LogLevel)
}
func main() {
setupRoutes()
}