From 6d3ecd837202b6f34e87fc33ae24c1cc2a197368 Mon Sep 17 00:00:00 2001 From: Ian King'ori Date: Tue, 27 Feb 2024 16:56:10 +0300 Subject: [PATCH] remote: use winio DialPipeContext for named pipes Signed-off-by: Ian King'ori --- driver/remote/driver.go | 24 ----------------- driver/remote/driver_unix.go | 37 ++++++++++++++++++++++++++ driver/remote/driver_windows.go | 46 +++++++++++++++++++++++++++++++++ go.mod | 2 +- 4 files changed, 84 insertions(+), 25 deletions(-) create mode 100644 driver/remote/driver_unix.go create mode 100644 driver/remote/driver_windows.go diff --git a/driver/remote/driver.go b/driver/remote/driver.go index 2f2ab58928f4..6575cf11c6ca 100644 --- a/driver/remote/driver.go +++ b/driver/remote/driver.go @@ -6,7 +6,6 @@ import ( "crypto/x509" "net" "os" - "strings" "time" "github.com/docker/buildx/driver" @@ -93,29 +92,6 @@ func (d *Driver) Client(ctx context.Context) (*client.Client, error) { return client.New(ctx, "", opts...) } -func (d *Driver) Dial(ctx context.Context) (net.Conn, error) { - network, addr, ok := strings.Cut(d.InitConfig.EndpointAddr, "://") - if !ok { - return nil, errors.Errorf("invalid endpoint address: %s", d.InitConfig.EndpointAddr) - } - - dialer := &net.Dialer{} - - conn, err := dialer.DialContext(ctx, network, addr) - if err != nil { - return nil, errors.WithStack(err) - } - - if d.tlsOpts != nil { - cfg, err := loadTLS(d.tlsOpts) - if err != nil { - return nil, errors.Wrap(err, "error loading tls config") - } - conn = tls.Client(conn, cfg) - } - return conn, nil -} - func loadTLS(opts *tlsOpts) (*tls.Config, error) { cfg := &tls.Config{ ServerName: opts.serverName, diff --git a/driver/remote/driver_unix.go b/driver/remote/driver_unix.go new file mode 100644 index 000000000000..497f2af2ae2d --- /dev/null +++ b/driver/remote/driver_unix.go @@ -0,0 +1,37 @@ +//go:build !windows +// +build !windows + +package remote + +import ( + "context" + "crypto/tls" + "net" + "strings" + + "github.com/pkg/errors" +) + +func (d *Driver) Dial(ctx context.Context) (net.Conn, error) { + network, addr, ok := strings.Cut(d.InitConfig.EndpointAddr, "://") + if !ok { + return nil, errors.Errorf("invalid endpoint address: %s", d.InitConfig.EndpointAddr) + } + + dialer := &net.Dialer{} + + conn, err := dialer.DialContext(ctx, network, addr) + + if err != nil { + return nil, errors.WithStack(err) + } + + if d.tlsOpts != nil { + cfg, err := loadTLS(d.tlsOpts) + if err != nil { + return nil, errors.Wrap(err, "error loading tls config") + } + conn = tls.Client(conn, cfg) + } + return conn, nil +} diff --git a/driver/remote/driver_windows.go b/driver/remote/driver_windows.go new file mode 100644 index 000000000000..c0065d5280e9 --- /dev/null +++ b/driver/remote/driver_windows.go @@ -0,0 +1,46 @@ +//go:build windows +// +build windows + +package remote + +import ( + "context" + "crypto/tls" + "net" + "strings" + + "github.com/Microsoft/go-winio" + "github.com/pkg/errors" +) + +func (d *Driver) Dial(ctx context.Context) (net.Conn, error) { + network, addr, ok := strings.Cut(d.InitConfig.EndpointAddr, "://") + if !ok { + return nil, errors.Errorf("invalid endpoint address: %s", d.InitConfig.EndpointAddr) + } + + dialer := &net.Dialer{} + + var conn net.Conn + var err error + + // dial context doesn't support named pipes + if network == "npipe" { + conn, err = winio.DialPipeContext(ctx, addr) + } else { + conn, err = dialer.DialContext(ctx, network, addr) + } + + if err != nil { + return nil, errors.WithStack(err) + } + + if d.tlsOpts != nil { + cfg, err := loadTLS(d.tlsOpts) + if err != nil { + return nil, errors.Wrap(err, "error loading tls config") + } + conn = tls.Client(conn, cfg) + } + return conn, nil +} diff --git a/go.mod b/go.mod index 3af267a72f49..42ff87c057d3 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.21 require ( github.com/Masterminds/semver/v3 v3.2.1 + github.com/Microsoft/go-winio v0.6.1 github.com/aws/aws-sdk-go-v2/config v1.26.6 github.com/compose-spec/compose-go/v2 v2.0.0-rc.8 github.com/containerd/console v1.0.4 @@ -60,7 +61,6 @@ require ( require ( github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24 // indirect github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect - github.com/Microsoft/go-winio v0.6.1 // indirect github.com/Microsoft/hcsshim v0.11.4 // indirect github.com/Shopify/logrus-bugsnag v0.0.0-20171204204709-577dee27f20d // indirect github.com/agext/levenshtein v1.2.3 // indirect