From 5128bfff613ab81b4803f92bef9f4dc51bf8aa36 Mon Sep 17 00:00:00 2001 From: naiba Date: Tue, 24 Dec 2024 23:28:37 +0800 Subject: [PATCH] feat: show online user id --- cmd/dashboard/controller/waf.go | 2 +- cmd/dashboard/controller/ws.go | 13 +++++++++---- service/singleton/frontend-templates.yaml | 2 +- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/cmd/dashboard/controller/waf.go b/cmd/dashboard/controller/waf.go index 96c3349..88d04f9 100644 --- a/cmd/dashboard/controller/waf.go +++ b/cmd/dashboard/controller/waf.go @@ -33,7 +33,7 @@ func listBlockedAddress(c *gin.Context) (*model.Value[[]*model.WAF], error) { } var waf []*model.WAF - if err := singleton.DB.Limit(limit).Offset(offset).Find(&waf).Error; err != nil { + if err := singleton.DB.Order("block_timestamp DESC").Limit(limit).Offset(offset).Find(&waf).Error; err != nil { return nil, err } diff --git a/cmd/dashboard/controller/ws.go b/cmd/dashboard/controller/ws.go index 0bbc440..2d5b008 100644 --- a/cmd/dashboard/controller/ws.go +++ b/cmd/dashboard/controller/ws.go @@ -119,7 +119,14 @@ func serverStream(c *gin.Context) (any, error) { userIp = c.RemoteIP() } + u, isMember := c.Get(model.CtxKeyAuthorizedUser) + var userId uint64 + if isMember { + userId = u.(*model.User).ID + } + singleton.AddOnlineUser(connId, &model.OnlineUser{ + UserID: userId, IP: userIp, ConnectedAt: time.Now(), Conn: conn, @@ -128,7 +135,7 @@ func serverStream(c *gin.Context) (any, error) { count := 0 for { - stat, err := getServerStat(c, count == 0) + stat, err := getServerStat(count == 0, isMember) if err != nil { continue } @@ -149,9 +156,7 @@ func serverStream(c *gin.Context) (any, error) { var requestGroup singleflight.Group -func getServerStat(c *gin.Context, withPublicNote bool) ([]byte, error) { - _, isMember := c.Get(model.CtxKeyAuthorizedUser) - authorized := isMember // TODO || isViewPasswordVerfied +func getServerStat(withPublicNote, authorized bool) ([]byte, error) { v, err, _ := requestGroup.Do(fmt.Sprintf("serverStats::%t", authorized), func() (interface{}, error) { singleton.SortedServerLock.RLock() defer singleton.SortedServerLock.RUnlock() diff --git a/service/singleton/frontend-templates.yaml b/service/singleton/frontend-templates.yaml index e114e6b..1028a50 100644 --- a/service/singleton/frontend-templates.yaml +++ b/service/singleton/frontend-templates.yaml @@ -9,7 +9,7 @@ name: "Official" repository: "https://github.com/hamster1963/nezha-dash-v1" author: "hamster1963" - version: "v1.8.0" + version: "v1.8.1" isofficial: true - path: "nazhua-dist" name: "Nazhua"