2025-09-20 07:35:50 +00:00
|
|
|
// Package middleware provides HTTP middleware functions for the 3x-ui web panel,
|
|
|
|
// including domain validation and URL redirection utilities.
|
2023-05-30 20:43:46 +00:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2024-05-22 15:51:55 +00:00
|
|
|
"net"
|
2023-05-30 20:43:46 +00:00
|
|
|
"net/http"
|
2024-07-14 21:55:04 +00:00
|
|
|
"strings"
|
2023-05-30 20:43:46 +00:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
2025-09-20 07:35:50 +00:00
|
|
|
// DomainValidatorMiddleware returns a Gin middleware that validates the request domain.
|
|
|
|
// It extracts the host from the request, strips any port number, and compares it
|
|
|
|
// against the configured domain. Requests from unauthorized domains are rejected
|
|
|
|
// with HTTP 403 Forbidden status.
|
2023-05-30 20:43:46 +00:00
|
|
|
func DomainValidatorMiddleware(domain string) gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
2024-08-08 08:52:59 +00:00
|
|
|
host := c.Request.Host
|
|
|
|
if colonIndex := strings.LastIndex(host, ":"); colonIndex != -1 {
|
|
|
|
host, _, _ = net.SplitHostPort(c.Request.Host)
|
2024-05-23 21:51:19 +00:00
|
|
|
}
|
2024-07-14 21:55:04 +00:00
|
|
|
|
|
|
|
if host != domain {
|
|
|
|
c.AbortWithStatus(http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Next()
|
2023-05-30 20:43:46 +00:00
|
|
|
}
|
|
|
|
}
|