wp-go/internal/middleware/validateservername.go
2023-02-05 21:06:04 +08:00

36 lines
694 B
Go

package middleware
import (
"github.com/fthvgb1/wp-go/internal/pkg/config"
"github.com/fthvgb1/wp-go/safety"
"github.com/gin-gonic/gin"
"net/http"
"strings"
)
func ValidateServerNames() (func(ctx *gin.Context), func()) {
var serverName safety.Map[string, struct{}]
fn := func() {
r := config.GetConfig().TrustServerNames
if len(r) > 0 {
for _, name := range r {
serverName.Store(name, struct{}{})
}
} else {
serverName.Flush()
}
}
fn()
return func(c *gin.Context) {
if serverName.Len() > 0 {
if _, ok := serverName.Load(strings.Split(c.Request.Host, ":")[0]); !ok {
c.Status(http.StatusForbidden)
c.Abort()
return
}
}
c.Next()
}, fn
}