matrix.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. package main
  2. import (
  3. "bufio"
  4. "log"
  5. "os"
  6. "strings"
  7. "sync"
  8. )
  9. //ByControlPlane contains all the channels we need.
  10. type ByControlPlane struct {
  11. BadTokens chan string
  12. GoodTokens chan string
  13. StatsTokens chan string
  14. }
  15. type bScore struct {
  16. BadScore float64
  17. GoodScore float64
  18. }
  19. type bMap struct {
  20. bScores map[string]bScore
  21. busy sync.Mutex
  22. }
  23. //ControPlane is the variabile
  24. var ControPlane ByControlPlane
  25. //ByClassifier is the structure containing our Pseudo-Bayes classifier.
  26. type ByClassifier struct {
  27. STATS sync.Map
  28. Matrix bMap
  29. }
  30. //AddStats adds the statistics after proper blocking.
  31. func (c *ByClassifier) AddStats(action string) {
  32. var one int64 = 1
  33. if v, ok := c.STATS.Load(action); ok {
  34. c.STATS.Store(action, v.(int64)+1)
  35. } else {
  36. c.STATS.Store(action, one)
  37. }
  38. }
  39. //IsBAD inserts a bad key in the right place.
  40. func (c *ByClassifier) IsBAD(key string) {
  41. c.Matrix.busy.Lock()
  42. defer c.Matrix.busy.Unlock()
  43. var t bScore
  44. if val, ok := c.Matrix.bScores[key]; ok {
  45. t.BadScore = val.BadScore + 1
  46. t.GoodScore = val.GoodScore
  47. } else {
  48. t.BadScore = 1
  49. t.GoodScore = 0
  50. }
  51. c.Matrix.bScores[key] = t
  52. }
  53. //IsGOOD inserts the key in the right place.
  54. func (c *ByClassifier) IsGOOD(key string) {
  55. c.Matrix.busy.Lock()
  56. defer c.Matrix.busy.Unlock()
  57. var t bScore
  58. if val, ok := c.Matrix.bScores[key]; ok {
  59. t.GoodScore = val.GoodScore + 1
  60. t.BadScore = val.BadScore
  61. } else {
  62. t.BadScore = 0
  63. t.GoodScore = 1
  64. }
  65. c.Matrix.bScores[key] = t
  66. }
  67. //Posterior calculates Shannon based entropy using bad and good as different distributions
  68. func (c *ByClassifier) Posterior(hdr string) map[string]float64 {
  69. c.Matrix.busy.Lock()
  70. defer c.Matrix.busy.Unlock()
  71. tokens := strings.Fields(sanitizeHeaders(hdr))
  72. lenTokens := float64(len(tokens))
  73. ff := make(map[string]float64)
  74. if lenTokens == 0 {
  75. ff["BAD"] = 0.5
  76. ff["GOOD"] = 0.5
  77. return ff
  78. }
  79. log.Println("Start classification of: ", tokens)
  80. var hBadM, hGoodM float64
  81. for _, tk := range tokens {
  82. if val, ok := c.Matrix.bScores[tk]; ok {
  83. if val.BadScore > 0 {
  84. hBadM += val.BadScore
  85. }
  86. if val.GoodScore > 0 {
  87. hGoodM += val.GoodScore
  88. }
  89. }
  90. }
  91. maxScore := float64(ProxyFlow.seniority) * lenTokens
  92. if maxScore == 0 {
  93. maxScore = 1
  94. }
  95. ff["BAD"] = hBadM / maxScore
  96. ff["GOOD"] = hGoodM / maxScore
  97. log.Println("Entropies: ", ff)
  98. return ff
  99. }
  100. func (c *ByClassifier) enroll() {
  101. ControPlane.BadTokens = make(chan string, 2048)
  102. ControPlane.GoodTokens = make(chan string, 2048)
  103. ControPlane.StatsTokens = make(chan string, 2048)
  104. c.Matrix.busy.Lock()
  105. c.Matrix.bScores = make(map[string]bScore)
  106. c.Matrix.busy.Unlock()
  107. c.readInitList("blacklist.txt", "BAD")
  108. c.readInitList("whitelist.txt", "GOOD")
  109. go c.readBadTokens()
  110. go c.readGoodTokens()
  111. go c.readStatsTokens()
  112. log.Println("Classifier populated...")
  113. }
  114. func (c *ByClassifier) readBadTokens() {
  115. log.Println("Start reading BAD tokens")
  116. for token := range ControPlane.BadTokens {
  117. log.Println("Received BAD Token: ", token)
  118. c.IsBAD(token)
  119. }
  120. }
  121. func (c *ByClassifier) readGoodTokens() {
  122. log.Println("Start reading GOOD tokens")
  123. for token := range ControPlane.GoodTokens {
  124. log.Println("Received GOOD Token: ", token)
  125. c.IsGOOD(token)
  126. }
  127. }
  128. func (c *ByClassifier) readStatsTokens() {
  129. log.Println("Start reading STATS tokens")
  130. for token := range ControPlane.StatsTokens {
  131. c.AddStats(token)
  132. }
  133. }
  134. func (c *ByClassifier) readInitList(filePath, class string) {
  135. inFile, err := os.Open(filePath)
  136. if err != nil {
  137. log.Println(err.Error() + `: ` + filePath)
  138. return
  139. }
  140. defer inFile.Close()
  141. scanner := bufio.NewScanner(inFile)
  142. for scanner.Scan() {
  143. if len(scanner.Text()) > 3 {
  144. switch class {
  145. case "BAD":
  146. log.Println("Loading into Blacklist: ", scanner.Text()) // the line
  147. c.IsBAD(scanner.Text())
  148. case "GOOD":
  149. log.Println("Loading into Whitelist: ", scanner.Text()) // the line
  150. c.IsGOOD(scanner.Text())
  151. }
  152. }
  153. }
  154. }