classifier.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io/ioutil"
  6. "log"
  7. "net/http"
  8. "net/http/httputil"
  9. "regexp"
  10. "strings"
  11. )
  12. func passAndLearn(resp *http.Response) error {
  13. ProxyFlow.response = resp
  14. ProxyFlow.seniority++
  15. req := ProxyFlow.request
  16. switch {
  17. case resp.StatusCode == 401:
  18. log.Println("401: We don't want to store credentials")
  19. case resp.StatusCode > 399:
  20. buf := bytes.NewBufferString(BlockMessage)
  21. resp.Body = ioutil.NopCloser(buf)
  22. resp.Status = "403 Forbidden"
  23. resp.StatusCode = 403
  24. resp.Header["Content-Length"] = []string{fmt.Sprint(buf.Len())}
  25. resp.Header.Set("Content-Encoding", "none")
  26. log.Println("Filing inside bad class")
  27. feedRequest(req, "BAD")
  28. default:
  29. log.Println("Filing inside Good Class: ", resp.StatusCode)
  30. feedRequest(req, "GOOD")
  31. }
  32. return nil
  33. }
  34. func blockAndlearn(resp *http.Response) error {
  35. ProxyFlow.response = resp
  36. ProxyFlow.seniority++
  37. req := ProxyFlow.request
  38. buf := bytes.NewBufferString(BlockMessage)
  39. resp.Body = ioutil.NopCloser(buf)
  40. resp.Status = "403 Forbidden"
  41. resp.StatusCode = 403
  42. resp.Header["Content-Length"] = []string{fmt.Sprint(buf.Len())}
  43. resp.Header.Set("Content-Encoding", "none")
  44. switch {
  45. case resp.StatusCode == 401:
  46. log.Println("401: We don't want to store credentials")
  47. case resp.StatusCode > 399:
  48. log.Println("Filing inside bad class")
  49. feedRequest(req, "BAD")
  50. default:
  51. log.Println("Filing inside Good Class: ", resp.StatusCode)
  52. feedRequest(req, "GOOD")
  53. }
  54. return nil
  55. }
  56. func sanitizeHeaders(s string) string {
  57. re := regexp.MustCompile(`[a-zA-Z]{4,32}|([{][/].*[}])|([0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12})`)
  58. matched := re.FindAllString(s, -1)
  59. tmpSt := strings.ToLower(strings.Join(matched, " "))
  60. tmpSt = strings.ReplaceAll(tmpSt, "{", "")
  61. tmpSt = strings.ReplaceAll(tmpSt, "}", "")
  62. log.Println("Matched: " + tmpSt)
  63. return tmpSt
  64. }
  65. func feedRequest(req *http.Request, class string) {
  66. feed := formatRequest(req)
  67. feed = sanitizeHeaders(feed)
  68. feedarray := strings.Fields(feed)
  69. fmt.Println(feedarray)
  70. if class == "BAD" {
  71. for _, token := range feedarray {
  72. log.Println("Feeding BAD token: ", token)
  73. ControPlane.BadTokens <- token
  74. }
  75. }
  76. if class == "GOOD" {
  77. for _, token := range feedarray {
  78. log.Println("Feeding GOOD Token:", token)
  79. ControPlane.GoodTokens <- token
  80. }
  81. }
  82. }
  83. func formatRequest(req *http.Request) string {
  84. ingestBody := req.ContentLength < 2048 && req.ContentLength > 1
  85. log.Println("Ingesting the body: ", ingestBody)
  86. requestDump, err := httputil.DumpRequest(req, ingestBody)
  87. if err != nil {
  88. fmt.Println(err)
  89. }
  90. return fmt.Sprintf("{%s} %s\n", req.URL.Path, requestDump)
  91. }