feat: custom error page

This commit is contained in:
Roi Feng
2025-02-14 00:11:21 -05:00
parent aea36d463b
commit 3182a8f753
4 changed files with 99 additions and 2 deletions

View File

@ -0,0 +1,71 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>403 Forbidden</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f9f9f9;
}
.container {
text-align: left;
max-width: 600px;
}
.icon svg {
width: 60px;
height: 60px;
stroke: #333;
}
.error-code {
font-size: 100px;
font-weight: bold;
color: #e74c3c;
}
.message {
font-size: 24px;
font-weight: bold;
color: #333;
}
.description {
margin-top: 10px;
color: #666;
}
.debug-info {
margin-top: 30px;
font-size: 12px;
color: #666;
}
.footer {
margin-top: 30px;
font-size: 12px;
color: #999;
}
.footer a {
color: red;
text-decoration: none;
}
</style>
</head>
<body>
<div class="container">
<div class="error-code">403</div>
<div class="message">Access Denied (403 Forbidden)</div>
<div class="description">You do not have permission to access this resource.</div>
<div class="debug-info">Node: {{.EdgeTag}}<br>
Your IP: {{.ConnectIP}}<br>
Date: {{.Date}}<br>
</div>
<div class="footer">
Security powered by <a href="https://github.com/Rayzggz/server_torii">Server Torii</a>
</div>
</div>
</body>
</html>

View File

@ -1,5 +1,7 @@
port: "25555"
rule_path: "/www/dev/server_torii/config/rules"
error_page: "/www/dev/server_torii/config/error_page"
node_name: "Server Torii"
connecting_ip_headers:
- "X-Real-IP"
connecting_uri_headers:

View File

@ -14,6 +14,8 @@ import (
type MainConfig struct {
Port string `yaml:"port"`
RulePath string `yaml:"rule_path"`
ErrorPage string `yaml:"error_page"`
NodeName string `yaml:"node_name"`
ConnectingIPHeaders []string `yaml:"connecting_ip_headers"`
ConnectingURIHeaders []string `yaml:"connecting_uri_headers"`
}

View File

@ -1,6 +1,7 @@
package server
import (
"html/template"
"log"
"net"
"net/http"
@ -9,6 +10,7 @@ import (
"server_torii/internal/config"
"server_torii/internal/dataType"
"strings"
"time"
)
type CheckFunc func(dataType.UserRequest, *config.RuleSet, *action.Decision)
@ -36,10 +38,30 @@ func StartServer(cfg *config.MainConfig, ruleSet *config.RuleSet) error {
if decision.HTTPCode == "200" {
w.WriteHeader(http.StatusOK)
w.Write([]byte("Allowed"))
w.Write([]byte("OK"))
} else if decision.HTTPCode == "403" {
tpl, err := template.ParseFiles(cfg.ErrorPage + "/" + decision.HTTPCode + ".html")
if err != nil {
http.Error(w, "500 - Internal Server Error", http.StatusInternalServerError)
return
}
data := struct {
EdgeTag string
ConnectIP string
Date string
}{
EdgeTag: cfg.NodeName,
ConnectIP: userRequestData.RemoteIP,
Date: time.Now().Format("2006-01-02 15:04:05"),
}
w.WriteHeader(http.StatusForbidden)
w.Write([]byte("Blocked"))
w.Header().Set("Content-Type", "text/html; charset=utf-8")
if err = tpl.Execute(w, data); err != nil {
http.Error(w, "500 - Internal Server Error", http.StatusInternalServerError)
return
}
} else {
// should not reach here
w.WriteHeader(http.StatusInternalServerError)