2016-10-08 13:34:37 +00:00
|
|
|
/*
|
2016-11-15 19:45:20 +00:00
|
|
|
Package client is a Go client for the Docker Engine API.
|
2016-10-08 13:34:37 +00:00
|
|
|
|
2016-11-15 19:45:20 +00:00
|
|
|
For more information about the Engine API, see the documentation:
|
2021-02-25 11:11:50 +00:00
|
|
|
https://docs.docker.com/engine/api/
|
2016-10-08 13:34:37 +00:00
|
|
|
|
2022-07-08 16:27:07 +00:00
|
|
|
# Usage
|
2016-10-08 13:34:37 +00:00
|
|
|
|
2023-03-31 20:15:01 +00:00
|
|
|
You use the library by constructing a client object using [NewClientWithOpts]
|
|
|
|
and calling methods on it. The client can be configured from environment
|
|
|
|
variables by passing the [FromEnv] option, or configured manually by passing any
|
|
|
|
of the other available [Opts].
|
2016-10-08 13:34:37 +00:00
|
|
|
|
|
|
|
For example, to list running containers (the equivalent of "docker ps"):
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/client"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2019-01-03 21:49:00 +00:00
|
|
|
cli, err := client.NewClientWithOpts(client.FromEnv)
|
2016-10-08 13:34:37 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, container := range containers {
|
|
|
|
fmt.Printf("%s %s\n", container.ID[:10], container.Image)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
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 (
|
2018-04-19 22:30:59 +00:00
|
|
|
"context"
|
2023-08-01 09:37:41 +00:00
|
|
|
"crypto/tls"
|
2018-02-16 18:24:57 +00:00
|
|
|
"net"
|
2016-09-06 18:46:37 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2017-08-11 17:47:02 +00:00
|
|
|
"path"
|
2016-09-06 18:46:37 +00:00
|
|
|
"strings"
|
2023-08-01 09:37:41 +00:00
|
|
|
"time"
|
2016-09-06 18:46:37 +00:00
|
|
|
|
2017-02-19 08:43:08 +00:00
|
|
|
"github.com/docker/docker/api"
|
2017-06-21 05:58:16 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/versions"
|
2016-09-09 03:44:25 +00:00
|
|
|
"github.com/docker/go-connections/sockets"
|
2018-02-16 18:24:57 +00:00
|
|
|
"github.com/pkg/errors"
|
2016-09-06 18:46:37 +00:00
|
|
|
)
|
|
|
|
|
client: define a "dummy" hostname to use for local connections
For local communications (npipe://, unix://), the hostname is not used,
but we need valid and meaningful hostname.
The current code used the client's `addr` as hostname in some cases, which
could contain the path for the unix-socket (`/var/run/docker.sock`), which
gets rejected by go1.20.6 and go1.19.11 because of a security fix for
[CVE-2023-29406 ][1], which was implemented in https://go.dev/issue/60374.
Prior versions go Go would clean the host header, and strip slashes in the
process, but go1.20.6 and go1.19.11 no longer do, and reject the host
header.
This patch introduces a `DummyHost` const, and uses this dummy host for
cases where we don't need an actual hostname.
Before this patch (using go1.20.6):
make GO_VERSION=1.20.6 TEST_FILTER=TestAttach test-integration
=== RUN TestAttachWithTTY
attach_test.go:46: assertion failed: error is not nil: http: invalid Host header
--- FAIL: TestAttachWithTTY (0.11s)
=== RUN TestAttachWithoutTTy
attach_test.go:46: assertion failed: error is not nil: http: invalid Host header
--- FAIL: TestAttachWithoutTTy (0.02s)
FAIL
With this patch applied:
make GO_VERSION=1.20.6 TEST_FILTER=TestAttach test-integration
INFO: Testing against a local daemon
=== RUN TestAttachWithTTY
--- PASS: TestAttachWithTTY (0.12s)
=== RUN TestAttachWithoutTTy
--- PASS: TestAttachWithoutTTy (0.02s)
PASS
[1]: https://github.com/advisories/GHSA-f8f7-69v5-w4vx
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-07-12 12:15:38 +00:00
|
|
|
// DummyHost is a hostname used for local communication.
|
|
|
|
//
|
|
|
|
// It acts as a valid formatted hostname for local connections (such as "unix://"
|
|
|
|
// or "npipe://") which do not require a hostname. It should never be resolved,
|
|
|
|
// but uses the special-purpose ".localhost" TLD (as defined in [RFC 2606, Section 2]
|
|
|
|
// and [RFC 6761, Section 6.3]).
|
|
|
|
//
|
|
|
|
// [RFC 7230, Section 5.4] defines that an empty header must be used for such
|
|
|
|
// cases:
|
|
|
|
//
|
|
|
|
// If the authority component is missing or undefined for the target URI,
|
|
|
|
// then a client MUST send a Host header field with an empty field-value.
|
|
|
|
//
|
|
|
|
// However, [Go stdlib] enforces the semantics of HTTP(S) over TCP, does not
|
|
|
|
// allow an empty header to be used, and requires req.URL.Scheme to be either
|
|
|
|
// "http" or "https".
|
|
|
|
//
|
|
|
|
// For further details, refer to:
|
|
|
|
//
|
|
|
|
// - https://github.com/docker/engine-api/issues/189
|
|
|
|
// - https://github.com/golang/go/issues/13624
|
|
|
|
// - https://github.com/golang/go/issues/61076
|
|
|
|
// - https://github.com/moby/moby/issues/45935
|
|
|
|
//
|
|
|
|
// [RFC 2606, Section 2]: https://www.rfc-editor.org/rfc/rfc2606.html#section-2
|
|
|
|
// [RFC 6761, Section 6.3]: https://www.rfc-editor.org/rfc/rfc6761#section-6.3
|
|
|
|
// [RFC 7230, Section 5.4]: https://datatracker.ietf.org/doc/html/rfc7230#section-5.4
|
|
|
|
// [Go stdlib]: https://github.com/golang/go/blob/6244b1946bc2101b01955468f1be502dbadd6807/src/net/http/transport.go#L558-L569
|
|
|
|
const DummyHost = "api.moby.localhost"
|
|
|
|
|
2016-09-06 18:46:37 +00:00
|
|
|
// Client is the API client that performs all operations
|
|
|
|
// against a docker server.
|
|
|
|
type Client struct {
|
2016-10-11 22:53:14 +00:00
|
|
|
// scheme sets the scheme for the client
|
|
|
|
scheme string
|
2016-09-06 18:46:37 +00:00
|
|
|
// host holds the server address to connect to
|
|
|
|
host string
|
|
|
|
// proto holds the client protocol i.e. unix.
|
|
|
|
proto string
|
|
|
|
// addr holds the client address.
|
|
|
|
addr string
|
|
|
|
// basePath holds the path to prepend to the requests.
|
|
|
|
basePath string
|
2016-09-09 03:44:25 +00:00
|
|
|
// client used to send and receive http requests.
|
|
|
|
client *http.Client
|
2016-09-06 18:46:37 +00:00
|
|
|
// version of the server to talk to.
|
|
|
|
version string
|
2023-06-01 20:42:46 +00:00
|
|
|
// userAgent is the User-Agent header to use for HTTP requests. It takes
|
|
|
|
// precedence over User-Agent headers set in customHTTPHeaders, and other
|
|
|
|
// header variables. When set to an empty string, the User-Agent header
|
|
|
|
// is removed, and no header is sent.
|
|
|
|
userAgent *string
|
2023-07-15 15:22:06 +00:00
|
|
|
// custom HTTP headers configured by users.
|
2016-09-06 18:46:37 +00:00
|
|
|
customHTTPHeaders map[string]string
|
2016-11-03 00:43:32 +00:00
|
|
|
// manualOverride is set to true when the version was set by users.
|
|
|
|
manualOverride bool
|
2019-04-08 12:14:07 +00:00
|
|
|
|
|
|
|
// negotiateVersion indicates if the client should automatically negotiate
|
|
|
|
// the API version to use when making requests. API version negotiation is
|
|
|
|
// performed on the first request, after which negotiated is set to "true"
|
|
|
|
// so that subsequent requests do not re-negotiate.
|
|
|
|
negotiateVersion bool
|
|
|
|
|
|
|
|
// negotiated indicates that API version negotiation took place
|
|
|
|
negotiated bool
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
|
2023-07-15 15:22:06 +00:00
|
|
|
// ErrRedirect is the error returned by checkRedirect when the request is non-GET.
|
|
|
|
var ErrRedirect = errors.New("unexpected redirect in response")
|
|
|
|
|
|
|
|
// CheckRedirect specifies the policy for dealing with redirect responses. It
|
|
|
|
// can be set on [http.Client.CheckRedirect] to prevent HTTP redirects for
|
|
|
|
// non-GET requests. It returns an [ErrRedirect] for non-GET request, otherwise
|
|
|
|
// returns a [http.ErrUseLastResponse], which is special-cased by http.Client
|
|
|
|
// to use the last response.
|
2017-03-27 09:05:35 +00:00
|
|
|
//
|
2023-07-15 15:22:06 +00:00
|
|
|
// Go 1.8 changed behavior for HTTP redirects (specifically 301, 307, and 308)
|
|
|
|
// in the client. The client (and by extension API client) can be made to send
|
|
|
|
// a request like "POST /containers//start" where what would normally be in the
|
|
|
|
// name section of the URL is empty. This triggers an HTTP 301 from the daemon.
|
2022-03-07 10:29:38 +00:00
|
|
|
//
|
2023-07-15 15:22:06 +00:00
|
|
|
// In go 1.8 this 301 is converted to a GET request, and ends up getting
|
2022-03-07 10:29:38 +00:00
|
|
|
// a 404 from the daemon. This behavior change manifests in the client in that
|
|
|
|
// before, the 301 was not followed and the client did not generate an error,
|
2023-07-15 15:22:06 +00:00
|
|
|
// but now results in a message like "Error response from daemon: page not found".
|
|
|
|
func CheckRedirect(_ *http.Request, via []*http.Request) error {
|
2017-03-27 09:05:35 +00:00
|
|
|
if via[0].Method == http.MethodGet {
|
|
|
|
return http.ErrUseLastResponse
|
|
|
|
}
|
|
|
|
return ErrRedirect
|
|
|
|
}
|
|
|
|
|
2022-03-07 10:29:38 +00:00
|
|
|
// NewClientWithOpts initializes a new API client with a default HTTPClient, and
|
|
|
|
// default API host and version. It also initializes the custom HTTP headers to
|
|
|
|
// add to each request.
|
|
|
|
//
|
2023-07-15 15:22:06 +00:00
|
|
|
// It takes an optional list of [Opt] functional arguments, which are applied in
|
2022-03-07 10:29:38 +00:00
|
|
|
// the order they're provided, which allows modifying the defaults when creating
|
|
|
|
// the client. For example, the following initializes a client that configures
|
2023-07-15 15:22:06 +00:00
|
|
|
// itself with values from environment variables ([FromEnv]), and has automatic
|
|
|
|
// API version negotiation enabled ([WithAPIVersionNegotiation]).
|
2022-03-07 10:29:38 +00:00
|
|
|
//
|
|
|
|
// cli, err := client.NewClientWithOpts(
|
|
|
|
// client.FromEnv,
|
|
|
|
// client.WithAPIVersionNegotiation(),
|
|
|
|
// )
|
2019-04-09 23:11:53 +00:00
|
|
|
func NewClientWithOpts(ops ...Opt) (*Client, error) {
|
2022-10-09 20:40:18 +00:00
|
|
|
hostURL, err := ParseHostURL(DefaultDockerHost)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := defaultHTTPClient(hostURL)
|
2016-09-06 18:46:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-31 19:28:33 +00:00
|
|
|
c := &Client{
|
|
|
|
host: DefaultDockerHost,
|
|
|
|
version: api.DefaultVersion,
|
|
|
|
client: client,
|
2022-10-09 20:40:18 +00:00
|
|
|
proto: hostURL.Scheme,
|
|
|
|
addr: hostURL.Host,
|
2018-01-31 19:28:33 +00:00
|
|
|
}
|
2016-09-06 18:46:37 +00:00
|
|
|
|
2018-01-31 19:28:33 +00:00
|
|
|
for _, op := range ops {
|
|
|
|
if err := op(c); err != nil {
|
|
|
|
return nil, err
|
2016-09-22 02:16:44 +00:00
|
|
|
}
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
|
2018-11-01 17:40:37 +00:00
|
|
|
if c.scheme == "" {
|
2023-08-01 10:13:25 +00:00
|
|
|
// TODO(stevvooe): This isn't really the right way to write clients in Go.
|
|
|
|
// `NewClient` should probably only take an `*http.Client` and work from there.
|
|
|
|
// Unfortunately, the model of having a host-ish/url-thingy as the connection
|
|
|
|
// string has us confusing protocol and transport layers. We continue doing
|
|
|
|
// this to avoid breaking existing clients but this should be addressed.
|
|
|
|
if c.tlsConfig() != nil {
|
2018-11-01 17:40:37 +00:00
|
|
|
c.scheme = "https"
|
2023-08-01 10:13:25 +00:00
|
|
|
} else {
|
|
|
|
c.scheme = "http"
|
2018-11-01 17:40:37 +00:00
|
|
|
}
|
2018-01-31 19:28:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2022-10-09 20:40:18 +00:00
|
|
|
func defaultHTTPClient(hostURL *url.URL) (*http.Client, error) {
|
2022-03-07 10:29:38 +00:00
|
|
|
transport := &http.Transport{}
|
2023-01-01 12:39:21 +00:00
|
|
|
err := sockets.ConfigureTransport(transport, hostURL.Scheme, hostURL.Host)
|
2018-01-31 19:28:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &http.Client{
|
|
|
|
Transport: transport,
|
|
|
|
CheckRedirect: CheckRedirect,
|
2016-09-06 18:46:37 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-08-01 10:13:25 +00:00
|
|
|
// tlsConfig returns the TLS configuration from the client's transport.
|
|
|
|
// It returns nil if the transport is not a [http.Transport], or if no
|
|
|
|
// TLSClientConfig is set.
|
|
|
|
func (cli *Client) tlsConfig() *tls.Config {
|
|
|
|
if tr, ok := cli.client.Transport.(*http.Transport); ok {
|
|
|
|
return tr.TLSClientConfig
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-07 16:22:11 +00:00
|
|
|
// Close the transport used by the client
|
2016-09-08 01:57:54 +00:00
|
|
|
func (cli *Client) Close() error {
|
|
|
|
if t, ok := cli.client.Transport.(*http.Transport); ok {
|
|
|
|
t.CloseIdleConnections()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-15 15:22:06 +00:00
|
|
|
// getAPIPath returns the versioned request path to call the API.
|
2016-09-06 18:46:37 +00:00
|
|
|
// It appends the query parameters to the path if they are not empty.
|
2019-04-08 12:14:07 +00:00
|
|
|
func (cli *Client) getAPIPath(ctx context.Context, p string, query url.Values) string {
|
2016-09-06 18:46:37 +00:00
|
|
|
var apiPath string
|
2019-04-08 12:14:07 +00:00
|
|
|
if cli.negotiateVersion && !cli.negotiated {
|
|
|
|
cli.NegotiateAPIVersion(ctx)
|
|
|
|
}
|
2016-09-06 18:46:37 +00:00
|
|
|
if cli.version != "" {
|
|
|
|
v := strings.TrimPrefix(cli.version, "v")
|
2017-09-07 17:46:23 +00:00
|
|
|
apiPath = path.Join(cli.basePath, "/v"+v, p)
|
2016-09-06 18:46:37 +00:00
|
|
|
} else {
|
2017-08-11 17:47:02 +00:00
|
|
|
apiPath = path.Join(cli.basePath, p)
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
2017-09-07 17:46:23 +00:00
|
|
|
return (&url.URL{Path: apiPath, RawQuery: query.Encode()}).String()
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
|
2017-09-07 16:22:11 +00:00
|
|
|
// ClientVersion returns the API version used by this client.
|
2016-09-06 18:46:37 +00:00
|
|
|
func (cli *Client) ClientVersion() string {
|
|
|
|
return cli.version
|
|
|
|
}
|
|
|
|
|
2022-03-07 10:29:38 +00:00
|
|
|
// NegotiateAPIVersion queries the API and updates the version to match the API
|
|
|
|
// version. NegotiateAPIVersion downgrades the client's API version to match the
|
|
|
|
// APIVersion if the ping version is lower than the default version. If the API
|
|
|
|
// version reported by the server is higher than the maximum version supported
|
|
|
|
// by the client, it uses the client's maximum version.
|
|
|
|
//
|
|
|
|
// If a manual override is in place, either through the "DOCKER_API_VERSION"
|
2023-07-15 15:22:06 +00:00
|
|
|
// ([EnvOverrideAPIVersion]) environment variable, or if the client is initialized
|
|
|
|
// with a fixed version ([WithVersion]), no negotiation is performed.
|
2022-03-07 10:29:38 +00:00
|
|
|
//
|
|
|
|
// If the API server's ping response does not contain an API version, or if the
|
|
|
|
// client did not get a successful ping response, it assumes it is connected with
|
|
|
|
// an old daemon that does not support API version negotiation, in which case it
|
|
|
|
// downgrades to the latest version of the API before version negotiation was
|
|
|
|
// added (1.24).
|
2017-06-21 05:58:16 +00:00
|
|
|
func (cli *Client) NegotiateAPIVersion(ctx context.Context) {
|
2019-04-08 12:14:07 +00:00
|
|
|
if !cli.manualOverride {
|
|
|
|
ping, _ := cli.Ping(ctx)
|
|
|
|
cli.negotiateAPIVersionPing(ping)
|
|
|
|
}
|
2017-06-21 05:58:16 +00:00
|
|
|
}
|
|
|
|
|
2022-03-07 10:29:38 +00:00
|
|
|
// NegotiateAPIVersionPing downgrades the client's API version to match the
|
|
|
|
// APIVersion in the ping response. If the API version in pingResponse is higher
|
|
|
|
// than the maximum version supported by the client, it uses the client's maximum
|
|
|
|
// version.
|
|
|
|
//
|
|
|
|
// If a manual override is in place, either through the "DOCKER_API_VERSION"
|
2023-07-15 15:22:06 +00:00
|
|
|
// ([EnvOverrideAPIVersion]) environment variable, or if the client is initialized
|
|
|
|
// with a fixed version ([WithVersion]), no negotiation is performed.
|
2022-03-07 10:29:38 +00:00
|
|
|
//
|
|
|
|
// If the API server's ping response does not contain an API version, we assume
|
|
|
|
// we are connected with an old daemon without API version negotiation support,
|
|
|
|
// and downgrade to the latest version of the API before version negotiation was
|
|
|
|
// added (1.24).
|
|
|
|
func (cli *Client) NegotiateAPIVersionPing(pingResponse types.Ping) {
|
2019-04-08 12:14:07 +00:00
|
|
|
if !cli.manualOverride {
|
2022-03-07 10:29:38 +00:00
|
|
|
cli.negotiateAPIVersionPing(pingResponse)
|
2016-11-03 00:43:32 +00:00
|
|
|
}
|
2019-04-08 12:14:07 +00:00
|
|
|
}
|
2016-11-03 00:43:32 +00:00
|
|
|
|
2019-04-08 12:14:07 +00:00
|
|
|
// negotiateAPIVersionPing queries the API and updates the version to match the
|
2022-03-07 10:29:38 +00:00
|
|
|
// API version from the ping response.
|
|
|
|
func (cli *Client) negotiateAPIVersionPing(pingResponse types.Ping) {
|
|
|
|
// default to the latest version before versioning headers existed
|
|
|
|
if pingResponse.APIVersion == "" {
|
|
|
|
pingResponse.APIVersion = "1.24"
|
2017-06-21 05:58:16 +00:00
|
|
|
}
|
|
|
|
|
2017-06-30 05:24:49 +00:00
|
|
|
// if the client is not initialized with a version, start with the latest supported version
|
|
|
|
if cli.version == "" {
|
|
|
|
cli.version = api.DefaultVersion
|
|
|
|
}
|
|
|
|
|
2017-09-28 01:12:13 +00:00
|
|
|
// if server version is lower than the client version, downgrade
|
2022-03-07 10:29:38 +00:00
|
|
|
if versions.LessThan(pingResponse.APIVersion, cli.version) {
|
|
|
|
cli.version = pingResponse.APIVersion
|
2017-06-21 05:58:16 +00:00
|
|
|
}
|
2019-04-08 12:14:07 +00:00
|
|
|
|
|
|
|
// Store the results, so that automatic API version negotiation (if enabled)
|
|
|
|
// won't be performed on the next request.
|
|
|
|
if cli.negotiateVersion {
|
|
|
|
cli.negotiated = true
|
|
|
|
}
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
|
2017-09-07 16:22:11 +00:00
|
|
|
// DaemonHost returns the host address used by the client
|
2017-05-17 14:46:15 +00:00
|
|
|
func (cli *Client) DaemonHost() string {
|
|
|
|
return cli.host
|
|
|
|
}
|
|
|
|
|
2018-05-15 13:12:30 +00:00
|
|
|
// HTTPClient returns a copy of the HTTP client bound to the server
|
|
|
|
func (cli *Client) HTTPClient() *http.Client {
|
2019-08-05 14:48:52 +00:00
|
|
|
c := *cli.client
|
|
|
|
return &c
|
2018-05-15 13:12:30 +00:00
|
|
|
}
|
|
|
|
|
2017-09-07 16:22:11 +00:00
|
|
|
// ParseHostURL parses a url string, validates the string is a host url, and
|
|
|
|
// returns the parsed URL
|
|
|
|
func ParseHostURL(host string) (*url.URL, error) {
|
2022-10-31 18:06:50 +00:00
|
|
|
proto, addr, ok := strings.Cut(host, "://")
|
|
|
|
if !ok || addr == "" {
|
2022-03-07 10:29:38 +00:00
|
|
|
return nil, errors.Errorf("unable to parse docker host `%s`", host)
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var basePath string
|
|
|
|
if proto == "tcp" {
|
|
|
|
parsed, err := url.Parse("tcp://" + addr)
|
|
|
|
if err != nil {
|
2017-09-07 16:22:11 +00:00
|
|
|
return nil, err
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
addr = parsed.Host
|
|
|
|
basePath = parsed.Path
|
|
|
|
}
|
2017-09-07 16:22:11 +00:00
|
|
|
return &url.URL{
|
|
|
|
Scheme: proto,
|
|
|
|
Host: addr,
|
|
|
|
Path: basePath,
|
|
|
|
}, nil
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
2016-10-21 05:41:54 +00:00
|
|
|
|
2022-03-07 10:29:38 +00:00
|
|
|
// Dialer returns a dialer for a raw stream connection, with an HTTP/1.1 header,
|
2023-07-15 15:22:06 +00:00
|
|
|
// that can be used for proxying the daemon connection. It is used by
|
|
|
|
// ["docker dial-stdio"].
|
2022-03-07 10:29:38 +00:00
|
|
|
//
|
2023-07-15 15:22:06 +00:00
|
|
|
// ["docker dial-stdio"]: https://github.com/docker/cli/pull/1014
|
2018-03-19 08:33:06 +00:00
|
|
|
func (cli *Client) Dialer() func(context.Context) (net.Conn, error) {
|
|
|
|
return func(ctx context.Context) (net.Conn, error) {
|
|
|
|
if transport, ok := cli.client.Transport.(*http.Transport); ok {
|
2018-09-04 22:09:44 +00:00
|
|
|
if transport.DialContext != nil && transport.TLSClientConfig == nil {
|
2018-03-19 08:33:06 +00:00
|
|
|
return transport.DialContext(ctx, cli.proto, cli.addr)
|
|
|
|
}
|
|
|
|
}
|
2023-08-01 09:37:41 +00:00
|
|
|
switch cli.proto {
|
|
|
|
case "unix":
|
|
|
|
return net.Dial(cli.proto, cli.addr)
|
|
|
|
case "npipe":
|
|
|
|
return sockets.DialPipe(cli.addr, 32*time.Second)
|
|
|
|
default:
|
2023-08-01 10:13:25 +00:00
|
|
|
if tlsConfig := cli.tlsConfig(); tlsConfig != nil {
|
2023-08-01 09:37:41 +00:00
|
|
|
return tls.Dial(cli.proto, cli.addr, tlsConfig)
|
|
|
|
}
|
|
|
|
return net.Dial(cli.proto, cli.addr)
|
|
|
|
}
|
2018-03-19 08:33:06 +00:00
|
|
|
}
|
|
|
|
}
|