p2p: split forward into 2 commands
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Jun 18, 2018 at 18:57 UTC
f5ab137fc09b402c5da329fff47b562ae19fce7e
4 files changed
+64
-28
core/commands/commands_test.go
+1
@@ -163,6 +163,7 @@ func TestCommands(t *testing.T) {
163
"/p2p",
164
"/p2p/close",
165
"/p2p/forward",
166
+ "/p2p/listen",
167
"/p2p/ls",
168
"/p2p/stream",
169
"/p2p/stream/close",
core/commands/p2p.go
+56
-21
@@ -62,6 +62,7 @@ are refined`,
62
"stream": p2pStreamCmd,
63
64
"forward": p2pForwardCmd,
65
+ "listen": p2pListenCmd,
66
"close": p2pCloseCmd,
67
"ls": p2pLsCmd,
68
},
@@ -69,19 +70,14 @@ are refined`,
70
71
var p2pForwardCmd = &cmds.Command{
72
Helptext: cmdkit.HelpText{
72
- Tagline: "Forward connections to or from libp2p services",
73
+ Tagline: "Forward connections to libp2p service",
74
ShortDescription: `
75
Forward connections made to <listen-address> to <target-address>.
76
77
<protocol> specifies the libp2p protocol name to use for libp2p
78
connections and/or handlers. It must be prefixed with '` + P2PProtoPrefix + `'.
79
79
-To create a libp2p service listener, specify '/ipfs' as <listen-address>
80
-
81
-Examples:
82
- ipfs p2p forward ` + P2PProtoPrefix + `myproto /ipfs /ip4/127.0.0.1/tcp/1234
83
- - Forward connections to 'myproto' libp2p service to 127.0.0.1:1234
84
-
80
+Example:
81
ipfs p2p forward ` + P2PProtoPrefix + `myproto /ip4/127.0.0.1/tcp/4567 /ipfs/QmPeer
82
- Forward connections to 127.0.0.1:4567 to '` + P2PProtoPrefix + `myproto' service on /ipfs/QmPeer
83
@@ -117,22 +113,61 @@ Examples:
113
return
114
}
115
120
- if strings.HasPrefix(listen, "/ipfs") {
121
- if listen != "/ipfs" {
122
- res.SetError(errors.New("only '/ipfs' is allowed as libp2p listen address"), cmdkit.ErrNormal)
123
- return
124
- }
116
+ if err := forwardLocal(n.Context(), n.P2P, n.Peerstore, proto, listen, target); err != nil {
117
+ res.SetError(err, cmdkit.ErrNormal)
118
+ return
119
+ }
120
+ res.SetOutput(nil)
121
+ },
122
+}
123
126
- if err := forwardRemote(n.Context(), n.P2P, proto, target); err != nil {
127
- res.SetError(err, cmdkit.ErrNormal)
128
- return
129
- }
130
- } else {
131
- if err := forwardLocal(n.Context(), n.P2P, n.Peerstore, proto, listen, target); err != nil {
132
- res.SetError(err, cmdkit.ErrNormal)
133
- return
134
- }
124
+var p2pListenCmd = &cmds.Command{
125
+ Helptext: cmdkit.HelpText{
126
+ Tagline: "Create libp2p service",
127
+ ShortDescription: `
128
+Create libp2p service and forward connections made to <target-address>.
129
+
130
+<protocol> specifies the libp2p handler name. It must be prefixed with '` + P2PProtoPrefix + `'.
131
+
132
+Example:
133
+ ipfs p2p listen ` + P2PProtoPrefix + `myproto /ip4/127.0.0.1/tcp/1234
134
+ - Forward connections to 'myproto' libp2p service to 127.0.0.1:1234
135
+
136
+`,
137
+ },
138
+ Arguments: []cmdkit.Argument{
139
+ cmdkit.StringArg("protocol", true, false, "Protocol name."),
140
+ cmdkit.StringArg("target-address", true, false, "Target endpoint."),
141
+ },
142
+ Options: []cmdkit.Option{
143
+ cmdkit.BoolOption("allow-custom-protocol", "Don't require /x/ prefix"),
144
+ },
145
+ Run: func(req cmds.Request, res cmds.Response) {
146
+ n, err := p2pGetNode(req)
147
+ if err != nil {
148
+ res.SetError(err, cmdkit.ErrNormal)
149
+ return
150
}
151
+
152
+ proto := req.Arguments()[0]
153
+ target := req.Arguments()[1]
154
+
155
+ allowCustom, _, err := req.Option("allow-custom-protocol").Bool()
156
+ if err != nil {
157
+ res.SetError(err, cmdkit.ErrNormal)
158
+ return
159
+ }
160
+
161
+ if !allowCustom && !strings.HasPrefix(proto, P2PProtoPrefix) {
162
+ res.SetError(errors.New("protocol name must be within '"+P2PProtoPrefix+"' namespace"), cmdkit.ErrNormal)
163
+ return
164
+ }
165
+
166
+ if err := forwardRemote(n.Context(), n.P2P, proto, target); err != nil {
167
+ res.SetError(err, cmdkit.ErrNormal)
168
+ return
169
+ }
170
+
171
res.SetOutput(nil)
172
},
173
}
docs/experimental-features.md
+2
-2
@@ -292,7 +292,7 @@ port `$APP_PORT`.
292
Then, configure the p2p listener by running:
293
294
```sh
295
-> ipfs p2p forward /x/kickass/1.0 /ipfs /ip4/127.0.0.1/tcp/$APP_PORT
295
+> ipfs p2p listen /x/kickass/1.0 /ip4/127.0.0.1/tcp/$APP_PORT
296
```
297
298
This will configure IPFS to forward all incoming `/x/kickass/1.0` streams to
@@ -341,7 +341,7 @@ _you can get `$SERVER_ID` by running `ipfs id -f "<id>\n"`_
341
***First, on the "server" node:***
342
343
```sh
344
-ipfs p2p forward /x/ssh /ipfs /ip4/127.0.0.1/tcp/22
344
+ipfs p2p listen /x/ssh /ip4/127.0.0.1/tcp/22
345
```
346
347
***Then, on "client" node:***
test/sharness/t0180-p2p.sh
+5
-5
@@ -38,7 +38,7 @@ test_expect_success "enable filestore config setting" '
38
'
39
40
test_expect_success 'start p2p listener' '
41
- ipfsi 0 p2p forward /x/p2p-test /ipfs /ip4/127.0.0.1/tcp/10101 2>&1 > listener-stdouterr.log
41
+ ipfsi 0 p2p listen /x/p2p-test /ip4/127.0.0.1/tcp/10101 2>&1 > listener-stdouterr.log
42
'
43
44
# Server to client communications
@@ -136,7 +136,7 @@ test_expect_success "'ipfs p2p ls' output looks good" '
136
'
137
138
test_expect_success "Cannot re-register app handler" '
139
- test_must_fail ipfsi 0 p2p forward /x/p2p-test /ipfs /ip4/127.0.0.1/tcp/10101
139
+ test_must_fail ipfsi 0 p2p listen /x/p2p-test /ip4/127.0.0.1/tcp/10101
140
'
141
142
test_expect_success "'ipfs p2p stream ls' output is empty" '
@@ -188,7 +188,7 @@ check_test_ports
188
test_expect_success "Setup: Idle stream(2)" '
189
ma-pipe-unidir --listen --pidFile=listener.pid recv /ip4/127.0.0.1/tcp/10101 &
190
191
- ipfsi 0 p2p forward /x/p2p-test2 /ipfs /ip4/127.0.0.1/tcp/10101 2>&1 > listener-stdouterr.log &&
191
+ ipfsi 0 p2p listen /x/p2p-test2 /ip4/127.0.0.1/tcp/10101 2>&1 > listener-stdouterr.log &&
192
ipfsi 1 p2p forward /x/p2p-test2 /ip4/127.0.0.1/tcp/10102 /ipfs/$PEERID_0 2>&1 > dialer-stdouterr.log &&
193
ma-pipe-unidir --pidFile=client.pid recv /ip4/127.0.0.1/tcp/10102 &
194
@@ -225,7 +225,7 @@ test_expect_success "'ipfs p2p stream close -a' closes streams" '
225
check_test_ports
226
227
test_expect_success "'ipfs p2p close' closes app numeric handlers" '
228
- ipfsi 0 p2p forward /x/1234 /ipfs /ip4/127.0.0.1/tcp/10101 &&
228
+ ipfsi 0 p2p listen /x/1234 /ip4/127.0.0.1/tcp/10101 &&
229
ipfsi 0 p2p close -p /x/1234 &&
230
ipfsi 0 p2p ls > actual &&
231
test_must_be_empty actual
@@ -240,7 +240,7 @@ test_expect_success "non /x/ scoped protocols are not allowed" '
240
check_test_ports
241
242
test_expect_success 'start p2p listener on custom proto' '
243
- ipfsi 0 p2p forward --allow-custom-protocol /p2p-test /ipfs /ip4/127.0.0.1/tcp/10101 2>&1 > listener-stdouterr.log &&
243
+ ipfsi 0 p2p listen --allow-custom-protocol /p2p-test /ip4/127.0.0.1/tcp/10101 2>&1 > listener-stdouterr.log &&
244
test_must_be_empty listener-stdouterr.log
245
'
246