2022-10-10 13:07:39 +00:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2023-02-17 15:36:54 +00:00
|
|
|
"github.com/fthvgb1/wp-go/helper/maps"
|
|
|
|
"github.com/fthvgb1/wp-go/internal/cmd/reload"
|
2023-01-18 15:02:59 +00:00
|
|
|
"github.com/fthvgb1/wp-go/internal/pkg/config"
|
2022-10-10 13:07:39 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2023-02-17 15:36:54 +00:00
|
|
|
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
|
2023-02-17 15:36:54 +00:00
|
|
|
m := map[string]struct{}{}
|
2022-11-16 02:17:29 +00:00
|
|
|
if len(r) > 0 {
|
|
|
|
for _, name := range r {
|
2023-02-17 15:36:54 +00:00
|
|
|
m[name] = struct{}{}
|
2022-11-16 02:17:29 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-17 15:36:54 +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) {
|
2023-02-17 15:36:54 +00:00
|
|
|
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()
|
2023-02-17 15:36:54 +00:00
|
|
|
}
|
2022-10-10 13:07:39 +00:00
|
|
|
}
|