From c0a2f32bd48f5b61595f3e4b81ed8bf0293c03f0 Mon Sep 17 00:00:00 2001 From: Chris Miller Date: Fri, 10 Jan 2020 00:37:51 +0000 Subject: [PATCH] Wait for shell --- sshd/terminal.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/sshd/terminal.go b/sshd/terminal.go index a7d2e82..74c9194 100644 --- a/sshd/terminal.go +++ b/sshd/terminal.go @@ -108,7 +108,8 @@ func NewTerminal(conn *ssh.ServerConn, ch ssh.NewChannel) (*Terminal, error) { done: make(chan struct{}), } - go term.listen(requests) + ready := make(chan struct{}) + go term.listen(requests, ready) go func() { // Keep-Alive Ticker @@ -129,6 +130,14 @@ func NewTerminal(conn *ssh.ServerConn, ch ssh.NewChannel) (*Terminal, error) { } }() + select { + case <-ready: // ok... + case <-term.done: + return nil, errors.New("terminal aborted") + case <-time.NewTimer(time.Minute).C: + return nil, errors.New("timed out starting terminal") + } + return &term, nil } @@ -160,7 +169,8 @@ func (t *Terminal) Close() error { } // Negotiate terminal type and settings -func (t *Terminal) listen(requests <-chan *ssh.Request) { +// ready is closed when the terminal is ready. +func (t *Terminal) listen(requests <-chan *ssh.Request, ready chan<- struct{}) { hasShell := false for req := range requests { @@ -172,6 +182,7 @@ func (t *Terminal) listen(requests <-chan *ssh.Request) { if !hasShell { ok = true hasShell = true + close(ready) } case "pty-req": width, height, ok = parsePtyRequest(req.Payload)