2016-02-22 14:44:12 -05:00
|
|
|
package winrm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/x509"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2017-04-26 12:15:30 -04:00
|
|
|
"sync"
|
2016-02-22 14:44:12 -05:00
|
|
|
|
|
|
|
"github.com/masterzen/winrm/soap"
|
|
|
|
)
|
|
|
|
|
2016-10-01 16:34:42 -04:00
|
|
|
// Client struct
|
2016-02-22 14:44:12 -05:00
|
|
|
type Client struct {
|
|
|
|
Parameters
|
2017-01-18 16:03:02 -05:00
|
|
|
username string
|
|
|
|
password string
|
|
|
|
useHTTPS bool
|
|
|
|
url string
|
|
|
|
http Transporter
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transporter does different transporters
|
|
|
|
// and init a Post request based on them
|
|
|
|
type Transporter interface {
|
|
|
|
// init request baset on the transport configurations
|
|
|
|
Post(*Client, *soap.SoapMessage) (string, error)
|
|
|
|
Transport(*Endpoint) error
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient will create a new remote client on url, connecting with user and password
|
|
|
|
// This function doesn't connect (connection happens only when CreateShell is called)
|
2016-10-01 16:34:42 -04:00
|
|
|
func NewClient(endpoint *Endpoint, user, password string) (*Client, error) {
|
|
|
|
return NewClientWithParameters(endpoint, user, password, DefaultParameters)
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
|
2016-10-01 16:34:42 -04:00
|
|
|
// NewClientWithParameters will create a new remote client on url, connecting with user and password
|
2016-02-22 14:44:12 -05:00
|
|
|
// This function doesn't connect (connection happens only when CreateShell is called)
|
2016-10-01 16:34:42 -04:00
|
|
|
func NewClientWithParameters(endpoint *Endpoint, user, password string, params *Parameters) (*Client, error) {
|
2016-02-22 14:44:12 -05:00
|
|
|
|
2017-01-18 16:03:02 -05:00
|
|
|
// alloc a new client
|
2016-10-01 16:34:42 -04:00
|
|
|
client := &Client{
|
2016-02-22 14:44:12 -05:00
|
|
|
Parameters: *params,
|
|
|
|
username: user,
|
|
|
|
password: password,
|
|
|
|
url: endpoint.url(),
|
|
|
|
useHTTPS: endpoint.HTTPS,
|
2017-01-18 16:03:02 -05:00
|
|
|
// default transport
|
|
|
|
http: &clientRequest{},
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
|
2017-01-18 16:03:02 -05:00
|
|
|
// switch to other transport if provided
|
2016-02-22 14:44:12 -05:00
|
|
|
if params.TransportDecorator != nil {
|
2017-01-18 16:03:02 -05:00
|
|
|
client.http = params.TransportDecorator()
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
2016-10-01 16:34:42 -04:00
|
|
|
|
2017-01-18 16:03:02 -05:00
|
|
|
// set the transport to some endpoint configuration
|
|
|
|
if err := client.http.Transport(endpoint); err != nil {
|
|
|
|
return nil, fmt.Errorf("Can't parse this key and certs: %s", err)
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
|
2017-01-18 16:03:02 -05:00
|
|
|
return client, nil
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
|
2016-10-01 16:34:42 -04:00
|
|
|
func readCACerts(certs []byte) (*x509.CertPool, error) {
|
2016-02-22 14:44:12 -05:00
|
|
|
certPool := x509.NewCertPool()
|
|
|
|
|
2016-10-01 16:34:42 -04:00
|
|
|
if !certPool.AppendCertsFromPEM(certs) {
|
2016-02-22 14:44:12 -05:00
|
|
|
return nil, fmt.Errorf("Unable to read certificates")
|
|
|
|
}
|
|
|
|
|
|
|
|
return certPool, nil
|
|
|
|
}
|
|
|
|
|
2016-10-01 16:34:42 -04:00
|
|
|
// CreateShell will create a WinRM Shell,
|
|
|
|
// which is the prealable for running commands.
|
|
|
|
func (c *Client) CreateShell() (*Shell, error) {
|
|
|
|
request := NewOpenShellRequest(c.url, &c.Parameters)
|
2016-02-22 14:44:12 -05:00
|
|
|
defer request.Free()
|
|
|
|
|
2016-10-01 16:34:42 -04:00
|
|
|
response, err := c.sendRequest(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
shellID, err := ParseOpenShellResponse(response)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
2016-10-01 16:34:42 -04:00
|
|
|
|
|
|
|
return c.NewShell(shellID), nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewShell will create a new WinRM Shell for the given shellID
|
|
|
|
func (c *Client) NewShell(id string) *Shell {
|
|
|
|
return &Shell{client: c, id: id}
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
|
2016-10-01 16:34:42 -04:00
|
|
|
// sendRequest exec the custom http func from the client
|
|
|
|
func (c *Client) sendRequest(request *soap.SoapMessage) (string, error) {
|
2017-01-18 16:03:02 -05:00
|
|
|
return c.http.Post(c, request)
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run will run command on the the remote host, writing the process stdout and stderr to
|
|
|
|
// the given writers. Note with this method it isn't possible to inject stdin.
|
2016-10-01 16:34:42 -04:00
|
|
|
func (c *Client) Run(command string, stdout io.Writer, stderr io.Writer) (int, error) {
|
|
|
|
shell, err := c.CreateShell()
|
2016-02-22 14:44:12 -05:00
|
|
|
if err != nil {
|
2016-10-01 16:34:42 -04:00
|
|
|
return 1, err
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
2016-10-01 16:34:42 -04:00
|
|
|
defer shell.Close()
|
|
|
|
cmd, err := shell.Execute(command)
|
2016-02-22 14:44:12 -05:00
|
|
|
if err != nil {
|
2016-10-01 16:34:42 -04:00
|
|
|
return 1, err
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
2016-10-01 16:34:42 -04:00
|
|
|
|
2017-04-26 12:15:30 -04:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(2)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
io.Copy(stdout, cmd.Stdout)
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
io.Copy(stderr, cmd.Stderr)
|
|
|
|
}()
|
2016-10-01 16:34:42 -04:00
|
|
|
|
2016-02-22 14:44:12 -05:00
|
|
|
cmd.Wait()
|
2017-04-26 12:15:30 -04:00
|
|
|
wg.Wait()
|
2016-10-01 16:34:42 -04:00
|
|
|
|
2016-02-22 14:44:12 -05:00
|
|
|
return cmd.ExitCode(), cmd.err
|
|
|
|
}
|
|
|
|
|
2016-10-01 16:34:42 -04:00
|
|
|
// RunWithString will run command on the the remote host, returning the process stdout and stderr
|
2016-02-22 14:44:12 -05:00
|
|
|
// as strings, and using the input stdin string as the process input
|
2016-10-01 16:34:42 -04:00
|
|
|
func (c *Client) RunWithString(command string, stdin string) (string, string, int, error) {
|
|
|
|
shell, err := c.CreateShell()
|
2016-02-22 14:44:12 -05:00
|
|
|
if err != nil {
|
2016-10-01 16:34:42 -04:00
|
|
|
return "", "", 1, err
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
defer shell.Close()
|
2016-10-01 16:34:42 -04:00
|
|
|
|
|
|
|
cmd, err := shell.Execute(command)
|
2016-02-22 14:44:12 -05:00
|
|
|
if err != nil {
|
2016-10-01 16:34:42 -04:00
|
|
|
return "", "", 1, err
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
if len(stdin) > 0 {
|
|
|
|
cmd.Stdin.Write([]byte(stdin))
|
|
|
|
}
|
2016-10-01 16:34:42 -04:00
|
|
|
|
2016-02-22 14:44:12 -05:00
|
|
|
var outWriter, errWriter bytes.Buffer
|
|
|
|
go io.Copy(&outWriter, cmd.Stdout)
|
|
|
|
go io.Copy(&errWriter, cmd.Stderr)
|
2016-10-01 16:34:42 -04:00
|
|
|
|
2016-02-22 14:44:12 -05:00
|
|
|
cmd.Wait()
|
2016-10-01 16:34:42 -04:00
|
|
|
|
2016-02-22 14:44:12 -05:00
|
|
|
return outWriter.String(), errWriter.String(), cmd.ExitCode(), cmd.err
|
|
|
|
}
|
|
|
|
|
2016-10-01 16:34:42 -04:00
|
|
|
// RunWithInput will run command on the the remote host, writing the process stdout and stderr to
|
2016-02-22 14:44:12 -05:00
|
|
|
// the given writers, and injecting the process stdin with the stdin reader.
|
|
|
|
// Warning stdin (not stdout/stderr) are bufferized, which means reading only one byte in stdin will
|
|
|
|
// send a winrm http packet to the remote host. If stdin is a pipe, it might be better for
|
|
|
|
// performance reasons to buffer it.
|
2016-10-01 16:34:42 -04:00
|
|
|
func (c Client) RunWithInput(command string, stdout, stderr io.Writer, stdin io.Reader) (int, error) {
|
|
|
|
shell, err := c.CreateShell()
|
2016-02-22 14:44:12 -05:00
|
|
|
if err != nil {
|
2016-10-01 16:34:42 -04:00
|
|
|
return 1, err
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
defer shell.Close()
|
2016-10-01 16:34:42 -04:00
|
|
|
cmd, err := shell.Execute(command)
|
2016-02-22 14:44:12 -05:00
|
|
|
if err != nil {
|
2016-10-01 16:34:42 -04:00
|
|
|
return 1, err
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
2016-10-01 16:34:42 -04:00
|
|
|
|
2016-02-22 14:44:12 -05:00
|
|
|
go io.Copy(cmd.Stdin, stdin)
|
|
|
|
go io.Copy(stdout, cmd.Stdout)
|
|
|
|
go io.Copy(stderr, cmd.Stderr)
|
2016-10-01 16:34:42 -04:00
|
|
|
|
2016-02-22 14:44:12 -05:00
|
|
|
cmd.Wait()
|
2016-10-01 16:34:42 -04:00
|
|
|
|
2016-02-22 14:44:12 -05:00
|
|
|
return cmd.ExitCode(), cmd.err
|
2017-01-18 16:03:02 -05:00
|
|
|
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|