Skip to content

Commit

Permalink
remote: use winio DialPipeContext for named pipes
Browse files Browse the repository at this point in the history
Signed-off-by: Ian King'ori <[email protected]>
  • Loading branch information
iankingori committed Feb 27, 2024
1 parent 85abcc4 commit 6d3ecd8
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 25 deletions.
24 changes: 0 additions & 24 deletions driver/remote/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"crypto/x509"
"net"
"os"
"strings"
"time"

"github.com/docker/buildx/driver"
Expand Down Expand Up @@ -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,
Expand Down
37 changes: 37 additions & 0 deletions driver/remote/driver_unix.go
Original file line number Diff line number Diff line change
@@ -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
}
46 changes: 46 additions & 0 deletions driver/remote/driver_windows.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6d3ecd8

Please sign in to comment.