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" "encoding/json"
"fmt" "fmt"
"io" "io"
"log"
"net/http" "net/http"
"net/url" "net/url"
"server_torii/internal/action" "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) resp, err := http.PostForm("https://api.hcaptcha.com/siteverify", data)
if err != nil { 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")) decision.SetResponse(action.Done, []byte("500"), []byte("bad"))
} }
defer func(Body io.ReadCloser) { defer func(Body io.ReadCloser) {
err := Body.Close() err := Body.Close()
if err != nil { if err != nil {
log.Printf("Error closing response body: %v", err) utils.LogError(reqData, "", fmt.Sprintf("Error closing response body: %v", err))
} }
}(resp.Body) }(resp.Body)
body, err := io.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { 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")) decision.SetResponse(action.Done, []byte("500"), []byte("bad"))
return return
} }
@ -84,7 +83,7 @@ func CheckCaptcha(r *http.Request, reqData dataType.UserRequest, ruleSet *config
var hCaptchaResp HCaptchaResponse var hCaptchaResp HCaptchaResponse
err = json.Unmarshal(body, &hCaptchaResp) err = json.Unmarshal(body, &hCaptchaResp)
if err != nil { 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")) decision.SetResponse(action.Done, []byte("500"), []byte("bad"))
return return
} }
@ -120,7 +119,7 @@ func verifyClearanceCookie(reqData dataType.UserRequest, ruleSet config.RuleSet)
timeNow := time.Now().Unix() timeNow := time.Now().Unix()
parsedTimestamp, err := strconv.ParseInt(timestamp, 10, 64) parsedTimestamp, err := strconv.ParseInt(timestamp, 10, 64)
if err != nil { if err != nil {
log.Printf("Error parsing timestamp: %v", err) utils.LogError(reqData, "", fmt.Sprintf("Error parsing timestamp: %v", err))
return false return false
} }
@ -157,7 +156,7 @@ func verifySessionIDCookie(reqData dataType.UserRequest, ruleSet config.RuleSet)
timeNow := time.Now().Unix() timeNow := time.Now().Unix()
parsedTimestamp, err := strconv.ParseInt(timestamp, 10, 64) parsedTimestamp, err := strconv.ParseInt(timestamp, 10, 64)
if err != nil { if err != nil {
log.Printf("Error parsing timestamp: %v", err) utils.LogError(reqData, "", fmt.Sprintf("Error parsing timestamp: %v", err))
return false return false
} }

View File

@ -1,10 +1,11 @@
package check package check
import ( import (
"log" "fmt"
"server_torii/internal/action" "server_torii/internal/action"
"server_torii/internal/config" "server_torii/internal/config"
"server_torii/internal/dataType" "server_torii/internal/dataType"
"server_torii/internal/utils"
) )
func HTTPFlood(reqData dataType.UserRequest, ruleSet *config.RuleSet, decision *action.Decision, sharedMem *dataType.SharedMemory) { 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 { for window, limit := range ruleSet.HTTPFloodRule.HTTPFloodSpeedLimit {
if sharedMem.HTTPFloodSpeedLimitCounter.Query(ipKey, window) > limit { 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")) decision.SetCode(action.Done, []byte("429"))
return return
} }
@ -24,7 +25,7 @@ func HTTPFlood(reqData dataType.UserRequest, ruleSet *config.RuleSet, decision *
for window, limit := range ruleSet.HTTPFloodRule.HTTPFloodSameURILimit { for window, limit := range ruleSet.HTTPFloodRule.HTTPFloodSameURILimit {
if sharedMem.HTTPFloodSameURILimitCounter.Query(uriKey, window) > limit { 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")) decision.SetCode(action.Done, []byte("429"))
return return
} }

View File

@ -5,6 +5,7 @@ import (
"server_torii/internal/action" "server_torii/internal/action"
"server_torii/internal/config" "server_torii/internal/config"
"server_torii/internal/dataType" "server_torii/internal/dataType"
"server_torii/internal/utils"
) )
func IPBlockList(reqData dataType.UserRequest, ruleSet *config.RuleSet, decision *action.Decision, sharedMem *dataType.SharedMemory) { 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 return
} }
if trie.Search(ip) { if trie.Search(ip) {
utils.LogInfo(reqData, "", "IPBlockList")
decision.SetCode(action.Done, []byte("403")) decision.SetCode(action.Done, []byte("403"))
} else { } else {
decision.Set(action.Continue) decision.Set(action.Continue)

View File

@ -4,12 +4,14 @@ import (
"server_torii/internal/action" "server_torii/internal/action"
"server_torii/internal/config" "server_torii/internal/config"
"server_torii/internal/dataType" "server_torii/internal/dataType"
"server_torii/internal/utils"
) )
func URLBlockList(reqData dataType.UserRequest, ruleSet *config.RuleSet, decision *action.Decision, sharedMem *dataType.SharedMemory) { func URLBlockList(reqData dataType.UserRequest, ruleSet *config.RuleSet, decision *action.Decision, sharedMem *dataType.SharedMemory) {
url := reqData.Uri url := reqData.Uri
list := ruleSet.URLBlockList list := ruleSet.URLBlockList
if list.Match(url) { if list.Match(url) {
utils.LogInfo(reqData, "", "URLBlockList")
decision.SetCode(action.Done, []byte("403")) decision.SetCode(action.Done, []byte("403"))
} else { } else {
decision.Set(action.Continue) decision.Set(action.Continue)

View File

@ -1,11 +1,13 @@
package check package check
import ( import (
"log" "errors"
"fmt"
"net" "net"
"server_torii/internal/action" "server_torii/internal/action"
"server_torii/internal/config" "server_torii/internal/config"
"server_torii/internal/dataType" "server_torii/internal/dataType"
"server_torii/internal/utils"
"strings" "strings"
) )
@ -33,9 +35,13 @@ func VerifyBot(reqData dataType.UserRequest, ruleSet *config.RuleSet, decision *
actualRDNS, err := net.LookupAddr(reqData.RemoteIP) actualRDNS, err := net.LookupAddr(reqData.RemoteIP)
if err != nil { if err != nil {
log.Printf("VerifyBot: LookupAddr failed for %s: %v", reqData.RemoteIP, err) var dnsErr *net.DNSError
decision.SetCode(action.Done, []byte("403")) //ignore the error if it is a not found error
return 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 { for _, rdns := range exptractRDNS {
@ -43,7 +49,7 @@ func VerifyBot(reqData dataType.UserRequest, ruleSet *config.RuleSet, decision *
if strings.Contains(actual, rdns) { if strings.Contains(actual, rdns) {
ips, err := net.LookupIP(actual) ips, err := net.LookupIP(actual)
if err != nil { 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")) decision.SetCode(action.Done, []byte("403"))
return 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")) decision.SetCode(action.Done, []byte("403"))
return return

View File

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

View File

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