improved rev console

This commit is contained in:
KingKDot
2026-05-13 22:32:49 -05:00
parent 27225566f4
commit 6ae44625f8
17 changed files with 446 additions and 471 deletions
+21 -37
View File
@@ -5,14 +5,10 @@ import (
"fmt"
"io"
"os"
"os/exec"
"runtime"
"sync"
"syscall"
"overlord-client/cmd/agent/wire"
"github.com/creack/pty"
)
type ConsoleHub struct {
@@ -23,8 +19,7 @@ type ConsoleHub struct {
type ConsoleSession struct {
id string
cmd *exec.Cmd
pty *os.File
pty ptyHandle
cancel context.CancelFunc
once sync.Once
}
@@ -35,6 +30,12 @@ type ConsoleStartRequest struct {
Rows int
}
type ptyHandle interface {
io.ReadWriteCloser
Resize(cols, rows uint16) error
Wait() (int, error)
}
func NewConsoleHub(env *Env) *ConsoleHub {
return &ConsoleHub{env: env, sessions: make(map[string]*ConsoleSession)}
}
@@ -62,22 +63,19 @@ func (h *ConsoleHub) Start(ctx context.Context, req ConsoleStartRequest) error {
shell := detectShell()
sessionCtx, cancel := context.WithCancel(ctx)
cmd := exec.CommandContext(sessionCtx, shell[0], shell[1:]...)
cmd.Env = os.Environ()
ptmx, err := pty.StartWithSize(cmd, &pty.Winsize{Cols: cols, Rows: rows})
handle, err := startPty(sessionCtx, shell, cols, rows)
if err != nil {
cancel()
h.emitError(ctx, req.SessionID, err)
h.emitError(req.SessionID, err)
return err
}
sess := &ConsoleSession{id: req.SessionID, cmd: cmd, pty: ptmx, cancel: cancel}
sess := &ConsoleSession{id: req.SessionID, pty: handle, cancel: cancel}
h.mu.Lock()
h.sessions[req.SessionID] = sess
h.mu.Unlock()
go h.forwardOutput(sessionCtx, sess)
go h.forwardOutput(sess)
return nil
}
@@ -101,7 +99,7 @@ func (h *ConsoleHub) Resize(sessionID string, cols, rows int) error {
if rows <= 0 {
rows = 36
}
return pty.Setsize(sess.pty, &pty.Winsize{Cols: uint16(cols), Rows: uint16(rows)})
return sess.pty.Resize(uint16(cols), uint16(rows))
}
func (h *ConsoleHub) Stop(sessionID string) {
@@ -129,28 +127,27 @@ func (h *ConsoleHub) StopAll() {
}
}
func (h *ConsoleHub) forwardOutput(ctx context.Context, sess *ConsoleSession) {
func (h *ConsoleHub) forwardOutput(sess *ConsoleSession) {
writeCtx := context.Background()
buf := make([]byte, 4096)
for {
n, err := sess.pty.Read(buf)
if n > 0 {
chunk := make([]byte, n)
copy(chunk, buf[:n])
_ = wire.WriteMsg(ctx, h.env.Conn, wire.ConsoleOutput{Type: "console_output", SessionID: sess.id, Data: chunk})
_ = wire.WriteMsg(writeCtx, h.env.Conn, wire.ConsoleOutput{Type: "console_output", SessionID: sess.id, Data: chunk})
}
if err != nil {
if err != io.EOF {
h.emitError(ctx, sess.id, err)
h.emitError(sess.id, err)
}
break
}
}
if sess.cmd != nil {
_ = sess.cmd.Wait()
}
exitCode := extractExitCode(sess.cmd)
_ = wire.WriteMsg(ctx, h.env.Conn, wire.ConsoleOutput{Type: "console_output", SessionID: sess.id, ExitCode: &exitCode})
exitCode, _ := sess.pty.Wait()
_ = wire.WriteMsg(writeCtx, h.env.Conn, wire.ConsoleOutput{Type: "console_output", SessionID: sess.id, ExitCode: &exitCode})
h.mu.Lock()
delete(h.sessions, sess.id)
@@ -158,8 +155,8 @@ func (h *ConsoleHub) forwardOutput(ctx context.Context, sess *ConsoleSession) {
sess.close()
}
func (h *ConsoleHub) emitError(ctx context.Context, sessionID string, err error) {
_ = wire.WriteMsg(ctx, h.env.Conn, wire.ConsoleOutput{Type: "console_output", SessionID: sessionID, Error: err.Error()})
func (h *ConsoleHub) emitError(sessionID string, err error) {
_ = wire.WriteMsg(context.Background(), h.env.Conn, wire.ConsoleOutput{Type: "console_output", SessionID: sessionID, Error: err.Error()})
}
func (h *ConsoleHub) get(sessionID string) *ConsoleSession {
@@ -173,9 +170,6 @@ func (sess *ConsoleSession) close() {
if sess.cancel != nil {
sess.cancel()
}
if sess.cmd != nil && sess.cmd.Process != nil {
_ = sess.cmd.Process.Kill()
}
if sess.pty != nil {
_ = sess.pty.Close()
}
@@ -197,13 +191,3 @@ func detectShell() []string {
}
return []string{"/bin/bash", "-l"}
}
func extractExitCode(cmd *exec.Cmd) int {
if cmd == nil || cmd.ProcessState == nil {
return -1
}
if status, ok := cmd.ProcessState.Sys().(syscall.WaitStatus); ok {
return status.ExitStatus()
}
return -1
}
@@ -0,0 +1,59 @@
//go:build !windows
package runtime
import (
"context"
"os"
"os/exec"
"syscall"
"github.com/creack/pty"
)
type unixPty struct {
f *os.File
cmd *exec.Cmd
}
func (u *unixPty) Read(p []byte) (int, error) { return u.f.Read(p) }
func (u *unixPty) Write(p []byte) (int, error) { return u.f.Write(p) }
func (u *unixPty) Close() error {
if u.cmd != nil && u.cmd.Process != nil {
_ = u.cmd.Process.Kill()
}
return u.f.Close()
}
func (u *unixPty) Resize(cols, rows uint16) error {
return pty.Setsize(u.f, &pty.Winsize{Cols: cols, Rows: rows})
}
func (u *unixPty) Wait() (int, error) {
if u.cmd == nil {
return -1, nil
}
err := u.cmd.Wait()
if err != nil {
if ee, ok := err.(*exec.ExitError); ok {
if status, ok := ee.Sys().(syscall.WaitStatus); ok {
return status.ExitStatus(), nil
}
return ee.ExitCode(), nil
}
return -1, err
}
return 0, nil
}
func startPty(ctx context.Context, shell []string, cols, rows uint16) (ptyHandle, error) {
cmd := exec.CommandContext(ctx, shell[0], shell[1:]...)
cmd.Env = append(os.Environ(), "TERM=xterm-256color")
f, err := pty.StartWithSize(cmd, &pty.Winsize{Cols: cols, Rows: rows})
if err != nil {
return nil, err
}
return &unixPty{f: f, cmd: cmd}, nil
}
@@ -0,0 +1,49 @@
//go:build windows
package runtime
import (
"context"
"strings"
"github.com/UserExistsError/conpty"
)
type winPty struct {
cp *conpty.ConPty
waitCh chan waitResult
}
type waitResult struct {
code int
err error
}
func (w *winPty) Read(p []byte) (int, error) { return w.cp.Read(p) }
func (w *winPty) Write(p []byte) (int, error) { return w.cp.Write(p) }
func (w *winPty) Close() error { return w.cp.Close() }
func (w *winPty) Resize(cols, rows uint16) error {
return w.cp.Resize(int(cols), int(rows))
}
func (w *winPty) Wait() (int, error) {
res := <-w.waitCh
return res.code, res.err
}
func startPty(ctx context.Context, shell []string, cols, rows uint16) (ptyHandle, error) {
cmdline := strings.Join(shell, " ")
cp, err := conpty.Start(cmdline, conpty.ConPtyDimensions(int(cols), int(rows)))
if err != nil {
return nil, err
}
w := &winPty{cp: cp, waitCh: make(chan waitResult, 1)}
go func() {
code, werr := cp.Wait(context.Background())
w.waitCh <- waitResult{code: int(code), err: werr}
}()
return w, nil
}