Skip to content

Commit

Permalink
improve ProxyCommand #46
Browse files Browse the repository at this point in the history
  • Loading branch information
lonnywong committed Oct 14, 2023
1 parent 615d234 commit 8a8cead
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 25 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.20
require (
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5
github.com/chzyer/readline v1.5.1
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/mattn/go-isatty v0.0.19
github.com/natefinch/npipe v0.0.0-20160621034901-c1b8fa8bdcce
github.com/skeema/knownhosts v1.2.1-0.20230916192230-09454b7d5683
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dchest/jsmin v0.0.0-20220218165748-59f39799265f h1:OGqDDftRTwrvUoL6pOG7rYTmWsTCvyEWFsMjg+HcOaA=
github.com/dchest/jsmin v0.0.0-20220218165748-59f39799265f/go.mod h1:Dv9D0NUlAsaQcGQZa5kc5mqR9ua72SmA8VXi4cd+cBw=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
github.com/josephspurrier/goversioninfo v1.4.0 h1:Puhl12NSHUSALHSuzYwPYQkqa2E1+7SrtAPJorKK0C8=
github.com/josephspurrier/goversioninfo v1.4.0/go.mod h1:JWzv5rKQr+MmW+LvM412ToT/IkYDZjaclF2pKDss8IY=
github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I=
Expand Down
15 changes: 9 additions & 6 deletions tssh/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -631,13 +631,16 @@ func execProxyCommand(param *loginParam) (net.Conn, string, error) {
command = strings.ReplaceAll(command, "%r", param.user)
debug("exec proxy command: %s", command)

var cmd *exec.Cmd
if !strings.ContainsAny(command, "'\"\\") {
tokens := strings.Fields(command)
cmd = exec.Command(tokens[0], tokens[1:]...)
} else {
cmd = createProxyCommand(command)
argv, err := splitCommandLine(command)
if err != nil || len(argv) == 0 {
return nil, command, fmt.Errorf("split proxy command failed: %v", err)
}
if enableDebugLogging {
for i, arg := range argv {
debug("proxy command argv[%d] = %s", i, arg)
}
}
cmd := exec.Command(argv[0], argv[1:]...)

cmdIn, err := cmd.StdinPipe()
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions tssh/term_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"strings"
"syscall"

"github.com/google/shlex"
"golang.org/x/term"
)

Expand Down Expand Up @@ -115,3 +116,7 @@ func isSshTmuxEnv() bool {

return isRemoteSshEnv(pid)
}

func splitCommandLine(command string) ([]string, error) {
return shlex.Split(command)
}
4 changes: 4 additions & 0 deletions tssh/term_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,7 @@ func getKeyboardInput() (*os.File, func(), error) {

return file, func() { _ = file.Close() }, nil
}

func splitCommandLine(command string) ([]string, error) {
return windows.DecomposeCommandLine(command)
}
5 changes: 0 additions & 5 deletions tssh/util_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ package tssh
import (
"bytes"
"os"
"os/exec"

"golang.org/x/sys/unix"
)
Expand Down Expand Up @@ -67,7 +66,3 @@ func isNoGUI() bool {
}
return isDockerEnv() || isRemoteSshEnv(os.Getppid()) || isSshTmuxEnv()
}

func createProxyCommand(command string) *exec.Cmd {
return exec.Command("sh", "-c", command)
}
5 changes: 0 additions & 5 deletions tssh/util_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"bytes"
"fmt"
"os"
"os/exec"
"strconv"
)

Expand Down Expand Up @@ -90,7 +89,3 @@ func isNoGUI() bool {
}
return isDockerEnv() || isRemoteSshEnv(os.Getppid()) || isSshTmuxEnv()
}

func createProxyCommand(command string) *exec.Cmd {
return exec.Command("sh", "-c", command)
}
9 changes: 0 additions & 9 deletions tssh/util_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@ SOFTWARE.
package tssh

import (
"fmt"
"os"
"os/exec"
"strings"
"syscall"
"unsafe"

"golang.org/x/sys/windows"
Expand Down Expand Up @@ -65,9 +62,3 @@ func isNoGUI() bool {
}
return false
}

func createProxyCommand(command string) *exec.Cmd {
cmd := exec.Command("cmd.exe")
cmd.SysProcAttr = &syscall.SysProcAttr{CmdLine: fmt.Sprintf("/c %s", command)}
return cmd
}

0 comments on commit 8a8cead

Please sign in to comment.