domainValidator.go 430 B

12345678910111213141516171819202122232425
  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.Request.Host
  11. if colonIndex := strings.LastIndex(host, ":"); colonIndex != -1 {
  12. host, _, _ = net.SplitHostPort(c.Request.Host)
  13. }
  14. if host != domain {
  15. c.AbortWithStatus(http.StatusForbidden)
  16. return
  17. }
  18. c.Next()
  19. }
  20. }