2019-11-22 11:35:16 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-11-28 11:16:14 +01:00
|
|
|
"encoding/json"
|
2019-11-22 11:35:16 +01:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2019-11-28 11:16:14 +01:00
|
|
|
type dumpRecord struct {
|
|
|
|
Token string
|
|
|
|
LastSeen string
|
|
|
|
}
|
|
|
|
|
|
|
|
type dumpStats struct {
|
|
|
|
Decision string
|
|
|
|
Amount int64
|
|
|
|
}
|
|
|
|
|
|
|
|
type dumpStruct struct {
|
|
|
|
GOOD []dumpRecord
|
|
|
|
BAD []dumpRecord
|
|
|
|
MEH []dumpRecord
|
|
|
|
STATS []dumpStats
|
|
|
|
}
|
|
|
|
|
2019-11-22 11:35:16 +01:00
|
|
|
// WriteToFile will print any string of text to a file safely by
|
|
|
|
// checking for errors and syncing at the end.
|
|
|
|
func writeToFile(filename string, data string) error {
|
|
|
|
file, err := os.Create(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
_, err = io.WriteString(file, data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return file.Sync()
|
|
|
|
}
|
|
|
|
|
|
|
|
func handlepanic() {
|
|
|
|
|
|
|
|
if a := recover(); a != nil {
|
|
|
|
fmt.Println("OPS!: Recovering from:", a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func saveBayesToFile() {
|
|
|
|
|
|
|
|
var tmpJSON string
|
2019-11-28 11:16:14 +01:00
|
|
|
var DumpJson = new(dumpStruct)
|
2019-11-22 11:35:16 +01:00
|
|
|
|
|
|
|
log.Println("Trying to write json file")
|
|
|
|
|
|
|
|
Classifier.BAD.Range(func(key interface{}, value interface{}) bool {
|
2019-11-28 11:16:14 +01:00
|
|
|
var t dumpRecord
|
|
|
|
t.Token = key.(string)
|
|
|
|
t.LastSeen = time.Unix(0, value.(int64)).String()
|
|
|
|
DumpJson.BAD = append(DumpJson.BAD, t)
|
2019-11-22 11:35:16 +01:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
Classifier.GOOD.Range(func(key interface{}, value interface{}) bool {
|
2019-11-28 11:16:14 +01:00
|
|
|
var t dumpRecord
|
|
|
|
t.Token = key.(string)
|
|
|
|
t.LastSeen = time.Unix(0, value.(int64)).String()
|
|
|
|
DumpJson.GOOD = append(DumpJson.GOOD, t)
|
2019-11-22 11:35:16 +01:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
Classifier.MEH.Range(func(key interface{}, value interface{}) bool {
|
2019-11-28 11:16:14 +01:00
|
|
|
var t dumpRecord
|
|
|
|
t.Token = key.(string)
|
|
|
|
t.LastSeen = time.Unix(0, value.(int64)).String()
|
|
|
|
DumpJson.MEH = append(DumpJson.MEH, t)
|
2019-11-22 11:35:16 +01:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
Classifier.STATS.Range(func(key interface{}, value interface{}) bool {
|
2019-11-28 11:16:14 +01:00
|
|
|
var t dumpStats
|
|
|
|
t.Decision = key.(string)
|
|
|
|
t.Amount = value.(int64)
|
|
|
|
DumpJson.STATS = append(DumpJson.STATS, t)
|
|
|
|
|
2019-11-22 11:35:16 +01:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
2019-11-28 11:16:14 +01:00
|
|
|
if tmpJ, e := json.MarshalIndent(DumpJson, "", " "); e == nil {
|
|
|
|
tmpJSON = fmt.Sprintf("%s", tmpJ)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
tmpJSON = e.Error()
|
|
|
|
}
|
|
|
|
|
2019-11-24 13:18:22 +01:00
|
|
|
dumpfile := os.Getenv("DUMPFILE")
|
|
|
|
if dumpfile == "" {
|
2019-11-28 11:16:14 +01:00
|
|
|
dumpfile = "bayes.json"
|
2019-11-24 13:18:22 +01:00
|
|
|
}
|
|
|
|
|
2019-11-22 11:35:16 +01:00
|
|
|
if DebugLog {
|
|
|
|
log.Println(tmpJSON)
|
|
|
|
} else {
|
2019-11-24 13:18:22 +01:00
|
|
|
writeToFile(dumpfile, tmpJSON)
|
|
|
|
log.Println("File saved: ", dumpfile)
|
2019-11-22 11:35:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func jsonEngine() {
|
|
|
|
|
|
|
|
for {
|
|
|
|
log.Println("Zardoz Seniority: ", ProxyFlow.seniority)
|
|
|
|
saveBayesToFile()
|
|
|
|
time.Sleep(1 * time.Minute)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
log.Printf("File Engine Starting")
|
|
|
|
|
|
|
|
go jsonEngine()
|
|
|
|
log.Printf("FIle Engine Started")
|
|
|
|
}
|