mirror of
https://github.com/aljazceru/njump.git
synced 2025-12-17 14:24:27 +01:00
Block aggressive bots
This commit is contained in:
20
agentblock.go
Normal file
20
agentblock.go
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func agentBlock(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
userAgent := r.UserAgent()
|
||||||
|
if strings.Contains(userAgent, "Amazonbot") {
|
||||||
|
// Drop the request by returning a 403 Forbidden response
|
||||||
|
http.Error(w, "Forbidden", http.StatusForbidden)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call the next handler in the chain
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
})
|
||||||
|
}
|
||||||
4
main.go
4
main.go
@@ -159,10 +159,12 @@ func main() {
|
|||||||
mux.HandleFunc("/{code}", renderEvent)
|
mux.HandleFunc("/{code}", renderEvent)
|
||||||
mux.HandleFunc("/{$}", renderHomepage)
|
mux.HandleFunc("/{$}", renderHomepage)
|
||||||
|
|
||||||
|
loggedMux := loggingMiddleware(mux)
|
||||||
|
agentMux := agentBlock(loggedMux)
|
||||||
|
|
||||||
corsHandler := cors.Default().Handler(
|
corsHandler := cors.Default().Handler(
|
||||||
http.HandlerFunc(
|
http.HandlerFunc(
|
||||||
ipblock(loggedMux), // Wrap loggedMux with IP blocking
|
ipblock(agentMux),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
go updateCloudflareRangesRoutine()
|
go updateCloudflareRangesRoutine()
|
||||||
|
|||||||
Reference in New Issue
Block a user