refactor: nodes analytics (#847)

* refactor: nodes analytics

* feat(debug): add pprof in debug mode

* refactor: websocket error handler
This commit is contained in:
Jacky 2025-02-05 17:25:29 +08:00
parent b1ba719cb1
commit cb4977e5ab
No known key found for this signature in database
GPG key ID: 215C21B10DF38B4D
17 changed files with 276 additions and 155 deletions

View file

@ -12,6 +12,34 @@ import (
"github.com/uozi-tech/cosy/logger"
)
// getToken from header, cookie or query
func getToken(c *gin.Context) (token string) {
if token = c.GetHeader("Authorization"); token != "" {
return
}
if token, _ = c.Cookie("token"); token != "" {
return token
}
if token = c.Query("token"); token != "" {
tokenBytes, _ := base64.StdEncoding.DecodeString(token)
return string(tokenBytes)
}
return ""
}
// getXNodeID from header or query
func getXNodeID(c *gin.Context) (xNodeID string) {
if xNodeID = c.GetHeader("X-Node-ID"); xNodeID != "" {
return xNodeID
}
return c.Query("x_node_id")
}
// AuthRequired is a middleware that checks if the user is authenticated
func AuthRequired() gin.HandlerFunc {
return func(c *gin.Context) {
abortWithAuthFailure := func() {
@ -20,20 +48,20 @@ func AuthRequired() gin.HandlerFunc {
})
}
token := c.GetHeader("Authorization")
xNodeID := getXNodeID(c)
if xNodeID != "" {
c.Set("ProxyNodeID", xNodeID)
}
token := getToken(c)
if token == "" {
if token = c.GetHeader("X-Node-Secret"); token != "" && token == settings.NodeSettings.Secret {
c.Set("Secret", token)
c.Next()
return
} else {
c.Set("ProxyNodeID", c.Query("x_node_id"))
tokenBytes, _ := base64.StdEncoding.DecodeString(c.Query("token"))
token = string(tokenBytes)
if token == "" {
abortWithAuthFailure()
return
}
abortWithAuthFailure()
return
}
}
@ -44,11 +72,6 @@ func AuthRequired() gin.HandlerFunc {
}
c.Set("user", u)
if nodeID := c.GetHeader("X-Node-ID"); nodeID != "" {
c.Set("ProxyNodeID", nodeID)
}
c.Next()
}
}
@ -70,6 +93,7 @@ func (f ServerFileSystemType) Exists(prefix string, _path string) bool {
return err == nil
}
// CacheJs is a middleware that send header to client to cache js file
func CacheJs() gin.HandlerFunc {
return func(c *gin.Context) {
if strings.Contains(c.Request.URL.String(), "js") {