2024-07-19 19:04:11 +08:00
|
|
|
package router
|
|
|
|
|
|
|
|
|
|
import (
|
2026-06-29 18:42:02 +08:00
|
|
|
"crypto/sha256"
|
|
|
|
|
"encoding/hex"
|
2025-10-14 16:51:48 +08:00
|
|
|
"io"
|
2025-08-13 15:40:42 +08:00
|
|
|
"net/http"
|
2025-10-14 16:51:48 +08:00
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"strings"
|
2025-08-13 15:40:42 +08:00
|
|
|
|
2026-04-23 17:00:50 +08:00
|
|
|
appauth "github.com/1Panel-dev/1Panel/core/app/auth"
|
2024-12-26 14:13:41 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/core/app/service"
|
2024-12-17 20:48:10 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/core/cmd/server/docs"
|
|
|
|
|
"github.com/1Panel-dev/1Panel/core/cmd/server/web"
|
2024-07-19 19:04:11 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/core/global"
|
|
|
|
|
"github.com/1Panel-dev/1Panel/core/i18n"
|
2025-11-24 16:14:06 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/core/init/swagger"
|
2024-07-19 19:04:11 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/core/middleware"
|
|
|
|
|
rou "github.com/1Panel-dev/1Panel/core/router"
|
2026-10-26 13:59:49 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/core/utils/mcpapi"
|
2025-03-26 16:47:35 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/core/utils/security"
|
2026-04-14 09:39:45 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/core/utils/xpack"
|
2024-07-19 19:04:11 +08:00
|
|
|
"github.com/gin-contrib/gzip"
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
Router *gin.Engine
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func setWebStatic(rootRouter *gin.RouterGroup) {
|
|
|
|
|
rootRouter.StaticFS("/public", http.FS(web.Favicon))
|
2024-12-23 15:49:57 +08:00
|
|
|
rootRouter.StaticFS("/favicon.ico", http.FS(web.Favicon))
|
2025-10-14 16:51:48 +08:00
|
|
|
RegisterImages(rootRouter)
|
2025-10-23 16:00:23 +08:00
|
|
|
setStaticResource(rootRouter)
|
2024-07-19 19:04:11 +08:00
|
|
|
rootRouter.GET("/assets/*filepath", func(c *gin.Context) {
|
2025-11-21 15:47:48 +08:00
|
|
|
c.Writer.Header().Set("Cache-Control", "private, max-age=2628000, immutable")
|
2025-10-09 16:01:00 +08:00
|
|
|
if c.Request.URL.Path[len(c.Request.URL.Path)-1] == '/' {
|
2025-10-14 16:51:48 +08:00
|
|
|
c.AbortWithStatus(http.StatusForbidden)
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-07-19 19:04:11 +08:00
|
|
|
staticServer := http.FileServer(http.FS(web.Assets))
|
|
|
|
|
staticServer.ServeHTTP(c.Writer, c.Request)
|
|
|
|
|
})
|
2024-12-23 15:49:57 +08:00
|
|
|
authService := service.NewIAuthService()
|
|
|
|
|
entrance := authService.GetSecurityEntrance()
|
|
|
|
|
if entrance != "" {
|
|
|
|
|
rootRouter.GET("/"+entrance, func(c *gin.Context) {
|
2025-01-15 18:25:59 +08:00
|
|
|
currentEntrance := authService.GetSecurityEntrance()
|
|
|
|
|
if currentEntrance != entrance {
|
2025-03-26 16:47:35 +08:00
|
|
|
security.HandleNotSecurity(c, "")
|
2024-12-23 15:49:57 +08:00
|
|
|
return
|
|
|
|
|
}
|
2025-03-26 16:47:35 +08:00
|
|
|
security.ToIndexHtml(c)
|
2024-12-23 15:49:57 +08:00
|
|
|
})
|
|
|
|
|
}
|
2024-07-19 19:04:11 +08:00
|
|
|
rootRouter.GET("/", func(c *gin.Context) {
|
2025-03-26 16:47:35 +08:00
|
|
|
if !security.CheckSecurity(c) {
|
2025-02-26 17:02:17 +08:00
|
|
|
return
|
|
|
|
|
}
|
2026-11-09 16:39:17 +08:00
|
|
|
entrance := authService.GetSecurityEntrance()
|
2025-02-26 17:02:17 +08:00
|
|
|
if entrance != "" {
|
2026-04-23 17:00:50 +08:00
|
|
|
appauth.SetSecurityEntranceCookie(c, entrance)
|
2025-02-26 17:02:17 +08:00
|
|
|
}
|
2024-07-19 19:04:11 +08:00
|
|
|
staticServer := http.FileServer(http.FS(web.IndexHtml))
|
|
|
|
|
staticServer.ServeHTTP(c.Writer, c.Request)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Routers() *gin.Engine {
|
2025-11-27 16:29:31 +08:00
|
|
|
Router = gin.New()
|
2026-11-08 22:39:54 +08:00
|
|
|
_ = Router.SetTrustedProxies(nil)
|
|
|
|
|
Router.Use(middleware.AuditClientIP())
|
2026-11-02 22:17:12 +08:00
|
|
|
Router.Use(middleware.MCPErrorMetadata())
|
2026-10-26 13:59:49 +08:00
|
|
|
Router.Use(middleware.MCPTokenBoundary())
|
|
|
|
|
Router.Use(middleware.MCPAPIIdentity())
|
2024-07-19 19:04:11 +08:00
|
|
|
Router.Use(i18n.UseI18n())
|
2026-11-02 22:17:12 +08:00
|
|
|
Router.Use(middleware.WhiteAllow(middleware.MCPSourcePolicy()))
|
2026-10-28 14:04:38 +08:00
|
|
|
Router.Use(middleware.BindDomain(middleware.MCPDomainPolicy))
|
2024-07-19 19:04:11 +08:00
|
|
|
|
|
|
|
|
swaggerRouter := Router.Group("1panel")
|
2024-08-06 16:20:31 +08:00
|
|
|
docs.SwaggerInfo.BasePath = "/api/v2"
|
2025-11-24 16:14:06 +08:00
|
|
|
swaggerRouter.Use(middleware.SessionAuth()).GET("/swagger/*any", swagger.SwaggerHandler())
|
|
|
|
|
|
2024-07-19 19:04:11 +08:00
|
|
|
PublicGroup := Router.Group("")
|
|
|
|
|
{
|
|
|
|
|
PublicGroup.Use(gzip.Gzip(gzip.DefaultCompression))
|
|
|
|
|
setWebStatic(PublicGroup)
|
|
|
|
|
}
|
2025-01-24 11:00:07 +08:00
|
|
|
if global.CONF.Base.IsDemo {
|
2024-07-25 18:13:57 +08:00
|
|
|
Router.Use(middleware.DemoHandle())
|
|
|
|
|
}
|
2026-10-26 13:59:49 +08:00
|
|
|
registerMCPTransport(Router)
|
2024-11-26 16:00:29 +08:00
|
|
|
|
2026-05-18 16:49:31 +08:00
|
|
|
Router.Use(middleware.FrontendFallback())
|
2026-09-30 15:21:21 +08:00
|
|
|
Router.Use(middleware.WebSocketOriginGuard())
|
2025-03-25 17:29:34 +08:00
|
|
|
Router.Use(middleware.GlobalLoading())
|
2026-04-23 17:00:50 +08:00
|
|
|
Router.Use(xpack.AuthProvider.CoreAPIAuthMiddleware())
|
2026-11-27 17:27:14 +08:00
|
|
|
Router.Use(middleware.OperationLog(middleware.MCPLogPolicy))
|
2025-03-25 17:29:34 +08:00
|
|
|
Router.Use(middleware.PasswordExpired())
|
2026-03-31 18:58:05 +08:00
|
|
|
Router.Use(middleware.CSRFTokenGuard())
|
2026-04-23 17:00:50 +08:00
|
|
|
Router.Use(xpack.AuthProvider.CoreRBACMiddlewares()...)
|
2026-11-09 18:31:43 +08:00
|
|
|
Router.Use(Proxy(panelMCPTaskProxyPolicy(), opsReportProxyPolicy()))
|
2025-03-25 17:29:34 +08:00
|
|
|
|
2024-07-25 14:43:41 +08:00
|
|
|
PrivateGroup := Router.Group("/api/v2/core")
|
2025-02-08 16:43:55 +08:00
|
|
|
PrivateGroup.Use(middleware.SetPasswordPublicKey())
|
2024-07-19 19:04:11 +08:00
|
|
|
for _, router := range rou.RouterGroupApp {
|
|
|
|
|
router.InitRouter(PrivateGroup)
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-23 18:14:20 +08:00
|
|
|
Router.NoRoute(func(c *gin.Context) {
|
2025-03-26 16:47:35 +08:00
|
|
|
if !security.HandleNotRoute(c) {
|
2025-02-26 17:02:17 +08:00
|
|
|
return
|
|
|
|
|
}
|
2025-03-26 16:47:35 +08:00
|
|
|
security.HandleNotSecurity(c, "")
|
2024-07-23 18:14:20 +08:00
|
|
|
})
|
|
|
|
|
|
2026-10-26 13:59:49 +08:00
|
|
|
mcpapi.SetHandler(Router)
|
2024-07-19 19:04:11 +08:00
|
|
|
return Router
|
|
|
|
|
}
|
2025-10-14 16:51:48 +08:00
|
|
|
|
|
|
|
|
func RegisterImages(rootRouter *gin.RouterGroup) {
|
|
|
|
|
staticDir := filepath.Join(global.CONF.Base.InstallDir, "1panel/uploads/theme")
|
|
|
|
|
rootRouter.GET("/api/v2/images/*filename", func(c *gin.Context) {
|
|
|
|
|
fileName := filepath.Base(c.Param("filename"))
|
|
|
|
|
filePath := filepath.Join(staticDir, fileName)
|
|
|
|
|
if !strings.HasPrefix(filePath, staticDir) {
|
|
|
|
|
c.AbortWithStatus(http.StatusForbidden)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
f, err := os.Open(filePath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Status(http.StatusNotFound)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer f.Close()
|
|
|
|
|
buf := make([]byte, 512)
|
|
|
|
|
n, _ := f.Read(buf)
|
|
|
|
|
content := buf[:n]
|
|
|
|
|
mimeType := http.DetectContentType(buf[:n])
|
|
|
|
|
if strings.Contains(string(content), "<svg") {
|
|
|
|
|
mimeType = "image/svg+xml"
|
|
|
|
|
}
|
|
|
|
|
_, _ = f.Seek(0, io.SeekStart)
|
|
|
|
|
c.Header("Content-Type", mimeType)
|
|
|
|
|
_, _ = io.Copy(c.Writer, f)
|
|
|
|
|
})
|
|
|
|
|
}
|
2025-10-23 16:00:23 +08:00
|
|
|
|
|
|
|
|
func setStaticResource(rootRouter *gin.RouterGroup) {
|
|
|
|
|
rootRouter.GET("/api/v2/static/*filename", func(c *gin.Context) {
|
2025-11-21 15:47:48 +08:00
|
|
|
c.Writer.Header().Set("Cache-Control", "private, max-age=2628000")
|
2025-10-23 16:00:23 +08:00
|
|
|
filename := c.Param("filename")
|
|
|
|
|
filePath := "static" + filename
|
|
|
|
|
data, err := web.Static.ReadFile(filePath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.AbortWithStatus(http.StatusNotFound)
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-06-29 18:42:02 +08:00
|
|
|
sum := sha256.Sum256(data)
|
|
|
|
|
etag := `"` + hex.EncodeToString(sum[:]) + `"`
|
|
|
|
|
c.Writer.Header().Set("ETag", etag)
|
|
|
|
|
if c.GetHeader("If-None-Match") == etag {
|
|
|
|
|
c.AbortWithStatus(http.StatusNotModified)
|
|
|
|
|
return
|
|
|
|
|
}
|
2025-10-23 16:00:23 +08:00
|
|
|
c.Writer.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
|
c.Writer.Write(data)
|
|
|
|
|
})
|
|
|
|
|
}
|