From cdb323b932eca25610e1777b1fdfb783fe8947f3 Mon Sep 17 00:00:00 2001 From: Igor Bukanov Date: Mon, 16 Sep 2024 23:52:24 +0200 Subject: [PATCH] Unix socket support The new Brave BAT payments service uses viproxy to expose the service in the nitro enclave. For local testing outside AWS we plan to simulate Nitro by using a container that does not have an IP network but rather uses Unix sockets to communicate to the outside world. To cover that add support for directing the traffic into a unix socket. For symmetry also allow to listen on UNIX sockets. Report an explicit error when net.Addr is unsupported. --- viproxy.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/viproxy.go b/viproxy.go index 62bb687..376549e 100644 --- a/viproxy.go +++ b/viproxy.go @@ -4,6 +4,7 @@ package viproxy import ( + "fmt" "io" "log" "net" @@ -56,6 +57,10 @@ func dial(addr net.Addr) (net.Conn, error) { conn, err = vsock.Dial(a.ContextID, a.Port, nil) case *net.TCPAddr: conn, err = net.DialTCP("tcp", nil, a) + case *net.UnixAddr: + conn, err = net.DialUnix(addr.Network(), nil, a) + default: + return nil, fmt.Errorf("unsupported address type %T", addr) } if err != nil { return nil, err @@ -73,6 +78,10 @@ func listen(addr net.Addr) (net.Listener, error) { ln, err = vsock.ListenContextID(a.ContextID, a.Port, nil) case *net.TCPAddr: ln, err = net.ListenTCP(a.Network(), a) + case *net.UnixAddr: + ln, err = net.ListenUnix(addr.Network(), a) + default: + return nil, fmt.Errorf("unsupported address type %T", addr) } if err != nil { return nil, err