mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-02-04 12:40:07 +00:00
feat: list & block online users
This commit is contained in:
@@ -132,6 +132,9 @@ func routers(r *gin.Engine, frontendDist fs.FS) {
|
||||
auth.GET("/waf", pCommonHandler(listBlockedAddress))
|
||||
auth.POST("/batch-delete/waf", adminHandler(batchDeleteBlockedAddress))
|
||||
|
||||
auth.GET("/online-user", pCommonHandler(listOnlineUser))
|
||||
auth.GET("/online-user/batch-block", adminHandler(batchBlockOnlineUser))
|
||||
|
||||
auth.PATCH("/setting", adminHandler(updateConfig))
|
||||
|
||||
r.NoRoute(fallbackToFrontend(frontendDist))
|
||||
|
||||
@@ -50,7 +50,7 @@ func listConfig(c *gin.Context) (model.SettingResponse, error) {
|
||||
// @Security BearerAuth
|
||||
// @Schemes
|
||||
// @Description Edit config
|
||||
// @Tags auth required
|
||||
// @Tags admin required
|
||||
// @Accept json
|
||||
// @Param body body model.SettingForm true "SettingForm"
|
||||
// @Produce json
|
||||
|
||||
@@ -2,6 +2,7 @@ package controller
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
@@ -76,7 +77,7 @@ func updateProfile(c *gin.Context) (any, error) {
|
||||
// @Security BearerAuth
|
||||
// @Schemes
|
||||
// @Description List user
|
||||
// @Tags auth required
|
||||
// @Tags admin required
|
||||
// @Produce json
|
||||
// @Success 200 {object} model.CommonResponse[[]model.User]
|
||||
// @Router /user [get]
|
||||
@@ -93,7 +94,7 @@ func listUser(c *gin.Context) ([]model.User, error) {
|
||||
// @Security BearerAuth
|
||||
// @Schemes
|
||||
// @Description Create user
|
||||
// @Tags auth required
|
||||
// @Tags admin required
|
||||
// @Accept json
|
||||
// @param request body model.UserForm true "User Request"
|
||||
// @Produce json
|
||||
@@ -135,7 +136,7 @@ func createUser(c *gin.Context) (uint64, error) {
|
||||
// @Security BearerAuth
|
||||
// @Schemes
|
||||
// @Description Batch delete users
|
||||
// @Tags auth required
|
||||
// @Tags admin required
|
||||
// @Accept json
|
||||
// @param request body []uint true "id list"
|
||||
// @Produce json
|
||||
@@ -154,3 +155,59 @@ func batchDeleteUser(c *gin.Context) (any, error) {
|
||||
err := singleton.OnUserDelete(ids, newGormError)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// List online users
|
||||
// @Summary List online users
|
||||
// @Security BearerAuth
|
||||
// @Schemes
|
||||
// @Description List online users
|
||||
// @Tags auth required
|
||||
// @Param limit query uint false "Page limit"
|
||||
// @Param offset query uint false "Page offset"
|
||||
// @Produce json
|
||||
// @Success 200 {object} model.PaginatedResponse[[]model.OnlineUser, model.OnlineUser]
|
||||
// @Router /online-user [get]
|
||||
func listOnlineUser(c *gin.Context) (*model.Value[[]*model.OnlineUser], error) {
|
||||
limit, err := strconv.Atoi(c.Query("limit"))
|
||||
if err != nil || limit < 1 {
|
||||
limit = 25
|
||||
}
|
||||
|
||||
offset, err := strconv.Atoi(c.Query("offset"))
|
||||
if err != nil || offset < 0 {
|
||||
offset = 0
|
||||
}
|
||||
|
||||
return &model.Value[[]*model.OnlineUser]{
|
||||
Value: singleton.GetOnlineUsers(limit, offset),
|
||||
Pagination: model.Pagination{
|
||||
Offset: offset,
|
||||
Limit: limit,
|
||||
Total: int64(singleton.GetOnlineUserCount()),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Batch block online user
|
||||
// @Summary Batch block online user
|
||||
// @Security BearerAuth
|
||||
// @Schemes
|
||||
// @Description Batch block online user
|
||||
// @Tags admin required
|
||||
// @Accept json
|
||||
// @Param request body []string true "block list"
|
||||
// @Produce json
|
||||
// @Success 200 {object} model.CommonResponse[any]
|
||||
// @Router /online-user/batch-block [patch]
|
||||
func batchBlockOnlineUser(c *gin.Context) (any, error) {
|
||||
var list []string
|
||||
if err := c.ShouldBindJSON(&list); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := singleton.BlockByIPs(list); err != nil {
|
||||
return nil, newGormError("%v", err)
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ func listBlockedAddress(c *gin.Context) (*model.Value[[]*model.WAF], error) {
|
||||
// @Security BearerAuth
|
||||
// @Schemes
|
||||
// @Description Edit server
|
||||
// @Tags auth required
|
||||
// @Tags admin required
|
||||
// @Accept json
|
||||
// @Param request body []string true "block list"
|
||||
// @Produce json
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/hashicorp/go-uuid"
|
||||
"golang.org/x/sync/singleflight"
|
||||
|
||||
"github.com/nezhahq/nezha/model"
|
||||
@@ -102,13 +103,29 @@ func checkSameOrigin(r *http.Request) bool {
|
||||
// @Success 200 {object} model.StreamServerData
|
||||
// @Router /ws/server [get]
|
||||
func serverStream(c *gin.Context) (any, error) {
|
||||
connId, err := uuid.GenerateUUID()
|
||||
if err != nil {
|
||||
return nil, newWsError("%v", err)
|
||||
}
|
||||
|
||||
conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
|
||||
if err != nil {
|
||||
return nil, newWsError("%v", err)
|
||||
}
|
||||
defer conn.Close()
|
||||
singleton.OnlineUsers.Add(1)
|
||||
defer singleton.OnlineUsers.Add(^uint64(0))
|
||||
|
||||
userIp := c.GetString(model.CtxKeyRealIPStr)
|
||||
if userIp == "" {
|
||||
userIp = c.RemoteIP()
|
||||
}
|
||||
|
||||
singleton.AddOnlineUser(connId, &model.OnlineUser{
|
||||
IP: userIp,
|
||||
ConnectedAt: time.Now(),
|
||||
Conn: conn,
|
||||
})
|
||||
defer singleton.RemoveOnlineUser(connId)
|
||||
|
||||
count := 0
|
||||
for {
|
||||
stat, err := getServerStat(c, count == 0)
|
||||
@@ -166,7 +183,7 @@ func getServerStat(c *gin.Context, withPublicNote bool) ([]byte, error) {
|
||||
|
||||
return utils.Json.Marshal(model.StreamServerData{
|
||||
Now: time.Now().Unix() * 1000,
|
||||
Online: singleton.OnlineUsers.Load(),
|
||||
Online: singleton.GetOnlineUserCount(),
|
||||
Servers: servers,
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user