From 80d52d1193f6e07116ffd46c0f209e375b6b1c56 Mon Sep 17 00:00:00 2001 From: Max Date: Thu, 20 Oct 2022 00:18:48 +0200 Subject: [PATCH] packages/ipfs-cluster: support IPFS API proxy with unix domain sockets --- .../ipfs-cluster/api/ipfsproxy/ipfsproxy.go | 30 +++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/packages/networking/ipfs-cluster/api/ipfsproxy/ipfsproxy.go b/packages/networking/ipfs-cluster/api/ipfsproxy/ipfsproxy.go index f5bd06f..0fb9895 100644 --- a/packages/networking/ipfs-cluster/api/ipfsproxy/ipfsproxy.go +++ b/packages/networking/ipfs-cluster/api/ipfsproxy/ipfsproxy.go @@ -24,6 +24,7 @@ import ( "github.com/ipfs-cluster/ipfs-cluster/adder/adderutils" "github.com/ipfs-cluster/ipfs-cluster/api" "github.com/ipfs-cluster/ipfs-cluster/rpcutil" + "github.com/tv42/httpunix" handlers "github.com/gorilla/handlers" mux "github.com/gorilla/mux" @@ -33,6 +34,7 @@ import ( path "github.com/ipfs/go-path" peer "github.com/libp2p/go-libp2p-core/peer" rpc "github.com/libp2p/go-libp2p-gorpc" + "github.com/multiformats/go-multiaddr" madns "github.com/multiformats/go-multiaddr-dns" manet "github.com/multiformats/go-multiaddr/net" @@ -129,6 +131,22 @@ func New(cfg *Config) (*Server, error) { return nil, err } + nodeScheme := "http" + if cfg.NodeHTTPS { + nodeScheme = "https" + } + + isUnixSocket := false + var unixTransport *httpunix.Transport + if unixSocketPath, err := nodeMAddr.ValueForProtocol(multiaddr.P_UNIX); err == nil { + unixTransport = &httpunix.Transport{} + unixTransport.RegisterLocation("ipfsproxyunix", unixSocketPath) + nodeAddr = "ipfsproxyunix" + + nodeScheme = nodeScheme + "+unix" + isUnixSocket = true + } + var listeners []net.Listener for _, addr := range cfg.ListenAddr { proxyNet, proxyAddr, err := manet.DialArgs(addr) @@ -143,10 +161,6 @@ func New(cfg *Config) (*Server, error) { listeners = append(listeners, l) } - nodeScheme := "http" - if cfg.NodeHTTPS { - nodeScheme = "https" - } nodeHTTPAddr := fmt.Sprintf("%s://%s", nodeScheme, nodeAddr) proxyURL, err := url.Parse(nodeHTTPAddr) if err != nil { @@ -195,7 +209,13 @@ func New(cfg *Config) (*Server, error) { s.SetKeepAlivesEnabled(true) // A reminder that this can be changed reverseProxy := httputil.NewSingleHostReverseProxy(proxyURL) - reverseProxy.Transport = http.DefaultTransport + if isUnixSocket { + t := &http.Transport{} + t.RegisterProtocol(httpunix.Scheme, unixTransport) + reverseProxy.Transport = t + } else { + reverseProxy.Transport = http.DefaultTransport + } ctx, cancel := context.WithCancel(context.Background()) proxy := &Server{ ctx: ctx,