Skip to content

Commit c3c17ea

Browse files
committed
add random timeout on deny
1 parent 2fd4fe2 commit c3c17ea

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

geoblock.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io"
88
"io/fs"
99
"log"
10+
"math/rand"
1011
"net"
1112
"net/http"
1213
"os"
@@ -55,6 +56,7 @@ type Config struct {
5556
HTTPStatusCodeDeniedRequest int `yaml:"httpStatusCodeDeniedRequest"`
5657
LogFilePath string `yaml:"logFilePath"`
5758
RedirectURLIfDenied string `yaml:"redirectUrlIfDenied"`
59+
DelayOnDenyMs int `yaml:"delayOnDenyMs"`
5860
}
5961

6062
type ipEntry struct {
@@ -94,6 +96,7 @@ type GeoBlock struct {
9496
logFile *os.File
9597
redirectURLIfDenied string
9698
name string
99+
delayOnDenyMs int
97100
}
98101

99102
// New created a new GeoBlock plugin.
@@ -178,6 +181,7 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h
178181
logFile: logFile,
179182
redirectURLIfDenied: config.RedirectURLIfDenied,
180183
name: name,
184+
delayOnDenyMs: config.DelayOnDenyMs,
181185
}, nil
182186
}
183187

@@ -202,7 +206,12 @@ func (a *GeoBlock) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
202206
rw.WriteHeader(http.StatusFound)
203207
return
204208
}
205-
209+
// Introduce a delay before responding (with +-50%)
210+
if a.delayOnDenyMs > 0 {
211+
randomFactor := 0.5 + rand.Float64() // between 0.5 and 1.5
212+
randomDelay := time.Duration(float64(a.delayOnDenyMs) * randomFactor)
213+
time.Sleep(time.Duration(randomDelay) * time.Millisecond)
214+
}
206215
rw.WriteHeader(a.httpStatusCodeDeniedRequest)
207216
return
208217
}

readme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ my-GeoBlock:
182182
unknownCountryApiResponse: "nil"
183183
blackListMode: false
184184
addCountryHeader: false
185+
delayOnDenyMs: 2000
185186
countries:
186187
- AF # Afghanistan
187188
- AL # Albania
@@ -526,3 +527,7 @@ Basically tells GeoBlock to only allow/deny a request based on the first IP addr
526527
### Define a custom log file `redirectUrlIfDenied`
527528

528529
Allows returning a HTTP 301 status code, which indicates that the requested resource has been moved. The URL which can be specified is used to redirect the client to. So instead of "blocking" the client, the client will be redirected to the configured URL.
530+
531+
### Define a custom delay on requests `delayOnDenyMs`
532+
533+
Add a +-50% random delay for deny requests. This is useful to limit spam from forbidden IPs.

0 commit comments

Comments
 (0)