mirror of
				https://github.com/komari-monitor/komari.git
				synced 2025-11-04 05:53:15 +00:00 
			
		
		
		
	feat: 终端添加日志
This commit is contained in:
		@@ -19,9 +19,10 @@ var (
 | 
				
			|||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type TerminalSession struct {
 | 
					type TerminalSession struct {
 | 
				
			||||||
	UUID    string
 | 
						UUID     string
 | 
				
			||||||
	Browser *websocket.Conn
 | 
						UserUUID string
 | 
				
			||||||
	Agent   *websocket.Conn
 | 
						Browser  *websocket.Conn
 | 
				
			||||||
 | 
						Agent    *websocket.Conn
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var TerminalSessionsMutex = &sync.Mutex{}
 | 
					var TerminalSessionsMutex = &sync.Mutex{}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -28,7 +28,7 @@ func AddClient(c *gin.Context) {
 | 
				
			|||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	user_uuid, _ := c.Get("uuid")
 | 
						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": ""})
 | 
						c.JSON(http.StatusOK, gin.H{"status": "success", "uuid": uuid, "token": token, "message": ""})
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -50,7 +50,7 @@ func EditClient(c *gin.Context) {
 | 
				
			|||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	user_uuid, _ := c.Get("uuid")
 | 
						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"})
 | 
						c.JSON(http.StatusOK, gin.H{"status": "success"})
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -65,7 +65,7 @@ func RemoveClient(c *gin.Context) {
 | 
				
			|||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	user_uuid, _ := c.Get("uuid")
 | 
						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"})
 | 
						c.JSON(200, gin.H{"status": "success"})
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -78,7 +78,7 @@ func ClearRecord(c *gin.Context) {
 | 
				
			|||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	user_uuid, _ := c.Get("uuid")
 | 
						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"})
 | 
						c.JSON(200, gin.H{"status": "success"})
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -23,6 +23,6 @@ func OrderWeight(c *gin.Context) {
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	uuid, _ := c.Get("uuid")
 | 
						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)
 | 
						api.RespondSuccess(c, nil)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -62,7 +62,7 @@ func Exec(c *gin.Context) {
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	uuid, _ := c.Get("uuid")
 | 
						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{
 | 
						api.RespondSuccess(c, gin.H{
 | 
				
			||||||
		"task_id": taskId,
 | 
							"task_id": taskId,
 | 
				
			||||||
		"clients": onlineClients,
 | 
							"clients": onlineClients,
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -33,7 +33,7 @@ func DeleteSession(c *gin.Context) {
 | 
				
			|||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	uuid, _ := c.Get("uuid")
 | 
						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)
 | 
						api.RespondSuccess(c, nil)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -45,6 +45,6 @@ func DeleteAllSession(c *gin.Context) {
 | 
				
			|||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	uuid, _ := c.Get("uuid")
 | 
						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)
 | 
						api.RespondSuccess(c, nil)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -44,7 +44,15 @@ func EditSettings(c *gin.Context) {
 | 
				
			|||||||
		api.RespondError(c, 500, "Failed to update settings: "+err.Error())
 | 
							api.RespondError(c, 500, "Failed to update settings: "+err.Error())
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	uuid, _ := c.Get("uuid")
 | 
						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)
 | 
						api.RespondSuccess(c, nil)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -49,7 +49,7 @@ func Login(c *gin.Context) {
 | 
				
			|||||||
			return
 | 
								return
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		c.SetCookie("session_token", session, 2592000, "/", "", false, true)
 | 
							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}})
 | 
							RespondSuccess(c, gin.H{"set-cookie": gin.H{"session_token": session}})
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
@@ -61,5 +61,6 @@ func Logout(c *gin.Context) {
 | 
				
			|||||||
	session, _ := c.Cookie("session_token")
 | 
						session, _ := c.Cookie("session_token")
 | 
				
			||||||
	accounts.DeleteSession(session)
 | 
						accounts.DeleteSession(session)
 | 
				
			||||||
	c.SetCookie("session_token", "", -1, "/", "", false, true)
 | 
						c.SetCookie("session_token", "", -1, "/", "", false, true)
 | 
				
			||||||
 | 
						logOperation.Log(c.ClientIP(), "", "logged out", "logout")
 | 
				
			||||||
	c.Redirect(302, "/")
 | 
						c.Redirect(302, "/")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -68,7 +68,8 @@ func OAuthCallback(c *gin.Context) {
 | 
				
			|||||||
			c.JSON(500, gin.H{"status": "error", "message": "Binding failed"})
 | 
								c.JSON(500, gin.H{"status": "error", "message": "Binding failed"})
 | 
				
			||||||
			return
 | 
								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
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -91,6 +92,6 @@ func OAuthCallback(c *gin.Context) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	// 设置cookie并返回
 | 
						// 设置cookie并返回
 | 
				
			||||||
	c.SetCookie("session_token", session, 2592000, "/", "", false, true)
 | 
						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")
 | 
						c.Redirect(302, "/admin")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -8,12 +8,14 @@ import (
 | 
				
			|||||||
	"github.com/gin-gonic/gin"
 | 
						"github.com/gin-gonic/gin"
 | 
				
			||||||
	"github.com/gorilla/websocket"
 | 
						"github.com/gorilla/websocket"
 | 
				
			||||||
	"github.com/komari-monitor/komari/database/clients"
 | 
						"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/utils"
 | 
				
			||||||
	"github.com/komari-monitor/komari/ws"
 | 
						"github.com/komari-monitor/komari/ws"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func RequestTerminal(c *gin.Context) {
 | 
					func RequestTerminal(c *gin.Context) {
 | 
				
			||||||
	uuid := c.Param("uuid")
 | 
						uuid := c.Param("uuid")
 | 
				
			||||||
 | 
						user_uuid, _ := c.Get("uuid")
 | 
				
			||||||
	_, err := clients.GetClientByUUID(uuid)
 | 
						_, err := clients.GetClientByUUID(uuid)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		c.JSON(400, gin.H{
 | 
							c.JSON(400, gin.H{
 | 
				
			||||||
@@ -39,9 +41,10 @@ func RequestTerminal(c *gin.Context) {
 | 
				
			|||||||
	// 新建一个终端连接
 | 
						// 新建一个终端连接
 | 
				
			||||||
	id := utils.GenerateRandomString(32)
 | 
						id := utils.GenerateRandomString(32)
 | 
				
			||||||
	session := &TerminalSession{
 | 
						session := &TerminalSession{
 | 
				
			||||||
		UUID:    uuid,
 | 
							UserUUID: user_uuid.(string),
 | 
				
			||||||
		Browser: conn,
 | 
							UUID:     uuid,
 | 
				
			||||||
		Agent:   nil,
 | 
							Browser:  conn,
 | 
				
			||||||
 | 
							Agent:    nil,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	TerminalSessionsMutex.Lock()
 | 
						TerminalSessionsMutex.Lock()
 | 
				
			||||||
@@ -92,13 +95,16 @@ func RequestTerminal(c *gin.Context) {
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
		TerminalSessionsMutex.Unlock()
 | 
							TerminalSessionsMutex.Unlock()
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
 | 
						logOperation.Log(c.ClientIP(), user_uuid.(string), "request, terminal id:"+id, "terminal")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func ForwardTerminal(id string) {
 | 
					func ForwardTerminal(id string) {
 | 
				
			||||||
	session, exists := TerminalSessions[id]
 | 
						session, exists := TerminalSessions[id]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if !exists || session == nil || session.Agent == nil || session.Browser == nil {
 | 
						if !exists || session == nil || session.Agent == nil || session.Browser == nil {
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						logOperation.Log(session.UserUUID, session.UUID, "established, terminal id:"+id, "terminal")
 | 
				
			||||||
	errChan := make(chan error, 1)
 | 
						errChan := make(chan error, 1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	go func() {
 | 
						go func() {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user