wp-go/internal/middleware/validateservername.go

34 lines
698 B
Go
Raw Permalink Normal View History

2022-10-10 13:07:39 +00:00
package middleware
import (
"github.com/fthvgb1/wp-go/helper/maps"
"github.com/fthvgb1/wp-go/internal/cmd/reload"
"github.com/fthvgb1/wp-go/internal/pkg/config"
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
}