wp-go/internal/middleware/validateservername.go

36 lines
694 B
Go
Raw Permalink Normal View History

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