Merge pull request #17597 from ioito/hotfix/qx-webconsole-command-record

fix(webconsole): ssh command record
This commit is contained in:
Zexi Li
2023-07-24 16:01:19 +08:00
committed by GitHub
3 changed files with 16 additions and 3 deletions
+2 -1
View File
@@ -171,7 +171,8 @@ func handleSshShell(ctx context.Context, w http.ResponseWriter, r *http.Request)
port, _ := env.Body.Int("port")
username, _ := env.Body.GetString("username")
password, _ := env.Body.GetString("password")
s := session.NewSshSession(ctx, env.ClientSessin, ip, port, username, password)
name, _ := env.Body.GetString("name")
s := session.NewSshSession(ctx, env.ClientSessin, name, ip, port, username, password)
handleSshSession(ctx, s, w)
}
+6
View File
@@ -62,6 +62,7 @@ func NewSshServer(s *session.SSession) (*WebsocketServer, error) {
}
type WebSocketBufferWriter struct {
s *session.SSession
ws *websocket.Conn
}
@@ -78,6 +79,9 @@ func (w *WebSocketBufferWriter) Write(p []byte) (int, error) {
}
p = []byte(string(buf))
}
if w.s != nil {
go w.s.GetRecorder().Write("", string(p))
}
err := w.ws.WriteMessage(websocket.BinaryMessage, p)
return len(p), err
}
@@ -131,6 +135,7 @@ func (s *WebsocketServer) initWs(w http.ResponseWriter, r *http.Request) error {
}
wsWriter := WebSocketBufferWriter{
s: s.Session,
ws: s.ws,
}
@@ -209,6 +214,7 @@ func (s *WebsocketServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
data, _ := base64.StdEncoding.DecodeString(input.Data.Data)
input.Data.Data = string(data)
}
go s.Session.GetRecorder().Write(input.Data.Data, "")
_, err = s.StdinPipe.Write([]byte(input.Data.Data))
if err != nil {
log.Errorf("write %s error: %v", input.Data.Data, err)
+8 -2
View File
@@ -39,6 +39,8 @@ type SSshSession struct {
us *mcclient.ClientSession
id string
// vm name
name string
Host string
Port int64
@@ -47,15 +49,19 @@ type SSshSession struct {
Password string
}
func NewSshSession(ctx context.Context, us *mcclient.ClientSession, ip string, port int64, username, password string) *SSshSession {
func NewSshSession(ctx context.Context, us *mcclient.ClientSession, name, ip string, port int64, username, password string) *SSshSession {
ret := &SSshSession{
us: us,
id: stringutils.UUID4(),
Port: port,
Host: ip,
name: name,
Username: username,
Password: password,
}
if len(ret.name) == 0 {
ret.name = ret.Username
}
if port <= 0 {
ret.Port = 22
}
@@ -79,7 +85,7 @@ func (s *SSshSession) GetProtocol() string {
}
func (s *SSshSession) GetRecordObject() *recorder.Object {
return nil
return recorder.NewObject(s.id, s.name, "server", s.Username, jsonutils.Marshal(map[string]interface{}{"ip": s.Host, "port": s.Port}))
}
func (s *SSshSession) GetCommand() *exec.Cmd {