mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2026-09-23 16:00:52 +00:00
* feat: Implement caching for settings retrieval and update logic - Introduced a caching mechanism for settings using go-cache to improve performance. - Updated the Create, Update, and UpdateOrCreate methods to cache values after database operations. - Modified the Get and GetValueByKey methods to utilize the cache for faster access. - Adjusted middleware to use the new GetValueByKey method for retrieving settings, enhancing code consistency. * feat: Enhance setting cache management with dynamic TTL - Introduced a function to determine cache TTL based on setting keys, allowing critical settings to have shorter cache durations. - Updated cache logic in Create, Update, Get, and GetValueByKey methods to utilize the new TTL management, improving performance and consistency in settings retrieval. * refactor: Simplify setting cache TTL management - Removed the dynamic TTL function and standardized the cache TTL to a fixed duration for all settings. - Updated cache logic in Create, Update, Get, and GetValueByKey methods to use the new fixed TTL, enhancing code clarity and maintainability. - Added a new function to restart the core service after resetting settings, improving the reset command's functionality. * refactor: Remove core restart functionality from reset commands - Eliminated the restartCoreAfterReset function to simplify the reset command logic. - Updated reset commands to return nil after setting changes, enhancing clarity and reducing unnecessary complexity.
90 lines
2.3 KiB
Go
90 lines
2.3 KiB
Go
package router
|
|
|
|
import (
|
|
"net/http"
|
|
"net/url"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/1Panel-dev/1Panel/core/init/proxy"
|
|
|
|
"github.com/1Panel-dev/1Panel/core/app/api/v2/helper"
|
|
"github.com/1Panel-dev/1Panel/core/app/repo"
|
|
"github.com/1Panel-dev/1Panel/core/cmd/server/res"
|
|
"github.com/1Panel-dev/1Panel/core/constant"
|
|
"github.com/1Panel-dev/1Panel/core/global"
|
|
"github.com/1Panel-dev/1Panel/core/utils/xpack"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func Proxy() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
reqPath := c.Request.URL.Path
|
|
if strings.HasPrefix(reqPath, "/1panel/swagger") || !strings.HasPrefix(c.Request.URL.Path, "/api/v2") {
|
|
c.Next()
|
|
return
|
|
}
|
|
if strings.HasPrefix(reqPath, "/api/v2/core") && !strings.HasPrefix(c.Request.URL.Path, "/api/v2/core/xpack") {
|
|
c.Next()
|
|
return
|
|
}
|
|
var nodeItem string
|
|
queryNode := c.Query("operateNode")
|
|
if queryNode != "" && queryNode != "undefined" {
|
|
nodeItem = queryNode
|
|
} else {
|
|
nodeItem = c.Request.Header.Get("CurrentNode")
|
|
}
|
|
currentNode, err := url.QueryUnescape(nodeItem)
|
|
if err != nil {
|
|
helper.ErrorWithDetail(c, http.StatusBadRequest, "ErrProxy", err)
|
|
return
|
|
}
|
|
|
|
apiReq := c.GetBool("API_AUTH")
|
|
|
|
if !apiReq && strings.HasPrefix(c.Request.URL.Path, "/api/v2/") && !isLocalAPI(c.Request.URL.Path) && !checkSession(c) {
|
|
data, _ := res.ErrorMsg.ReadFile("html/401.html")
|
|
c.Data(401, "text/html; charset=utf-8", data)
|
|
c.Abort()
|
|
return
|
|
}
|
|
|
|
if !strings.HasPrefix(c.Request.URL.Path, "/api/v2/core") && (currentNode == "local" || len(currentNode) == 0) {
|
|
defer func() {
|
|
if err := recover(); err != nil && err != http.ErrAbortHandler {
|
|
global.LOG.Debug(err)
|
|
}
|
|
}()
|
|
proxy.LocalAgentProxy.ServeHTTP(c.Writer, c.Request)
|
|
c.Abort()
|
|
return
|
|
}
|
|
xpack.Proxy(c, currentNode)
|
|
c.Abort()
|
|
}
|
|
}
|
|
|
|
func checkSession(c *gin.Context) bool {
|
|
psession, err := global.SESSION.Get(c)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
settingRepo := repo.NewISettingRepo()
|
|
sessionTimeout, err := settingRepo.GetValueByKey("SessionTimeout")
|
|
if err != nil {
|
|
return false
|
|
}
|
|
lifeTime, _ := strconv.Atoi(sessionTimeout)
|
|
ssl, err := settingRepo.GetValueByKey("SSL")
|
|
if err != nil {
|
|
return false
|
|
}
|
|
_ = global.SESSION.Set(c, psession, ssl == constant.StatusEnable, lifeTime)
|
|
return true
|
|
}
|
|
|
|
func isLocalAPI(urlPath string) bool {
|
|
return urlPath == "/api/v2/core/xpack/sync/ssl"
|
|
}
|