Browse Source

Back to Json dump, with readable timestamp

Loweel 4 years ago
parent
commit
82c40dc3e6
4 changed files with 61 additions and 19 deletions
  1. 12 0
      bayes.json
  2. 2 2
      classifier.go
  3. 44 14
      file.go
  4. 3 3
      handler.go

+ 12 - 0
bayes.json

@@ -0,0 +1,12 @@
+{
+ "GOOD": [
+  {}
+ ],
+ "BAD": [
+  {}
+ ],
+ "MEH": [
+  {}
+ ],
+ "STATS": null
+}

+ 2 - 2
classifier.go

@@ -11,7 +11,7 @@ import (
 	"strings"
 )
 
-func classifierDecide(resp *http.Response) error {
+func passAndLearn(resp *http.Response) error {
 
 	ProxyFlow.response = resp
 	ProxyFlow.seniority++
@@ -37,7 +37,7 @@ func classifierDecide(resp *http.Response) error {
 	return nil
 }
 
-func blockWithoutLock(resp *http.Response) error {
+func blockAndlearn(resp *http.Response) error {
 
 	ProxyFlow.response = resp
 	ProxyFlow.seniority++

+ 44 - 14
file.go

@@ -1,14 +1,31 @@
 package main
 
 import (
+	"encoding/json"
 	"fmt"
 	"io"
-
 	"log"
 	"os"
 	"time"
 )
 
+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
+}
+
 // 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 {
@@ -35,40 +52,53 @@ func handlepanic() {
 func saveBayesToFile() {
 
 	var tmpJSON string
+	var DumpJson = new(dumpStruct)
 
 	log.Println("Trying to write json file")
 
-	tmpJSON = fmt.Sprintln("\nBAD: ")
-
 	Classifier.BAD.Range(func(key interface{}, value interface{}) bool {
-		tmpJSON = fmt.Sprintf("\t%s %-32s: %d\n", tmpJSON, key.(string), value.(int64))
+		var t dumpRecord
+		t.Token = key.(string)
+		t.LastSeen = time.Unix(0, value.(int64)).String()
+		DumpJson.BAD = append(DumpJson.BAD, t)
 		return true
 	})
 
-	tmpJSON = fmt.Sprintf("%s \rGOOD: \n", tmpJSON)
-
 	Classifier.GOOD.Range(func(key interface{}, value interface{}) bool {
-		tmpJSON = fmt.Sprintf("\t%s %-32s: %d\n", tmpJSON, key.(string), value.(int64))
+		var t dumpRecord
+		t.Token = key.(string)
+		t.LastSeen = time.Unix(0, value.(int64)).String()
+		DumpJson.GOOD = append(DumpJson.GOOD, t)
 		return true
 	})
 
-	tmpJSON = fmt.Sprintf("%s \rMEH: \n", tmpJSON)
-
 	Classifier.MEH.Range(func(key interface{}, value interface{}) bool {
-		tmpJSON = fmt.Sprintf("\t%s %-32s: %d\n", tmpJSON, key.(string), value.(int64))
+		var t dumpRecord
+		t.Token = key.(string)
+		t.LastSeen = time.Unix(0, value.(int64)).String()
+		DumpJson.MEH = append(DumpJson.MEH, t)
 		return true
 	})
 
-	tmpJSON = fmt.Sprintf("%s \rSTATS: \n", tmpJSON)
-
 	Classifier.STATS.Range(func(key interface{}, value interface{}) bool {
-		tmpJSON = fmt.Sprintf("\t%s %-32s: %d\n", tmpJSON, key.(string), value.(int64))
+		var t dumpStats
+		t.Decision = key.(string)
+		t.Amount = value.(int64)
+		DumpJson.STATS = append(DumpJson.STATS, t)
+
 		return true
 	})
 
+	if tmpJ, e := json.MarshalIndent(DumpJson, "", " "); e == nil {
+		tmpJSON = fmt.Sprintf("%s", tmpJ)
+
+	} else {
+		tmpJSON = e.Error()
+	}
+
 	dumpfile := os.Getenv("DUMPFILE")
 	if dumpfile == "" {
-		dumpfile = "bayes.txt"
+		dumpfile = "bayes.json"
 	}
 
 	if DebugLog {

+ 3 - 3
handler.go

@@ -19,19 +19,19 @@ func handler(p *httputil.ReverseProxy) func(http.ResponseWriter, *http.Request)
 
 		switch action {
 		case "BLOCK", "BLOCKLEARN":
-			p.ModifyResponse = blockWithoutLock
+			p.ModifyResponse = blockAndlearn
 			log.Println("Request Blocked")
 			p.ServeHTTP(w, r)
 
 		case "PASS", "PASSLEARN":
-			p.ModifyResponse = classifierDecide
+			p.ModifyResponse = passAndLearn
 			w.Header().Set("Probabilities", fmt.Sprintf("%v ", probs))
 			p.ServeHTTP(w, r)
 			log.Println("Passing Request")
 
 		default:
 			log.Println("No Decision: PASS and LEARN")
-			p.ModifyResponse = classifierDecide
+			p.ModifyResponse = passAndLearn
 			w.Header().Set("Probabilities", fmt.Sprintf("%v ", probs))
 			p.ServeHTTP(w, r)