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"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
func DomainValidatorMiddleware(domain string) gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
2024-05-24 00:51:19 +03:00
|
|
|
host := c.GetHeader("X-Forwarded-Host")
|
|
|
|
if host == "" {
|
|
|
|
host = c.GetHeader("X-Real-IP")
|
2023-05-30 23:43:46 +03:00
|
|
|
}
|
2024-05-24 00:51:19 +03:00
|
|
|
if host == "" {
|
|
|
|
host, _, _ := net.SplitHostPort(c.Request.Host)
|
|
|
|
if host != domain {
|
|
|
|
c.AbortWithStatus(http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
2023-05-30 23:43:46 +03:00
|
|
|
c.Next()
|
2024-05-24 00:51:19 +03:00
|
|
|
}
|
2023-05-30 23:43:46 +03:00
|
|
|
}
|
|
|
|
}
|