Skip to content

Commit

Permalink
fix: handle escape sequences properly (#33)
Browse files Browse the repository at this point in the history
* fix: import

Signed-off-by: Carlos A Becker <[email protected]>

* fix: ctrl+d et al

* wip

Signed-off-by: Carlos A Becker <[email protected]>

* wip

Signed-off-by: Carlos A Becker <[email protected]>

* fix: windows

* fix: simplify

Signed-off-by: Carlos A Becker <[email protected]>

* fix: deps

Signed-off-by: Carlos A Becker <[email protected]>
  • Loading branch information
caarlos0 authored Feb 4, 2022
1 parent b9b12eb commit 1808cc8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion client_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func parsePrivateKey(path string, password []byte) (gossh.AuthMethod, error) {
pwderr := &gossh.PassphraseMissingError{}
if errors.As(err, &pwderr) {
fmt.Printf("Enter the password for %q: ", path)
password, err = term.ReadPassword(int(os.Stdin.Fd()))
password, err := term.ReadPassword(int(os.Stdin.Fd()))
fmt.Println()
if err != nil {
return nil, fmt.Errorf("failed to read password: %q", err)
Expand Down
22 changes: 19 additions & 3 deletions client_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"os/user"
"path/filepath"

gossh "golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
"golang.org/x/term"
)
Expand All @@ -30,7 +30,7 @@ func (c *localClient) Connect(e *Endpoint) error {
if err != nil {
return fmt.Errorf("failed to setup a authentication method: %w", err)
}
conf := &gossh.ClientConfig{
conf := &ssh.ClientConfig{
User: firstNonEmpty(e.User, user.Username),
Auth: methods,
HostKeyCallback: hostKeyCallback(e, filepath.Join(user.HomeDir, ".ssh/known_hosts")),
Expand Down Expand Up @@ -64,8 +64,24 @@ func (c *localClient) Connect(e *Endpoint) error {
}

if e.RequestTTY || e.RemoteCommand == "" {
fd := int(os.Stdout.Fd())
if !term.IsTerminal(fd) {
return fmt.Errorf("requested a TTY, but current session is not TTY, aborting")
}

log.Println("requesting tty")
w, h, err := term.GetSize(int(os.Stdout.Fd()))
originalState, err := term.MakeRaw(fd)
if err != nil {
return fmt.Errorf("failed get terminal state: %w", err)
}

defer func() {
if err := term.Restore(fd, originalState); err != nil {
log.Println("couldn't restore terminal state:", err)
}
}()

w, h, err := term.GetSize(fd)
if err != nil {
return fmt.Errorf("failed to get term size: %w", err)
}
Expand Down
5 changes: 4 additions & 1 deletion client_remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ func (c *remoteClient) Connect(e *Endpoint) error {

if e.RemoteCommand == "" || e.RequestTTY {
log.Println("requesting tty")
pty, winch, _ := c.session.Pty()
pty, winch, ok := c.session.Pty()
if !ok {
return fmt.Errorf("requested a tty, but current session doesn't allow one")
}
w := pty.Window
if err := session.RequestPty(pty.Term, w.Height, w.Width, nil); err != nil {
return fmt.Errorf("failed to request pty: %w", err)
Expand Down

0 comments on commit 1808cc8

Please sign in to comment.