2024-07-19 19:04:11 +08:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"strings"
|
|
|
|
|
|
2024-08-07 13:52:34 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/core/app/api/v2/helper"
|
2024-07-19 19:04:11 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/core/app/repo"
|
2026-05-19 17:30:50 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/core/utils/security"
|
2024-07-19 19:04:11 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
)
|
|
|
|
|
|
2026-10-28 14:04:38 +08:00
|
|
|
// DomainPolicy is configured by the router and must only trust server-verified state.
|
|
|
|
|
type DomainPolicy func(*gin.Context) bool
|
|
|
|
|
|
|
|
|
|
func BindDomain(policies ...DomainPolicy) gin.HandlerFunc {
|
|
|
|
|
policies = append([]DomainPolicy(nil), policies...)
|
2024-07-19 19:04:11 +08:00
|
|
|
return func(c *gin.Context) {
|
2026-10-28 14:04:38 +08:00
|
|
|
for _, policy := range policies {
|
|
|
|
|
if policy != nil && policy(c) {
|
|
|
|
|
c.Next()
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-10-26 13:59:49 +08:00
|
|
|
}
|
2025-09-17 15:11:59 +08:00
|
|
|
localRequest := c.GetBool("LOCAL_REQUEST")
|
|
|
|
|
if localRequest {
|
|
|
|
|
c.Next()
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-07-19 19:04:11 +08:00
|
|
|
settingRepo := repo.NewISettingRepo()
|
2026-02-25 16:40:20 +08:00
|
|
|
bindDomain, err := settingRepo.GetValueByKey("BindDomain")
|
2024-07-19 19:04:11 +08:00
|
|
|
if err != nil {
|
2025-01-24 11:00:07 +08:00
|
|
|
helper.InternalServer(c, err)
|
2024-07-19 19:04:11 +08:00
|
|
|
return
|
|
|
|
|
}
|
2026-02-25 16:40:20 +08:00
|
|
|
if len(bindDomain) == 0 {
|
2024-07-19 19:04:11 +08:00
|
|
|
c.Next()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
domains := c.Request.Host
|
|
|
|
|
parts := strings.Split(c.Request.Host, ":")
|
|
|
|
|
if len(parts) > 0 {
|
|
|
|
|
domains = parts[0]
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-25 16:40:20 +08:00
|
|
|
if domains != bindDomain {
|
2026-05-19 17:30:50 +08:00
|
|
|
code := security.LoadErrCode()
|
2025-05-19 22:46:29 +08:00
|
|
|
helper.ErrWithHtml(c, code, "err_domain")
|
2024-07-19 19:04:11 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
c.Next()
|
|
|
|
|
}
|
|
|
|
|
}
|