1
0

domainValidator.go 544 B

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