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