working POST

This commit is contained in:
Dan Ballard 2020-09-26 14:56:53 -07:00
parent 7149d63496
commit c4541b38d3
1 changed files with 24 additions and 32 deletions

56
main.go
View File

@ -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)
}