2014-05-01 20:40:13 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
2014-12-01 20:57:53 -05:00
|
|
|
"errors"
|
2014-05-01 20:40:13 -04:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httputil"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
2014-12-01 20:57:53 -05:00
|
|
|
"time"
|
2014-05-01 20:40:13 -04:00
|
|
|
|
2014-10-24 13:12:35 -04:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2014-07-24 18:19:50 -04:00
|
|
|
"github.com/docker/docker/api"
|
|
|
|
"github.com/docker/docker/dockerversion"
|
2014-09-30 02:16:27 -04:00
|
|
|
"github.com/docker/docker/pkg/promise"
|
2014-09-17 11:04:56 -04:00
|
|
|
"github.com/docker/docker/pkg/stdcopy"
|
2014-07-24 18:19:50 -04:00
|
|
|
"github.com/docker/docker/pkg/term"
|
2014-05-01 20:40:13 -04:00
|
|
|
)
|
|
|
|
|
2014-12-01 20:57:53 -05:00
|
|
|
type tlsClientCon struct {
|
|
|
|
*tls.Conn
|
|
|
|
rawConn net.Conn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *tlsClientCon) CloseWrite() error {
|
|
|
|
// Go standard tls.Conn doesn't provide the CloseWrite() method so we do it
|
|
|
|
// on its underlying connection.
|
|
|
|
if cwc, ok := c.rawConn.(interface {
|
|
|
|
CloseWrite() error
|
|
|
|
}); ok {
|
|
|
|
return cwc.CloseWrite()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func tlsDial(network, addr string, config *tls.Config) (net.Conn, error) {
|
|
|
|
return tlsDialWithDialer(new(net.Dialer), network, addr, config)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to copy Go's implementation of tls.Dial (pkg/cryptor/tls/tls.go) in
|
|
|
|
// order to return our custom tlsClientCon struct which holds both the tls.Conn
|
|
|
|
// object _and_ its underlying raw connection. The rationale for this is that
|
|
|
|
// we need to be able to close the write end of the connection when attaching,
|
|
|
|
// which tls.Conn does not provide.
|
|
|
|
func tlsDialWithDialer(dialer *net.Dialer, network, addr string, config *tls.Config) (net.Conn, error) {
|
|
|
|
// We want the Timeout and Deadline values from dialer to cover the
|
|
|
|
// whole process: TCP connection and TLS handshake. This means that we
|
|
|
|
// also need to start our own timers now.
|
|
|
|
timeout := dialer.Timeout
|
|
|
|
|
|
|
|
if !dialer.Deadline.IsZero() {
|
|
|
|
deadlineTimeout := dialer.Deadline.Sub(time.Now())
|
|
|
|
if timeout == 0 || deadlineTimeout < timeout {
|
|
|
|
timeout = deadlineTimeout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var errChannel chan error
|
|
|
|
|
|
|
|
if timeout != 0 {
|
|
|
|
errChannel = make(chan error, 2)
|
|
|
|
time.AfterFunc(timeout, func() {
|
|
|
|
errChannel <- errors.New("")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
rawConn, err := dialer.Dial(network, addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-01-30 10:50:27 -05:00
|
|
|
// When we set up a TCP connection for hijack, there could be long periods
|
|
|
|
// of inactivity (a long running command with no output) that in certain
|
|
|
|
// network setups may cause ECONNTIMEOUT, leaving the client in an unknown
|
|
|
|
// state. Setting TCP KeepAlive on the socket connection will prohibit
|
|
|
|
// ECONNTIMEOUT unless the socket connection truly is broken
|
|
|
|
if tcpConn, ok := rawConn.(*net.TCPConn); ok {
|
|
|
|
tcpConn.SetKeepAlive(true)
|
|
|
|
tcpConn.SetKeepAlivePeriod(30 * time.Second)
|
|
|
|
}
|
2014-12-01 20:57:53 -05:00
|
|
|
|
|
|
|
colonPos := strings.LastIndex(addr, ":")
|
|
|
|
if colonPos == -1 {
|
|
|
|
colonPos = len(addr)
|
|
|
|
}
|
|
|
|
hostname := addr[:colonPos]
|
|
|
|
|
|
|
|
// If no ServerName is set, infer the ServerName
|
|
|
|
// from the hostname we're connecting to.
|
|
|
|
if config.ServerName == "" {
|
|
|
|
// Make a copy to avoid polluting argument or default.
|
|
|
|
c := *config
|
|
|
|
c.ServerName = hostname
|
|
|
|
config = &c
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := tls.Client(rawConn, config)
|
|
|
|
|
|
|
|
if timeout == 0 {
|
|
|
|
err = conn.Handshake()
|
|
|
|
} else {
|
|
|
|
go func() {
|
|
|
|
errChannel <- conn.Handshake()
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = <-errChannel
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
rawConn.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is Docker difference with standard's crypto/tls package: returned a
|
|
|
|
// wrapper which holds both the TLS and raw connections.
|
|
|
|
return &tlsClientCon{conn, rawConn}, nil
|
|
|
|
}
|
|
|
|
|
2014-05-01 20:40:13 -04:00
|
|
|
func (cli *DockerCli) dial() (net.Conn, error) {
|
|
|
|
if cli.tlsConfig != nil && cli.proto != "unix" {
|
2014-12-01 20:57:53 -05:00
|
|
|
// Notice this isn't Go standard's tls.Dial function
|
|
|
|
return tlsDial(cli.proto, cli.addr, cli.tlsConfig)
|
2014-05-01 20:40:13 -04:00
|
|
|
}
|
|
|
|
return net.Dial(cli.proto, cli.addr)
|
|
|
|
}
|
|
|
|
|
2014-09-10 20:06:59 -04:00
|
|
|
func (cli *DockerCli) hijack(method, path string, setRawTerminal bool, in io.ReadCloser, stdout, stderr io.Writer, started chan io.Closer, data interface{}) error {
|
2014-05-01 20:40:13 -04:00
|
|
|
defer func() {
|
|
|
|
if started != nil {
|
|
|
|
close(started)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2014-09-10 20:06:59 -04:00
|
|
|
params, err := cli.encodeData(data)
|
2014-09-09 01:51:53 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req, err := http.NewRequest(method, fmt.Sprintf("/v%s%s", api.APIVERSION, path), params)
|
2014-05-01 20:40:13 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req.Header.Set("User-Agent", "Docker-Client/"+dockerversion.VERSION)
|
2015-01-07 11:19:14 -05:00
|
|
|
req.Header.Set("Content-Type", "text/plain")
|
2014-12-13 07:00:59 -05:00
|
|
|
req.Header.Set("Connection", "Upgrade")
|
|
|
|
req.Header.Set("Upgrade", "tcp")
|
2014-05-01 20:40:13 -04:00
|
|
|
req.Host = cli.addr
|
|
|
|
|
|
|
|
dial, err := cli.dial()
|
2015-01-30 10:50:27 -05:00
|
|
|
// When we set up a TCP connection for hijack, there could be long periods
|
|
|
|
// of inactivity (a long running command with no output) that in certain
|
|
|
|
// network setups may cause ECONNTIMEOUT, leaving the client in an unknown
|
|
|
|
// state. Setting TCP KeepAlive on the socket connection will prohibit
|
|
|
|
// ECONNTIMEOUT unless the socket connection truly is broken
|
|
|
|
if tcpConn, ok := dial.(*net.TCPConn); ok {
|
|
|
|
tcpConn.SetKeepAlive(true)
|
|
|
|
tcpConn.SetKeepAlivePeriod(30 * time.Second)
|
|
|
|
}
|
2014-05-01 20:40:13 -04:00
|
|
|
if err != nil {
|
|
|
|
if strings.Contains(err.Error(), "connection refused") {
|
|
|
|
return fmt.Errorf("Cannot connect to the Docker daemon. Is 'docker -d' running on this host?")
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
clientconn := httputil.NewClientConn(dial, nil)
|
|
|
|
defer clientconn.Close()
|
|
|
|
|
|
|
|
// Server hijacks the connection, error 'connection closed' expected
|
|
|
|
clientconn.Do(req)
|
|
|
|
|
|
|
|
rwc, br := clientconn.Hijack()
|
|
|
|
defer rwc.Close()
|
|
|
|
|
|
|
|
if started != nil {
|
|
|
|
started <- rwc
|
|
|
|
}
|
|
|
|
|
|
|
|
var receiveStdout chan error
|
|
|
|
|
|
|
|
var oldState *term.State
|
|
|
|
|
2014-09-10 09:35:48 -04:00
|
|
|
if in != nil && setRawTerminal && cli.isTerminalIn && os.Getenv("NORAW") == "" {
|
|
|
|
oldState, err = term.SetRawTerminal(cli.inFd)
|
2014-05-01 20:40:13 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-09-10 09:35:48 -04:00
|
|
|
defer term.RestoreTerminal(cli.inFd, oldState)
|
2014-05-01 20:40:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if stdout != nil || stderr != nil {
|
2014-09-30 02:16:27 -04:00
|
|
|
receiveStdout = promise.Go(func() (err error) {
|
2014-05-01 20:40:13 -04:00
|
|
|
defer func() {
|
|
|
|
if in != nil {
|
2014-09-10 09:35:48 -04:00
|
|
|
if setRawTerminal && cli.isTerminalIn {
|
|
|
|
term.RestoreTerminal(cli.inFd, oldState)
|
2014-05-01 20:40:13 -04:00
|
|
|
}
|
|
|
|
// For some reason this Close call blocks on darwin..
|
|
|
|
// As the client exists right after, simply discard the close
|
|
|
|
// until we find a better solution.
|
|
|
|
if runtime.GOOS != "darwin" {
|
|
|
|
in.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// When TTY is ON, use regular copy
|
2014-07-16 13:47:12 -04:00
|
|
|
if setRawTerminal && stdout != nil {
|
2014-05-01 20:40:13 -04:00
|
|
|
_, err = io.Copy(stdout, br)
|
|
|
|
} else {
|
2014-09-17 11:04:56 -04:00
|
|
|
_, err = stdcopy.StdCopy(stdout, stderr, br)
|
2014-05-01 20:40:13 -04:00
|
|
|
}
|
2014-07-24 16:37:44 -04:00
|
|
|
log.Debugf("[hijack] End of stdout")
|
2014-05-01 20:40:13 -04:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-09-30 02:16:27 -04:00
|
|
|
sendStdin := promise.Go(func() error {
|
2014-05-01 20:40:13 -04:00
|
|
|
if in != nil {
|
|
|
|
io.Copy(rwc, in)
|
2014-07-24 16:37:44 -04:00
|
|
|
log.Debugf("[hijack] End of stdin")
|
2014-05-01 20:40:13 -04:00
|
|
|
}
|
2014-12-01 20:57:53 -05:00
|
|
|
|
|
|
|
if conn, ok := rwc.(interface {
|
|
|
|
CloseWrite() error
|
|
|
|
}); ok {
|
|
|
|
if err := conn.CloseWrite(); err != nil {
|
2014-08-13 21:36:26 -04:00
|
|
|
log.Debugf("Couldn't send EOF: %s", err)
|
2014-05-01 20:40:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Discard errors due to pipe interruption
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if stdout != nil || stderr != nil {
|
|
|
|
if err := <-receiveStdout; err != nil {
|
2014-07-24 16:37:44 -04:00
|
|
|
log.Debugf("Error receiveStdout: %s", err)
|
2014-05-01 20:40:13 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-10 09:35:48 -04:00
|
|
|
if !cli.isTerminalIn {
|
2014-05-01 20:40:13 -04:00
|
|
|
if err := <-sendStdin; err != nil {
|
2014-07-24 16:37:44 -04:00
|
|
|
log.Debugf("Error sendStdin: %s", err)
|
2014-05-01 20:40:13 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|