mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-11-25 20:15:01 +03:00
1e4282bd23
DomainValidatorMiddleware compared the saved domain with the request host byte for byte. Host names are case-insensitive and may end with the root's dot, so a domain saved as Panel.Example.com, with a stray space, or a Host header like panel.example.com. answered every request with 403, the settings page included, and the admin could not undo it from the UI. Both sides are now normalized before the comparison. Saving the settings stores the domain in that form and refuses a scheme, port or path, which could never equal the port-stripped host and locked the panel the same way.
38 lines
1.1 KiB
Go
38 lines
1.1 KiB
Go
// Package middleware provides HTTP middleware functions for the 3x-ui web panel,
|
|
// including domain validation utilities.
|
|
package middleware
|
|
|
|
import (
|
|
"net"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// 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.
|
|
func DomainValidatorMiddleware(domain string) gin.HandlerFunc {
|
|
domain = normalizeHost(domain)
|
|
return func(c *gin.Context) {
|
|
host := c.Request.Host
|
|
if colonIndex := strings.LastIndex(host, ":"); colonIndex != -1 {
|
|
host, _, _ = net.SplitHostPort(c.Request.Host)
|
|
}
|
|
|
|
if normalizeHost(host) != domain {
|
|
c.AbortWithStatus(http.StatusForbidden)
|
|
return
|
|
}
|
|
|
|
c.Next()
|
|
}
|
|
}
|
|
|
|
// Host names are case-insensitive and may end with the root's dot (RFC 4343, 1034).
|
|
func normalizeHost(host string) string {
|
|
return strings.TrimSuffix(strings.ToLower(strings.TrimSpace(host)), ".")
|
|
}
|