From 0c1526cc7e8dd67cac8b082b9675b6b52d0a0005 Mon Sep 17 00:00:00 2001 From: ivcosla Date: Mon, 12 Aug 2019 19:10:56 +0200 Subject: [PATCH 1/5] format --- cmd/therealssh-cli/commands/root.go | 75 ++++++++++++++++++++++++++++- 1 file changed, 74 insertions(+), 1 deletion(-) diff --git a/cmd/therealssh-cli/commands/root.go b/cmd/therealssh-cli/commands/root.go index 3778bd5239..fcad7b7deb 100644 --- a/cmd/therealssh-cli/commands/root.go +++ b/cmd/therealssh-cli/commands/root.go @@ -21,13 +21,24 @@ import ( ssh "github.com/skycoin/skywire/pkg/therealssh" ) -var rpcAddr string +var ( + rpcAddr string + ptyMode bool + ptyRows uint16 + ptyCols uint16 + ptyX uint16 + ptyY uint16 +) var rootCmd = &cobra.Command{ Use: "SSH-cli [user@]remotePK [command] [args...]", Short: "Client for the SSH-client app", Args: cobra.MinimumNArgs(1), Run: func(_ *cobra.Command, args []string) { + if ptyMode { + runInPTY(args) + return + } client, err := rpc.DialHTTP("tcp", rpcAddr) if err != nil { log.Fatal("RPC connection failed:", err) @@ -117,6 +128,63 @@ var rootCmd = &cobra.Command{ }, } +func runInPTY(args []string) { + client, err := rpc.DialHTTP("tcp", rpcAddr) + if err != nil { + log.Fatal("RPC connection failed:", err) + } + + username, pk, err := resolveUser(args[0]) + if err != nil { + log.Fatal("Invalid user/pk pair: ", err) + } + + remotePK := cipher.PubKey{} + if err := remotePK.UnmarshalText([]byte(pk)); err != nil { + log.Fatal("Invalid remote PubKey: ", err) + } + + ptyArgs := &ssh.RequestPTYArgs{ + Username: username, + RemotePK: remotePK, + Size: &pty.Winsize{ + Rows: uint16(ptyRows), + Cols: ptyCols, + X: ptyX, + Y: ptyY, + }, + } + + var channelID uint32 + if err := client.Call("RPCClient.RequestPTY", ptyArgs, &channelID); err != nil { + log.Fatal("Failed to request PTY:", err) + } + + var socketPath string + execArgs := &ssh.ExecArgs{ + ChannelID: channelID, + CommandWithArgs: args[1:], + } + + err = client.Call("RPCClient.Run", execArgs, &socketPath) + if err != nil { + log.Fatal(err) + } + + conn, err := net.DialUnix("unix", nil, &net.UnixAddr{Name: socketPath, Net: "unix"}) + if err != nil { + log.Fatal(err) + } + + b := make([]byte, 6024) + _, err = conn.Read(b) // nolint + if err != nil { + log.Fatal(err) + } + + fmt.Print(string(b)) +} + func resolveUser(arg string) (username string, pk string, err error) { components := strings.Split(arg, "@") if len(components) == 1 { @@ -137,6 +205,11 @@ func resolveUser(arg string) (username string, pk string, err error) { func init() { rootCmd.Flags().StringVarP(&rpcAddr, "rpc", "", ":2222", "RPC address to connect to") + rootCmd.Flags().BoolVarP(&ptyMode, "pty", "", false, "Whether to run the command in a simulated PTY or not") + rootCmd.Flags().Uint16VarP(&ptyRows, "ptyrows", "", 100, "PTY Rows. Applicable if run with pty flag") + rootCmd.Flags().Uint16VarP(&ptyCols, "ptycols", "", 100, "PTY Cols. Applicable if run with pty flag") + rootCmd.Flags().Uint16VarP(&ptyX, "ptyx", "", 100, "PTY X. Applicable if run with pty flag") + rootCmd.Flags().Uint16VarP(&ptyY, "ptyy", "", 100, "PTY Y. Applicable if run with pty flag") } // Execute executes root CLI command. From a368f746f1ee354c0e118e8e35fd42a77ae10ccf Mon Sep 17 00:00:00 2001 From: ivcosla Date: Tue, 13 Aug 2019 11:29:03 +0200 Subject: [PATCH 2/5] added buffer size flag --- cmd/therealssh-cli/commands/root.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/cmd/therealssh-cli/commands/root.go b/cmd/therealssh-cli/commands/root.go index fcad7b7deb..799a558c95 100644 --- a/cmd/therealssh-cli/commands/root.go +++ b/cmd/therealssh-cli/commands/root.go @@ -22,12 +22,13 @@ import ( ) var ( - rpcAddr string - ptyMode bool - ptyRows uint16 - ptyCols uint16 - ptyX uint16 - ptyY uint16 + rpcAddr string + ptyMode bool + ptyRows uint16 + ptyCols uint16 + ptyX uint16 + ptyY uint16 + ptyBufferSize uint32 ) var rootCmd = &cobra.Command{ @@ -176,7 +177,7 @@ func runInPTY(args []string) { log.Fatal(err) } - b := make([]byte, 6024) + b := make([]byte, ptyBufferSize) _, err = conn.Read(b) // nolint if err != nil { log.Fatal(err) @@ -210,6 +211,7 @@ func init() { rootCmd.Flags().Uint16VarP(&ptyCols, "ptycols", "", 100, "PTY Cols. Applicable if run with pty flag") rootCmd.Flags().Uint16VarP(&ptyX, "ptyx", "", 100, "PTY X. Applicable if run with pty flag") rootCmd.Flags().Uint16VarP(&ptyY, "ptyy", "", 100, "PTY Y. Applicable if run with pty flag") + rootCmd.Flags().Uint32VarP(&ptyBufferSize, "ptybuffer", "", 1024, "PTY Buffer size to store command output") } // Execute executes root CLI command. From 6b7052a776fd60e964a520955bb0eb5fd7d3e3fb Mon Sep 17 00:00:00 2001 From: ivcosla Date: Tue, 13 Aug 2019 11:51:48 +0200 Subject: [PATCH 3/5] removed nolint --- cmd/therealssh-cli/commands/root.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/therealssh-cli/commands/root.go b/cmd/therealssh-cli/commands/root.go index 799a558c95..595fec958d 100644 --- a/cmd/therealssh-cli/commands/root.go +++ b/cmd/therealssh-cli/commands/root.go @@ -178,7 +178,7 @@ func runInPTY(args []string) { } b := make([]byte, ptyBufferSize) - _, err = conn.Read(b) // nolint + _, err = conn.Read(b) if err != nil { log.Fatal(err) } From 050ccdf1f6d22aaefa356c4413a6b7f0313480f8 Mon Sep 17 00:00:00 2001 From: ivcosla Date: Tue, 13 Aug 2019 12:02:36 +0200 Subject: [PATCH 4/5] marked pty test to not be tested under travis --- pkg/therealssh/pty_test.go | 42 +------------------------------------- 1 file changed, 1 insertion(+), 41 deletions(-) diff --git a/pkg/therealssh/pty_test.go b/pkg/therealssh/pty_test.go index e9fab6d468..7b617e1b75 100644 --- a/pkg/therealssh/pty_test.go +++ b/pkg/therealssh/pty_test.go @@ -1,4 +1,4 @@ -// +build dragonfly freebsd linux netbsd openbsd +// +build !no_ci package therealssh @@ -16,46 +16,6 @@ import ( "github.com/skycoin/skywire/pkg/routing" ) -/* func TestRunInPTY(t *testing.T) { - dialConn, acceptConn := net.Pipe() - pd := PipeDialer{PipeWithRoutingAddr{dialConn}, acceptConn} - _, client, err := NewClient(":9999", pd) - require.NoError(t, err) - - server := NewServer(MockAuthorizer{}) - - go func() { - server.Serve(PipeWithRoutingAddr{acceptConn}) // nolint - }() - - _, ch, err := client.OpenChannel(cipher.PubKey{}) - require.NoError(t, err) - - cuser, err := user.Current() - require.NoError(t, err) - - args := RequestPTYArgs{ - Username: cuser.Username, - RemotePK: cipher.PubKey{}, - Size: &pty.Winsize{ - Rows: 100, - Cols: 100, - X: 100, - Y: 100, - }, - } - _, err = ch.Request(RequestPTY, args.ToBinary()) - require.NoError(t, err) - - _, err = ch.Request(RequestExecWithoutShell, []byte("ls")) - require.NoError(t, err) - - b := make([]byte, 6024) - _, err = ch.Read(b) - require.NoError(t, err) - require.Contains(t, string(b), "pty_test.go") -} -*/ func TestRunRPC(t *testing.T) { dialConn, acceptConn := net.Pipe() From 77d3a482a89ab19f1d403b6d61c00f92cde73c30 Mon Sep 17 00:00:00 2001 From: ivcosla Date: Tue, 13 Aug 2019 12:13:18 +0200 Subject: [PATCH 5/5] format --- pkg/therealssh/pty_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/therealssh/pty_test.go b/pkg/therealssh/pty_test.go index 7b617e1b75..7756ddd4cd 100644 --- a/pkg/therealssh/pty_test.go +++ b/pkg/therealssh/pty_test.go @@ -16,7 +16,6 @@ import ( "github.com/skycoin/skywire/pkg/routing" ) - func TestRunRPC(t *testing.T) { dialConn, acceptConn := net.Pipe() pd := PipeDialer{PipeWithRoutingAddr{dialConn}, acceptConn}