Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Pty.TerminalModes #131

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ func TestPty(t *testing.T) {
term := "xterm"
winWidth := 40
winHeight := 80
TerminalModes := make(gossh.TerminalModes)
ttyOPOSPEED := uint32(38400)
TerminalModes[gossh.TTY_OP_OSPEED] = ttyOPOSPEED
done := make(chan bool)
session, _, cleanup := newTestSession(t, &Server{
Handler: func(s Session) {
Expand All @@ -215,11 +218,15 @@ func TestPty(t *testing.T) {
if ptyReq.Window.Height != winHeight {
t.Fatalf("expected window height %#v but got %#v", winHeight, ptyReq.Window.Height)
}
mode := ptyReq.TerminalModes[gossh.TTY_OP_OSPEED]
if ptyReq.TerminalModes[gossh.TTY_OP_OSPEED] != ttyOPOSPEED {
t.Fatalf("expected mode %#v but got %#v", ttyOPOSPEED, mode)
}
close(done)
},
}, nil)
defer cleanup()
if err := session.RequestPty(term, winHeight, winWidth, gossh.TerminalModes{}); err != nil {
if err := session.RequestPty(term, winHeight, winWidth, TerminalModes); err != nil {
t.Fatalf("expected nil but got %v", err)
}
if err := session.Shell(); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ type Window struct {

// Pty represents a PTY request and configuration.
type Pty struct {
Term string
Window Window
// HELP WANTED: terminal modes!
Term string
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it needs a gofmt

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sorry, I forgot it. I will modify this.

Window Window
TerminalModes gossh.TerminalModes
}

// Serve accepts incoming SSH connections on the listener l, creating a new
Expand Down
57 changes: 43 additions & 14 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ import (
"golang.org/x/crypto/ssh"
)

type ptyRequestMsg struct {
Term string
Columns uint32
Rows uint32
Width uint32
Height uint32
Modelist string
}

const (
ttyOPEND = 0
)

func generateSigner() (ssh.Signer, error) {
key, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
Expand All @@ -17,26 +30,42 @@ func generateSigner() (ssh.Signer, error) {
}

func parsePtyRequest(s []byte) (pty Pty, ok bool) {
term, s, ok := parseString(s)
if !ok {
return
}
width32, s, ok := parseUint32(s)
if !ok {
return
reqMsg := &ptyRequestMsg{}
err := ssh.Unmarshal(s, reqMsg)
if err != nil {
return Pty{}, false
}
height32, _, ok := parseUint32(s)
if !ok {
return

modes := []byte(reqMsg.Modelist)

mode := struct {
Key uint8
Val uint32
}{}

TerminalModes := make(ssh.TerminalModes, 0)
for {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The description on how terminal modes are actually passed can be found here: https://tools.ietf.org/html/rfc4254#section-8

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if I understand what you mean. Do you mean to set terminal modes to []byte type?

if len(modes) < 1 || modes[0] == ttyOPEND || len(modes) < 5 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This actually handles 2 different conditions - I'd argue len(modes) < 1 || modes[0] == ttyOPEND are success, but len(modes) < 5 is a failure.

break
}
b := modes[:5]
err = ssh.Unmarshal(b, &mode)
if err != nil {
return Pty{}, false
}
TerminalModes[mode.Key] = mode.Val
modes = modes[6:]
}

pty = Pty{
Term: term,
Term: reqMsg.Term,
Window: Window{
Width: int(width32),
Height: int(height32),
Width: int(reqMsg.Columns),
Height: int(reqMsg.Rows),
},
TerminalModes: TerminalModes,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be enough for now, but we may want to have an example or a convenience function that will handle this properly. Setting TerminalModes is not fun. This is just an example: https://github.com/golang/crypto/blob/master/ssh/terminal/util.go#L37

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean like this ?

func ApplyTerminalModes(fd int, terminalModes ssh.TerminalModes) bool {
	termios, err := unix.IoctlGetTermios(fd, unix.TCGETS)
	fmt.Println(termios, err)
	for opcode, val := range terminalModes {
		switch opcode {
		case ssh.TTY_OP_ISPEED:
			termios.TTY_OP_OSPEED = val
		case ssh.TTY_OP_OSPEED:
			termios.TTY_OP_OSPEED = val
		
		// case char
		case ssh.VINTR:
			termios.Cc[ssh.VINTR] = val
		case ssh.VQUIT:
			termios.Cc[ssh.VQUIT] = val
		//...
		//case mode
		case ssh.ECHO:
			if val {
				termios.Lflag |= ssh.ECHO
			} else {
				termios.Lflag &= ~ssh.ECHO
			}

		default
			// Others modes ...
		}

	}

	if err := unix.IoctlSetTermios(fd, unix.TCSETS, termios); err != nil {
		return nil, err
	}
	return true
}

This is the reference code https://github.com/openssh/openssh-portable/blob/1a7217ac063e48cf0082895aeee81ed2b8a57191/ttymodes.c#L397, the syntax may not be correct.

}
return
return pty, true
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are already implicitly returned because of the named params. Please either switch back to the implicit return or remove the variable names from the function definition.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌

}

func parseWinchRequest(s []byte) (win Window, ok bool) {
Expand Down