From 11590d2d5dabcd01315191fe9de77466932a9a1f Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Sun, 27 Sep 2020 13:17:57 -0700 Subject: [PATCH] add counter and logging for debuging tweaks --- counter.go | 33 +++++++++++++++++++++++++++++++++ main.go | 21 +++++++++++++++------ 2 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 counter.go diff --git a/counter.go b/counter.go new file mode 100644 index 0000000..7e362e1 --- /dev/null +++ b/counter.go @@ -0,0 +1,33 @@ +package main + +import ( + "strconv" + "sync" +) + +type Counter struct { + count int + lock sync.Mutex +} + +func NewCounter() *Counter { + return &Counter{count: 0} +} + +func (c *Counter) Add() { + c.lock.Lock() + defer c.lock.Unlock() + c.count += 1 +} + +func (c *Counter) Sub() { + c.lock.Lock() + defer c.lock.Unlock() + c.count -= 1 +} + +func (c *Counter) String() string { + c.lock.Lock() + defer c.lock.Unlock() + return strconv.Itoa(c.count) +} \ No newline at end of file diff --git a/main.go b/main.go index dd8fa5a..c9c1195 100644 --- a/main.go +++ b/main.go @@ -18,6 +18,7 @@ const SameCookieTimeLimitMins = 10 type Ip2LastSeen map[string]time.Time var cookiesToIps sync.Map // map [ cookie string] Ip2LastSeen +var counter *Counter func main() { var listenPort = flag.Int("listenPort", 5999, "port to listen on for incoming HTTP connections") @@ -35,12 +36,20 @@ func main() { } log.Infof("Starting ddosFilter on %v -> %v...\n", *listenPort, *proxyPort) - + counter = NewCounter() + go logger() listen(*listenPort, *proxyPort) } +func logger() { + for { + time.Sleep(5*time.Second) + log.Infof("Thread count: %v\n", counter.String()) + } +} + func listen(listenPort, proxyPort int) { - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { filter(w, r, listenPort, proxyPort)}) + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { counter.Add(); filter(w, r, listenPort, proxyPort); counter.Sub()}) addr := "127.0.0.1:" + strconv.Itoa(listenPort) err := http.ListenAndServe(addr, nil) if err != nil { @@ -51,8 +60,9 @@ func listen(listenPort, proxyPort int) { func filter(res http.ResponseWriter, req *http.Request, listenPort, proxyPort int) { ip := req.RemoteAddr - log.Debugf("%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") + log.Debugf("ip: %v cookie: %v\n", ip, cookie) if err != nil { pass(res, req, listenPort, proxyPort) return @@ -114,7 +124,7 @@ func copyHeader(source http.Header, dest *http.Header){ } func pass(res http.ResponseWriter, req *http.Request, listenPort, proxyPort int) { - log.Debugf("Request pass to proxy") + //log.Debugf("Request pass to proxy") //log.Infof("orig: %v\n", req.Host) @@ -127,8 +137,7 @@ func pass(res http.ResponseWriter, req *http.Request, listenPort, proxyPort int) //log.Infof("req: %v\n", req) var transport http.Transport - resp, err := transport.RoundTrip(req) //rr) - + resp, err := transport.RoundTrip(req) if err != nil { log.Errorf("Error fetching: %v\n", err.Error())