Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rpc): cross-platform support for /unix/ socket maddrs in Addresses.API #10019

Merged
merged 6 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions client/rpc/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"net"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -98,11 +99,29 @@ func ApiAddr(ipfspath string) (ma.Multiaddr, error) {

// NewApi constructs HttpApi with specified endpoint.
func NewApi(a ma.Multiaddr) (*HttpApi, error) {
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DisableKeepAlives: true,
}

network, address, err := manet.DialArgs(a)
if err != nil {
return nil, err
}
if network == "unix" {
transport.DialContext = func(_ context.Context, _, _ string) (net.Conn, error) {
return net.Dial("unix", address)
}
c := &http.Client{
Transport: transport,
}
// This will create an API client which
// makes requests to `http://unix`.
return NewURLApiWithClient(network, c)
}

c := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DisableKeepAlives: true,
},
Transport: transport,
}

return NewApiWithClient(a, c)
Expand Down
11 changes: 11 additions & 0 deletions test/cli/harness/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,17 @@ func (n *Node) checkAPI(authorization string) bool {
log.Debugf("node %d API addr not available yet: %s", n.ID, err.Error())
return false
}

if unixAddr, err := apiAddr.ValueForProtocol(multiaddr.P_UNIX); err == nil {
parts := strings.SplitN(unixAddr, "/", 2)
if len(parts) < 1 {
panic("malformed unix socket address")
}
fileName := "/" + parts[1]
_, err := os.Stat(fileName)
return !errors.Is(err, fs.ErrNotExist)
}

ip, err := apiAddr.ValueForProtocol(multiaddr.P_IP4)
if err != nil {
panic(err)
Expand Down
55 changes: 55 additions & 0 deletions test/cli/rpc_unixsocket_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package cli

import (
"context"
//"net"
//"net/http"
gammazero marked this conversation as resolved.
Show resolved Hide resolved
"path"
"testing"

rpcapi "github.com/ipfs/kubo/client/rpc"
///"github.com/ipfs/kubo/client/rpc/auth"
"github.com/ipfs/kubo/config"
"github.com/ipfs/kubo/test/cli/harness"
"github.com/multiformats/go-multiaddr"
//manet "github.com/multiformats/go-multiaddr/net"
"github.com/stretchr/testify/require"
)

func TestRPCUnixSocket(t *testing.T) {
node := harness.NewT(t).NewNode().Init()

sockDir := node.Dir
sockAddr := path.Join("/unix", sockDir, "sock")

node.UpdateConfig(func(cfg *config.Config) {
//cfg.Addresses.API = append(cfg.Addresses.API, sockPath)
cfg.Addresses.API = []string{sockAddr}
})
t.Log("Starting daemon with unix socket:", sockAddr)
node.StartDaemon()

unixMaddr, err := multiaddr.NewMultiaddr(sockAddr)
require.NoError(t, err)

apiClient, err := rpcapi.NewApi(unixMaddr)
require.NoError(t, err)

var ver struct {
Version string
}
err = apiClient.Request("version").Exec(context.Background(), &ver)
require.NoError(t, err)
require.NotEmpty(t, ver)
t.Log("Got version:", ver.Version)

var res struct {
ID string
}
err = apiClient.Request("id").Exec(context.Background(), &res)
require.NoError(t, err)
require.NotEmpty(t, res)
t.Log("Got ID:", res.ID)

node.StopDaemon()
}
Loading