mirror of
https://github.com/gazer-x/komari.git
synced 2026-06-22 00:05:52 +08:00
90aad4b48a
- Moved API route registration to dedicated init files for better organization. - Introduced event listeners for server initialization to dynamically register routes. - Removed redundant configuration loading in routers.go. - Added new API routes for various functionalities including client management, admin tasks, and notifications. - Implemented a standardized response structure for API responses. - Established WebSocket connections for terminal sessions and improved session management. - Created a new database initialization for default admin account creation. - Enhanced gRPC server setup for Nezha compatibility with dynamic configuration updates.
44 lines
994 B
Go
44 lines
994 B
Go
package api_v1
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/komari-monitor/komari/internal/api_v1/resp"
|
|
"github.com/komari-monitor/komari/internal/database/accounts"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func AdminAuthMiddleware() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
// API key authentication
|
|
apiKey := c.GetHeader("Authorization")
|
|
if isApiKeyValid(apiKey) {
|
|
c.Set("api_key", apiKey)
|
|
c.Next()
|
|
return
|
|
}
|
|
// session-based authentication
|
|
session, err := c.Cookie("session_token")
|
|
if err != nil {
|
|
resp.RespondError(c, http.StatusUnauthorized, "Unauthorized.")
|
|
c.Abort()
|
|
return
|
|
}
|
|
|
|
// Komari is a single user system
|
|
uuid, err := accounts.GetSession(session)
|
|
if err != nil {
|
|
resp.RespondError(c, http.StatusUnauthorized, "Unauthorized.")
|
|
c.Abort()
|
|
return
|
|
}
|
|
accounts.UpdateLatest(session, c.Request.UserAgent(), c.ClientIP())
|
|
// 将 session 和 用户 UUID 传递到后续处理器
|
|
c.Set("session", session)
|
|
c.Set("uuid", uuid)
|
|
|
|
c.Next()
|
|
}
|
|
}
|