From 3182a8f753c0bfc9ad4fe50f779cc530de712091 Mon Sep 17 00:00:00 2001
From: Roi Feng <37480123+Rayzggz@users.noreply.github.com>
Date: Fri, 14 Feb 2025 00:11:21 -0500
Subject: [PATCH] feat: custom error page
---
config/error_page/403.html | 71 ++++++++++++++++++++++++++++++++++++++
config/torii.yml | 2 ++
internal/config/config.go | 2 ++
internal/server/server.go | 26 ++++++++++++--
4 files changed, 99 insertions(+), 2 deletions(-)
create mode 100644 config/error_page/403.html
diff --git a/config/error_page/403.html b/config/error_page/403.html
new file mode 100644
index 0000000..68a0a4d
--- /dev/null
+++ b/config/error_page/403.html
@@ -0,0 +1,71 @@
+
+
+
+
+
+ 403 Forbidden
+
+
+
+
+
403
+
Access Denied (403 Forbidden)
+
You do not have permission to access this resource.
+
Node: {{.EdgeTag}}
+ Your IP: {{.ConnectIP}}
+ Date: {{.Date}}
+
+
+
+
+
+
diff --git a/config/torii.yml b/config/torii.yml
index 303d62c..3403b1e 100644
--- a/config/torii.yml
+++ b/config/torii.yml
@@ -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:
diff --git a/internal/config/config.go b/internal/config/config.go
index 5489baa..27ea85c 100644
--- a/internal/config/config.go
+++ b/internal/config/config.go
@@ -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"`
}
diff --git a/internal/server/server.go b/internal/server/server.go
index cfbbfac..a5a6b3e 100644
--- a/internal/server/server.go
+++ b/internal/server/server.go
@@ -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)