Commit 501abdd1 authored by Raúl Kripalani's avatar Raúl Kripalani Committed by Steven Allen

WIP DNS resolution for API endpoint on ipfs bin (#5249)

License: MIT
Signed-off-by: default avatarRaúl Kripalani <raul.kripalani@consensys.net>
parent 4fd8a6ef
......@@ -34,6 +34,7 @@ import (
osh "gx/ipfs/QmXuBJ7DR6k3rmUEKtvVMhwjmXDuJgXXPUt4LQXKBMsU93/go-os-helper"
ma "gx/ipfs/QmYmsdtJ3HsodkePE3eU3TsCaP2YvPZJ4LoXnNkDE5Tpt7/go-multiaddr"
loggables "gx/ipfs/QmZ4zF1mBrt8C2mSCM4ZYE4aAnv78f7GvrzufJC4G5tecK/go-libp2p-loggables"
mdns "gx/ipfs/QmfXU2MhWoegxHoeMd3A2ytL2P6CY4FfqGWc23LTNWBwZt/go-multiaddr-dns"
)
// log is the command logger
......@@ -235,7 +236,7 @@ func commandShouldRunOnDaemon(details cmdDetails, req *cmds.Request, cctx *oldcm
// did user specify an api to use for this command?
apiAddrStr, _ := req.Options[corecmds.ApiOption].(string)
client, err := getApiClient(cctx.ConfigRoot, apiAddrStr)
client, err := getApiClient(req.Context, cctx.ConfigRoot, apiAddrStr)
if err == repo.ErrApiNotRunning {
if apiAddrStr != "" && req.Command != daemonCmd {
// if user SPECIFIED an api, and this cmd is not daemon
......@@ -406,7 +407,7 @@ var checkIPFSWinFmt = "Otherwise check:\n\ttasklist | findstr ipfs"
// getApiClient checks the repo, and the given options, checking for
// a running API service. if there is one, it returns a client.
// otherwise, it returns errApiNotRunning, or another error.
func getApiClient(repoPath, apiAddrStr string) (http.Client, error) {
func getApiClient(ctx context.Context, repoPath, apiAddrStr string) (http.Client, error) {
var apiErrorFmt string
switch {
case osh.IsUnix():
......@@ -440,14 +441,34 @@ func getApiClient(repoPath, apiAddrStr string) (http.Client, error) {
if len(addr.Protocols()) == 0 {
return nil, fmt.Errorf(apiErrorFmt, repoPath, "multiaddr doesn't provide any protocols")
}
return apiClientForAddr(addr)
return apiClientForAddr(ctx, addr)
}
func apiClientForAddr(addr ma.Multiaddr) (http.Client, error) {
_, host, err := manet.DialArgs(addr)
func apiClientForAddr(ctx context.Context, addr ma.Multiaddr) (http.Client, error) {
addrs, err := mdns.Resolve(ctx, addr)
if err != nil {
return nil, err
}
return http.NewClient(host, http.ClientWithAPIPrefix(corehttp.APIPath)), nil
dialer := &manet.Dialer{}
for _, addr := range addrs {
ctx, cancelFunc := context.WithTimeout(ctx, 5*time.Second)
defer cancelFunc()
conn, err := dialer.DialContext(ctx, addr)
if err != nil {
log.Errorf("connection to %s failed, error: %s", addr, err)
continue
}
conn.Close()
_, host, err := manet.DialArgs(addr)
if err != nil {
continue
}
return http.NewClient(host, http.ClientWithAPIPrefix(corehttp.APIPath)), nil
}
return nil, errors.New("non-resolvable API endpoint")
}
......@@ -539,6 +539,12 @@
"hash": "QmPyxJ2QS7L5FhGkNYkNcXHGjDhvGHueJ4auqAstFHYxy5",
"name": "go-cidutil",
"version": "0.0.2"
},
{
"author": "lgierth",
"hash": "QmfXU2MhWoegxHoeMd3A2ytL2P6CY4FfqGWc23LTNWBwZt",
"name": "go-multiaddr-dns",
"version": "0.2.4"
}
],
"gxVersion": "0.10.0",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment