2023-05-19 21:47:46 +08:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/api/v1/helper"
|
2023-06-01 10:38:11 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/repo"
|
2023-05-19 21:47:46 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
func WhiteAllow() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
2023-06-01 10:38:11 +08:00
|
|
|
settingRepo := repo.NewISettingRepo()
|
|
|
|
status, err := settingRepo.Get(settingRepo.WithByKey("AllowIPs"))
|
|
|
|
if err != nil {
|
2023-06-25 11:50:13 +08:00
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
2023-06-01 10:38:11 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(status.Value) == 0 {
|
2023-05-19 21:47:46 +08:00
|
|
|
c.Next()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
clientIP := c.ClientIP()
|
2023-06-01 10:38:11 +08:00
|
|
|
for _, ip := range strings.Split(status.Value, ",") {
|
2023-05-19 21:47:46 +08:00
|
|
|
if len(ip) != 0 && ip == clientIP {
|
|
|
|
c.Next()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrIP, constant.ErrTypeInternalServer, errors.New("IP address not allowed"))
|
|
|
|
}
|
|
|
|
}
|