feat: error page

This commit is contained in:
2025-12-09 14:15:57 +08:00
parent cb61ce99bf
commit 8e2ab62297
3 changed files with 37 additions and 3 deletions

View File

@@ -4,6 +4,8 @@ import (
"strings"
"github.com/gofiber/fiber/v3"
errorpage "github.com/wgh136/cloudflare-error-page"
)
func UnsupportedRegionMiddleware(c fiber.Ctx) error {
@@ -20,10 +22,39 @@ func UnsupportedRegionMiddleware(c fiber.Ctx) error {
}
if string(c.Request().Header.Peek("Unsupported-Region")) == "true" {
// Return a 403 Forbidden response with an empty html for unsupported regions
h, err := generateForbiddenPage(c)
if err != nil {
return err
}
c.Response().Header.Add("Content-Type", "text/html")
c.Status(fiber.StatusForbidden)
return c.SendString("<html></html>")
return c.SendString(h)
}
return c.Next()
}
func generateForbiddenPage(c fiber.Ctx) (string, error) {
params := errorpage.Params{
"error_code": 403,
"title": "Forbidden",
"browser_status": map[string]interface{}{
"status": "error",
"status_text": "Error",
},
"cloudflare_status": map[string]interface{}{
"status": "ok",
"status_text": "Working",
},
"host_status": map[string]interface{}{
"status": "ok",
"location": c.Hostname(),
},
"error_source": "cloudflare",
"what_happened": "<p>The service is not available in your region.</p>",
"what_can_i_do": "<p>Please try again in a few minutes.</p>",
"client_ip": c.IP(),
}
return errorpage.Render(params, nil)
}