mirror of
https://github.com/Rayzggz/server_torii.git
synced 2025-06-16 20:21:22 +08:00
feat: log error
This commit is contained in:
@ -2,6 +2,7 @@ port: "25555"
|
|||||||
web_path: "/torii"
|
web_path: "/torii"
|
||||||
rule_path: "/www/dev/server_torii/config/rules"
|
rule_path: "/www/dev/server_torii/config/rules"
|
||||||
error_page: "/www/dev/server_torii/config/error_page"
|
error_page: "/www/dev/server_torii/config/error_page"
|
||||||
|
log_path: "/www/dev/server_torii/log/access.log"
|
||||||
node_name: "Server Torii"
|
node_name: "Server Torii"
|
||||||
connecting_host_headers:
|
connecting_host_headers:
|
||||||
- "Torii-Real-Host"
|
- "Torii-Real-Host"
|
||||||
|
@ -6,6 +6,7 @@ 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"
|
||||||
@ -57,12 +58,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)
|
||||||
decision.SetResponse(action.Done, []byte("500"), []byte("bad"))
|
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)
|
body, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Printf("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
|
||||||
}
|
}
|
||||||
@ -70,6 +78,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)
|
||||||
decision.SetResponse(action.Done, []byte("500"), []byte("bad"))
|
decision.SetResponse(action.Done, []byte("500"), []byte("bad"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -105,6 +114,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)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,6 +16,7 @@ type MainConfig struct {
|
|||||||
WebPath string `yaml:"web_path"`
|
WebPath string `yaml:"web_path"`
|
||||||
RulePath string `yaml:"rule_path"`
|
RulePath string `yaml:"rule_path"`
|
||||||
ErrorPage string `yaml:"error_page"`
|
ErrorPage string `yaml:"error_page"`
|
||||||
|
LogPath string `yaml:"log_path"`
|
||||||
NodeName string `yaml:"node_name"`
|
NodeName string `yaml:"node_name"`
|
||||||
ConnectingHostHeaders []string `yaml:"connecting_host_headers"`
|
ConnectingHostHeaders []string `yaml:"connecting_host_headers"`
|
||||||
ConnectingIPHeaders []string `yaml:"connecting_ip_headers"`
|
ConnectingIPHeaders []string `yaml:"connecting_ip_headers"`
|
||||||
|
@ -3,6 +3,7 @@ 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"
|
||||||
@ -32,10 +33,15 @@ func CheckMain(w http.ResponseWriter, userRequestData dataType.UserRequest, rule
|
|||||||
|
|
||||||
if bytes.Compare(decision.HTTPCode, []byte("200")) == 0 {
|
if bytes.Compare(decision.HTTPCode, []byte("200")) == 0 {
|
||||||
w.WriteHeader(http.StatusOK)
|
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 {
|
} 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)
|
||||||
http.Error(w, "500 - Internal Server Error", http.StatusInternalServerError)
|
http.Error(w, "500 - Internal Server Error", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -52,6 +58,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)
|
||||||
http.Error(w, "500 - Internal Server Error", http.StatusInternalServerError)
|
http.Error(w, "500 - Internal Server Error", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -59,18 +66,21 @@ 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)
|
||||||
http.Error(w, "500 - Internal Server Error", http.StatusInternalServerError)
|
http.Error(w, "500 - Internal Server Error", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.WriteHeader(http.StatusServiceUnavailable)
|
w.WriteHeader(http.StatusServiceUnavailable)
|
||||||
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, nil); err != nil {
|
if err = tpl.Execute(w, nil); err != nil {
|
||||||
|
log.Printf("Error template: %v", err)
|
||||||
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)
|
||||||
http.Error(w, "500 - Internal Server Error", http.StatusInternalServerError)
|
http.Error(w, "500 - Internal Server Error", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -94,7 +94,6 @@ func getClientIP(cfg *config.MainConfig, r *http.Request) string {
|
|||||||
remoteAddr := r.RemoteAddr
|
remoteAddr := r.RemoteAddr
|
||||||
ipStr, _, err := net.SplitHostPort(remoteAddr)
|
ipStr, _, err := net.SplitHostPort(remoteAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
//TODO: log error
|
|
||||||
clientIP = remoteAddr
|
clientIP = remoteAddr
|
||||||
} else {
|
} else {
|
||||||
clientIP = ipStr
|
clientIP = ipStr
|
||||||
|
@ -3,6 +3,7 @@ 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"
|
||||||
@ -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.HTTPCode, []byte("200")) == 0 {
|
||||||
if bytes.Compare(decision.ResponseData, []byte("bad")) == 0 {
|
if bytes.Compare(decision.ResponseData, []byte("bad")) == 0 {
|
||||||
w.WriteHeader(http.StatusOK)
|
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
|
return
|
||||||
} else if bytes.Compare(decision.ResponseData, []byte("good")) == 0 {
|
} else if bytes.Compare(decision.ResponseData, []byte("good")) == 0 {
|
||||||
w.Header().Set("Set-Cookie", "__torii_clearance="+string(check.GenClearance(reqData, *ruleSet))+"; Path=/; HttpOnly")
|
w.Header().Set("Set-Cookie", "__torii_clearance="+string(check.GenClearance(reqData, *ruleSet))+"; Path=/; HttpOnly")
|
||||||
w.WriteHeader(http.StatusOK)
|
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 {
|
} else {
|
||||||
//should not be here
|
//should not be here
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
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 {
|
} else {
|
||||||
tpl, err := template.ParseFiles(cfg.ErrorPage + "/403.html")
|
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)
|
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
|
// Start server
|
||||||
stop := make(chan os.Signal, 1)
|
stop := make(chan os.Signal, 1)
|
||||||
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
|
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
Reference in New Issue
Block a user