feat: add UnsupportedRegionMiddleware to handle requests from unsupported regions

This commit is contained in:
2025-11-28 19:12:31 +08:00
parent 1544c535de
commit 1d78207004
2 changed files with 31 additions and 0 deletions

View File

@@ -19,6 +19,8 @@ func main() {
Format: "[${ip}]:${port} ${status} - ${method} ${path}\n", Format: "[${ip}]:${port} ${status} - ${method} ${path}\n",
})) }))
app.Use(middleware.UnsupportedRegionMiddleware)
app.Use(middleware.ErrorHandler) app.Use(middleware.ErrorHandler)
app.Use(middleware.RealUserMiddleware) app.Use(middleware.RealUserMiddleware)

View File

@@ -0,0 +1,29 @@
package middleware
import (
"strings"
"github.com/gofiber/fiber/v3"
)
func UnsupportedRegionMiddleware(c fiber.Ctx) error {
path := string(c.Request().URI().Path())
// Skip static file requests
if strings.Contains(path, ".") {
return c.Next()
}
// Skip API requests
if strings.HasPrefix(path, "/api") {
return c.Next()
}
if string(c.Request().Header.Peek("Unsupported-Region")) == "true" {
// Return a 403 Forbidden response with an empty html for unsupported regions
c.Response().Header.Add("Content-Type", "text/html")
c.Status(fiber.StatusForbidden)
return c.SendString("<html></html>")
}
return c.Next()
}