Handler: Make handler configurable
This commit is contained in:
parent
6a94b58990
commit
b5b634b579
18
heff/http.go
18
heff/http.go
|
@ -7,26 +7,30 @@ import (
|
|||
"sync"
|
||||
)
|
||||
|
||||
var pool sync.Pool
|
||||
var DefaultHoneypot = NewHoneypot(DefaultMarkovMap, 100*1<<10)
|
||||
|
||||
func getBuffer() []byte {
|
||||
func NewHoneypot(mm MarkovMap, buffsize int) http.HandlerFunc {
|
||||
var pool sync.Pool
|
||||
|
||||
getBuffer := func() []byte {
|
||||
x := pool.Get()
|
||||
if buf, ok := x.([]byte); ok {
|
||||
return buf
|
||||
} else {
|
||||
return make([]byte, 100*1<<10)
|
||||
return make([]byte, buffsize)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func putBuffer(buf []byte) {
|
||||
putBuffer := func(buf []byte) {
|
||||
pool.Put(buf)
|
||||
}
|
||||
}
|
||||
|
||||
func Honeypot(w http.ResponseWriter, r *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
log.Printf("Started writing: %v", r.URL)
|
||||
buf := getBuffer()
|
||||
defer putBuffer(buf)
|
||||
io.WriteString(w, "<HTML>\n<BODY>\n")
|
||||
n, err := io.CopyBuffer(w, DefaultMarkovMap, buf)
|
||||
log.Printf("Wrote: %d (%v)", n, err)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -37,7 +37,7 @@ func main() {
|
|||
path = "/"
|
||||
}
|
||||
|
||||
http.HandleFunc(path, heff.Honeypot)
|
||||
http.HandleFunc(path, heff.DefaultHoneypot)
|
||||
|
||||
log.Fatal(http.ListenAndServe(addr, nil))
|
||||
|
||||
|
|
Loading…
Reference in New Issue