mirror of
https://github.com/gazer-x/komari.git
synced 2026-06-21 15:55:51 +08:00
refactor: 添加事件触发逻辑以增强客户端和用户操作的跟踪
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package api_rpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/komari-monitor/komari/pkg/rpc"
|
||||
)
|
||||
|
||||
const GroupClient = "client"
|
||||
|
||||
func init() {
|
||||
RegisterWithGroupAndMeta("register", GroupClient, registerClient, &rpc.MethodMeta{
|
||||
Name: "RegisterClient",
|
||||
Description: "Register a new client with the server (Auto discovery).",
|
||||
Params: []rpc.ParamMeta{
|
||||
{
|
||||
Name: "name",
|
||||
Type: "string",
|
||||
Description: "The name of the client to register.",
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func registerClient(ctx context.Context, req *rpc.JsonRpcRequest) (any, *rpc.JsonRpcError) {
|
||||
var params struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
req.BindParams(¶ms)
|
||||
return nil, rpc.MakeError(rpc.InternalError, "Not impl yet", nil)
|
||||
}
|
||||
@@ -4,9 +4,11 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gookit/event"
|
||||
"github.com/komari-monitor/komari/internal/database/auditlog"
|
||||
"github.com/komari-monitor/komari/internal/database/clients"
|
||||
"github.com/komari-monitor/komari/internal/database/records"
|
||||
"github.com/komari-monitor/komari/internal/eventType"
|
||||
"github.com/komari-monitor/komari/internal/ws"
|
||||
)
|
||||
|
||||
@@ -30,6 +32,10 @@ func AddClient(c *gin.Context) {
|
||||
}
|
||||
user_uuid, _ := c.Get("uuid")
|
||||
auditlog.Log(c.ClientIP(), user_uuid.(string), "create client:"+uuid, "info")
|
||||
event.Trigger(eventType.ClientCreated, event.M{
|
||||
"client": uuid,
|
||||
"token": token,
|
||||
})
|
||||
c.JSON(http.StatusOK, gin.H{"status": "success", "uuid": uuid, "token": token, "message": ""})
|
||||
}
|
||||
|
||||
@@ -52,6 +58,10 @@ func EditClient(c *gin.Context) {
|
||||
}
|
||||
user_uuid, _ := c.Get("uuid")
|
||||
auditlog.Log(c.ClientIP(), user_uuid.(string), "edit client:"+uuid, "info")
|
||||
event.Trigger(eventType.ClientUpdated, event.M{
|
||||
"client": uuid,
|
||||
"data": req,
|
||||
})
|
||||
c.JSON(http.StatusOK, gin.H{"status": "success"})
|
||||
}
|
||||
|
||||
@@ -70,6 +80,9 @@ func RemoveClient(c *gin.Context) {
|
||||
c.JSON(200, gin.H{"status": "success"})
|
||||
ws.DeleteConnectedClients(uuid)
|
||||
ws.DeleteLatestReport(uuid)
|
||||
event.Trigger(eventType.ClientDeleted, event.M{
|
||||
"client": uuid,
|
||||
})
|
||||
}
|
||||
|
||||
func ClearRecord(c *gin.Context) {
|
||||
|
||||
@@ -2,9 +2,11 @@ package client
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gookit/event"
|
||||
api "github.com/komari-monitor/komari/internal/api_v1"
|
||||
"github.com/komari-monitor/komari/internal/conf"
|
||||
"github.com/komari-monitor/komari/internal/database/clients"
|
||||
"github.com/komari-monitor/komari/internal/eventType"
|
||||
"github.com/komari-monitor/komari/pkg/utils"
|
||||
)
|
||||
|
||||
@@ -37,5 +39,9 @@ func RegisterClient(c *gin.Context) {
|
||||
api.RespondError(c, 500, "Failed to create client: "+err.Error())
|
||||
return
|
||||
}
|
||||
event.Trigger(eventType.ClientCreated, event.M{
|
||||
"client": uuid,
|
||||
"token": token,
|
||||
})
|
||||
api.RespondSuccess(c, gin.H{"uuid": uuid, "token": token})
|
||||
}
|
||||
|
||||
@@ -11,12 +11,14 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gookit/event"
|
||||
"github.com/gorilla/websocket"
|
||||
api "github.com/komari-monitor/komari/internal/api_v1"
|
||||
"github.com/komari-monitor/komari/internal/common"
|
||||
"github.com/komari-monitor/komari/internal/database/clients"
|
||||
"github.com/komari-monitor/komari/internal/database/models"
|
||||
"github.com/komari-monitor/komari/internal/database/tasks"
|
||||
"github.com/komari-monitor/komari/internal/eventType"
|
||||
"github.com/komari-monitor/komari/internal/notifier"
|
||||
"github.com/komari-monitor/komari/internal/ws"
|
||||
"github.com/patrickmn/go-cache"
|
||||
@@ -123,13 +125,18 @@ func WebSocketReport(c *gin.Context) {
|
||||
go oldConn.Close()
|
||||
}
|
||||
ws.SetConnectedClients(uuid, conn)
|
||||
log.Printf("Client %s is reconnect success, connID: %d", uuid, conn.ID)
|
||||
//log.Printf("Client %s is reconnect success, connID: %d", uuid, conn.ID)
|
||||
go notifier.OnlineNotification(uuid, conn.ID)
|
||||
defer func() {
|
||||
ws.DeleteClientConditionally(uuid, conn)
|
||||
notifier.OfflineNotification(uuid, conn.ID)
|
||||
}()
|
||||
|
||||
event.Trigger(eventType.ClientWebsocketConnected, event.M{
|
||||
"client": uuid,
|
||||
"online": len(ws.GetConnectedClients()),
|
||||
"connID": conn.ID,
|
||||
})
|
||||
// 首先处理第一次ws conn收到的消息
|
||||
processMessage(conn, message, uuid)
|
||||
|
||||
@@ -143,8 +150,18 @@ func WebSocketReport(c *gin.Context) {
|
||||
}
|
||||
break // 任何读错误(包括超时)都意味着连接已断开,退出循环
|
||||
}
|
||||
event.Trigger(eventType.ClientMessageReceived, event.M{
|
||||
"client": uuid,
|
||||
"type": "websocket",
|
||||
"message": string(message),
|
||||
})
|
||||
processMessage(conn, message, uuid)
|
||||
}
|
||||
event.Trigger(eventType.ClientWebsocketDisconnected, event.M{
|
||||
"client": uuid,
|
||||
"online": len(ws.GetConnectedClients()),
|
||||
"connID": conn.ID,
|
||||
})
|
||||
}
|
||||
|
||||
// 将消息处理逻辑提取到一个函数中,方便复用
|
||||
|
||||
@@ -4,9 +4,11 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gookit/event"
|
||||
"github.com/komari-monitor/komari/internal/database/clients"
|
||||
"github.com/komari-monitor/komari/internal/database/models"
|
||||
"github.com/komari-monitor/komari/internal/database/tasks"
|
||||
"github.com/komari-monitor/komari/internal/eventType"
|
||||
)
|
||||
|
||||
func TaskResult(c *gin.Context) {
|
||||
@@ -33,4 +35,9 @@ func TaskResult(c *gin.Context) {
|
||||
}
|
||||
|
||||
c.JSON(200, gin.H{"status": "success", "message": "Task result updated successfully"})
|
||||
event.Trigger(eventType.TaskUpdated, event.M{
|
||||
"task": req.TaskId,
|
||||
"client": clientId,
|
||||
"data": req,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -3,8 +3,10 @@ package client
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/gookit/event"
|
||||
"github.com/komari-monitor/komari/internal/conf"
|
||||
"github.com/komari-monitor/komari/internal/database/clients"
|
||||
"github.com/komari-monitor/komari/internal/eventType"
|
||||
"github.com/komari-monitor/komari/internal/geoip"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -80,6 +82,11 @@ func UploadBasicInfo(c *gin.Context) {
|
||||
c.JSON(500, gin.H{"status": "error", "error": err})
|
||||
return
|
||||
}
|
||||
|
||||
event.Trigger(eventType.ClientMessageReceived, event.M{
|
||||
"client": cbi["uuid"],
|
||||
"type": "basic_info",
|
||||
"data": cbi,
|
||||
})
|
||||
c.JSON(200, gin.H{"status": "success"})
|
||||
|
||||
}
|
||||
|
||||
@@ -93,5 +93,13 @@ func Logout(c *gin.Context) {
|
||||
accounts.DeleteSession(session)
|
||||
c.SetCookie("session_token", "", -1, "/", "", false, true)
|
||||
auditlog.Log(c.ClientIP(), "", "logged out", "logout")
|
||||
event.Trigger(eventType.UserLogout, event.M{
|
||||
"ip": c.ClientIP(),
|
||||
"ua": c.Request.UserAgent(),
|
||||
"header": c.Request.Header,
|
||||
"referrer": c.Request.Referer(),
|
||||
"host": c.Request.Host,
|
||||
})
|
||||
c.Redirect(302, "/")
|
||||
|
||||
}
|
||||
|
||||
@@ -88,7 +88,12 @@ func OAuthCallback(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
auditlog.Log(c.ClientIP(), user.UUID, "bound external account (OAuth)"+fmt.Sprintf(",sso_id: %s", sso_id), "login")
|
||||
event.Trigger(eventType.UserOidcBound, event.M{
|
||||
"user": user.UUID,
|
||||
"sso_id": sso_id,
|
||||
})
|
||||
c.Redirect(302, "/admin")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package client
|
||||
|
||||
import "fmt"
|
||||
|
||||
func Create(name string) error {
|
||||
return fmt.Errorf("not implemented")
|
||||
}
|
||||
@@ -3,8 +3,10 @@ package accounts
|
||||
import (
|
||||
"image"
|
||||
|
||||
"github.com/gookit/event"
|
||||
"github.com/komari-monitor/komari/internal/database/dbcore"
|
||||
"github.com/komari-monitor/komari/internal/database/models"
|
||||
"github.com/komari-monitor/komari/internal/eventType"
|
||||
"github.com/pquerna/otp/totp"
|
||||
)
|
||||
|
||||
@@ -29,7 +31,14 @@ func Generate2Fa() (string, image.Image, error) {
|
||||
|
||||
func Enable2Fa(uuid, secret string) error {
|
||||
db := dbcore.GetDBInstance()
|
||||
return db.Model(&models.User{}).Where("uuid = ?", uuid).Update("two_factor", secret).Error
|
||||
err := db.Model(&models.User{}).Where("uuid = ?", uuid).Update("two_factor", secret).Error
|
||||
if err != nil {
|
||||
event.Trigger(eventType.UserTwoFaAdded, event.M{
|
||||
"user": uuid,
|
||||
})
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func Verify2Fa(uuid, code string) (bool, error) {
|
||||
@@ -54,5 +63,12 @@ func Verify2Fa(uuid, code string) (bool, error) {
|
||||
|
||||
func Disable2Fa(uuid string) error {
|
||||
db := dbcore.GetDBInstance()
|
||||
return db.Model(&models.User{}).Where("uuid = ?", uuid).Update("two_factor", "").Error
|
||||
err := db.Model(&models.User{}).Where("uuid = ?", uuid).Update("two_factor", "").Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
event.Trigger(eventType.UserTwoFaRemoved, event.M{
|
||||
"user": uuid,
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -7,8 +7,10 @@ import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/gookit/event"
|
||||
"github.com/komari-monitor/komari/internal/database/dbcore"
|
||||
"github.com/komari-monitor/komari/internal/database/models"
|
||||
"github.com/komari-monitor/komari/internal/eventType"
|
||||
"github.com/komari-monitor/komari/pkg/utils"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@@ -179,5 +181,18 @@ func UpdateUser(uuid string, name, password, sso_type *string) error {
|
||||
if password != nil {
|
||||
DeleteAllSessions()
|
||||
}
|
||||
if name != nil {
|
||||
event.Trigger(eventType.UserUpdateUsername, event.M{
|
||||
"user": uuid,
|
||||
})
|
||||
} else if password != nil {
|
||||
event.Trigger(eventType.UserUpdatePassword, event.M{
|
||||
"user": uuid,
|
||||
})
|
||||
} else if sso_type != nil && *sso_type == "" {
|
||||
event.Trigger(eventType.UserOidcUnbound, event.M{
|
||||
"user": uuid,
|
||||
})
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -6,10 +6,12 @@ import (
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/gookit/event"
|
||||
"github.com/komari-monitor/komari/internal/conf"
|
||||
"github.com/komari-monitor/komari/internal/database/dbcore"
|
||||
"github.com/komari-monitor/komari/internal/database/models"
|
||||
messageevent "github.com/komari-monitor/komari/internal/database/models/messageEvent"
|
||||
"github.com/komari-monitor/komari/internal/eventType"
|
||||
"github.com/komari-monitor/komari/internal/geoip"
|
||||
"github.com/komari-monitor/komari/internal/messageSender"
|
||||
"github.com/komari-monitor/komari/pkg/utils"
|
||||
@@ -97,6 +99,9 @@ func DeleteSession(session string) (err error) {
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
event.Trigger(eventType.UserSessionRevoked, event.M{
|
||||
"session": session,
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -3,8 +3,10 @@ package tasks
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/gookit/event"
|
||||
"github.com/komari-monitor/komari/internal/database/dbcore"
|
||||
"github.com/komari-monitor/komari/internal/database/models"
|
||||
"github.com/komari-monitor/komari/internal/eventType"
|
||||
)
|
||||
|
||||
func CreateTask(taskId string, clients []string, command string) error {
|
||||
@@ -32,6 +34,11 @@ func CreateTask(taskId string, clients []string, command string) error {
|
||||
if len(taskResults) > 0 {
|
||||
return db.Create(&taskResults).Error
|
||||
}
|
||||
event.Trigger(eventType.TaskCreated, event.M{
|
||||
"task": taskId,
|
||||
"clients": clients,
|
||||
"command": command,
|
||||
})
|
||||
return nil
|
||||
}
|
||||
func GetTaskByTaskId(taskId string) (*models.Task, error) {
|
||||
|
||||
@@ -4,10 +4,12 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/gookit/event"
|
||||
"github.com/komari-monitor/komari/internal/database/auditlog"
|
||||
"github.com/komari-monitor/komari/internal/database/clients"
|
||||
"github.com/komari-monitor/komari/internal/database/models"
|
||||
messageevent "github.com/komari-monitor/komari/internal/database/models/messageEvent"
|
||||
"github.com/komari-monitor/komari/internal/eventType"
|
||||
"github.com/komari-monitor/komari/internal/messageSender"
|
||||
"github.com/komari-monitor/komari/internal/ws"
|
||||
)
|
||||
@@ -112,6 +114,10 @@ func CheckAndAutoRenewal(client models.Client) {
|
||||
Emoji: "🔄",
|
||||
Message: fmt.Sprintf("• %s until %s\n", client.Name, newExpireTime.Format("2006-01-02")),
|
||||
})
|
||||
event.Trigger(eventType.ClientRenewed, event.M{
|
||||
"client": client,
|
||||
"new_expire_at": newExpireTime,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user