mirror of
https://github.com/wgh136/nysoure.git
synced 2025-12-16 15:51:14 +00:00
feat: add UnsupportedRegionMiddleware to handle requests from unsupported regions
This commit is contained in:
2
main.go
2
main.go
@@ -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)
|
||||||
|
|||||||
29
server/middleware/unsupported_region_middleware.go
Normal file
29
server/middleware/unsupported_region_middleware.go
Normal 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()
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user