add dns support in p2p forward connect
License: MIT Signed-off-by: Kejie Zhang <601172892@qq.com>
Kejie Zhang committed
Sep 30, 2018 at 13:54 UTC
51fa159e4ad43b4049178ac2f79df4a375561003
2 files changed
+63
-9
core/commands/p2p.go
+43
-7
@@ -9,6 +9,7 @@ import (
9
"strconv"
10
"strings"
11
"text/tabwriter"
12
+ "time"
13
14
cmds "github.com/ipfs/go-ipfs/commands"
15
core "github.com/ipfs/go-ipfs/core"
@@ -19,6 +20,7 @@ import (
20
ma "gx/ipfs/QmYmsdtJ3HsodkePE3eU3TsCaP2YvPZJ4LoXnNkDE5Tpt7/go-multiaddr"
21
"gx/ipfs/QmZNkThpqfVXs9GNbexPrfBbXSLNYeKrE7jwFM2oqHbyqN/go-libp2p-protocol"
22
"gx/ipfs/QmesXvbRGyKQn1XbPHx1Mr5E6RTJYR9c8zwFVuGZq9Aa1j/go-ipfs-addr"
23
+ madns "gx/ipfs/QmfXU2MhWoegxHoeMd3A2ytL2P6CY4FfqGWc23LTNWBwZt/go-multiaddr-dns"
24
)
25
26
// P2PProtoPrefix is the default required prefix for protocol names
@@ -49,7 +51,7 @@ type P2PStreamsOutput struct {
51
Streams []P2PStreamInfoOutput
52
}
53
52
-var (
54
+const (
55
allowCustomProtocolOptionName = "allow-custom-protocol"
56
allOptionName = "all"
57
protocolOptionName = "protocol"
@@ -57,6 +59,8 @@ var (
59
targetAddressOptionName = "target-address"
60
)
61
62
+var resolveTimeout = 10 * time.Second
63
+
64
// P2PCmd is the 'ipfs p2p' command
65
var P2PCmd = &cmds.Command{
66
Helptext: cmdkit.HelpText{
@@ -120,7 +124,7 @@ Example:
124
return
125
}
126
123
- target, err := ipfsaddr.ParseString(targetOpt)
127
+ targets, err := parseIpfsAddr(targetOpt)
128
if err != nil {
129
res.SetError(err, cmdkit.ErrNormal)
130
return
@@ -137,7 +141,7 @@ Example:
141
return
142
}
143
140
- if err := forwardLocal(n.Context(), n.P2P, n.Peerstore, proto, listen, target); err != nil {
144
+ if err := forwardLocal(n.Context(), n.P2P, n.Peerstore, proto, listen, targets); err != nil {
145
res.SetError(err, cmdkit.ErrNormal)
146
return
147
}
@@ -145,6 +149,37 @@ Example:
149
},
150
}
151
152
+// parseIpfsAddr is a function that takes in addr string and return ipfsAddrs
153
+func parseIpfsAddr(addr string) ([]ipfsaddr.IPFSAddr, error) {
154
+ mutiladdr, err := ma.NewMultiaddr(addr)
155
+ if err != nil {
156
+ return nil, err
157
+ }
158
+ if _, err := mutiladdr.ValueForProtocol(ma.P_IPFS); err == nil {
159
+ iaddrs := make([]ipfsaddr.IPFSAddr, 1)
160
+ iaddrs[0], err = ipfsaddr.ParseMultiaddr(mutiladdr)
161
+ if err != nil {
162
+ return nil, err
163
+ }
164
+ return iaddrs, nil
165
+ }
166
+ // resolve mutiladdr whose protocol is not ma.P_IPFS
167
+ ctx, cancel := context.WithTimeout(context.Background(), resolveTimeout)
168
+ addrs, err := madns.Resolve(ctx, mutiladdr)
169
+ cancel()
170
+ if len(addrs) == 0 {
171
+ return nil, errors.New("fail to resolve the multiaddr:" + mutiladdr.String())
172
+ }
173
+ iaddrs := make([]ipfsaddr.IPFSAddr, len(addrs))
174
+ for i, addr := range addrs {
175
+ iaddrs[i], err = ipfsaddr.ParseMultiaddr(addr)
176
+ if err != nil {
177
+ return nil, err
178
+ }
179
+ }
180
+ return iaddrs, nil
181
+}
182
+
183
var p2pListenCmd = &cmds.Command{
184
Helptext: cmdkit.HelpText{
185
Tagline: "Create libp2p service",
@@ -212,13 +247,14 @@ func forwardRemote(ctx context.Context, p *p2p.P2P, proto protocol.ID, target ma
247
}
248
249
// forwardLocal forwards local connections to a libp2p service
215
-func forwardLocal(ctx context.Context, p *p2p.P2P, ps pstore.Peerstore, proto protocol.ID, bindAddr ma.Multiaddr, addr ipfsaddr.IPFSAddr) error {
216
- if addr != nil {
250
+func forwardLocal(ctx context.Context, p *p2p.P2P, ps pstore.Peerstore, proto protocol.ID, bindAddr ma.Multiaddr, addrs []ipfsaddr.IPFSAddr) error {
251
+ for _, addr := range addrs {
252
ps.AddAddr(addr.ID(), addr.Multiaddr(), pstore.TempAddrTTL)
253
}
219
-
254
// TODO: return some info
221
- _, err := p.ForwardLocal(ctx, addr.ID(), proto, bindAddr)
255
+ // the length of the addrs must large than 0
256
+ // peerIDs in addr must be the same and choose addr[0] to connect
257
+ _, err := p.ForwardLocal(ctx, addrs[0].ID(), proto, bindAddr)
258
return err
259
}
260
test/sharness/t0180-p2p.sh
+20
-2
@@ -24,7 +24,8 @@ check_test_ports() {
24
test_expect_success "test ports are closed" '
25
(! (netstat -lnp | grep "LISTEN" | grep ":10101 ")) &&
26
(! (netstat -lnp | grep "LISTEN" | grep ":10102 "))&&
27
- (! (netstat -lnp | grep "LISTEN" | grep ":10103 "))
27
+ (! (netstat -lnp | grep "LISTEN" | grep ":10103 ")) &&
28
+ (! (netstat -lnp | grep "LISTEN" | grep ":10104 "))
29
'
30
}
31
check_test_ports
@@ -73,10 +74,27 @@ test_server_to_client() {
74
75
spawn_sending_server
76
76
-test_expect_success 'S->C Setup client side' '
77
+test_expect_success 'S->C(/ipfs/peerID) Setup client side' '
78
ipfsi 1 p2p forward /x/p2p-test /ip4/127.0.0.1/tcp/10102 /ipfs/${PEERID_0} 2>&1 > dialer-stdouterr.log
79
'
80
81
+test_expect_success 'S->C Setup(dnsaddr/addr/ipfs/peerID) client side' '
82
+ ipfsi 1 p2p forward /x/p2p-test /ip4/127.0.0.1/tcp/10103 /dnsaddr/bootstrap.libp2p.io/ipfs/${PEERID_0} 2>&1 > dialer-stdouterr.log
83
+'
84
+
85
+test_expect_success 'S->C Setup(dnsaddr/addr) client side' '
86
+ ipfsi 1 p2p forward /x/p2p-test /ip4/127.0.0.1/tcp/10104 /dnsaddr/bootstrap.libp2p.io/ 2>&1 > dialer-stdouterr.log
87
+'
88
+
89
+
90
+test_expect_success 'S->C Output is empty' '
91
+ test_must_be_empty dialer-stdouterr.log
92
+'
93
+
94
+test_expect_success "'ipfs p2p ls | grep' succeeds" '
95
+ ipfsi 1 p2p ls | grep "/x/p2p-test /ip4/127.0.0.1/tcp/10104"
96
+'
97
+
98
test_server_to_client
99
100
test_expect_success 'S->C Connect with dead server' '