@cryptotaxi247 / kubo / commits / 501abdd13

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

License: MIT Signed-off-by: Raúl Kripalani <raul.kripalani@consensys.net>

Raúl Kripalani committed Aug 11, 2018 at 13:29 UTC 501abdd1303a10c42ca38f7619e03c44938fe282
2 files changed +33 -6
cmd/ipfs/main.go
+27 -6
@@ -34,6 +34,7 @@ import (
34 osh "gx/ipfs/QmXuBJ7DR6k3rmUEKtvVMhwjmXDuJgXXPUt4LQXKBMsU93/go-os-helper"
35 ma "gx/ipfs/QmYmsdtJ3HsodkePE3eU3TsCaP2YvPZJ4LoXnNkDE5Tpt7/go-multiaddr"
36 loggables "gx/ipfs/QmZ4zF1mBrt8C2mSCM4ZYE4aAnv78f7GvrzufJC4G5tecK/go-libp2p-loggables"
37 + mdns "gx/ipfs/QmfXU2MhWoegxHoeMd3A2ytL2P6CY4FfqGWc23LTNWBwZt/go-multiaddr-dns"
38 )
39
40 // log is the command logger
@@ -235,7 +236,7 @@ func commandShouldRunOnDaemon(details cmdDetails, req *cmds.Request, cctx *oldcm
236 // did user specify an api to use for this command?
237 apiAddrStr, _ := req.Options[corecmds.ApiOption].(string)
238
238 - client, err := getApiClient(cctx.ConfigRoot, apiAddrStr)
239 + client, err := getApiClient(req.Context, cctx.ConfigRoot, apiAddrStr)
240 if err == repo.ErrApiNotRunning {
241 if apiAddrStr != "" && req.Command != daemonCmd {
242 // if user SPECIFIED an api, and this cmd is not daemon
@@ -406,7 +407,7 @@ var checkIPFSWinFmt = "Otherwise check:\n\ttasklist | findstr ipfs"
407 // getApiClient checks the repo, and the given options, checking for
408 // a running API service. if there is one, it returns a client.
409 // otherwise, it returns errApiNotRunning, or another error.
409 -func getApiClient(repoPath, apiAddrStr string) (http.Client, error) {
410 +func getApiClient(ctx context.Context, repoPath, apiAddrStr string) (http.Client, error) {
411 var apiErrorFmt string
412 switch {
413 case osh.IsUnix():
@@ -440,14 +441,34 @@ func getApiClient(repoPath, apiAddrStr string) (http.Client, error) {
441 if len(addr.Protocols()) == 0 {
442 return nil, fmt.Errorf(apiErrorFmt, repoPath, "multiaddr doesn't provide any protocols")
443 }
443 - return apiClientForAddr(addr)
444 + return apiClientForAddr(ctx, addr)
445 }
446
446 -func apiClientForAddr(addr ma.Multiaddr) (http.Client, error) {
447 - _, host, err := manet.DialArgs(addr)
447 +func apiClientForAddr(ctx context.Context, addr ma.Multiaddr) (http.Client, error) {
448 + addrs, err := mdns.Resolve(ctx, addr)
449 if err != nil {
450 return nil, err
451 }
452
452 - return http.NewClient(host, http.ClientWithAPIPrefix(corehttp.APIPath)), nil
453 + dialer := &manet.Dialer{}
454 + for _, addr := range addrs {
455 + ctx, cancelFunc := context.WithTimeout(ctx, 5*time.Second)
456 + defer cancelFunc()
457 +
458 + conn, err := dialer.DialContext(ctx, addr)
459 + if err != nil {
460 + log.Errorf("connection to %s failed, error: %s", addr, err)
461 + continue
462 + }
463 + conn.Close()
464 +
465 + _, host, err := manet.DialArgs(addr)
466 + if err != nil {
467 + continue
468 + }
469 +
470 + return http.NewClient(host, http.ClientWithAPIPrefix(corehttp.APIPath)), nil
471 + }
472 +
473 + return nil, errors.New("non-resolvable API endpoint")
474 }
package.json
+6
@@ -539,6 +539,12 @@
539 "hash": "QmPyxJ2QS7L5FhGkNYkNcXHGjDhvGHueJ4auqAstFHYxy5",
540 "name": "go-cidutil",
541 "version": "0.0.2"
542 + },
543 + {
544 + "author": "lgierth",
545 + "hash": "QmfXU2MhWoegxHoeMd3A2ytL2P6CY4FfqGWc23LTNWBwZt",
546 + "name": "go-multiaddr-dns",
547 + "version": "0.2.4"
548 }
549 ],
550 "gxVersion": "0.10.0",