Skip to content

Commit a453912

Browse files
committedMar 31, 2022
fix: improve proxy commands
1 parent afa917d commit a453912

File tree

3 files changed

+36
-21
lines changed

3 files changed

+36
-21
lines changed
 

‎helper/cmd/proxy_commands/run.go

+12-11
Original file line numberDiff line numberDiff line change
@@ -58,23 +58,20 @@ func (cmd *RunCmd) Run(_ *cobra.Command, args []string) error {
5858
}
5959
defer session.Close()
6060

61-
// check if we should use a pty
61+
// check if we should use a pty#
62+
var (
63+
width = 0
64+
height = 0
65+
)
66+
6267
tty, t := terminal.SetupTTY(os.Stdin, os.Stdout)
6368
if tty {
6469
info, ok := term.GetFdInfo(t.In)
6570
if ok {
6671
winSize, err := term.GetWinsize(info)
6772
if err == nil {
68-
err = session.RequestPty("xterm", int(winSize.Height), int(winSize.Width), ssh.TerminalModes{
69-
ssh.ECHO: 0,
70-
ssh.TTY_OP_ISPEED: 28800,
71-
ssh.TTY_OP_OSPEED: 28800,
72-
})
73-
if err != nil {
74-
return errors.Wrap(err, "request pty")
75-
}
76-
77-
// TODO: terminal resize
73+
width = int(winSize.Width)
74+
height = int(winSize.Height)
7875
}
7976
}
8077
}
@@ -87,6 +84,10 @@ func (cmd *RunCmd) Run(_ *cobra.Command, args []string) error {
8784

8885
// marshal command and execute command
8986
proxyCommand := &types.ProxyCommand{
87+
TTY: tty,
88+
Width: width,
89+
Height: height,
90+
9091
Env: os.Environ(),
9192
Args: args,
9293
WorkingDir: currentWorkingDir,

‎helper/types/proxy_command.go

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package types
22

33
type ProxyCommand struct {
4+
TTY bool `json:"tty,omitempty"`
5+
Height int `json:"height,omitempty"`
6+
Width int `json:"width,omitempty"`
7+
48
Env []string `json:"env,omitempty"`
59
Args []string `json:"args,omitempty"`
610
WorkingDir string `json:"workingDir,omitempty"`

‎pkg/devspace/services/proxycommands/server.go

+20-10
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ type Server struct {
7979
}
8080

8181
func (s *Server) handler(sess ssh.Session) {
82-
cmd, err := s.getCommand(sess)
82+
cmd, payload, err := s.getCommand(sess)
8383
if err != nil {
8484
s.exitWithError(sess, errors.Wrap(err, "construct command"))
8585
return
@@ -98,9 +98,19 @@ func (s *Server) handler(sess ssh.Session) {
9898
}
9999

100100
// start shell session
101-
ptyReq, winCh, isPty := sess.Pty()
102-
if isPty && runtime.GOOS != "windows" {
103-
err = sshhelper.HandlePTY(sess, ptyReq, winCh, cmd, func(reader io.Reader) io.Reader {
101+
if payload.TTY && runtime.GOOS != "windows" {
102+
winSizeChan := make(chan ssh.Window, 1)
103+
winSizeChan <- ssh.Window{
104+
Width: payload.Width,
105+
Height: payload.Height,
106+
}
107+
err = sshhelper.HandlePTY(sess, ssh.Pty{
108+
Term: "xterm",
109+
Window: ssh.Window{
110+
Width: payload.Width,
111+
Height: payload.Height,
112+
},
113+
}, winSizeChan, cmd, func(reader io.Reader) io.Reader {
104114
return reader
105115
})
106116
} else {
@@ -113,19 +123,19 @@ func (s *Server) handler(sess ssh.Session) {
113123
s.exitWithError(sess, err)
114124
}
115125

116-
func (s *Server) getCommand(sess ssh.Session) (*exec.Cmd, error) {
126+
func (s *Server) getCommand(sess ssh.Session) (*exec.Cmd, *types.ProxyCommand, error) {
117127
var cmd *exec.Cmd
118128
rawCommand := sess.RawCommand()
119129
if len(rawCommand) == 0 {
120-
return nil, fmt.Errorf("command required")
130+
return nil, nil, fmt.Errorf("command required")
121131
}
122132

123133
command := &types.ProxyCommand{}
124134
err := json.Unmarshal([]byte(rawCommand), &command)
125135
if err != nil {
126-
return nil, fmt.Errorf("parse command: %v", err)
136+
return nil, nil, fmt.Errorf("parse command: %v", err)
127137
} else if len(command.Args) == 0 {
128-
return nil, fmt.Errorf("command is empty")
138+
return nil, nil, fmt.Errorf("command is empty")
129139
}
130140

131141
var reverseCommand *latest.ProxyCommand
@@ -136,7 +146,7 @@ func (s *Server) getCommand(sess ssh.Session) (*exec.Cmd, error) {
136146
}
137147
}
138148
if reverseCommand == nil {
139-
return nil, fmt.Errorf("command not allowed")
149+
return nil, nil, fmt.Errorf("command not allowed")
140150
}
141151

142152
c := reverseCommand.Command
@@ -168,7 +178,7 @@ func (s *Server) getCommand(sess ssh.Session) (*exec.Cmd, error) {
168178
s.log.Debugf("run command '%s %s' locally", c, strings.Join(args, " "))
169179
cmd.Env = append(cmd.Env, command.Env...)
170180
cmd.Env = append(cmd.Env, os.Environ()...)
171-
return cmd, nil
181+
return cmd, command, nil
172182
}
173183

174184
func (s *Server) transformPath(originalPath string) string {

0 commit comments

Comments
 (0)
Please sign in to comment.