handler.go 1.5 KB

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