Skip to content

Commit

Permalink
window size fix
Browse files Browse the repository at this point in the history
  • Loading branch information
wardviaene committed Aug 25, 2018
1 parent e24f0ef commit a1c96c9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
11 changes: 8 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ func main() {
log.Panicln(err)
}
if e.ipAddr != nil {
err = startSSH()
err = startSSH(*e.ipAddr, *e.keyName)
if err != nil {
fmt.Printf("Error: %v\n\n", err)
}
Expand Down
32 changes: 16 additions & 16 deletions ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package main

import (
"encoding/binary"
"github.com/docker/docker/pkg/term"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
"golang.org/x/crypto/ssh/terminal"

"fmt"
"io/ioutil"
Expand All @@ -14,21 +14,21 @@ import (
"syscall"
)

func startSSH() error {
func startSSH(ipAddr, keyName string) error {
width := 80
height := 24
sshConfig := &ssh.ClientConfig{
User: "ec2-user",
Auth: []ssh.AuthMethod{
SSHAgent(),
PublicKeyFile("/keys/" + *e.keyName),
PublicKeyFile("/keys/" + keyName),
},
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
return nil
},
}
fmt.Printf("Opening connection to %v:22 with key %v", *e.ipAddr, "/keys/"+*e.keyName)
connection, err := ssh.Dial("tcp", *e.ipAddr+":22", sshConfig)
fmt.Printf("Opening connection to %v:22 with SSHAgent or key %v", ipAddr, "/keys/"+keyName)
connection, err := ssh.Dial("tcp", ipAddr+":22", sshConfig)
if err != nil {
return fmt.Errorf("Failed to dial: %s", err)
}
Expand All @@ -46,24 +46,24 @@ func startSSH() error {
ssh.ECHO: 1,
}

fd := os.Stdin.Fd()
fd := int(os.Stdin.Fd())

if term.IsTerminal(fd) {
oldState, err := term.MakeRaw(fd)
if terminal.IsTerminal(fd) {
oldState, err := terminal.MakeRaw(fd)
if err != nil {
return err
}

defer term.RestoreTerminal(fd, oldState)
defer terminal.Restore(fd, oldState)

winsize, err := term.GetWinsize(fd)
tmpWidth, tmpHeight, err := terminal.GetSize(fd)
if err == nil {
width = int(winsize.Width)
height = int(winsize.Height)
width = tmpWidth
height = tmpHeight
}
}

if err := session.RequestPty("xterm", width, height, modes); err != nil {
if err := session.RequestPty("xterm", height, width, modes); err != nil {
session.Close()
return fmt.Errorf("request for pseudo terminal failed: %s", err)
}
Expand Down Expand Up @@ -114,15 +114,15 @@ func monitorChanges(session *ssh.Session, fd uintptr) {
func termSize(fd uintptr) []byte {
size := make([]byte, 16)

winsize, err := term.GetWinsize(fd)
width, height, err := terminal.GetSize(int(fd))
if err != nil {
binary.BigEndian.PutUint32(size, uint32(80))
binary.BigEndian.PutUint32(size[4:], uint32(24))
return size
}

binary.BigEndian.PutUint32(size, uint32(winsize.Width))
binary.BigEndian.PutUint32(size[4:], uint32(winsize.Height))
binary.BigEndian.PutUint32(size, uint32(width))
binary.BigEndian.PutUint32(size[4:], uint32(height))

return size
}

0 comments on commit a1c96c9

Please sign in to comment.