2022-10-10 13:07:39 +00:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gin-gonic/gin"
|
2023-01-13 06:04:32 +00:00
|
|
|
"github/fthvgb1/wp-go/internal/pkg/config"
|
2022-11-16 02:17:29 +00:00
|
|
|
"github/fthvgb1/wp-go/safety"
|
2022-10-10 13:07:39 +00:00
|
|
|
"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
|
|
|
}
|