mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-03-01 03:24:14 +08:00
43 lines
949 B
Go
43 lines
949 B
Go
package middleware
|
|
|
|
import (
|
|
"errors"
|
|
"github.com/1Panel-dev/1Panel/core/utils/common"
|
|
"strings"
|
|
|
|
"github.com/1Panel-dev/1Panel/core/app/api/v2/helper"
|
|
"github.com/1Panel-dev/1Panel/core/app/repo"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func WhiteAllow() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
settingRepo := repo.NewISettingRepo()
|
|
status, err := settingRepo.Get(repo.WithByKey("AllowIPs"))
|
|
if err != nil {
|
|
helper.InternalServer(c, err)
|
|
return
|
|
}
|
|
|
|
if len(status.Value) == 0 {
|
|
c.Next()
|
|
return
|
|
}
|
|
clientIP := c.ClientIP()
|
|
for _, ip := range strings.Split(status.Value, ",") {
|
|
if len(ip) == 0 {
|
|
continue
|
|
}
|
|
if ip == clientIP || (strings.Contains(ip, "/") && common.CheckIpInCidr(ip, clientIP)) {
|
|
c.Next()
|
|
return
|
|
}
|
|
}
|
|
if LoadErrCode() != 200 {
|
|
helper.ErrResponse(c, LoadErrCode())
|
|
return
|
|
}
|
|
helper.ErrorWithDetail(c, 310, "ErrInternalServer", errors.New("IP address not allowed"))
|
|
}
|
|
}
|