feat: Block HTTP FLOOD

This commit is contained in:
Roi Feng
2025-04-24 21:02:19 -04:00
parent 5fe6db509d
commit 1a977b2bce
2 changed files with 26 additions and 4 deletions

View File

@ -17,8 +17,7 @@ func HTTPFlood(reqData dataType.UserRequest, ruleSet *config.RuleSet, decision *
for window, limit := range ruleSet.HTTPFloodRule.HTTPFloodSpeedLimit {
if sharedMem.HTTPFloodSpeedLimitCounter.Query(ipKey, window) > limit {
log.Printf("HTTPFlood rate limit exceeded: IP %s, window %d, limit %d", ipKey, window, limit)
//decision.SetResponse(action.Done, []byte("403"), nil)
decision.Set(action.Continue)
decision.SetCode(action.Done, []byte("429"))
return
}
}
@ -26,8 +25,7 @@ func HTTPFlood(reqData dataType.UserRequest, ruleSet *config.RuleSet, decision *
for window, limit := range ruleSet.HTTPFloodRule.HTTPFloodSameURILimit {
if sharedMem.HTTPFloodSameURILimitCounter.Query(uriKey, window) > limit {
log.Printf("HTTPFlood URI rate limit exceeded: IP %s, URI %s, window %d, limit %d", ipKey, reqData.Uri, window, limit)
//decision.SetResponse(action.Done, []byte("403"), nil)
decision.Set(action.Continue)
decision.SetCode(action.Done, []byte("429"))
return
}
}

View File

@ -81,6 +81,30 @@ func CheckMain(w http.ResponseWriter, userRequestData dataType.UserRequest, rule
return
}
} else if bytes.Compare(decision.HTTPCode, []byte("429")) == 0 {
tpl, err := template.ParseFiles(cfg.ErrorPage + "/429.html")
if err != nil {
log.Printf("Error template: %v", err)
http.Error(w, "500 - Internal Server Error", http.StatusInternalServerError)
return
}
data := struct {
EdgeTag string
ConnectIP string
Date string
}{
EdgeTag: cfg.NodeName,
ConnectIP: userRequestData.RemoteIP,
Date: time.Now().Format("2006-01-02 15:04:05"),
}
w.WriteHeader(http.StatusTooManyRequests)
w.Header().Set("Content-Type", "text/html; charset=utf-8")
if err = tpl.Execute(w, data); err != nil {
log.Printf("Error template: %v", err)
http.Error(w, "500 - Internal Server Error", http.StatusInternalServerError)
return
}
} else {
//should never happen
log.Printf("Error access in wrong state: %v", decision)