wp-go/app/middleware/validateservername.go

34 lines
686 B
Go
Raw Permalink Normal View History

2022-10-10 13:07:39 +00:00
package middleware
import (
2023-05-04 12:36:17 +00:00
"github.com/fthvgb1/wp-go/app/pkg/config"
2023-11-12 13:39:04 +00:00
"github.com/fthvgb1/wp-go/cache/reload"
"github.com/fthvgb1/wp-go/helper/maps"
2022-10-10 13:07:39 +00:00
"github.com/gin-gonic/gin"
"net/http"
"strings"
)
func ValidateServerNames() func(ctx *gin.Context) {
sites := reload.VarsBy(func() map[string]struct{} {
2023-02-05 13:06:04 +00:00
r := config.GetConfig().TrustServerNames
m := map[string]struct{}{}
2022-11-16 02:17:29 +00:00
if len(r) > 0 {
for _, name := range r {
m[name] = struct{}{}
2022-11-16 02:17:29 +00:00
}
}
return m
})
2022-11-16 02:17:29 +00:00
2022-10-10 13:07:39 +00:00
return func(c *gin.Context) {
m := sites.Load()
if len(m) > 0 && !maps.IsExists(m, strings.Split(c.Request.Host, ":")[0]) {
c.Status(http.StatusForbidden)
c.Abort()
return
2022-10-10 13:07:39 +00:00
}
2022-11-16 02:17:29 +00:00
c.Next()
}
2022-10-10 13:07:39 +00:00
}