1
0

domainValidator.go 446 B

12345678910111213141516171819202122232425
  1. package middleware
  2. import (
  3. "net"
  4. "net/http"
  5. "github.com/gin-gonic/gin"
  6. )
  7. func DomainValidatorMiddleware(domain string) gin.HandlerFunc {
  8. return func(c *gin.Context) {
  9. host := c.GetHeader("X-Forwarded-Host")
  10. if host == "" {
  11. host = c.GetHeader("X-Real-IP")
  12. }
  13. if host == "" {
  14. host, _, _ := net.SplitHostPort(c.Request.Host)
  15. if host != domain {
  16. c.AbortWithStatus(http.StatusForbidden)
  17. return
  18. }
  19. c.Next()
  20. }
  21. }
  22. }