handler.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "math"
  6. "net/http"
  7. "net/http/httputil"
  8. )
  9. func handler(p *httputil.ReverseProxy) func(http.ResponseWriter, *http.Request) {
  10. return func(w http.ResponseWriter, r *http.Request) {
  11. //put the request inside our structure
  12. ProxyFlow.request = r
  13. probs := Classifier.Posterior(formatRequest(r))
  14. log.Printf("Posterior Probabilities: %+v\n", probs)
  15. action := quadrant(probs)
  16. ControPlane.StatsTokens <- action
  17. switch action {
  18. case "BLOCK", "BLOCKLEARN":
  19. p.ModifyResponse = blockAndlearn
  20. log.Println("Request Blocked")
  21. p.ServeHTTP(w, r)
  22. case "PASS", "PASSLEARN":
  23. p.ModifyResponse = passAndLearn
  24. w.Header().Set("Probabilities", fmt.Sprintf("%v ", probs))
  25. p.ServeHTTP(w, r)
  26. log.Println("Passing Request")
  27. default:
  28. log.Println("No Decision: PASS and LEARN")
  29. p.ModifyResponse = passAndLearn
  30. w.Header().Set("Probabilities", fmt.Sprintf("%v ", probs))
  31. p.ServeHTTP(w, r)
  32. }
  33. }
  34. }
  35. func quadrant(p map[string]float64) string {
  36. sure := math.Abs(p["BAD"]-p["GOOD"]) >= ProxyFlow.sensitivity
  37. badish := p["BAD"] > p["GOOD"]
  38. goodish := p["GOOD"] > p["BAD"]
  39. if ProxyFlow.seniority < Maturity {
  40. log.Println("Seniority too low. Waiting.")
  41. return "YOUNG"
  42. }
  43. if sure {
  44. if goodish {
  45. return "PASS"
  46. }
  47. if badish {
  48. return "BLOCK"
  49. }
  50. } else {
  51. if goodish {
  52. return "PASSLEARN"
  53. }
  54. if badish {
  55. return "BLOCKLEARN"
  56. }
  57. }
  58. return "MEH"
  59. }