From f5ab137fc09b402c5da329fff47b562ae19fce7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Mon, 18 Jun 2018 18:57:27 +0200 Subject: [PATCH] p2p: split forward into 2 commands MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Ɓukasz Magiera --- core/commands/commands_test.go | 1 + core/commands/p2p.go | 79 ++++++++++++++++++++++++---------- docs/experimental-features.md | 4 +- test/sharness/t0180-p2p.sh | 10 ++--- 4 files changed, 65 insertions(+), 29 deletions(-) diff --git a/core/commands/commands_test.go b/core/commands/commands_test.go index 4944a32d3..a3bef025f 100644 --- a/core/commands/commands_test.go +++ b/core/commands/commands_test.go @@ -163,6 +163,7 @@ func TestCommands(t *testing.T) { "/p2p", "/p2p/close", "/p2p/forward", + "/p2p/listen", "/p2p/ls", "/p2p/stream", "/p2p/stream/close", diff --git a/core/commands/p2p.go b/core/commands/p2p.go index f5b8eab99..364b55ca7 100644 --- a/core/commands/p2p.go +++ b/core/commands/p2p.go @@ -62,6 +62,7 @@ are refined`, "stream": p2pStreamCmd, "forward": p2pForwardCmd, + "listen": p2pListenCmd, "close": p2pCloseCmd, "ls": p2pLsCmd, }, @@ -69,19 +70,14 @@ are refined`, var p2pForwardCmd = &cmds.Command{ Helptext: cmdkit.HelpText{ - Tagline: "Forward connections to or from libp2p services", + Tagline: "Forward connections to libp2p service", ShortDescription: ` Forward connections made to to . specifies the libp2p protocol name to use for libp2p connections and/or handlers. It must be prefixed with '` + P2PProtoPrefix + `'. -To create a libp2p service listener, specify '/ipfs' as - -Examples: - ipfs p2p forward ` + P2PProtoPrefix + `myproto /ipfs /ip4/127.0.0.1/tcp/1234 - - Forward connections to 'myproto' libp2p service to 127.0.0.1:1234 - +Example: ipfs p2p forward ` + P2PProtoPrefix + `myproto /ip4/127.0.0.1/tcp/4567 /ipfs/QmPeer - Forward connections to 127.0.0.1:4567 to '` + P2PProtoPrefix + `myproto' service on /ipfs/QmPeer @@ -117,26 +113,65 @@ Examples: return } - if strings.HasPrefix(listen, "/ipfs") { - if listen != "/ipfs" { - res.SetError(errors.New("only '/ipfs' is allowed as libp2p listen address"), cmdkit.ErrNormal) - return - } - - if err := forwardRemote(n.Context(), n.P2P, proto, target); err != nil { - res.SetError(err, cmdkit.ErrNormal) - return - } - } else { - if err := forwardLocal(n.Context(), n.P2P, n.Peerstore, proto, listen, target); err != nil { - res.SetError(err, cmdkit.ErrNormal) - return - } + if err := forwardLocal(n.Context(), n.P2P, n.Peerstore, proto, listen, target); err != nil { + res.SetError(err, cmdkit.ErrNormal) + return } res.SetOutput(nil) }, } +var p2pListenCmd = &cmds.Command{ + Helptext: cmdkit.HelpText{ + Tagline: "Create libp2p service", + ShortDescription: ` +Create libp2p service and forward connections made to . + + specifies the libp2p handler name. It must be prefixed with '` + P2PProtoPrefix + `'. + +Example: + ipfs p2p listen ` + P2PProtoPrefix + `myproto /ip4/127.0.0.1/tcp/1234 + - Forward connections to 'myproto' libp2p service to 127.0.0.1:1234 + +`, + }, + Arguments: []cmdkit.Argument{ + cmdkit.StringArg("protocol", true, false, "Protocol name."), + cmdkit.StringArg("target-address", true, false, "Target endpoint."), + }, + Options: []cmdkit.Option{ + cmdkit.BoolOption("allow-custom-protocol", "Don't require /x/ prefix"), + }, + Run: func(req cmds.Request, res cmds.Response) { + n, err := p2pGetNode(req) + if err != nil { + res.SetError(err, cmdkit.ErrNormal) + return + } + + proto := req.Arguments()[0] + target := req.Arguments()[1] + + allowCustom, _, err := req.Option("allow-custom-protocol").Bool() + if err != nil { + res.SetError(err, cmdkit.ErrNormal) + return + } + + if !allowCustom && !strings.HasPrefix(proto, P2PProtoPrefix) { + res.SetError(errors.New("protocol name must be within '"+P2PProtoPrefix+"' namespace"), cmdkit.ErrNormal) + return + } + + if err := forwardRemote(n.Context(), n.P2P, proto, target); err != nil { + res.SetError(err, cmdkit.ErrNormal) + return + } + + res.SetOutput(nil) + }, +} + // forwardRemote forwards libp2p service connections to a manet address func forwardRemote(ctx context.Context, p *p2p.P2P, proto string, target string) error { if strings.HasPrefix(target, "/ipfs") { diff --git a/docs/experimental-features.md b/docs/experimental-features.md index d714e6242..29f9f5e3a 100644 --- a/docs/experimental-features.md +++ b/docs/experimental-features.md @@ -292,7 +292,7 @@ port `$APP_PORT`. Then, configure the p2p listener by running: ```sh -> ipfs p2p forward /x/kickass/1.0 /ipfs /ip4/127.0.0.1/tcp/$APP_PORT +> ipfs p2p listen /x/kickass/1.0 /ip4/127.0.0.1/tcp/$APP_PORT ``` 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 "\n"`_ ***First, on the "server" node:*** ```sh -ipfs p2p forward /x/ssh /ipfs /ip4/127.0.0.1/tcp/22 +ipfs p2p listen /x/ssh /ip4/127.0.0.1/tcp/22 ``` ***Then, on "client" node:*** diff --git a/test/sharness/t0180-p2p.sh b/test/sharness/t0180-p2p.sh index 10207a384..4ae81daa6 100755 --- a/test/sharness/t0180-p2p.sh +++ b/test/sharness/t0180-p2p.sh @@ -38,7 +38,7 @@ test_expect_success "enable filestore config setting" ' ' test_expect_success 'start p2p listener' ' - ipfsi 0 p2p forward /x/p2p-test /ipfs /ip4/127.0.0.1/tcp/10101 2>&1 > listener-stdouterr.log + ipfsi 0 p2p listen /x/p2p-test /ip4/127.0.0.1/tcp/10101 2>&1 > listener-stdouterr.log ' # Server to client communications @@ -136,7 +136,7 @@ test_expect_success "'ipfs p2p ls' output looks good" ' ' test_expect_success "Cannot re-register app handler" ' - test_must_fail ipfsi 0 p2p forward /x/p2p-test /ipfs /ip4/127.0.0.1/tcp/10101 + test_must_fail ipfsi 0 p2p listen /x/p2p-test /ip4/127.0.0.1/tcp/10101 ' test_expect_success "'ipfs p2p stream ls' output is empty" ' @@ -188,7 +188,7 @@ check_test_ports test_expect_success "Setup: Idle stream(2)" ' ma-pipe-unidir --listen --pidFile=listener.pid recv /ip4/127.0.0.1/tcp/10101 & - ipfsi 0 p2p forward /x/p2p-test2 /ipfs /ip4/127.0.0.1/tcp/10101 2>&1 > listener-stdouterr.log && + ipfsi 0 p2p listen /x/p2p-test2 /ip4/127.0.0.1/tcp/10101 2>&1 > listener-stdouterr.log && ipfsi 1 p2p forward /x/p2p-test2 /ip4/127.0.0.1/tcp/10102 /ipfs/$PEERID_0 2>&1 > dialer-stdouterr.log && ma-pipe-unidir --pidFile=client.pid recv /ip4/127.0.0.1/tcp/10102 & @@ -225,7 +225,7 @@ test_expect_success "'ipfs p2p stream close -a' closes streams" ' check_test_ports test_expect_success "'ipfs p2p close' closes app numeric handlers" ' - ipfsi 0 p2p forward /x/1234 /ipfs /ip4/127.0.0.1/tcp/10101 && + ipfsi 0 p2p listen /x/1234 /ip4/127.0.0.1/tcp/10101 && ipfsi 0 p2p close -p /x/1234 && ipfsi 0 p2p ls > actual && test_must_be_empty actual @@ -240,7 +240,7 @@ test_expect_success "non /x/ scoped protocols are not allowed" ' check_test_ports test_expect_success 'start p2p listener on custom proto' ' - ipfsi 0 p2p forward --allow-custom-protocol /p2p-test /ipfs /ip4/127.0.0.1/tcp/10101 2>&1 > listener-stdouterr.log && + ipfsi 0 p2p listen --allow-custom-protocol /p2p-test /ip4/127.0.0.1/tcp/10101 2>&1 > listener-stdouterr.log && test_must_be_empty listener-stdouterr.log '