package server import ( "context" "crypto/rand" "encoding/hex" "errors" "fmt" "log" "os/exec" "strings" "sync" "syscall" "time" "unicode/utf8" "git.a-wan.net/alan/pipelines/internal/protocol" ) // terminalStore persists the machine's terminal list; *pipeline.Store // implements it. type terminalStore interface { SaveTerminal(ctx context.Context, terminal protocol.TerminalRecord, keep int) error DeleteTerminal(ctx context.Context, id string) error Terminals(ctx context.Context) ([]protocol.TerminalRecord, error) } // terminalManager owns the machine's terminals (terminal.go in protocol). type terminalManager struct { base context.Context store terminalStore clients *terminalClients // login is the login command of a terminal starting in a directory. login func(dir string) (*exec.Cmd, error) promptWait time.Duration home func() (string, error) now func() time.Time flush time.Duration mu sync.Mutex terms map[string]*terminalProcess wg sync.WaitGroup } func newTerminalManager(base context.Context, store terminalStore) *terminalManager { return &terminalManager{ base: base, store: store, clients: &terminalClients{}, login: terminalLoginCommand, promptWait: terminalPromptWait, home: OperatorHomeDirectory, now: time.Now, flush: 50 * time.Millisecond, terms: map[string]*terminalProcess{}, } } func newTerminalID() (string, error) { var b [8]byte if _, err := rand.Read(b[:]); err != nil { return "", err } return "term_" + hex.EncodeToString(b[:]), nil } // Open starts a terminal in cwd at cols x rows at the machine's login prompt // and returns its record once the prompt shows. clientID (when set) is // subscribed to its output before the login writes. A login that cannot ask // for the password is refused. func (m *terminalManager) Open(ctx context.Context, clientID, cwd string, cols, rows int) (protocol.TerminalRecord, error) { if m.store == nil { return protocol.TerminalRecord{}, errors.New("terminals need the server's store, which is not configured") } if !chatShellTerminals { return protocol.TerminalRecord{}, errors.New(protocol.ChatShellTerminalUnsupported) } home, err := m.home() if err != nil { return protocol.TerminalRecord{}, err } dir, err := resolveTerminalCwd(home, cwd) if err != nil { return protocol.TerminalRecord{}, err } id, err := newTerminalID() if err != nil { return protocol.TerminalRecord{}, err } cmd, err := m.login(dir) if err != nil { return protocol.TerminalRecord{}, err } p, err := m.start(clientID, id, dir, cmd, cols, rows) if err != nil { return protocol.TerminalRecord{}, err } if err := m.awaitPrompt(p); err != nil { m.refuse(p) return protocol.TerminalRecord{}, err } record := p.snapshot(false, 0) if err := m.store.SaveTerminal(ctx, record, protocol.TerminalEndedKept); err != nil { m.refuse(p) return protocol.TerminalRecord{}, fmt.Errorf("could not start the terminal: %v", err) } record = p.snapshot(false, 0) m.publishRecord(record) return record, nil } // start runs cmd in a new terminal's PTY and supervises it; the terminal // counts against the limit from here on. func (m *terminalManager) start(clientID, id, dir string, cmd *exec.Cmd, cols, rows int) (*terminalProcess, error) { m.mu.Lock() defer m.mu.Unlock() if m.base.Err() != nil { return nil, errors.New("the server is stopping") } if running := m.runningLocked(); running >= protocol.TerminalOpenLimit { return nil, fmt.Errorf("this machine already has %d open terminals; close one first", running) } cols, rows = chatShellTerminalSize(cols, rows) shell := strings.Join(cmd.Args[:min(len(cmd.Args), 3)], " ") record := protocol.TerminalRecord{ID: id, Cwd: dir, Shell: shell, CreatedAt: m.now().UnixMilli(), Status: protocol.TerminalRunning, Cols: cols, Rows: rows} p := newTerminalProcess(record) terminal, err := p.startAttachment(cmd) if err != nil { return nil, fmt.Errorf("could not start the terminal: %v", err) } m.terms[id] = p if clientID != "" { m.clients.subscribe(clientID, id) } m.wg.Add(1) go m.supervise(p, cmd, terminal) go p.pumpInput() return p, nil } // awaitPrompt waits for the login's password prompt: a login that ends, // opens a shell without asking, or stays silent is refused. func (m *terminalManager) awaitPrompt(p *terminalProcess) error { timer := time.NewTimer(m.promptWait) defer timer.Stop() for { login, _ := p.loginState() switch { case login.unprompted: return errors.New("su opened a shell without asking for a password (does the account have none?); terminals are refused") case login.prompted: return nil } select { case <-p.loginChanged: case <-p.done: login, output := p.loginState() if login.prompted && !login.unprompted { return nil } return fmt.Errorf("the login prompt (su) ended before asking for a password: %s", terminalLastLine(output)) case <-timer.C: return fmt.Errorf("the login prompt (su) did not ask for a password within %s; terminals are refused", m.promptWait) } } } // refuse ends a terminal that never showed its prompt, without listing it. func (m *terminalManager) refuse(p *terminalProcess) { p.mu.Lock() p.closing, p.refused = true, true p.mu.Unlock() p.signal(syscall.SIGKILL) <-p.done } // terminalLastLine is the last non-empty line of a terminal's output, for a // refusal message. func terminalLastLine(output string) string { lines := strings.Split(strings.ReplaceAll(output, "\r", ""), "\n") for i := len(lines) - 1; i >= 0; i-- { if line := strings.TrimSpace(lines[i]); line != "" { return line } } return "it printed nothing" } func (m *terminalManager) runningLocked() int { running := 0 for _, p := range m.terms { if p.running() { running++ } } return running } // List is every recorded terminal, oldest first, with the live state of the // ones this process holds. func (m *terminalManager) List(ctx context.Context) ([]protocol.TerminalRecord, error) { if m.store == nil { return nil, errors.New("terminals need the server's store, which is not configured") } records, err := m.store.Terminals(ctx) if err != nil { return nil, err } for i, record := range records { if p := m.process(record.ID); p != nil { records[i] = p.snapshot(false, 0) } } return records, nil } func (m *terminalManager) process(id string) *terminalProcess { m.mu.Lock() defer m.mu.Unlock() return m.terms[id] } func (m *terminalManager) live(id string) (*terminalProcess, error) { p := m.process(id) if p == nil || !p.running() { return nil, errors.New("this terminal is not running") } return p, nil } // Input queues data for a running terminal, in arrival order. It is kept // nowhere else: what the shell echoes is output. func (m *terminalManager) Input(id, data string) error { switch { case data == "": return errors.New("input is required") case len(data) > protocol.ChatShellInputLimit: return errors.New("the input exceeds 16 KiB; send it in parts") case !utf8.ValidString(data): return errors.New("input must be UTF-8 text") } p, err := m.live(id) if err != nil { return err } select { case <-p.done: return errors.New("this terminal is not running") case p.input <- []byte(data): return nil default: return errors.New("the terminal is not reading its input; wait for it or press Ctrl-C") } } // Resize sets a running terminal's size and returns its record. func (m *terminalManager) Resize(id string, cols, rows int) (protocol.TerminalRecord, error) { if cols <= 0 || rows <= 0 { return protocol.TerminalRecord{}, errors.New("positive cols and rows are required") } p, err := m.live(id) if err != nil { return protocol.TerminalRecord{}, err } cols, rows = chatShellTerminalSize(cols, rows) p.mu.Lock() p.record.Cols, p.record.Rows = cols, rows terminal := p.pty p.mu.Unlock() if terminal != nil { if err := resizeChatShellTerminal(terminal, cols, rows); err != nil { return protocol.TerminalRecord{}, err } } return p.snapshot(false, 0), nil } // Output is one terminal with its retained output from byte after on. A // terminal ended before this process started has no output. func (m *terminalManager) Output(ctx context.Context, id string, after int64) (protocol.TerminalRecord, error) { if p := m.process(id); p != nil { return p.snapshot(true, after), nil } records, err := m.List(ctx) if err != nil { return protocol.TerminalRecord{}, err } for _, record := range records { if record.ID == id { return record, nil } } return protocol.TerminalRecord{}, errors.New("this machine has no such terminal") } // Close ends a terminal (killing its login and shell) and forgets // it. It waits briefly for the shell to go and returns the closed record. func (m *terminalManager) Close(ctx context.Context, id string) (protocol.TerminalRecord, error) { if m.store == nil { return protocol.TerminalRecord{}, errors.New("terminals need the server's store, which is not configured") } p := m.process(id) if p != nil && p.running() { p.mu.Lock() p.closing = true p.mu.Unlock() p.signal(syscall.SIGHUP) select { case <-p.done: case <-time.After(3 * time.Second): p.signal(syscall.SIGKILL) <-p.done } return p.snapshot(false, 0), nil } record, err := m.Output(ctx, id, 0) if err != nil { return protocol.TerminalRecord{}, err } m.forget(id) record.Output, record.OutputStart = "", 0 record.Status = protocol.TerminalClosed m.publishRecord(record) return record, nil } // forget drops a terminal's record and retained output. func (m *terminalManager) forget(id string) { m.mu.Lock() delete(m.terms, id) m.mu.Unlock() ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() if err := m.store.DeleteTerminal(ctx, id); err != nil { log.Printf("terminal delete %s: %v", id, err) } } func (m *terminalManager) save(record protocol.TerminalRecord) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() if err := m.store.SaveTerminal(ctx, record, protocol.TerminalEndedKept); err != nil { log.Printf("terminal save %s: %v", record.ID, err) } } func (m *terminalManager) publishRecord(record protocol.TerminalRecord) { record.Output, record.OutputStart = "", 0 m.clients.publish(protocol.TerminalEventFrame{TerminalID: record.ID, Terminal: &record}) } // Wait blocks until every supervised terminal has been released (tests, // shutdown). func (m *terminalManager) Wait() { m.wg.Wait() }