feat: Use Logx to log

This commit is contained in:
Roi Feng
2025-04-28 23:33:53 -04:00
parent 705230dd0d
commit e73a786311
7 changed files with 41 additions and 30 deletions

View File

@ -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
}

View File

@ -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
}

View File

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

View File

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

View File

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

View File

@ -2,13 +2,14 @@ package server
import (
"bytes"
"fmt"
"html/template"
"log"
"net/http"
"server_torii/internal/action"
"server_torii/internal/check"
"server_torii/internal/config"
"server_torii/internal/dataType"
"server_torii/internal/utils"
"time"
)
@ -37,13 +38,13 @@ func CheckMain(w http.ResponseWriter, userRequestData dataType.UserRequest, rule
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte("OK"))
if err != nil {
log.Printf("Error write response: %v", err)
utils.LogError(userRequestData, fmt.Sprintf("Error writing response: %v", err), "CheckMain")
return
}
} else if bytes.Compare(decision.HTTPCode, []byte("403")) == 0 {
tpl, err := template.ParseFiles(cfg.ErrorPage + "/403.html")
if err != nil {
log.Printf("Error template: %v", err)
utils.LogError(userRequestData, fmt.Sprintf("Error parsing template: %v", err), "CheckMain")
http.Error(w, "500 - Internal Server Error", http.StatusInternalServerError)
return
}
@ -60,7 +61,7 @@ func CheckMain(w http.ResponseWriter, userRequestData dataType.UserRequest, rule
w.WriteHeader(http.StatusForbidden)
w.Header().Set("Content-Type", "text/html; charset=utf-8")
if err = tpl.Execute(w, data); err != nil {
log.Printf("Error template: %v", err)
utils.LogError(userRequestData, fmt.Sprintf("Error executing template: %v", err), "CheckMain")
http.Error(w, "500 - Internal Server Error", http.StatusInternalServerError)
return
}
@ -68,7 +69,7 @@ func CheckMain(w http.ResponseWriter, userRequestData dataType.UserRequest, rule
} else if bytes.Compare(decision.HTTPCode, []byte("CAPTCHA")) == 0 {
tpl, err := template.ParseFiles(cfg.ErrorPage + "/CAPTCHA.html")
if err != nil {
log.Printf("Error template: %v", err)
utils.LogError(userRequestData, fmt.Sprintf("Error parsing template: %v", err), "CheckMain")
http.Error(w, "500 - Internal Server Error", http.StatusInternalServerError)
return
}
@ -76,7 +77,7 @@ func CheckMain(w http.ResponseWriter, userRequestData dataType.UserRequest, rule
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusServiceUnavailable)
if err = tpl.Execute(w, nil); err != nil {
log.Printf("Error template: %v", err)
utils.LogError(userRequestData, fmt.Sprintf("Error executing template: %v", err), "CheckMain")
http.Error(w, "500 - Internal Server Error", http.StatusInternalServerError)
return
}
@ -84,7 +85,7 @@ func CheckMain(w http.ResponseWriter, userRequestData dataType.UserRequest, rule
} 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)
utils.LogError(userRequestData, fmt.Sprintf("Error parsing template: %v", err), "CheckMain")
http.Error(w, "500 - Internal Server Error", http.StatusInternalServerError)
return
}
@ -100,14 +101,14 @@ func CheckMain(w http.ResponseWriter, userRequestData dataType.UserRequest, rule
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)
utils.LogError(userRequestData, fmt.Sprintf("Error executing template: %v", err), "CheckMain")
http.Error(w, "500 - Internal Server Error", http.StatusInternalServerError)
return
}
} else {
//should never happen
log.Printf("Error access in wrong state: %v", decision)
utils.LogError(userRequestData, fmt.Sprintf("Error access in wrong state: %v", decision), "CheckMain")
http.Error(w, "500 - Internal Server Error", http.StatusInternalServerError)
return
}

View File

@ -3,12 +3,12 @@ package server
import (
"bytes"
"html/template"
"log"
"net/http"
"server_torii/internal/action"
"server_torii/internal/check"
"server_torii/internal/config"
"server_torii/internal/dataType"
"server_torii/internal/utils"
"time"
)
@ -24,7 +24,7 @@ func CheckTorii(w http.ResponseWriter, r *http.Request, reqData dataType.UserReq
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte("bad"))
if err != nil {
log.Printf("Error writing response: %v", err)
utils.LogError(reqData, "Error writing response: "+err.Error(), "CheckTorii")
return
}
return
@ -32,7 +32,7 @@ func CheckTorii(w http.ResponseWriter, r *http.Request, reqData dataType.UserReq
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte("badSession"))
if err != nil {
log.Printf("Error writing response: %v", err)
utils.LogError(reqData, "Error writing response: "+err.Error(), "CheckTorii")
return
}
return
@ -41,7 +41,7 @@ func CheckTorii(w http.ResponseWriter, r *http.Request, reqData dataType.UserReq
w.WriteHeader(http.StatusOK)
_, err := w.Write(decision.ResponseData)
if err != nil {
log.Printf("Error writing response: %v", err)
utils.LogError(reqData, "Error writing response: "+err.Error(), "CheckTorii")
return
}
} else {
@ -49,7 +49,7 @@ func CheckTorii(w http.ResponseWriter, r *http.Request, reqData dataType.UserReq
w.WriteHeader(http.StatusInternalServerError)
_, err := w.Write([]byte("500 - Internal Server Error"))
if err != nil {
log.Printf("Error writing response: %v", err)
utils.LogError(reqData, "Error writing response: "+err.Error(), "CheckTorii")
return
}
}