mirror of
https://github.com/Rayzggz/server_torii.git
synced 2025-06-16 04:01:22 +08:00
feat: log error
This commit is contained in:
@ -2,6 +2,7 @@ port: "25555"
|
||||
web_path: "/torii"
|
||||
rule_path: "/www/dev/server_torii/config/rules"
|
||||
error_page: "/www/dev/server_torii/config/error_page"
|
||||
log_path: "/www/dev/server_torii/log/access.log"
|
||||
node_name: "Server Torii"
|
||||
connecting_host_headers:
|
||||
- "Torii-Real-Host"
|
||||
|
@ -6,6 +6,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"server_torii/internal/action"
|
||||
@ -57,12 +58,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)
|
||||
decision.SetResponse(action.Done, []byte("500"), []byte("bad"))
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
defer func(Body io.ReadCloser) {
|
||||
err := Body.Close()
|
||||
if err != nil {
|
||||
log.Printf("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)
|
||||
decision.SetResponse(action.Done, []byte("500"), []byte("bad"))
|
||||
return
|
||||
}
|
||||
@ -70,6 +78,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)
|
||||
decision.SetResponse(action.Done, []byte("500"), []byte("bad"))
|
||||
return
|
||||
}
|
||||
@ -105,6 +114,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)
|
||||
return false
|
||||
}
|
||||
|
||||
|
@ -16,6 +16,7 @@ type MainConfig struct {
|
||||
WebPath string `yaml:"web_path"`
|
||||
RulePath string `yaml:"rule_path"`
|
||||
ErrorPage string `yaml:"error_page"`
|
||||
LogPath string `yaml:"log_path"`
|
||||
NodeName string `yaml:"node_name"`
|
||||
ConnectingHostHeaders []string `yaml:"connecting_host_headers"`
|
||||
ConnectingIPHeaders []string `yaml:"connecting_ip_headers"`
|
||||
|
@ -3,6 +3,7 @@ package server
|
||||
import (
|
||||
"bytes"
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"server_torii/internal/action"
|
||||
"server_torii/internal/check"
|
||||
@ -32,10 +33,15 @@ func CheckMain(w http.ResponseWriter, userRequestData dataType.UserRequest, rule
|
||||
|
||||
if bytes.Compare(decision.HTTPCode, []byte("200")) == 0 {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("OK"))
|
||||
_, err := w.Write([]byte("OK"))
|
||||
if err != nil {
|
||||
log.Printf("Error write response: %v", err)
|
||||
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)
|
||||
http.Error(w, "500 - Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@ -52,6 +58,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)
|
||||
http.Error(w, "500 - Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@ -59,18 +66,21 @@ 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)
|
||||
http.Error(w, "500 - Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
if err = tpl.Execute(w, nil); 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)
|
||||
http.Error(w, "500 - Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
@ -94,7 +94,6 @@ func getClientIP(cfg *config.MainConfig, r *http.Request) string {
|
||||
remoteAddr := r.RemoteAddr
|
||||
ipStr, _, err := net.SplitHostPort(remoteAddr)
|
||||
if err != nil {
|
||||
//TODO: log error
|
||||
clientIP = remoteAddr
|
||||
} else {
|
||||
clientIP = ipStr
|
||||
|
@ -3,6 +3,7 @@ package server
|
||||
import (
|
||||
"bytes"
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"server_torii/internal/action"
|
||||
"server_torii/internal/check"
|
||||
@ -21,16 +22,28 @@ func CheckTorii(w http.ResponseWriter, r *http.Request, reqData dataType.UserReq
|
||||
if bytes.Compare(decision.HTTPCode, []byte("200")) == 0 {
|
||||
if bytes.Compare(decision.ResponseData, []byte("bad")) == 0 {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("bad"))
|
||||
_, err := w.Write([]byte("bad"))
|
||||
if err != nil {
|
||||
log.Printf("Error writing response: %v", err)
|
||||
return
|
||||
}
|
||||
return
|
||||
} else if bytes.Compare(decision.ResponseData, []byte("good")) == 0 {
|
||||
w.Header().Set("Set-Cookie", "__torii_clearance="+string(check.GenClearance(reqData, *ruleSet))+"; Path=/; HttpOnly")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(decision.ResponseData)
|
||||
_, err := w.Write(decision.ResponseData)
|
||||
if err != nil {
|
||||
log.Printf("Error writing response: %v", err)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
//should not be here
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte("500 - Internal Server Error"))
|
||||
_, err := w.Write([]byte("500 - Internal Server Error"))
|
||||
if err != nil {
|
||||
log.Printf("Error writing response: %v", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
} else {
|
||||
tpl, err := template.ParseFiles(cfg.ErrorPage + "/403.html")
|
||||
|
0
log/access.log
Normal file
0
log/access.log
Normal file
13
main.go
13
main.go
@ -29,6 +29,19 @@ func main() {
|
||||
|
||||
log.Printf("Ready to start server on port %s", cfg.Port)
|
||||
|
||||
//set log file
|
||||
logFile, err := os.OpenFile(cfg.LogPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to open log file: %v", err)
|
||||
}
|
||||
defer func(logFile *os.File) {
|
||||
err := logFile.Close()
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to close log file: %v", err)
|
||||
}
|
||||
}(logFile)
|
||||
log.SetOutput(logFile)
|
||||
|
||||
// Start server
|
||||
stop := make(chan os.Signal, 1)
|
||||
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
|
||||
|
Reference in New Issue
Block a user