Skip to content

Commit

Permalink
add dns support in p2p forward connect
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Kejie Zhang <601172892@qq.com>
  • Loading branch information
kjzz committed Sep 30, 2018
1 parent 2a53f1d commit b3ee63f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
50 changes: 43 additions & 7 deletions core/commands/p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -49,14 +51,16 @@ type P2PStreamsOutput struct {
Streams []P2PStreamInfoOutput
}

var (
const (
allowCustomProtocolOptionName = "allow-custom-protocol"
allOptionName = "all"
protocolOptionName = "protocol"
listenAddressOptionName = "listen-address"
targetAddressOptionName = "target-address"
)

var connectionTimeout = 10 * time.Second

// P2PCmd is the 'ipfs p2p' command
var P2PCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Expand Down Expand Up @@ -120,7 +124,7 @@ Example:
return
}

target, err := ipfsaddr.ParseString(targetOpt)
targets, err := parseIpfsAddr(targetOpt)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
Expand All @@ -137,14 +141,45 @@ 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
}
res.SetOutput(nil)
},
}

// 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",
Expand Down Expand Up @@ -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
}

Expand Down
10 changes: 9 additions & 1 deletion test/sharness/t0180-p2p.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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' '
Expand Down

0 comments on commit b3ee63f

Please sign in to comment.