handler.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. w.Header().Set("Probabilities", fmt.Sprintf("%v ", probs))
  21. log.Println("Request Blocked")
  22. p.ServeHTTP(w, r)
  23. case "PASS", "PASSLEARN":
  24. p.ModifyResponse = passAndLearn
  25. w.Header().Set("Probabilities", fmt.Sprintf("%v ", probs))
  26. p.ServeHTTP(w, r)
  27. log.Println("Passing Request")
  28. default:
  29. log.Println("No Decision: PASS and LEARN")
  30. p.ModifyResponse = passAndLearn
  31. w.Header().Set("Probabilities", fmt.Sprintf("%v ", probs))
  32. p.ServeHTTP(w, r)
  33. }
  34. }
  35. }
  36. func quadrant(p map[string]float64) string {
  37. sure := math.Abs(p["BAD"]-p["GOOD"]) >= ProxyFlow.sensitivity
  38. badish := p["BAD"] > p["GOOD"]
  39. goodish := p["GOOD"] > p["BAD"]
  40. if ProxyFlow.seniority < Maturity {
  41. log.Println("Seniority too low. Waiting.")
  42. return "YOUNG"
  43. }
  44. if sure {
  45. if goodish {
  46. return "PASS"
  47. }
  48. if badish {
  49. return "BLOCK"
  50. }
  51. } else {
  52. if goodish {
  53. return "PASSLEARN"
  54. }
  55. if badish {
  56. return "BLOCKLEARN"
  57. }
  58. }
  59. return "MEH"
  60. }