2024-07-19 19:04:11 +08:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
|
|
import (
|
2026-06-17 15:31:51 +08:00
|
|
|
"net"
|
2024-07-19 19:04:11 +08:00
|
|
|
"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/common"
|
|
|
|
|
"github.com/1Panel-dev/1Panel/core/utils/security"
|
2024-07-19 19:04:11 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
)
|
|
|
|
|
|
2026-11-02 22:17:12 +08:00
|
|
|
type SourcePolicy struct {
|
|
|
|
|
Bypass func(*gin.Context) bool
|
|
|
|
|
Resolve func(*gin.Context, string) (clientIP string, allowed bool)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func WhiteAllow(policies ...SourcePolicy) gin.HandlerFunc {
|
|
|
|
|
policies = append([]SourcePolicy(nil), policies...)
|
2024-07-19 19:04:11 +08:00
|
|
|
return func(c *gin.Context) {
|
2026-11-02 22:17:12 +08:00
|
|
|
for _, policy := range policies {
|
|
|
|
|
if policy.Bypass == nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
allowed := policy.Bypass(c)
|
|
|
|
|
if c.IsAborted() {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if allowed {
|
|
|
|
|
c.Next()
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-10-26 13:59:49 +08:00
|
|
|
}
|
2025-09-17 15:11:59 +08:00
|
|
|
tokenString := c.GetHeader("X-Panel-Local-Token")
|
2026-08-21 17:33:14 +08:00
|
|
|
remoteIP := common.GetRealClientIP(c)
|
|
|
|
|
if isLocalSyncRequest(c.Request.URL.Path, remoteIP, tokenString) {
|
2025-09-17 15:11:59 +08:00
|
|
|
c.Set("LOCAL_REQUEST", true)
|
|
|
|
|
c.Next()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-19 19:04:11 +08:00
|
|
|
settingRepo := repo.NewISettingRepo()
|
2026-08-21 17:33:14 +08:00
|
|
|
trustedProxies, err := settingRepo.GetValueByKey("AllowIPTrustedProxies")
|
|
|
|
|
if err != nil {
|
|
|
|
|
helper.InternalServer(c, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-11-02 22:17:12 +08:00
|
|
|
clientIP := ""
|
|
|
|
|
for _, policy := range policies {
|
|
|
|
|
if policy.Resolve == nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
resolved, allowed := policy.Resolve(c, trustedProxies)
|
|
|
|
|
if c.IsAborted() {
|
2026-10-26 13:59:49 +08:00
|
|
|
return
|
|
|
|
|
}
|
2026-11-02 22:17:12 +08:00
|
|
|
if resolved != "" {
|
|
|
|
|
clientIP = resolved
|
2026-10-26 13:59:49 +08:00
|
|
|
}
|
2026-11-02 22:17:12 +08:00
|
|
|
if allowed {
|
2026-10-26 13:59:49 +08:00
|
|
|
c.Next()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-02 22:17:12 +08:00
|
|
|
if clientIP == "" {
|
|
|
|
|
clientIP = common.ResolveClientIP(c, trustedProxies)
|
|
|
|
|
}
|
2026-08-21 17:33:14 +08:00
|
|
|
|
2026-02-25 16:40:20 +08:00
|
|
|
allowIPs, err := settingRepo.GetValueByKey("AllowIPs")
|
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(allowIPs) == 0 {
|
2024-07-19 19:04:11 +08:00
|
|
|
c.Next()
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-02-25 16:40:20 +08:00
|
|
|
for _, ip := range strings.Split(allowIPs, ",") {
|
2024-07-19 19:04:11 +08:00
|
|
|
if len(ip) == 0 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2025-02-26 17:02:17 +08:00
|
|
|
if ip == clientIP || (strings.Contains(ip, "/") && common.CheckIpInCidr(ip, clientIP)) {
|
2024-07-19 19:04:11 +08:00
|
|
|
c.Next()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-05-19 17:30:50 +08:00
|
|
|
code := security.LoadErrCode()
|
2025-05-19 22:46:29 +08:00
|
|
|
helper.ErrWithHtml(c, code, "err_ip_limit")
|
2024-07-19 19:04:11 +08:00
|
|
|
}
|
|
|
|
|
}
|
2026-06-17 15:31:51 +08:00
|
|
|
|
|
|
|
|
func isLocalSyncRequest(reqPath, clientIP, token string) bool {
|
|
|
|
|
ip := net.ParseIP(clientIP)
|
|
|
|
|
if ip == nil || !ip.IsLoopback() {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch reqPath {
|
2026-11-05 15:06:12 +08:00
|
|
|
case "/api/v2/core/xpack/sync/ssl", "/api/v2/core/xpack/sync/php/extensions":
|
2026-06-17 15:31:51 +08:00
|
|
|
return token != ""
|
|
|
|
|
case "/api/v2/core/settings/ssl/reload":
|
2026-06-26 18:08:38 +08:00
|
|
|
return token != ""
|
2026-06-17 15:31:51 +08:00
|
|
|
default:
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|