mirror of
https://github.com/wgh136/nysoure.git
synced 2025-12-16 15:51:14 +00:00
feat: error page
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user