2017-04-14 19:26:32 +02:00
|
|
|
package sshbytestream
|
|
|
|
|
2017-04-15 15:43:50 +02:00
|
|
|
import (
|
2017-04-26 20:25:53 +02:00
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2017-04-15 15:43:50 +02:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2017-04-30 23:35:08 +02:00
|
|
|
type Error struct {
|
|
|
|
Stderr []byte
|
|
|
|
WaitErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e Error) Error() string {
|
|
|
|
return fmt.Sprintf("ssh command failed with error: %v. stderr:\n%s\n", e.WaitErr, e.Stderr)
|
|
|
|
}
|
|
|
|
|
2017-04-29 20:09:09 +02:00
|
|
|
type SSHTransport struct {
|
2017-04-30 16:11:33 +02:00
|
|
|
Host string
|
|
|
|
User string
|
|
|
|
Port uint16
|
|
|
|
IdentityFile string
|
|
|
|
SSHCommand string
|
|
|
|
Options []string
|
2017-04-29 20:09:09 +02:00
|
|
|
}
|
|
|
|
|
2017-04-15 15:43:50 +02:00
|
|
|
var SSHCommand string = "ssh"
|
|
|
|
|
|
|
|
func Incoming() (wc io.ReadWriteCloser, err error) {
|
2017-04-14 19:26:32 +02:00
|
|
|
// derivce ReadWriteCloser from stdin & stdout
|
2017-04-15 15:43:50 +02:00
|
|
|
return IncomingReadWriteCloser{}, nil
|
2017-04-14 19:26:32 +02:00
|
|
|
}
|
|
|
|
|
2017-04-15 15:43:50 +02:00
|
|
|
type IncomingReadWriteCloser struct{}
|
|
|
|
|
|
|
|
func (f IncomingReadWriteCloser) Read(p []byte) (n int, err error) {
|
|
|
|
return os.Stdin.Read(p)
|
2017-04-14 19:26:32 +02:00
|
|
|
}
|
|
|
|
|
2017-04-15 15:43:50 +02:00
|
|
|
func (f IncomingReadWriteCloser) Write(p []byte) (n int, err error) {
|
|
|
|
return os.Stdout.Write(p)
|
|
|
|
}
|
2017-04-14 19:26:32 +02:00
|
|
|
|
2017-04-26 20:25:53 +02:00
|
|
|
func (f IncomingReadWriteCloser) Close() (err error) {
|
2017-04-30 23:35:08 +02:00
|
|
|
if err = os.Stdin.Close(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = os.Stdout.Close(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
2017-04-14 19:26:32 +02:00
|
|
|
}
|
|
|
|
|
2017-04-29 20:09:09 +02:00
|
|
|
func Outgoing(remote SSHTransport) (conn io.ReadWriteCloser, err error) {
|
2017-04-15 15:43:50 +02:00
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
2017-04-30 16:11:33 +02:00
|
|
|
sshArgs := make([]string, 0, 2*len(remote.Options)+4)
|
2017-04-15 15:43:50 +02:00
|
|
|
sshArgs = append(sshArgs,
|
|
|
|
"-p", fmt.Sprintf("%d", remote.Port),
|
2017-04-30 16:11:33 +02:00
|
|
|
"-q",
|
|
|
|
"-i", remote.IdentityFile,
|
2017-04-15 15:43:50 +02:00
|
|
|
"-o", "BatchMode=yes",
|
|
|
|
)
|
2017-04-26 20:25:53 +02:00
|
|
|
for _, option := range remote.Options {
|
2017-04-15 15:43:50 +02:00
|
|
|
sshArgs = append(sshArgs, "-o", option)
|
|
|
|
}
|
|
|
|
sshArgs = append(sshArgs, fmt.Sprintf("%s@%s", remote.User, remote.Host))
|
|
|
|
|
2017-04-30 16:11:33 +02:00
|
|
|
var sshCommand = SSHCommand
|
|
|
|
if len(remote.SSHCommand) > 0 {
|
|
|
|
sshCommand = SSHCommand
|
|
|
|
}
|
|
|
|
cmd := exec.CommandContext(ctx, sshCommand, sshArgs...)
|
|
|
|
|
|
|
|
// Clear environment of cmd
|
|
|
|
cmd.Env = []string{}
|
2017-04-15 15:43:50 +02:00
|
|
|
|
|
|
|
var in io.WriteCloser
|
|
|
|
var out io.ReadCloser
|
|
|
|
|
|
|
|
if in, err = cmd.StdinPipe(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-04-26 20:25:53 +02:00
|
|
|
if out, err = cmd.StdoutPipe(); err != nil {
|
2017-04-15 15:43:50 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-30 23:35:08 +02:00
|
|
|
stderrBuf := bytes.NewBuffer(make([]byte, 0, 1024))
|
|
|
|
cmd.Stderr = stderrBuf
|
|
|
|
|
|
|
|
f := &ForkedSSHReadWriteCloser{
|
2017-04-26 20:25:53 +02:00
|
|
|
RemoteStdin: in,
|
|
|
|
RemoteStdout: out,
|
|
|
|
Cancel: cancel,
|
|
|
|
Command: cmd,
|
2017-04-15 15:43:50 +02:00
|
|
|
exitWaitGroup: &sync.WaitGroup{},
|
|
|
|
}
|
|
|
|
|
|
|
|
f.exitWaitGroup.Add(1)
|
2017-04-30 16:11:33 +02:00
|
|
|
if err = cmd.Start(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-04-15 15:43:50 +02:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer f.exitWaitGroup.Done()
|
2017-04-30 23:35:08 +02:00
|
|
|
|
|
|
|
// stderr output is only relevant for errors if the exit code is non-zero
|
2017-04-30 16:11:33 +02:00
|
|
|
if err := cmd.Wait(); err != nil {
|
2017-04-30 23:35:08 +02:00
|
|
|
f.SSHCommandError = Error{
|
|
|
|
Stderr: stderrBuf.Bytes(),
|
|
|
|
WaitErr: err,
|
|
|
|
}
|
|
|
|
// fmt.Fprintf(os.Stderr, "ssh conn wait err: %#v\n", f.SSHCommandError.(Error))
|
|
|
|
} else {
|
|
|
|
f.SSHCommandError = nil
|
2017-04-15 15:43:50 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return f, nil
|
2017-04-14 19:26:32 +02:00
|
|
|
}
|
|
|
|
|
2017-04-26 20:25:53 +02:00
|
|
|
type ForkedSSHReadWriteCloser struct {
|
2017-04-30 23:35:08 +02:00
|
|
|
RemoteStdin io.Writer
|
|
|
|
RemoteStdout io.Reader
|
|
|
|
Command *exec.Cmd
|
|
|
|
Cancel context.CancelFunc
|
|
|
|
exitWaitGroup *sync.WaitGroup
|
|
|
|
SSHCommandError error
|
2017-04-15 15:43:50 +02:00
|
|
|
}
|
2017-04-14 19:26:32 +02:00
|
|
|
|
2017-04-30 23:35:08 +02:00
|
|
|
func (f *ForkedSSHReadWriteCloser) Read(p []byte) (n int, err error) {
|
|
|
|
if f.SSHCommandError != nil {
|
|
|
|
return 0, f.SSHCommandError
|
|
|
|
}
|
|
|
|
if n, err = f.RemoteStdout.Read(p); err == io.EOF {
|
|
|
|
// the ssh command has exited, but we need to wait for post-portem to finish
|
|
|
|
f.exitWaitGroup.Wait()
|
|
|
|
if f.SSHCommandError != nil {
|
|
|
|
err = f.SSHCommandError
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
2017-04-14 19:26:32 +02:00
|
|
|
}
|
|
|
|
|
2017-04-30 23:35:08 +02:00
|
|
|
func (f *ForkedSSHReadWriteCloser) Write(p []byte) (n int, err error) {
|
|
|
|
if f.SSHCommandError != nil {
|
|
|
|
return 0, f.SSHCommandError
|
|
|
|
}
|
|
|
|
if n, err = f.RemoteStdin.Write(p); err == io.EOF {
|
|
|
|
// the ssh command has exited, but we need to wait for post-portem to finish
|
|
|
|
f.exitWaitGroup.Wait()
|
|
|
|
if f.SSHCommandError != nil {
|
|
|
|
err = f.SSHCommandError
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
2017-04-15 15:43:50 +02:00
|
|
|
}
|
2017-04-14 19:26:32 +02:00
|
|
|
|
2017-04-30 23:35:08 +02:00
|
|
|
func (f *ForkedSSHReadWriteCloser) Close() (err error) {
|
|
|
|
// TODO should check SSHCommandError?
|
2017-04-15 15:43:50 +02:00
|
|
|
f.Cancel()
|
|
|
|
f.exitWaitGroup.Wait()
|
2017-04-30 23:35:08 +02:00
|
|
|
return f.SSHCommandError
|
2017-04-26 20:25:53 +02:00
|
|
|
}
|