2023-05-30 20:43:46 +00:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2024-05-22 15:51:55 +00:00
|
|
|
"net"
|
2023-05-30 20:43:46 +00:00
|
|
|
"net/http"
|
2024-07-14 21:55:04 +00:00
|
|
|
"strings"
|
2023-05-30 20:43:46 +00:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
func DomainValidatorMiddleware(domain string) gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
2024-08-08 08:52:59 +00:00
|
|
|
host := c.Request.Host
|
|
|
|
if colonIndex := strings.LastIndex(host, ":"); colonIndex != -1 {
|
|
|
|
host, _, _ = net.SplitHostPort(c.Request.Host)
|
2024-05-23 21:51:19 +00:00
|
|
|
}
|
2024-07-14 21:55:04 +00:00
|
|
|
|
|
|
|
if host != domain {
|
|
|
|
c.AbortWithStatus(http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Next()
|
2023-05-30 20:43:46 +00:00
|
|
|
}
|
|
|
|
}
|