mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-11-13 20:53:49 +08:00
发布v2.8.4版本,更新内容请查看:https://github.com/bufanyun/hotgo/tree/v2.0/docs/guide-zh-CN/addon-version-upgrade.md
This commit is contained in:
@@ -13,7 +13,10 @@ import (
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
"hotgo/api/admin/monitor"
|
||||
"hotgo/internal/consts"
|
||||
"hotgo/internal/dao"
|
||||
"hotgo/internal/model/entity"
|
||||
"hotgo/internal/model/input/form"
|
||||
"hotgo/internal/service"
|
||||
"hotgo/internal/websocket"
|
||||
"hotgo/utility/simple"
|
||||
"hotgo/utility/useragent"
|
||||
@@ -29,8 +32,8 @@ type cMonitor struct {
|
||||
wsManager *websocket.ClientManager
|
||||
}
|
||||
|
||||
// Offline 下线用户
|
||||
func (c *cMonitor) Offline(ctx context.Context, req *monitor.OfflineReq) (res *monitor.OfflineRes, err error) {
|
||||
// UserOffline 下线用户
|
||||
func (c *cMonitor) UserOffline(ctx context.Context, req *monitor.UserOfflineReq) (res *monitor.UserOfflineRes, err error) {
|
||||
client := c.wsManager.GetClient(req.Id)
|
||||
if client == nil {
|
||||
err = gerror.New("客户端已离线")
|
||||
@@ -44,15 +47,10 @@ func (c *cMonitor) Offline(ctx context.Context, req *monitor.OfflineReq) (res *m
|
||||
return
|
||||
}
|
||||
|
||||
// View 获取指定信息
|
||||
func (c *cMonitor) View(ctx context.Context, req *monitor.OnlineViewReq) (res *monitor.OnlineViewRes, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// OnlineList 获取在线列表
|
||||
func (c *cMonitor) OnlineList(ctx context.Context, req *monitor.OnlineListReq) (res *monitor.OnlineListRes, err error) {
|
||||
// UserOnlineList 获取用户在线列表
|
||||
func (c *cMonitor) UserOnlineList(ctx context.Context, req *monitor.UserOnlineListReq) (res *monitor.UserOnlineListRes, err error) {
|
||||
var (
|
||||
clients []*monitor.OnlineModel
|
||||
clients []*monitor.UserOnlineModel
|
||||
i int
|
||||
)
|
||||
|
||||
@@ -69,16 +67,24 @@ func (c *cMonitor) OnlineList(ctx context.Context, req *monitor.OnlineListReq) (
|
||||
continue
|
||||
}
|
||||
|
||||
if req.Username != "" && req.Username != conn.User.Username {
|
||||
continue
|
||||
}
|
||||
|
||||
if req.Addr != "" && !gstr.Contains(conn.Addr, req.Addr) {
|
||||
continue
|
||||
}
|
||||
|
||||
clients = append(clients, &monitor.OnlineModel{
|
||||
if len(req.FirstTime) == 2 && (conn.User.LoginAt.Before(req.FirstTime[0]) || conn.User.LoginAt.After(req.FirstTime[1])) {
|
||||
continue
|
||||
}
|
||||
|
||||
clients = append(clients, &monitor.UserOnlineModel{
|
||||
ID: conn.ID,
|
||||
Addr: conn.Addr,
|
||||
Os: useragent.GetOs(conn.UserAgent),
|
||||
Browser: useragent.GetBrowser(conn.UserAgent),
|
||||
FirstTime: gtime.New(conn.User.LoginAt).Unix(),
|
||||
FirstTime: conn.User.LoginAt.Unix(),
|
||||
HeartbeatTime: conn.HeartbeatTime,
|
||||
App: conn.User.App,
|
||||
UserId: conn.User.Id,
|
||||
@@ -87,14 +93,18 @@ func (c *cMonitor) OnlineList(ctx context.Context, req *monitor.OnlineListReq) (
|
||||
})
|
||||
}
|
||||
|
||||
res = new(monitor.OnlineListRes)
|
||||
res.PageCount = form.CalPageCount(len(clients), req.PerPage)
|
||||
res.Page = req.Page
|
||||
res.PerPage = req.PerPage
|
||||
res = new(monitor.UserOnlineListRes)
|
||||
res.PageRes.Pack(req, len(clients))
|
||||
|
||||
sort.Slice(clients, func(i, j int) bool {
|
||||
if clients[i].FirstTime == clients[j].FirstTime {
|
||||
return clients[i].ID < clients[j].ID
|
||||
}
|
||||
return clients[i].FirstTime < clients[j].FirstTime
|
||||
})
|
||||
|
||||
sort.Sort(monitor.OnlineModels(clients))
|
||||
isDemo := g.Cfg().MustGet(ctx, "hotgo.isDemo", false).Bool()
|
||||
_, perPage, offset := form.CalPage(ctx, req.Page, req.PerPage)
|
||||
_, perPage, offset := form.CalPage(req.Page, req.PerPage)
|
||||
|
||||
for k, v := range clients {
|
||||
if k >= offset && i <= perPage {
|
||||
@@ -107,3 +117,138 @@ func (c *cMonitor) OnlineList(ctx context.Context, req *monitor.OnlineListReq) (
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// NetOnlineList 获取服务在线列表
|
||||
func (c *cMonitor) NetOnlineList(ctx context.Context, req *monitor.NetOnlineListReq) (res *monitor.NetOnlineListRes, err error) {
|
||||
var (
|
||||
clients []*monitor.NetOnlineModel
|
||||
i int
|
||||
cols = dao.SysServeLicense.Columns()
|
||||
serv = service.TCPServer().Instance()
|
||||
models *entity.SysServeLicense
|
||||
)
|
||||
|
||||
conns := serv.GetClients()
|
||||
if len(conns) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
for _, conn := range conns {
|
||||
v := &monitor.NetOnlineModel{
|
||||
AuthMeta: conn.Auth,
|
||||
Id: conn.CID,
|
||||
IsAuth: conn.Auth != nil,
|
||||
Addr: conn.RemoteAddr().String(),
|
||||
Port: gstr.SubStrFromEx(conn.LocalAddr().String(), `:`),
|
||||
FirstTime: conn.FirstTime,
|
||||
HeartbeatTime: conn.Heartbeat,
|
||||
Proto: "TCP",
|
||||
}
|
||||
|
||||
if v.IsAuth {
|
||||
if err = dao.SysServeLicense.Ctx(ctx).Where(cols.Appid, conn.Auth.AppId).Where(cols.Group, conn.Auth.Group).Scan(&models); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if models == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
v.LicenseId = models.Id
|
||||
v.LicenseName = models.Name
|
||||
v.LoginTimes = models.LoginTimes
|
||||
v.Online = serv.GetAppIdOnline(models.Appid)
|
||||
v.OnlineLimit = models.OnlineLimit
|
||||
}
|
||||
|
||||
if req.Addr != "" && !gstr.Contains(v.Addr, req.Addr) {
|
||||
continue
|
||||
}
|
||||
|
||||
if req.Name != "" && !gstr.Contains(v.Name, req.Name) {
|
||||
continue
|
||||
}
|
||||
|
||||
if req.Group != "" && (!v.IsAuth || v.Group != req.Group) {
|
||||
continue
|
||||
}
|
||||
|
||||
if req.AppId != "" && (!v.IsAuth || v.AppId != req.AppId) {
|
||||
continue
|
||||
}
|
||||
|
||||
ft := gtime.New(conn.FirstTime)
|
||||
if len(req.FirstTime) == 2 && (ft.Before(req.FirstTime[0]) || ft.After(req.FirstTime[1])) {
|
||||
continue
|
||||
}
|
||||
clients = append(clients, v)
|
||||
}
|
||||
|
||||
res = new(monitor.NetOnlineListRes)
|
||||
res.PageRes.Pack(req, len(clients))
|
||||
|
||||
sort.Slice(clients, func(i, j int) bool {
|
||||
return clients[i].Id > clients[j].Id
|
||||
})
|
||||
|
||||
_, perPage, offset := form.CalPage(req.Page, req.PerPage)
|
||||
for k, v := range clients {
|
||||
if k >= offset && i <= perPage {
|
||||
res.List = append(res.List, v)
|
||||
i++
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// NetOption 获取服务选项
|
||||
func (c *cMonitor) NetOption(ctx context.Context, req *monitor.NetOptionReq) (res *monitor.NetOptionRes, err error) {
|
||||
res = new(monitor.NetOptionRes)
|
||||
|
||||
// 授权分组
|
||||
for k, v := range consts.LicenseGroupNameMap {
|
||||
res.LicenseGroup = append(res.LicenseGroup, &form.Select{
|
||||
Value: k,
|
||||
Name: v,
|
||||
Label: v,
|
||||
})
|
||||
}
|
||||
sort.Sort(res.LicenseGroup)
|
||||
|
||||
for _, v := range service.TCPServer().Instance().GetRoutes() {
|
||||
// 无需勾选的路由
|
||||
disabled := false
|
||||
if v.Id == "ServerLoginReq" || v.Id == "ServerHeartbeatReq" {
|
||||
disabled = true
|
||||
}
|
||||
|
||||
res.Routes = append(res.Routes, &monitor.RouteSelect{
|
||||
Value: v.Id,
|
||||
Label: v.Id,
|
||||
Disabled: disabled,
|
||||
IsRPC: v.IsRPC,
|
||||
})
|
||||
}
|
||||
|
||||
sort.Slice(res.Routes, func(i, j int) bool {
|
||||
if res.Routes[i].IsRPC && !res.Routes[j].IsRPC {
|
||||
return true
|
||||
} else if !res.Routes[i].IsRPC && res.Routes[j].IsRPC {
|
||||
return false
|
||||
}
|
||||
return res.Routes[i].Label < res.Routes[j].Label
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// NetOffline 下线服务
|
||||
func (c *cMonitor) NetOffline(ctx context.Context, req *monitor.NetOfflineReq) (res *monitor.NetOfflineRes, err error) {
|
||||
conn := service.TCPServer().Instance().GetClientById(req.Id)
|
||||
if conn == nil {
|
||||
err = gerror.New("客户端不在线")
|
||||
return
|
||||
}
|
||||
// 关闭连接
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user