2018-02-05 21:05:59 +00:00
|
|
|
package client // import "github.com/docker/docker/client"
|
2016-09-06 18:46:37 +00:00
|
|
|
|
|
|
|
import (
|
2017-05-15 19:59:15 +00:00
|
|
|
"bufio"
|
2018-04-19 22:30:59 +00:00
|
|
|
"context"
|
2016-09-06 18:46:37 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2016-10-31 16:39:38 +00:00
|
|
|
"net/http"
|
2016-09-06 18:46:37 +00:00
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
2019-08-28 11:46:32 +00:00
|
|
|
"github.com/docker/docker/api/types/versions"
|
2017-05-15 19:59:15 +00:00
|
|
|
"github.com/pkg/errors"
|
2023-12-13 23:48:14 +00:00
|
|
|
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
|
2016-09-06 18:46:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// postHijacked sends a POST request and hijacks the connection.
|
|
|
|
func (cli *Client) postHijacked(ctx context.Context, path string, query url.Values, body interface{}, headers map[string][]string) (types.HijackedResponse, error) {
|
|
|
|
bodyEncoded, err := encodeData(body)
|
|
|
|
if err != nil {
|
|
|
|
return types.HijackedResponse{}, err
|
|
|
|
}
|
2023-07-14 16:23:59 +00:00
|
|
|
req, err := cli.buildRequest(ctx, http.MethodPost, cli.getAPIPath(ctx, path, query), bodyEncoded, headers)
|
2016-09-06 18:46:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return types.HijackedResponse{}, err
|
|
|
|
}
|
2023-07-14 16:23:59 +00:00
|
|
|
conn, mediaType, err := cli.setupHijackConn(req, "tcp")
|
2016-09-06 18:46:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return types.HijackedResponse{}, err
|
|
|
|
}
|
|
|
|
|
2019-08-28 11:46:32 +00:00
|
|
|
return types.NewHijackedResponse(conn, mediaType), err
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
|
2019-04-02 04:08:16 +00:00
|
|
|
// DialHijack returns a hijacked connection with negotiated protocol proto.
|
|
|
|
func (cli *Client) DialHijack(ctx context.Context, url, proto string, meta map[string][]string) (net.Conn, error) {
|
2023-07-14 16:23:59 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, nil)
|
2019-04-02 04:08:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req = cli.addHeaders(req, meta)
|
|
|
|
|
2023-07-14 16:23:59 +00:00
|
|
|
conn, _, err := cli.setupHijackConn(req, proto)
|
2019-08-28 11:46:32 +00:00
|
|
|
return conn, err
|
2019-04-02 04:08:16 +00:00
|
|
|
}
|
|
|
|
|
2023-07-14 17:59:45 +00:00
|
|
|
func (cli *Client) setupHijackConn(req *http.Request, proto string) (_ net.Conn, _ string, retErr error) {
|
2023-07-14 16:23:59 +00:00
|
|
|
ctx := req.Context()
|
2017-05-15 19:59:15 +00:00
|
|
|
req.Header.Set("Connection", "Upgrade")
|
|
|
|
req.Header.Set("Upgrade", proto)
|
|
|
|
|
2018-03-19 08:33:06 +00:00
|
|
|
dialer := cli.Dialer()
|
|
|
|
conn, err := dialer(ctx)
|
2017-05-15 19:59:15 +00:00
|
|
|
if err != nil {
|
2019-08-28 11:46:32 +00:00
|
|
|
return nil, "", errors.Wrap(err, "cannot connect to the Docker daemon. Is 'docker daemon' running on this host?")
|
2017-05-15 19:59:15 +00:00
|
|
|
}
|
2023-12-10 06:10:03 +00:00
|
|
|
defer func() {
|
|
|
|
if retErr != nil {
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
}()
|
2017-05-15 19:59:15 +00: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 := conn.(*net.TCPConn); ok {
|
2023-07-15 00:37:03 +00:00
|
|
|
_ = tcpConn.SetKeepAlive(true)
|
|
|
|
_ = tcpConn.SetKeepAlivePeriod(30 * time.Second)
|
2017-05-15 19:59:15 +00:00
|
|
|
}
|
|
|
|
|
2023-12-10 06:10:03 +00:00
|
|
|
hc := &hijackedConn{conn, bufio.NewReader(conn)}
|
2017-05-15 19:59:15 +00:00
|
|
|
|
|
|
|
// Server hijacks the connection, error 'connection closed' expected
|
2023-12-13 23:48:14 +00:00
|
|
|
resp, err := otelhttp.NewTransport(hc).RoundTrip(req)
|
2023-12-10 06:10:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusSwitchingProtocols {
|
|
|
|
_ = resp.Body.Close()
|
|
|
|
return nil, "", fmt.Errorf("unable to upgrade to %s, received %d", proto, resp.StatusCode)
|
2017-05-15 19:59:15 +00:00
|
|
|
}
|
|
|
|
|
2023-12-10 06:10:03 +00:00
|
|
|
if hc.r.Buffered() > 0 {
|
2018-03-07 18:23:03 +00:00
|
|
|
// If there is buffered content, wrap the connection. We return an
|
2019-08-28 11:46:32 +00:00
|
|
|
// object that implements CloseWrite if the underlying connection
|
2018-03-07 18:23:03 +00:00
|
|
|
// implements it.
|
2023-12-10 06:10:03 +00:00
|
|
|
if _, ok := hc.Conn.(types.CloseWriter); ok {
|
|
|
|
conn = &hijackedConnCloseWriter{hc}
|
2018-03-07 18:23:03 +00:00
|
|
|
} else {
|
2023-12-10 06:10:03 +00:00
|
|
|
conn = hc
|
2018-03-07 18:23:03 +00:00
|
|
|
}
|
2017-05-15 19:59:15 +00:00
|
|
|
} else {
|
2023-12-10 06:10:03 +00:00
|
|
|
hc.r.Reset(nil)
|
2017-05-15 19:59:15 +00:00
|
|
|
}
|
|
|
|
|
2019-08-28 11:46:32 +00:00
|
|
|
var mediaType string
|
|
|
|
if versions.GreaterThanOrEqualTo(cli.ClientVersion(), "1.42") {
|
|
|
|
// Prior to 1.42, Content-Type is always set to raw-stream and not relevant
|
|
|
|
mediaType = resp.Header.Get("Content-Type")
|
|
|
|
}
|
|
|
|
|
2023-12-10 06:10:03 +00:00
|
|
|
return conn, mediaType, nil
|
2017-05-15 19:59:15 +00:00
|
|
|
}
|
|
|
|
|
2018-03-07 18:23:03 +00:00
|
|
|
// hijackedConn wraps a net.Conn and is returned by setupHijackConn in the case
|
|
|
|
// that a) there was already buffered data in the http layer when Hijack() was
|
|
|
|
// called, and b) the underlying net.Conn does *not* implement CloseWrite().
|
|
|
|
// hijackedConn does not implement CloseWrite() either.
|
2017-05-15 19:59:15 +00:00
|
|
|
type hijackedConn struct {
|
|
|
|
net.Conn
|
|
|
|
r *bufio.Reader
|
|
|
|
}
|
|
|
|
|
2023-12-10 06:10:03 +00:00
|
|
|
func (c *hijackedConn) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
|
|
if err := req.Write(c.Conn); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return http.ReadResponse(c.r, req)
|
|
|
|
}
|
|
|
|
|
2017-05-15 19:59:15 +00:00
|
|
|
func (c *hijackedConn) Read(b []byte) (int, error) {
|
|
|
|
return c.r.Read(b)
|
|
|
|
}
|
2018-03-07 18:23:03 +00:00
|
|
|
|
|
|
|
// hijackedConnCloseWriter is a hijackedConn which additionally implements
|
|
|
|
// CloseWrite(). It is returned by setupHijackConn in the case that a) there
|
|
|
|
// was already buffered data in the http layer when Hijack() was called, and b)
|
|
|
|
// the underlying net.Conn *does* implement CloseWrite().
|
2018-03-21 22:29:44 +00:00
|
|
|
type hijackedConnCloseWriter struct {
|
|
|
|
*hijackedConn
|
|
|
|
}
|
2018-03-07 18:23:03 +00:00
|
|
|
|
|
|
|
var _ types.CloseWriter = &hijackedConnCloseWriter{}
|
|
|
|
|
|
|
|
func (c *hijackedConnCloseWriter) CloseWrite() error {
|
|
|
|
conn := c.Conn.(types.CloseWriter)
|
|
|
|
return conn.CloseWrite()
|
|
|
|
}
|