ipfsproxy: do not pre-resolve dns addrs / support api over unix sockets

These two changes were made to the ipfshttp connector already.

First, node_multiaddress should not be pre-resolved, only using the first result of the resolution.

Second, we should support node_multiaddresses that are pointing to unix sockets, as the kubo daemon can listen to requests that way and
it is better than tcp for local requests.
This commit is contained in:
Hector Sanjuan 2024-01-30 17:01:11 +01:00 committed by James Andariese
parent 61644365e0
commit 289a635105

View File

@ -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,7 +34,6 @@ import (
logging "github.com/ipfs/go-log/v2"
rpc "github.com/libp2p/go-libp2p-gorpc"
peer "github.com/libp2p/go-libp2p/core/peer"
madns "github.com/multiformats/go-multiaddr-dns"
manet "github.com/multiformats/go-multiaddr/net"
"go.opencensus.io/plugin/ochttp"
@ -61,10 +61,13 @@ type Server struct {
config *Config
nodeScheme string
nodeAddr string
nodeNetwork string
rpcClient *rpc.Client
rpcReady chan struct{}
transport http.RoundTripper // to the proxied kubo RPC API
listeners []net.Listener // proxy listener
server *http.Server // proxy server
reverseProxy *httputil.ReverseProxy // allows to talk to IPFS
@ -111,22 +114,21 @@ func New(cfg *Config) (*Server, error) {
return nil, err
}
nodeMAddr := cfg.NodeAddr
// dns multiaddresses need to be resolved first
if madns.Matches(nodeMAddr) {
ctx, cancel := context.WithTimeout(context.Background(), DNSTimeout)
defer cancel()
resolvedAddrs, err := madns.Resolve(ctx, cfg.NodeAddr)
nodeNetwork, nodeAddr, err := manet.DialArgs(cfg.NodeAddr)
if err != nil {
logger.Error(err)
return nil, err
}
nodeMAddr = resolvedAddrs[0]
}
_, nodeAddr, err := manet.DialArgs(nodeMAddr)
if err != nil {
return nil, err
transport := http.DefaultTransport
if nodeNetwork == "unix" {
unixTransport := &httpunix.Transport{
DialTimeout: time.Second,
}
unixTransport.RegisterLocation("ipfs", nodeAddr)
t := &http.Transport{}
t.RegisterProtocol(httpunix.Scheme, unixTransport)
transport = t
}
var listeners []net.Listener
@ -195,14 +197,16 @@ func New(cfg *Config) (*Server, error) {
s.SetKeepAlivesEnabled(true) // A reminder that this can be changed
reverseProxy := httputil.NewSingleHostReverseProxy(proxyURL)
reverseProxy.Transport = http.DefaultTransport
reverseProxy.Transport = transport
ctx, cancel := context.WithCancel(context.Background())
proxy := &Server{
ctx: ctx,
config: cfg,
cancel: cancel,
nodeAddr: nodeHTTPAddr,
nodeNetwork: nodeNetwork,
nodeScheme: nodeScheme,
transport: transport,
rpcReady: make(chan struct{}, 1),
listeners: listeners,
server: s,