Read handles pty->process messages (stdin, resize) Called in a loop from remotecommand as long as the process is running
(p []byte)
| 71 | // Read handles pty->process messages (stdin, resize) |
| 72 | // Called in a loop from remotecommand as long as the process is running |
| 73 | func (t TerminalSession) Read(p []byte) (int, error) { |
| 74 | session := TerminalSessions.Get(t.Id) |
| 75 | if session.TimeOut.Before(time.Now()) { |
| 76 | _ = TerminalSessions.Sessions[session.Id].sockJSSession.Close(2, "the connection has been disconnected. Please reconnect") |
| 77 | return 0, errors.New("the connection has been disconnected. Please reconnect") |
| 78 | } |
| 79 | TerminalSessions.Set(session.Id, session) |
| 80 | m, err := session.sockJSSession.Recv() |
| 81 | if err != nil { |
| 82 | // Send terminated signal to process to avoid resource leak |
| 83 | return copy(p, END_OF_TRANSMISSION), err |
| 84 | } |
| 85 | |
| 86 | var msg TerminalMessage |
| 87 | if err := json.Unmarshal([]byte(m), &msg); err != nil { |
| 88 | return copy(p, END_OF_TRANSMISSION), err |
| 89 | } |
| 90 | |
| 91 | switch msg.Op { |
| 92 | case "stdin": |
| 93 | return copy(p, msg.Data), nil |
| 94 | case "resize": |
| 95 | session.SizeChan <- remotecommand.TerminalSize{Width: msg.Cols, Height: msg.Rows} |
| 96 | return 0, nil |
| 97 | default: |
| 98 | return copy(p, END_OF_TRANSMISSION), fmt.Errorf("unknown message type '%s'", msg.Op) |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | // Write handles process->pty stdout |
| 103 | // Called from remotecommand whenever there is any output |
no test coverage detected