https://github.com/khezen/tarpit
http tarpit middleware
https://github.com/khezen/tarpit
delay go golang http-middleware tarpit
Last synced: 10 months ago
JSON representation
http tarpit middleware
- Host: GitHub
- URL: https://github.com/khezen/tarpit
- Owner: khezen
- License: mit
- Created: 2018-06-25T20:41:59.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-01-01T09:50:45.000Z (about 7 years ago)
- Last Synced: 2025-04-22T19:13:29.199Z (10 months ago)
- Topics: delay, go, golang, http-middleware, tarpit
- Language: Go
- Homepage:
- Size: 28.3 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# tarpit
[](https://godoc.org/github.com/khezen/tarpit)
* simple HTTP middleware that purposely delays incoming request
* repeted requests to a given resource increase the delay
* enable TCP keep alive to keep the client from timing out
One typical use case is to protect authentication from brute force.
## example
The following example applies tarpit based on IP address. It is possible to apply tarpit based on any data provided in the request.
```golang
package main
import (
"net/http"
"github.com/khezen/tarpit"
)
var tarpitMiddleware = tarpit.New(tarpit.DefaultFreeReqCount, tarpit.DefaultDelay, tarpit.DefaultResetPeriod)
func handleGetMedicine(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet{
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
ipAddr := r.Header.Get("X-Forwarded-For")
err := tarpitMiddleware.Tar(ipAddr, w, r)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.Write([]byte("Here is your medicine"))
}
func main() {
http.HandleFunc("/drugs-store/v1/medicine", handleGetMedicine)
writeTimeout := 30*time.Second
err := tarpit.ListenAndServe(":80", nil, writeTimeout)
if err != nil {
panic(err)
}
}
```