1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- package main
- import (
- "fmt"
- "log"
- "math"
- "net/http"
- "net/http/httputil"
- )
- func handler(p *httputil.ReverseProxy) func(http.ResponseWriter, *http.Request) {
- return func(w http.ResponseWriter, r *http.Request) {
- //put the request inside our structure
- ProxyFlow.request = r
- log.Println("Received HTTP Request")
- probs := ZClassifier.Posterior(formatRequest(r))
- log.Printf("Posterior Probabilities: %+v\n", probs)
- action := quadrant(probs)
- ControPlane.StatsTokens <- action
- switch action {
- case "BLOCK", "BLOCKLEARN":
- p.ModifyResponse = blockAndlearn
- w.Header().Set("Probabilities", fmt.Sprintf("%v ", probs))
- log.Println("Request Blocked")
- p.ServeHTTP(w, r)
- case "PASS", "PASSLEARN":
- p.ModifyResponse = passAndLearn
- w.Header().Set("Probabilities", fmt.Sprintf("%v ", probs))
- p.ServeHTTP(w, r)
- log.Println("Passing Request")
- default:
- log.Println("No Decision: PASS and LEARN")
- p.ModifyResponse = passAndLearn
- w.Header().Set("Probabilities", fmt.Sprintf("%v ", probs))
- p.ServeHTTP(w, r)
- }
- }
- }
- func quadrant(p map[string]float64) string {
- sure := math.Abs(p["BAD"]-p["GOOD"]) >= ProxyFlow.sensitivity
- badish := p["BAD"] > p["GOOD"]
- goodish := p["GOOD"] > p["BAD"]
- if ProxyFlow.seniority < Maturity {
- log.Println("Seniority too low. Waiting.")
- return "YOUNG"
- }
- if sure {
- if goodish {
- return "PASS"
- }
- if badish {
- return "BLOCK"
- }
- } else {
- if goodish {
- return "PASSLEARN"
- }
- if badish {
- return "BLOCKLEARN"
- }
- }
- return "MEH"
- }
|