Block aggressive bots

This commit is contained in:
dtonon
2024-09-26 07:54:55 +02:00
parent 743e0057e3
commit acb6039d33
2 changed files with 23 additions and 1 deletions

20
agentblock.go Normal file
View 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)
})
}

View File

@@ -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()