Skip to content

Commit 299f5ac

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

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-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
}

0 commit comments

Comments
 (0)