diff --git a/core/commands/p2p.go b/core/commands/p2p.go index 13ff1262d5ea..1847b7604bc0 100644 --- a/core/commands/p2p.go +++ b/core/commands/p2p.go @@ -19,6 +19,8 @@ import ( ma "gx/ipfs/QmYmsdtJ3HsodkePE3eU3TsCaP2YvPZJ4LoXnNkDE5Tpt7/go-multiaddr" "gx/ipfs/QmZNkThpqfVXs9GNbexPrfBbXSLNYeKrE7jwFM2oqHbyqN/go-libp2p-protocol" pstore "gx/ipfs/QmfAQMFpgDU2U4BXG64qVr8HSiictfWvkSBz7Y2oDj65st/go-libp2p-peerstore" + madns "gx/ipfs/QmfXU2MhWoegxHoeMd3A2ytL2P6CY4FfqGWc23LTNWBwZt/go-multiaddr-dns" + "time" ) // P2PProtoPrefix is the default required prefix for protocol names @@ -49,7 +51,7 @@ type P2PStreamsOutput struct { Streams []P2PStreamInfoOutput } -var ( +const ( allowCustomProtocolOptionName = "allow-custom-protocol" allOptionName = "all" protocolOptionName = "protocol" @@ -57,6 +59,8 @@ var ( targetAddressOptionName = "target-address" ) +var connectionTimeout = 10 * time.Second + // P2PCmd is the 'ipfs p2p' command var P2PCmd = &cmds.Command{ Helptext: cmdkit.HelpText{ @@ -120,7 +124,7 @@ Example: return } - target, err := ipfsaddr.ParseString(targetOpt) + targets, err := parseIpfsAddr(targetOpt) if err != nil { res.SetError(err, cmdkit.ErrNormal) return @@ -137,7 +141,7 @@ Example: return } - if err := forwardLocal(n.Context(), n.P2P, n.Peerstore, proto, listen, target); err != nil { + if err := forwardLocal(n.Context(), n.P2P, n.Peerstore, proto, listen, targets); err != nil { res.SetError(err, cmdkit.ErrNormal) return } @@ -145,6 +149,37 @@ Example: }, } +// parseIpfsAddr is a function that takes in addr string and return ipfsAddrs +func parseIpfsAddr(addr string) ([]ipfsaddr.IPFSAddr, error) { + mutiladdr, err := ma.NewMultiaddr(addr) + if err != nil { + return nil, err + } + if strings.Contains(mutiladdr.String(), "/ipfs/Qm") { + iaddrs := make([]ipfsaddr.IPFSAddr, 1) + iaddrs[0], err = ipfsaddr.ParseMultiaddr(mutiladdr) + if err != nil { + return nil, err + } + return iaddrs, nil + } + // resolve mutiladdr without `/ipfs/Qm` + ctx, cancel := context.WithTimeout(context.Background(), connectionTimeout) + addrs, err := madns.Resolve(ctx, mutiladdr) + cancel() + if len(addrs) == 0 { + return nil, fmt.Errorf("non-resolvable multiaddr about %v", mutiladdr) + } + iaddrs := make([]ipfsaddr.IPFSAddr, len(addrs)) + for i, addr := range addrs { + iaddrs[i], err = ipfsaddr.ParseMultiaddr(addr) + if err != nil { + return nil, err + } + } + return iaddrs, nil +} + var p2pListenCmd = &cmds.Command{ Helptext: cmdkit.HelpText{ Tagline: "Create libp2p service", @@ -212,13 +247,14 @@ func forwardRemote(ctx context.Context, p *p2p.P2P, proto protocol.ID, target ma } // forwardLocal forwards local connections to a libp2p service -func forwardLocal(ctx context.Context, p *p2p.P2P, ps pstore.Peerstore, proto protocol.ID, bindAddr ma.Multiaddr, addr ipfsaddr.IPFSAddr) error { - if addr != nil { +func forwardLocal(ctx context.Context, p *p2p.P2P, ps pstore.Peerstore, proto protocol.ID, bindAddr ma.Multiaddr, addrs []ipfsaddr.IPFSAddr) error { + for _, addr := range addrs { ps.AddAddr(addr.ID(), addr.Multiaddr(), pstore.TempAddrTTL) } - // TODO: return some info - _, err := p.ForwardLocal(ctx, addr.ID(), proto, bindAddr) + // the length of the addrs must large than 0 + // peerIDs in addr must be the same and choose addr[0] to connect + _, err := p.ForwardLocal(ctx, addrs[0].ID(), proto, bindAddr) return err } diff --git a/test/sharness/t0180-p2p.sh b/test/sharness/t0180-p2p.sh index 544cd1f6f6a9..88e854104090 100755 --- a/test/sharness/t0180-p2p.sh +++ b/test/sharness/t0180-p2p.sh @@ -73,10 +73,18 @@ test_server_to_client() { spawn_sending_server -test_expect_success 'S->C Setup client side' ' +test_expect_success 'S->C(/ipfs/peerID) Setup client side' ' ipfsi 1 p2p forward /x/p2p-test /ip4/127.0.0.1/tcp/10102 /ipfs/${PEERID_0} 2>&1 > dialer-stdouterr.log ' +test_expect_success 'S->C Setup(dnsaddr/addr) client side' ' + ipfsi 1 p2p forward /x/p2p-test /ip4/127.0.0.1/tcp/10103 /dnsaddr/bootstrap.libp2p.io/ 2>&1 > dialer-stdouterr.log +' + +test_expect_success 'S->C Setup(dnsaddr/addr/ipfs/peerID) client side' ' + ipfsi 1 p2p forward /x/p2p-test /ip4/127.0.0.1/tcp/10104 /dnsaddr/bootstrap.libp2p.io/ipfs/${PEERID_0} 2>&1 > dialer-stdouterr.log +' + test_server_to_client test_expect_success 'S->C Connect with dead server' '