mirror of
https://github.com/Rayzggz/server_torii.git
synced 2025-06-23 15:31:31 +08:00
feat: Use Logx to log
This commit is contained in:
@ -6,7 +6,6 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"server_torii/internal/action"
|
||||
@ -64,19 +63,19 @@ func CheckCaptcha(r *http.Request, reqData dataType.UserRequest, ruleSet *config
|
||||
|
||||
resp, err := http.PostForm("https://api.hcaptcha.com/siteverify", data)
|
||||
if err != nil {
|
||||
log.Printf("Error sending request to hCaptcha: %v", err)
|
||||
utils.LogError(reqData, "", fmt.Sprintf("Error sending request to hCaptcha: %v", err))
|
||||
decision.SetResponse(action.Done, []byte("500"), []byte("bad"))
|
||||
}
|
||||
defer func(Body io.ReadCloser) {
|
||||
err := Body.Close()
|
||||
if err != nil {
|
||||
log.Printf("Error closing response body: %v", err)
|
||||
utils.LogError(reqData, "", fmt.Sprintf("Error closing response body: %v", err))
|
||||
}
|
||||
}(resp.Body)
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Printf("Error reading response from hCaptcha: %v", err)
|
||||
utils.LogError(reqData, "", fmt.Sprintf("Error reading response from hCaptcha: %v", err))
|
||||
decision.SetResponse(action.Done, []byte("500"), []byte("bad"))
|
||||
return
|
||||
}
|
||||
@ -84,7 +83,7 @@ func CheckCaptcha(r *http.Request, reqData dataType.UserRequest, ruleSet *config
|
||||
var hCaptchaResp HCaptchaResponse
|
||||
err = json.Unmarshal(body, &hCaptchaResp)
|
||||
if err != nil {
|
||||
log.Printf("Error parsing response from hCaptcha: %v", err)
|
||||
utils.LogError(reqData, "", fmt.Sprintf("Error parsing response from hCaptcha: %v", err))
|
||||
decision.SetResponse(action.Done, []byte("500"), []byte("bad"))
|
||||
return
|
||||
}
|
||||
@ -120,7 +119,7 @@ func verifyClearanceCookie(reqData dataType.UserRequest, ruleSet config.RuleSet)
|
||||
timeNow := time.Now().Unix()
|
||||
parsedTimestamp, err := strconv.ParseInt(timestamp, 10, 64)
|
||||
if err != nil {
|
||||
log.Printf("Error parsing timestamp: %v", err)
|
||||
utils.LogError(reqData, "", fmt.Sprintf("Error parsing timestamp: %v", err))
|
||||
return false
|
||||
}
|
||||
|
||||
@ -157,7 +156,7 @@ func verifySessionIDCookie(reqData dataType.UserRequest, ruleSet config.RuleSet)
|
||||
timeNow := time.Now().Unix()
|
||||
parsedTimestamp, err := strconv.ParseInt(timestamp, 10, 64)
|
||||
if err != nil {
|
||||
log.Printf("Error parsing timestamp: %v", err)
|
||||
utils.LogError(reqData, "", fmt.Sprintf("Error parsing timestamp: %v", err))
|
||||
return false
|
||||
}
|
||||
|
||||
|
@ -1,10 +1,11 @@
|
||||
package check
|
||||
|
||||
import (
|
||||
"log"
|
||||
"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) {
|
||||
@ -16,7 +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)
|
||||
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
|
||||
}
|
||||
@ -24,7 +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)
|
||||
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
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"server_torii/internal/action"
|
||||
"server_torii/internal/config"
|
||||
"server_torii/internal/dataType"
|
||||
"server_torii/internal/utils"
|
||||
)
|
||||
|
||||
func IPBlockList(reqData dataType.UserRequest, ruleSet *config.RuleSet, decision *action.Decision, sharedMem *dataType.SharedMemory) {
|
||||
@ -15,6 +16,7 @@ func IPBlockList(reqData dataType.UserRequest, ruleSet *config.RuleSet, decision
|
||||
return
|
||||
}
|
||||
if trie.Search(ip) {
|
||||
utils.LogInfo(reqData, "", "IPBlockList")
|
||||
decision.SetCode(action.Done, []byte("403"))
|
||||
} else {
|
||||
decision.Set(action.Continue)
|
||||
|
@ -4,12 +4,14 @@ import (
|
||||
"server_torii/internal/action"
|
||||
"server_torii/internal/config"
|
||||
"server_torii/internal/dataType"
|
||||
"server_torii/internal/utils"
|
||||
)
|
||||
|
||||
func URLBlockList(reqData dataType.UserRequest, ruleSet *config.RuleSet, decision *action.Decision, sharedMem *dataType.SharedMemory) {
|
||||
url := reqData.Uri
|
||||
list := ruleSet.URLBlockList
|
||||
if list.Match(url) {
|
||||
utils.LogInfo(reqData, "", "URLBlockList")
|
||||
decision.SetCode(action.Done, []byte("403"))
|
||||
} else {
|
||||
decision.Set(action.Continue)
|
||||
|
@ -1,11 +1,13 @@
|
||||
package check
|
||||
|
||||
import (
|
||||
"log"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"server_torii/internal/action"
|
||||
"server_torii/internal/config"
|
||||
"server_torii/internal/dataType"
|
||||
"server_torii/internal/utils"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@ -33,9 +35,13 @@ func VerifyBot(reqData dataType.UserRequest, ruleSet *config.RuleSet, decision *
|
||||
|
||||
actualRDNS, err := net.LookupAddr(reqData.RemoteIP)
|
||||
if err != nil {
|
||||
log.Printf("VerifyBot: LookupAddr failed for %s: %v", reqData.RemoteIP, err)
|
||||
decision.SetCode(action.Done, []byte("403"))
|
||||
return
|
||||
var dnsErr *net.DNSError
|
||||
//ignore the error if it is a not found error
|
||||
if !(errors.As(err, &dnsErr) && dnsErr.IsNotFound) {
|
||||
utils.LogInfo(reqData, "", fmt.Sprintf("VerifyBot: lookupAddr failed: %v", err))
|
||||
decision.SetCode(action.Done, []byte("403"))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
for _, rdns := range exptractRDNS {
|
||||
@ -43,7 +49,7 @@ func VerifyBot(reqData dataType.UserRequest, ruleSet *config.RuleSet, decision *
|
||||
if strings.Contains(actual, rdns) {
|
||||
ips, err := net.LookupIP(actual)
|
||||
if err != nil {
|
||||
log.Printf("VerifyBot: LookupIP failed for %s: %v", actual, err)
|
||||
utils.LogInfo(reqData, "", fmt.Sprintf("VerifyBot: LookupIP failed: %v", err))
|
||||
decision.SetCode(action.Done, []byte("403"))
|
||||
return
|
||||
}
|
||||
@ -56,7 +62,7 @@ func VerifyBot(reqData dataType.UserRequest, ruleSet *config.RuleSet, decision *
|
||||
}
|
||||
}
|
||||
}
|
||||
log.Printf("VerifyBot: IP lookup failed for %s: %v", reqData.RemoteIP, err)
|
||||
utils.LogInfo(reqData, "", fmt.Sprintf("VerifyBot: LookupAddr failed: %v", err))
|
||||
decision.SetCode(action.Done, []byte("403"))
|
||||
return
|
||||
|
||||
|
Reference in New Issue
Block a user