mirror of
https://github.com/Rayzggz/server_torii.git
synced 2025-06-17 04:31:22 +08:00
35 lines
1.2 KiB
Go
35 lines
1.2 KiB
Go
package check
|
|
|
|
import (
|
|
"fmt"
|
|
"server_torii/internal/action"
|
|
"server_torii/internal/config"
|
|
"server_torii/internal/dataType"
|
|
"server_torii/internal/utils"
|
|
)
|
|
|
|
func HTTPFlood(reqData dataType.UserRequest, ruleSet *config.RuleSet, decision *action.Decision, sharedMem *dataType.SharedMemory) {
|
|
ipKey := reqData.RemoteIP
|
|
sharedMem.HTTPFloodSpeedLimitCounter.Add(ipKey, 1)
|
|
|
|
uriKey := reqData.RemoteIP + "|" + reqData.Uri
|
|
sharedMem.HTTPFloodSameURILimitCounter.Add(uriKey, 1)
|
|
|
|
for window, limit := range ruleSet.HTTPFloodRule.HTTPFloodSpeedLimit {
|
|
if sharedMem.HTTPFloodSpeedLimitCounter.Query(ipKey, window) > limit {
|
|
utils.LogInfo(reqData, "", fmt.Sprintf("HTTPFlood rate limit exceeded: IP %s window %d limit %d", ipKey, window, limit))
|
|
decision.SetCode(action.Done, []byte("429"))
|
|
return
|
|
}
|
|
}
|
|
|
|
for window, limit := range ruleSet.HTTPFloodRule.HTTPFloodSameURILimit {
|
|
if sharedMem.HTTPFloodSameURILimitCounter.Query(uriKey, window) > limit {
|
|
utils.LogInfo(reqData, "", fmt.Sprintf("HTTPFlood URI rate limit exceeded: IP %s URI %s window %d limit %d", ipKey, reqData.Uri, window, limit))
|
|
decision.SetCode(action.Done, []byte("429"))
|
|
return
|
|
}
|
|
}
|
|
decision.Set(action.Continue)
|
|
}
|