From c4541b38d3646ac5b27a4ad353337def0184f3b1 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Sat, 26 Sep 2020 14:56:53 -0700 Subject: [PATCH] working POST --- main.go | 56 ++++++++++++++++++++++++-------------------------------- 1 file changed, 24 insertions(+), 32 deletions(-) diff --git a/main.go b/main.go index 941f6f8..52023f6 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "io/ioutil" "net/http" "strconv" + "strings" "sync" "time" ) @@ -21,10 +22,20 @@ var cookiesToIps sync.Map // map [ cookie string] Ip2LastSeen func main() { var listenPort = flag.Int("listenPort", 5999, "port to listen on for incoming HTTP connections") var proxyPort = flag.Int("proxyPort", 6000, "port to forward connections to that pass the filter") + var logLevel = flag.String("logLevel", "warn", "debug, info, warn, or err") flag.Parse() - log.SetLevel(log.LevelInfo) + if *logLevel == "debug" { + log.SetLevel(log.LevelDebug) + } else if *logLevel == "info" { + log.SetLevel(log.LevelInfo) + } else if *logLevel == "warn" { + log.SetLevel(log.LevelWarn) + } else if *logLevel == "err" { + log.SetLevel(log.LevelError) + } log.Infof("Starting ddosFilter on %v -> %v...\n", *listenPort, *proxyPort) + listen(*listenPort, *proxyPort) } @@ -40,7 +51,7 @@ func listen(listenPort, proxyPort int) { func filter(res http.ResponseWriter, req *http.Request, listenPort, proxyPort int) { ip := req.RemoteAddr - log.Infof("%v: Request %v %v\n", ip, req.Host, req.URL) + log.Debugf("%v: Request %v %v\n", ip, req.Host, req.URL) cookie, err := req.Cookie("i_like_gogits") if err != nil { pass(res, req, listenPort, proxyPort) @@ -79,7 +90,7 @@ func filter(res http.ResponseWriter, req *http.Request, listenPort, proxyPort in return } - log.Infof("different IP in the last %v minutes, 404ing\n", SameCookieTimeLimitMins) + log.Debugf("different IP in the last %v minutes, 404ing\n", SameCookieTimeLimitMins) res.WriteHeader(http.StatusNotFound) fmt.Fprint(res, "404 - suspected botnet") } @@ -103,36 +114,18 @@ func copyHeader(source http.Header, dest *http.Header){ } func pass(res http.ResponseWriter, req *http.Request, listenPort, proxyPort int) { - log.Infoln("Request pass to proxy") + log.Debugf("Request pass to proxy") - log.Infof("orig: %v\n", req.Host) - //req.Host = "http://" + strings.Replace(req.Host, strconv.Itoa(listenPort), strconv.Itoa(proxyPort), 1)i - req.Host = "git.danballard.com" + //log.Infof("orig: %v\n", req.Host) + req.Host = strings.Replace(req.Host, strconv.Itoa(listenPort), strconv.Itoa(proxyPort), 1) + //req.Host = "git.danballard.com" - req.URL.Host = "git.danballard.com" - req.URL.Scheme = "https" + req.URL.Host = req.Host //"git.danballard.com" + req.URL.Scheme = "http" + //log.Infof("req: %v\n", req) - if req.Method == "POST" { - body, _ := ioutil.ReadAll(req.Body) - fmt.Printf("POST Body: %v\n", string(body)); - } - - /* Works but loses header and cookies */ - /*rr, err := http.NewRequest(req.Method, req.Host + req.URL.Path, req.Body) - for _, cookie := range req.Cookies() { - rr.AddCookie(cookie) - log.Infof("copy cookie: %v\n", cookie.String()) - }*/ - - log.Infof("req: %v\n", req) - //log.Infof("rr: %v\n", rr) - /*if rr.Method == "POST" { - body, _ := ioutil.ReadAll(rr.Body) - fmt.Printf("POST Body: %v\n", string(body)) - }*/ - //log.Infof("rr body: %v\n", rr.Body.) var transport http.Transport resp, err := transport.RoundTrip(req) //rr) @@ -145,7 +138,6 @@ func pass(res http.ResponseWriter, req *http.Request, listenPort, proxyPort int) defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) - //log.Infof("read BODY: %v\n", string(body)) if err != nil { log.Error(err) return @@ -153,10 +145,10 @@ func pass(res http.ResponseWriter, req *http.Request, listenPort, proxyPort int) dH := res.Header() copyHeader(resp.Header, &dH) - dH.Add("Requested-Host", rr.Host) + dH.Add("Requested-Host", req.Host) res.WriteHeader(resp.StatusCode) - n, err := res.Write(body) - log.Infof("res.write n: %v err: %v\n", n, err) + /*n, err := */ res.Write(body) + //log.Infof("res.write n: %v err: %v\n", n, err) }