mirror of
https://github.com/komari-monitor/komari.git
synced 2025-11-03 13:33:14 +00:00
feat: 终端添加日志
This commit is contained in:
@@ -20,6 +20,7 @@ var (
|
||||
|
||||
type TerminalSession struct {
|
||||
UUID string
|
||||
UserUUID string
|
||||
Browser *websocket.Conn
|
||||
Agent *websocket.Conn
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ func AddClient(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
user_uuid, _ := c.Get("uuid")
|
||||
logOperation.Log(c.ClientIP(), user_uuid.(string), "Client created", "info")
|
||||
logOperation.Log(c.ClientIP(), user_uuid.(string), "create client:"+uuid, "info")
|
||||
c.JSON(http.StatusOK, gin.H{"status": "success", "uuid": uuid, "token": token, "message": ""})
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ func EditClient(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
user_uuid, _ := c.Get("uuid")
|
||||
logOperation.Log(c.ClientIP(), user_uuid.(string), "Client edited:"+uuid, "info")
|
||||
logOperation.Log(c.ClientIP(), user_uuid.(string), "edit client:"+uuid, "info")
|
||||
c.JSON(http.StatusOK, gin.H{"status": "success"})
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ func RemoveClient(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
user_uuid, _ := c.Get("uuid")
|
||||
logOperation.Log(c.ClientIP(), user_uuid.(string), "Client deleted:"+uuid, "warn")
|
||||
logOperation.Log(c.ClientIP(), user_uuid.(string), "delete client:"+uuid, "warn")
|
||||
c.JSON(200, gin.H{"status": "success"})
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ func ClearRecord(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
user_uuid, _ := c.Get("uuid")
|
||||
logOperation.Log(c.ClientIP(), user_uuid.(string), "All records cleared", "warn")
|
||||
logOperation.Log(c.ClientIP(), user_uuid.(string), "clear records", "warn")
|
||||
c.JSON(200, gin.H{"status": "success"})
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,6 @@ func OrderWeight(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
uuid, _ := c.Get("uuid")
|
||||
logOperation.Log(c.ClientIP(), uuid.(string), "Client weights updated", "info")
|
||||
logOperation.Log(c.ClientIP(), uuid.(string), "order clients", "info")
|
||||
api.RespondSuccess(c, nil)
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@ func Exec(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
uuid, _ := c.Get("uuid")
|
||||
logOperation.Log(c.ClientIP(), uuid.(string), "Command executed, task id: "+taskId, "warn")
|
||||
logOperation.Log(c.ClientIP(), uuid.(string), "REC, task id: "+taskId, "warn")
|
||||
api.RespondSuccess(c, gin.H{
|
||||
"task_id": taskId,
|
||||
"clients": onlineClients,
|
||||
|
||||
@@ -33,7 +33,7 @@ func DeleteSession(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
uuid, _ := c.Get("uuid")
|
||||
logOperation.Log(c.ClientIP(), uuid.(string), "Session deleted", "info")
|
||||
logOperation.Log(c.ClientIP(), uuid.(string), "delete session", "info")
|
||||
api.RespondSuccess(c, nil)
|
||||
}
|
||||
|
||||
@@ -45,6 +45,6 @@ func DeleteAllSession(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
uuid, _ := c.Get("uuid")
|
||||
logOperation.Log(c.ClientIP(), uuid.(string), "All sessions deleted", "info")
|
||||
logOperation.Log(c.ClientIP(), uuid.(string), "delete all sessions", "warn")
|
||||
api.RespondSuccess(c, nil)
|
||||
}
|
||||
|
||||
@@ -44,7 +44,15 @@ func EditSettings(c *gin.Context) {
|
||||
api.RespondError(c, 500, "Failed to update settings: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
uuid, _ := c.Get("uuid")
|
||||
logOperation.Log(c.ClientIP(), uuid.(string), "Settings updated", "info")
|
||||
message := "update settings: "
|
||||
for key := range cfg {
|
||||
message += key + ", "
|
||||
}
|
||||
if len(message) > 2 {
|
||||
message = message[:len(message)-2]
|
||||
}
|
||||
logOperation.Log(c.ClientIP(), uuid.(string), message, "info")
|
||||
api.RespondSuccess(c, nil)
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ func Login(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
c.SetCookie("session_token", session, 2592000, "/", "", false, true)
|
||||
logOperation.Log(c.ClientIP(), uuid, "User logged in(password)", "login")
|
||||
logOperation.Log(c.ClientIP(), uuid, "logged in (password)", "login")
|
||||
RespondSuccess(c, gin.H{"set-cookie": gin.H{"session_token": session}})
|
||||
return
|
||||
} else {
|
||||
@@ -61,5 +61,6 @@ func Logout(c *gin.Context) {
|
||||
session, _ := c.Cookie("session_token")
|
||||
accounts.DeleteSession(session)
|
||||
c.SetCookie("session_token", "", -1, "/", "", false, true)
|
||||
logOperation.Log(c.ClientIP(), "", "logged out", "logout")
|
||||
c.Redirect(302, "/")
|
||||
}
|
||||
|
||||
@@ -68,7 +68,8 @@ func OAuthCallback(c *gin.Context) {
|
||||
c.JSON(500, gin.H{"status": "error", "message": "Binding failed"})
|
||||
return
|
||||
}
|
||||
c.Redirect(302, "/")
|
||||
logOperation.Log(c.ClientIP(), user.UUID, "bound external account (OAuth)"+fmt.Sprintf(",sso_id: %s", sso_id), "login")
|
||||
c.Redirect(302, "/admin")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -91,6 +92,6 @@ func OAuthCallback(c *gin.Context) {
|
||||
|
||||
// 设置cookie并返回
|
||||
c.SetCookie("session_token", session, 2592000, "/", "", false, true)
|
||||
logOperation.Log(c.ClientIP(), user.UUID, "User logged in (OAuth)", "login")
|
||||
logOperation.Log(c.ClientIP(), user.UUID, "logged in (OAuth)", "login")
|
||||
c.Redirect(302, "/admin")
|
||||
}
|
||||
|
||||
@@ -8,12 +8,14 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/komari-monitor/komari/database/clients"
|
||||
"github.com/komari-monitor/komari/database/logOperation"
|
||||
"github.com/komari-monitor/komari/utils"
|
||||
"github.com/komari-monitor/komari/ws"
|
||||
)
|
||||
|
||||
func RequestTerminal(c *gin.Context) {
|
||||
uuid := c.Param("uuid")
|
||||
user_uuid, _ := c.Get("uuid")
|
||||
_, err := clients.GetClientByUUID(uuid)
|
||||
if err != nil {
|
||||
c.JSON(400, gin.H{
|
||||
@@ -39,6 +41,7 @@ func RequestTerminal(c *gin.Context) {
|
||||
// 新建一个终端连接
|
||||
id := utils.GenerateRandomString(32)
|
||||
session := &TerminalSession{
|
||||
UserUUID: user_uuid.(string),
|
||||
UUID: uuid,
|
||||
Browser: conn,
|
||||
Agent: nil,
|
||||
@@ -92,13 +95,16 @@ func RequestTerminal(c *gin.Context) {
|
||||
}
|
||||
TerminalSessionsMutex.Unlock()
|
||||
})
|
||||
logOperation.Log(c.ClientIP(), user_uuid.(string), "request, terminal id:"+id, "terminal")
|
||||
}
|
||||
|
||||
func ForwardTerminal(id string) {
|
||||
session, exists := TerminalSessions[id]
|
||||
|
||||
if !exists || session == nil || session.Agent == nil || session.Browser == nil {
|
||||
return
|
||||
}
|
||||
logOperation.Log(session.UserUUID, session.UUID, "established, terminal id:"+id, "terminal")
|
||||
errChan := make(chan error, 1)
|
||||
|
||||
go func() {
|
||||
|
||||
Reference in New Issue
Block a user