From a33c3f6a45b145251490dff3fc6ca0efda74c44c Mon Sep 17 00:00:00 2001 From: Christophe Fergeau Date: Thu, 17 Aug 2023 10:25:33 +0200 Subject: [PATCH 1/3] transport: Only build unixgram on darwin ListenUnixgram is implemented/built on all unix platforms, but it's really only needed for vfkit support, which is darwin-only, This commit makes this explicit by returning an error from ListenUnixgram on all platforms but darwin. It's easy enough to reenable it on more platforms if needed. Signed-off-by: Christophe Fergeau --- pkg/transport/listen_linux.go | 14 -------------- pkg/transport/listen_windows.go | 8 -------- .../{unixgram_unix.go => unixgram_darwin.go} | 4 ++-- pkg/transport/unixgram_nondarwin.go | 17 +++++++++++++++++ 4 files changed, 19 insertions(+), 24 deletions(-) rename pkg/transport/{unixgram_unix.go => unixgram_darwin.go} (97%) create mode 100644 pkg/transport/unixgram_nondarwin.go diff --git a/pkg/transport/listen_linux.go b/pkg/transport/listen_linux.go index 9f9ce8540..18dc831ef 100644 --- a/pkg/transport/listen_linux.go +++ b/pkg/transport/listen_linux.go @@ -31,17 +31,3 @@ func Listen(endpoint string) (net.Listener, error) { return nil, errors.New("unexpected scheme") } } - -func ListenUnixgram(endpoint string) (*net.UnixConn, error) { - parsed, err := url.Parse(endpoint) - if err != nil { - return nil, err - } - if parsed.Scheme != "unixgram" { - return nil, errors.New("unexpected scheme") - } - return net.ListenUnixgram("unixgram", &net.UnixAddr{ - Name: parsed.Path, - Net: "unixgram", - }) -} diff --git a/pkg/transport/listen_windows.go b/pkg/transport/listen_windows.go index cafb24735..423f887e2 100644 --- a/pkg/transport/listen_windows.go +++ b/pkg/transport/listen_windows.go @@ -33,11 +33,3 @@ func Listen(endpoint string) (net.Listener, error) { return nil, errors.New("unexpected scheme") } } - -func ListenUnixgram(endpoint string) (net.Conn, error) { - return nil, errors.New("unsupported 'unixgram' scheme") -} - -func AcceptVfkit(listeningConn net.Conn) (net.Conn, error) { - return nil, errors.New("vfkit is unsupported on Windows") -} diff --git a/pkg/transport/unixgram_unix.go b/pkg/transport/unixgram_darwin.go similarity index 97% rename from pkg/transport/unixgram_unix.go rename to pkg/transport/unixgram_darwin.go index c5e33ad08..12d3c50a0 100644 --- a/pkg/transport/unixgram_unix.go +++ b/pkg/transport/unixgram_darwin.go @@ -1,5 +1,5 @@ -//go:build !windows -// +build !windows +//go:build darwin +// +build darwin package transport diff --git a/pkg/transport/unixgram_nondarwin.go b/pkg/transport/unixgram_nondarwin.go new file mode 100644 index 000000000..09a994877 --- /dev/null +++ b/pkg/transport/unixgram_nondarwin.go @@ -0,0 +1,17 @@ +//go:build !darwin +// +build !darwin + +package transport + +import ( + "errors" + "net" +) + +func ListenUnixgram(_ string) (net.Conn, error) { + return nil, errors.New("unsupported 'unixgram' scheme") +} + +func AcceptVfkit(_ net.Conn) (net.Conn, error) { + return nil, errors.New("vfkit is unsupported on this platform") +} From 35c4af4b8ebdee2f1b7244eb7bce6e647e685e5e Mon Sep 17 00:00:00 2001 From: Christophe Fergeau Date: Thu, 17 Aug 2023 10:17:33 +0200 Subject: [PATCH 2/3] transport: Refactor 'Listen' code At the moment, the Listen code is implemented for each platform, but the URL parsing code, and `unix` and `tcp` code are the same. There's also no 'generic' fallback for other platforms which don't need a vsock implementation. This commit replaces the per-platform Listen implementation with a generic one doing the URL parsing and then calling a per-platform `listenURL`. It also introduces a `defaultListenURL` function with the `unix` and `tcp` code paths. This way, the per-platform files only contain the platform-specific code. Signed-off-by: Christophe Fergeau --- pkg/transport/listen.go | 26 ++++++++++++++++++++++++++ pkg/transport/listen_darwin.go | 12 ++---------- pkg/transport/listen_generic.go | 13 +++++++++++++ pkg/transport/listen_linux.go | 13 +++---------- pkg/transport/listen_windows.go | 13 ++----------- 5 files changed, 46 insertions(+), 31 deletions(-) create mode 100644 pkg/transport/listen.go create mode 100644 pkg/transport/listen_generic.go diff --git a/pkg/transport/listen.go b/pkg/transport/listen.go new file mode 100644 index 000000000..a0909bb6b --- /dev/null +++ b/pkg/transport/listen.go @@ -0,0 +1,26 @@ +package transport + +import ( + "errors" + "net" + "net/url" +) + +func defaultListenURL(url *url.URL) (net.Listener, error) { + switch url.Scheme { + case "unix": + return net.Listen(url.Scheme, url.Path) + case "tcp": + return net.Listen("tcp", url.Host) + default: + return nil, errors.New("unexpected scheme") + } +} + +func Listen(endpoint string) (net.Listener, error) { + parsed, err := url.Parse(endpoint) + if err != nil { + return nil, err + } + return listenURL(parsed) +} diff --git a/pkg/transport/listen_darwin.go b/pkg/transport/listen_darwin.go index 3fa9d00c5..bea3042c2 100644 --- a/pkg/transport/listen_darwin.go +++ b/pkg/transport/listen_darwin.go @@ -12,11 +12,7 @@ import ( const DefaultURL = "vsock://null:1024/vm_directory" -func Listen(endpoint string) (net.Listener, error) { - parsed, err := url.Parse(endpoint) - if err != nil { - return nil, err - } +func listenURL(parsed *url.URL) (net.Listener, error) { switch parsed.Scheme { case "vsock": port, err := strconv.Atoi(parsed.Port()) @@ -31,12 +27,8 @@ func Listen(endpoint string) (net.Listener, error) { Name: path, Net: "unix", }) - case "unix": - return net.Listen("unix", parsed.Path) - case "tcp": - return net.Listen("tcp", parsed.Host) default: - return nil, errors.New("unexpected scheme") + return defaultListenURL(parsed) } } diff --git a/pkg/transport/listen_generic.go b/pkg/transport/listen_generic.go new file mode 100644 index 000000000..5553cd3b3 --- /dev/null +++ b/pkg/transport/listen_generic.go @@ -0,0 +1,13 @@ +//go:build !darwin && !linux && !windows +// +build !darwin,!linux,!windows + +package transport + +import ( + "net" + "net/url" +) + +func listenURL(url *url.URL) (net.Listener, error) { + return defaultListenURL(url) +} diff --git a/pkg/transport/listen_linux.go b/pkg/transport/listen_linux.go index 18dc831ef..6590e68b1 100644 --- a/pkg/transport/listen_linux.go +++ b/pkg/transport/listen_linux.go @@ -1,7 +1,6 @@ package transport import ( - "errors" "net" "net/url" "strconv" @@ -11,11 +10,7 @@ import ( const DefaultURL = "vsock://:1024" -func Listen(endpoint string) (net.Listener, error) { - parsed, err := url.Parse(endpoint) - if err != nil { - return nil, err - } +func listenURL(parsed *url.URL) (net.Listener, error) { switch parsed.Scheme { case "vsock": port, err := strconv.Atoi(parsed.Port()) @@ -23,11 +18,9 @@ func Listen(endpoint string) (net.Listener, error) { return nil, err } return mdlayhervsock.Listen(uint32(port), nil) - case "unix", "unixpacket": + case "unixpacket": return net.Listen(parsed.Scheme, parsed.Path) - case "tcp": - return net.Listen("tcp", parsed.Host) default: - return nil, errors.New("unexpected scheme") + return defaultListenURL(parsed) } } diff --git a/pkg/transport/listen_windows.go b/pkg/transport/listen_windows.go index 423f887e2..20c5d0356 100644 --- a/pkg/transport/listen_windows.go +++ b/pkg/transport/listen_windows.go @@ -1,7 +1,6 @@ package transport import ( - "errors" "net" "net/url" @@ -10,11 +9,7 @@ import ( const DefaultURL = "vsock://00000400-FACB-11E6-BD58-64006A7986D3" -func Listen(endpoint string) (net.Listener, error) { - parsed, err := url.Parse(endpoint) - if err != nil { - return nil, err - } +func listenURL(parsed *url.URL) (net.Listener, error) { switch parsed.Scheme { case "vsock": svcid, err := hvsock.GUIDFromString(parsed.Hostname()) @@ -25,11 +20,7 @@ func Listen(endpoint string) (net.Listener, error) { VMID: hvsock.GUIDWildcard, ServiceID: svcid, }) - case "unix": - return net.Listen(parsed.Scheme, parsed.Path) - case "tcp": - return net.Listen("tcp", parsed.Host) default: - return nil, errors.New("unexpected scheme") + return defaultListenURL(parsed) } } From 4dbb0eda1038813aa0e0b0498603c89165736b26 Mon Sep 17 00:00:00 2001 From: Christophe Fergeau Date: Thu, 17 Aug 2023 10:22:49 +0200 Subject: [PATCH 3/3] build: Add freebsd build to make cross This ensures we don't break freebsd builds. Signed-off-by: Christophe Fergeau --- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index d21cdbdb2..faab6824a 100644 --- a/Makefile +++ b/Makefile @@ -50,6 +50,7 @@ image: .PHONY: cross cross: $(TOOLS_BINDIR)/makefat + GOARCH=amd64 GOOS=freebsd go build $(LDFLAGS) -o bin/gvproxy-freebsd-amd64 ./cmd/gvproxy GOARCH=amd64 GOOS=windows go build $(LDFLAGS) -o bin/gvproxy-windows.exe ./cmd/gvproxy GOARCH=amd64 GOOS=darwin go build $(LDFLAGS) -o bin/gvproxy-darwin-amd64 ./cmd/gvproxy GOARCH=arm64 GOOS=darwin go build $(LDFLAGS) -o bin/gvproxy-darwin-arm64 ./cmd/gvproxy