2022-08-16 15:30:23 +00:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2024-11-21 14:09:00 +00:00
|
|
|
"crypto/md5"
|
|
|
|
"encoding/hex"
|
|
|
|
"net"
|
2022-09-19 11:42:06 +00:00
|
|
|
"strconv"
|
2024-11-21 14:09:00 +00:00
|
|
|
"strings"
|
2022-09-19 11:42:06 +00:00
|
|
|
|
2022-10-17 08:32:31 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/api/v1/helper"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/repo"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
2022-08-16 15:30:23 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
func SessionAuth() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
if method, exist := c.Get("authMethod"); exist && method == constant.AuthMethodJWT {
|
|
|
|
c.Next()
|
2023-03-29 03:46:14 +00:00
|
|
|
return
|
2022-08-16 15:30:23 +00:00
|
|
|
}
|
2024-11-21 14:09:00 +00:00
|
|
|
panelToken := c.GetHeader("1Panel-Token")
|
|
|
|
panelTimestamp := c.GetHeader("1Panel-Timestamp")
|
|
|
|
if panelToken != "" || panelTimestamp != "" {
|
|
|
|
if global.CONF.System.ApiInterfaceStatus == "enable" {
|
|
|
|
clientIP := c.ClientIP()
|
|
|
|
if !isValid1PanelToken(panelToken, panelTimestamp) {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrUnauthorized, constant.ErrApiConfigKeyInvalid, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isIPInWhiteList(clientIP) {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrUnauthorized, constant.ErrApiConfigIPInvalid, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Next()
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrUnauthorized, constant.ErrApiConfigStatusInvalid, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-08 10:47:15 +00:00
|
|
|
sId, err := c.Cookie(constant.SessionName)
|
2022-08-16 15:30:23 +00:00
|
|
|
if err != nil {
|
2022-08-17 03:47:56 +00:00
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrUnauthorized, constant.ErrTypeNotLogin, nil)
|
2022-08-16 15:30:23 +00:00
|
|
|
return
|
|
|
|
}
|
2022-09-19 11:42:06 +00:00
|
|
|
psession, err := global.SESSION.Get(sId)
|
|
|
|
if err != nil {
|
2022-08-17 03:47:56 +00:00
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrUnauthorized, constant.ErrTypeNotLogin, nil)
|
2022-08-16 15:30:23 +00:00
|
|
|
return
|
|
|
|
}
|
2022-09-19 11:42:06 +00:00
|
|
|
settingRepo := repo.NewISettingRepo()
|
|
|
|
setting, err := settingRepo.Get(settingRepo.WithByKey("SessionTimeout"))
|
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("create operation record failed, err: %v", err)
|
|
|
|
}
|
|
|
|
lifeTime, _ := strconv.Atoi(setting.Value)
|
|
|
|
_ = global.SESSION.Set(sId, psession, lifeTime)
|
2022-08-16 15:30:23 +00:00
|
|
|
c.Next()
|
|
|
|
}
|
|
|
|
}
|
2024-11-21 14:09:00 +00:00
|
|
|
|
|
|
|
func isValid1PanelToken(panelToken string, panelTimestamp string) bool {
|
|
|
|
system1PanelToken := global.CONF.System.ApiKey
|
2024-12-09 07:29:33 +00:00
|
|
|
if panelToken == GenerateMD5("1panel"+system1PanelToken+panelTimestamp) {
|
2024-11-21 14:09:00 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func isIPInWhiteList(clientIP string) bool {
|
|
|
|
ipWhiteString := global.CONF.System.IpWhiteList
|
|
|
|
ipWhiteList := strings.Split(ipWhiteString, "\n")
|
|
|
|
for _, cidr := range ipWhiteList {
|
|
|
|
if cidr == "0.0.0.0" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
_, ipNet, err := net.ParseCIDR(cidr)
|
|
|
|
if err != nil {
|
|
|
|
if cidr == clientIP {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if ipNet.Contains(net.ParseIP(clientIP)) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func GenerateMD5(input string) string {
|
|
|
|
hash := md5.New()
|
|
|
|
hash.Write([]byte(input))
|
|
|
|
return hex.EncodeToString(hash.Sum(nil))
|
|
|
|
}
|