|
| 1 | +/* |
| 2 | + Copyright 2026 Docker Compose CLI authors |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package desktop |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "net" |
| 22 | + "net/http" |
| 23 | + "net/url" |
| 24 | + "os" |
| 25 | + "path/filepath" |
| 26 | + "strings" |
| 27 | + |
| 28 | + "github.com/moby/moby/client" |
| 29 | + "github.com/sirupsen/logrus" |
| 30 | + |
| 31 | + "github.com/docker/compose/v5/internal/memnet" |
| 32 | +) |
| 33 | + |
| 34 | +// Endpoint returns the Docker Desktop API socket endpoint advertised via the |
| 35 | +// engine info labels, or "" when the active engine is not Docker Desktop. |
| 36 | +func Endpoint(ctx context.Context, apiClient client.APIClient) (string, error) { |
| 37 | + res, err := apiClient.Info(ctx, client.InfoOptions{}) |
| 38 | + if err != nil { |
| 39 | + return "", err |
| 40 | + } |
| 41 | + for _, l := range res.Info.Labels { |
| 42 | + if k, v, ok := strings.Cut(l, "="); ok && k == EngineLabel { |
| 43 | + return v, nil |
| 44 | + } |
| 45 | + } |
| 46 | + return "", nil |
| 47 | +} |
| 48 | + |
| 49 | +// httpProxySocketEndpoint derives Docker Desktop's HTTP proxy socket endpoint |
| 50 | +// from a Docker Desktop socket endpoint in the same directory. Returns "" |
| 51 | +// when the input is not a recognized form or when the derived unix socket |
| 52 | +// does not exist (older DD versions or non-DD installs). |
| 53 | +// |
| 54 | +// On macOS/Linux: unix:///path/to/Data/docker-cli.sock → unix:///path/to/Data/httpproxy.sock |
| 55 | +// On Windows: npipe://./pipe/dockerCli → npipe://./pipe/dockerHttpProxy |
| 56 | +func httpProxySocketEndpoint(endpoint string) string { |
| 57 | + if sockPath, ok := strings.CutPrefix(endpoint, "unix://"); ok { |
| 58 | + proxyPath := filepath.Join(filepath.Dir(sockPath), "httpproxy.sock") |
| 59 | + if _, err := os.Stat(proxyPath); err != nil { |
| 60 | + return "" |
| 61 | + } |
| 62 | + return "unix://" + proxyPath |
| 63 | + } |
| 64 | + if strings.HasPrefix(endpoint, "npipe://") { |
| 65 | + return "npipe://./pipe/dockerHttpProxy" |
| 66 | + } |
| 67 | + return "" |
| 68 | +} |
| 69 | + |
| 70 | +// ProxyTransport returns an http.RoundTripper that routes traffic through |
| 71 | +// Docker Desktop's PAC-aware HTTP proxy when DD exposes the proxy socket. |
| 72 | +// Otherwise http.DefaultTransport is returned. Pass "" for endpoint when DD |
| 73 | +// is not the active engine. |
| 74 | +func ProxyTransport(endpoint string) http.RoundTripper { |
| 75 | + proxyEndpoint := httpProxySocketEndpoint(endpoint) |
| 76 | + if proxyEndpoint == "" { |
| 77 | + logrus.Debug("Docker Desktop HTTP proxy not available; using default HTTP transport") |
| 78 | + return http.DefaultTransport |
| 79 | + } |
| 80 | + logrus.Debugf("routing OCI traffic through Docker Desktop HTTP proxy at %s", proxyEndpoint) |
| 81 | + return &http.Transport{ |
| 82 | + Proxy: http.ProxyURL(&url.URL{Scheme: "http"}), |
| 83 | + DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) { |
| 84 | + return memnet.DialEndpoint(ctx, proxyEndpoint) |
| 85 | + }, |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +// ProxyTransportFor discovers the Docker Desktop endpoint via apiClient and |
| 90 | +// returns the matching transport, falling back to http.DefaultTransport on |
| 91 | +// discovery failure. |
| 92 | +func ProxyTransportFor(ctx context.Context, apiClient client.APIClient) http.RoundTripper { |
| 93 | + endpoint, err := Endpoint(ctx, apiClient) |
| 94 | + if err != nil { |
| 95 | + logrus.Debugf("could not detect Docker Desktop endpoint, using default HTTP transport: %v", err) |
| 96 | + return http.DefaultTransport |
| 97 | + } |
| 98 | + return ProxyTransport(endpoint) |
| 99 | +} |
0 commit comments