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 f2a2302
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 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"
"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,36 @@ 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 a IpfsAddr
func parseIpfsAddr(addr string) ([]ipfsaddr.IPFSAddr, error) {
mutiladdr, err := ma.NewMultiaddr(addr)
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), ConnectionTimeout)
raddrs, err := madns.Resolve(ctx, mutiladdr)
cancel()
if len(raddrs) == 0 {
return nil, fmt.Errorf("non-resolvable multiaddr about %v", mutiladdr)
}
iaddrs := make([]ipfsaddr.IPFSAddr, len(raddrs))
for i, maddr := range raddrs {
iaddrs[i], err = ipfsaddr.ParseMultiaddr(maddr)
if err != nil {
return nil, fmt.Errorf("invalid peer address: " + err.Error())
}
}
return iaddrs, nil
}

var p2pListenCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "Create libp2p service",
Expand Down Expand Up @@ -212,13 +238,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

0 comments on commit f2a2302

Please sign in to comment.