zardoz/matrix.go

269 lines
5.1 KiB
Go
Raw Normal View History

2019-11-22 11:35:16 +01:00
package main
import (
"fmt"
"log"
2019-12-03 16:47:24 +01:00
"os"
2019-11-22 11:35:16 +01:00
"sort"
"strconv"
"strings"
"sync"
"time"
)
//ByControlPlane contains all the channels we need.
type ByControlPlane struct {
BadTokens chan string
GoodTokens chan string
StatsTokens chan string
}
//ControPlane is the variabile
var ControPlane ByControlPlane
//ByClassifier is the structure containing our Pseudo-Bayes classifier.
type ByClassifier struct {
GOOD sync.Map
BAD sync.Map
MEH sync.Map
STATS sync.Map
}
//AddStats adds the statistics after proper blocking.
func (c *ByClassifier) AddStats(action string) {
var one int64 = 1
if v, ok := c.STATS.Load(action); ok {
c.STATS.Store(action, v.(int64)+1)
} else {
c.STATS.Store(action, one)
}
}
//IsBAD inserts a bad key in the right place.
func (c *ByClassifier) IsBAD(key string) {
if _, ok := c.MEH.Load(key); ok {
c.MEH.Store(key, time.Now().UnixNano())
2019-12-04 14:35:40 +01:00
log.Println("Updated BAD into MEH: ", key)
2019-11-22 11:35:16 +01:00
return
}
if _, ok := c.GOOD.Load(key); ok {
c.MEH.Store(key, time.Now().UnixNano())
c.GOOD.Delete(key)
2019-12-04 14:35:40 +01:00
log.Println("Moved to MEH from GOOD: ", key)
2019-11-22 11:35:16 +01:00
return
}
c.BAD.Store(key, time.Now().UnixNano())
2019-12-04 14:35:40 +01:00
log.Println("Stored into BAD: ", key)
2019-11-22 11:35:16 +01:00
}
//IsGOOD inserts the key in the right place.
func (c *ByClassifier) IsGOOD(key string) {
if _, ok := c.MEH.Load(key); ok {
c.MEH.Store(key, time.Now().UnixNano())
2019-12-04 14:35:40 +01:00
log.Println("Updated GOOD into MEH: ", key)
2019-11-22 11:35:16 +01:00
return
}
if _, ok := c.BAD.Load(key); ok {
c.MEH.Store(key, time.Now().UnixNano())
c.BAD.Delete(key)
2019-12-04 14:35:40 +01:00
log.Println("Moved to MEH from BAD: ", key)
2019-11-22 11:35:16 +01:00
return
}
c.GOOD.Store(key, time.Now().UnixNano())
2019-12-04 14:35:40 +01:00
log.Println("Stored into GOOD: ", key)
2019-11-22 11:35:16 +01:00
}
//Posterior calculates the posterior probabilities in pseudo-bayes.
func (c *ByClassifier) Posterior(hdr string) map[string]float64 {
headers := strings.Fields(sanitizeHeaders(hdr))
var result = make(map[string]float64)
result["BAD"] = 0
result["GOOD"] = 0
var tmpResGood, tmpResBad, tmpTotal float64
for _, token := range headers {
if _, ok := c.BAD.Load(token); ok {
tmpResBad++
tmpTotal++
}
if _, ok := c.GOOD.Load(token); ok {
tmpResGood++
tmpTotal++
}
}
if tmpTotal == 0 {
tmpTotal = 1
}
log.Printf("Bad Tokens: %f, Good Tokens %f , Total %f\n", tmpResBad, tmpResGood, tmpTotal)
result["BAD"] = tmpResBad / tmpTotal
result["GOOD"] = tmpResGood / tmpTotal
return result
}
//Janitor keeps the maps under a certain size, keeping the biggest values.
func (c *ByClassifier) Janitor(size int) {
log.Println("Janitor Running")
sortMap(&c.BAD, size)
sortMap(&c.GOOD, size)
sortMap(&c.MEH, size)
log.Println("Janitor Finished.")
}
//CleanThread is the Janitor thread
func (c *ByClassifier) CleanThread() {
for {
MaxSize, err := strconv.Atoi(fmt.Sprintf("%d", Maturity))
if err != nil {
MaxSize = 1000
log.Println("Maxsize converted to: ", MaxSize)
}
log.Println("Janitor Maxsize is now:", MaxSize)
time.Sleep(10 * time.Second)
c.Janitor(MaxSize)
}
}
func (c *ByClassifier) enroll() {
ControPlane.BadTokens = make(chan string, 2048)
ControPlane.GoodTokens = make(chan string, 2048)
ControPlane.StatsTokens = make(chan string, 2048)
2019-12-04 14:35:40 +01:00
c.IsBAD("/Penis/bad")
c.IsGOOD("/Gun/good")
2019-11-22 11:35:16 +01:00
c.MEH.Store("Dildo", time.Now().UnixNano())
go c.readBadTokens()
go c.readGoodTokens()
go c.readStatsTokens()
log.Println("Classifier populated...")
go c.CleanThread()
2019-12-03 16:17:36 +01:00
go c.CleanMEH()
2019-11-22 11:35:16 +01:00
log.Println("Janitor Started")
}
func sortMap(unsorted *sync.Map, size int) {
type Myt struct {
Name string
Num int64
}
var tempCont []Myt
var tc Myt
unsorted.Range(func(key interface{}, value interface{}) bool {
tc.Name = key.(string)
tc.Num = value.(int64)
tempCont = append(tempCont, tc)
return true
})
sort.Slice(tempCont, func(i, j int) bool { return tempCont[i].Num > tempCont[j].Num })
2019-11-28 15:21:27 +01:00
if size > 0 && len(tempCont) > size {
2019-11-22 11:35:16 +01:00
tempCont = tempCont[:size]
}
unsorted.Range(func(key interface{}, value interface{}) bool {
unsorted.Delete(key)
return true
})
for _, val := range tempCont {
unsorted.Store(val.Name, val.Num)
}
}
func (c *ByClassifier) readBadTokens() {
log.Println("Start reading BAD tokens")
for token := range ControPlane.BadTokens {
2019-12-04 14:35:40 +01:00
log.Println("Received BAD Token: ", token)
2019-11-22 11:35:16 +01:00
c.IsBAD(token)
}
}
func (c *ByClassifier) readGoodTokens() {
log.Println("Start reading GOOD tokens")
for token := range ControPlane.GoodTokens {
2019-12-04 14:35:40 +01:00
log.Println("Received GOOD Token: ", token)
2019-11-22 11:35:16 +01:00
c.IsGOOD(token)
}
}
func (c *ByClassifier) readStatsTokens() {
log.Println("Start reading STATS tokens")
for token := range ControPlane.StatsTokens {
c.AddStats(token)
}
}
2019-12-03 16:17:36 +01:00
//CleanMEH cleans periodically the spurious tokens.
func (c *ByClassifier) CleanMEH() {
2019-12-03 16:47:24 +01:00
var err error
2019-12-03 16:45:58 +01:00
ProxyFlow.refreshtime, err = time.ParseDuration(os.Getenv("REFRESHTIME"))
if err != nil {
ProxyFlow.refreshtime = time.Duration(48 * time.Hour)
}
2019-12-03 16:43:49 +01:00
log.Println("Clean MEH Thread running each: ", ProxyFlow.refreshtime)
2019-12-03 16:17:36 +01:00
2019-12-03 16:43:49 +01:00
for a := range time.Tick(ProxyFlow.refreshtime) {
2019-12-03 16:17:36 +01:00
c.MEH.Range(func(key interface{}, value interface{}) bool {
c.MEH.Delete(key)
return true
})
log.Println("MEH Cleaned at:", a)
}
}