add counter and logging for debuging tweaks
This commit is contained in:
parent
f936a4cae9
commit
11590d2d5d
|
@ -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)
|
||||||
|
}
|
21
main.go
21
main.go
|
@ -18,6 +18,7 @@ const SameCookieTimeLimitMins = 10
|
||||||
type Ip2LastSeen map[string]time.Time
|
type Ip2LastSeen map[string]time.Time
|
||||||
|
|
||||||
var cookiesToIps sync.Map // map [ cookie string] Ip2LastSeen
|
var cookiesToIps sync.Map // map [ cookie string] Ip2LastSeen
|
||||||
|
var counter *Counter
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var listenPort = flag.Int("listenPort", 5999, "port to listen on for incoming HTTP connections")
|
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)
|
log.Infof("Starting ddosFilter on %v -> %v...\n", *listenPort, *proxyPort)
|
||||||
|
|
||||||
|
counter = NewCounter()
|
||||||
|
go logger()
|
||||||
listen(*listenPort, *proxyPort)
|
listen(*listenPort, *proxyPort)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func logger() {
|
||||||
|
for {
|
||||||
|
time.Sleep(5*time.Second)
|
||||||
|
log.Infof("Thread count: %v\n", counter.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func listen(listenPort, proxyPort int) {
|
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)
|
addr := "127.0.0.1:" + strconv.Itoa(listenPort)
|
||||||
err := http.ListenAndServe(addr, nil)
|
err := http.ListenAndServe(addr, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -51,8 +60,9 @@ func listen(listenPort, proxyPort int) {
|
||||||
|
|
||||||
func filter(res http.ResponseWriter, req *http.Request, listenPort, proxyPort int) {
|
func filter(res http.ResponseWriter, req *http.Request, listenPort, proxyPort int) {
|
||||||
ip := req.RemoteAddr
|
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")
|
cookie, err := req.Cookie("i_like_gogits")
|
||||||
|
log.Debugf("ip: %v cookie: %v\n", ip, cookie)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
pass(res, req, listenPort, proxyPort)
|
pass(res, req, listenPort, proxyPort)
|
||||||
return
|
return
|
||||||
|
@ -114,7 +124,7 @@ func copyHeader(source http.Header, dest *http.Header){
|
||||||
}
|
}
|
||||||
|
|
||||||
func pass(res http.ResponseWriter, req *http.Request, listenPort, proxyPort int) {
|
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)
|
//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)
|
//log.Infof("req: %v\n", req)
|
||||||
|
|
||||||
var transport http.Transport
|
var transport http.Transport
|
||||||
resp, err := transport.RoundTrip(req) //rr)
|
resp, err := transport.RoundTrip(req)
|
||||||
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Error fetching: %v\n", err.Error())
|
log.Errorf("Error fetching: %v\n", err.Error())
|
||||||
|
|
Loading…
Reference in New Issue