diff --git a/core/commands/p2p.go b/core/commands/p2p.go index aa7def1b3..95c6cc02d 100644 --- a/core/commands/p2p.go +++ b/core/commands/p2p.go @@ -96,11 +96,15 @@ Examples: return } - //TODO: Do we really want/need implicit prefix? - proto := "/p2p/" + req.Arguments()[0] + proto := req.Arguments()[0] listen := req.Arguments()[1] target := req.Arguments()[2] + if !strings.HasPrefix(proto, "/p2p/") { + res.SetError(errors.New("protocol name must be within '/p2p/' namespace"), cmdkit.ErrNormal) + return + } + if strings.HasPrefix(listen, "/ipfs") { if listen != "/ipfs" { res.SetError(errors.New("only '/ipfs' is allowed as libp2p listen address"), cmdkit.ErrNormal) @@ -246,9 +250,6 @@ var p2pCloseCmd = &cmds.Command{ match := func(listener p2p.Listener) bool { out := true - if p || !strings.HasPrefix(proto, "/p2p/") { - proto = "/p2p/" + proto - } if p { out = out && (proto == listener.Protocol()) diff --git a/docs/experimental-features.md b/docs/experimental-features.md index b8cb655d5..400e6fe7b 100644 --- a/docs/experimental-features.md +++ b/docs/experimental-features.md @@ -277,7 +277,7 @@ The `p2p` command needs to be enabled in config: First, pick a protocol name for your application. Think of the protocol name as a port number, just significantly more user-friendly. In this example, we're -going to use `/kickass/1.0`. +going to use `/p2p/kickass/1.0`. ***Setup:*** @@ -292,7 +292,7 @@ port `$APP_PORT`. Then, configure the p2p listener by running: ```sh -> ipfs p2p forward /kickass/1.0 /ipfs /ip4/127.0.0.1/tcp/$APP_PORT +> ipfs p2p forward /p2p/kickass/1.0 /ipfs /ip4/127.0.0.1/tcp/$APP_PORT ``` This will configure IPFS to forward all incoming `/p2p/kickass/1.0` streams to @@ -306,7 +306,7 @@ connections on `127.0.0.1:SOME_PORT` to the server node listening on `/p2p/kickass/1.0`. ```sh -> ipfs p2p forward /kickass/1.0 /ip4/127.0.0.1/tcp/$SOME_PORT /ipfs/$SERVER_ID +> ipfs p2p forward /p2p/kickass/1.0 /ip4/127.0.0.1/tcp/$SOME_PORT /ipfs/$SERVER_ID ``` Next, have your application open a connection to `127.0.0.1:$SOME_PORT`. This @@ -341,13 +341,13 @@ _you can get `$SERVER_ID` by running `ipfs id -f "\n"`_ ***First, on the "server" node:*** ```sh -ipfs p2p forward ssh /ipfs /ip4/127.0.0.1/tcp/22 +ipfs p2p forward /p2p/ssh /ipfs /ip4/127.0.0.1/tcp/22 ``` ***Then, on "client" node:*** ```sh -ipfs p2p forward ssh /ip4/127.0.0.1/tcp/2222 /ipfs/$SERVER_ID +ipfs p2p forward /p2p/ssh /ip4/127.0.0.1/tcp/2222 /ipfs/$SERVER_ID ``` You should now be able to connect to your ssh server through a libp2p connection diff --git a/test/sharness/t0180-p2p.sh b/test/sharness/t0180-p2p.sh index 71b8b5003..940ce1092 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 p2p-test /ipfs /ip4/127.0.0.1/tcp/10101 2>&1 > listener-stdouterr.log + ipfsi 0 p2p forward /p2p/p2p-test /ipfs /ip4/127.0.0.1/tcp/10101 2>&1 > listener-stdouterr.log ' # Server to client communications @@ -69,7 +69,7 @@ test_server_to_client() { spawn_sending_server test_expect_success 'S->C Setup client side' ' - ipfsi 1 p2p forward p2p-test /ip4/127.0.0.1/tcp/10102 /ipfs/${PEERID_0} 2>&1 > dialer-stdouterr.log + ipfsi 1 p2p forward /p2p/p2p-test /ip4/127.0.0.1/tcp/10102 /ipfs/${PEERID_0} 2>&1 > dialer-stdouterr.log ' test_server_to_client @@ -87,7 +87,7 @@ spawn_sending_server test_server_to_client test_expect_success 'S->C Close local listener' ' - ipfsi 1 p2p close -p p2p-test + ipfsi 1 p2p close -p /p2p/p2p-test ' check_test_ports @@ -102,7 +102,7 @@ test_expect_success 'C->S Spawn receiving server' ' ' test_expect_success 'C->S Setup client side' ' - ipfsi 1 p2p forward p2p-test /ip4/127.0.0.1/tcp/10102 /ipfs/${PEERID_0} 2>&1 > dialer-stdouterr.log + ipfsi 1 p2p forward /p2p/p2p-test /ip4/127.0.0.1/tcp/10102 /ipfs/${PEERID_0} 2>&1 > dialer-stdouterr.log ' test_expect_success 'C->S Connect and receive data' ' @@ -119,7 +119,7 @@ test_expect_success 'C->S Output looks good' ' ' test_expect_success 'C->S Close local listener' ' - ipfsi 1 p2p close -p p2p-test + ipfsi 1 p2p close -p /p2p/p2p-test ' check_test_ports @@ -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 p2p-test /ipfs /ip4/127.0.0.1/tcp/10101 + test_must_fail ipfsi 0 p2p forward /p2p/p2p-test /ipfs /ip4/127.0.0.1/tcp/10101 ' test_expect_success "'ipfs p2p stream ls' output is empty" ' @@ -147,7 +147,7 @@ test_expect_success "'ipfs p2p stream ls' output is empty" ' test_expect_success "Setup: Idle stream" ' ma-pipe-unidir --listen --pidFile=listener.pid recv /ip4/127.0.0.1/tcp/10101 & - ipfsi 1 p2p forward p2p-test /ip4/127.0.0.1/tcp/10102 /ipfs/$PEERID_0 2>&1 > dialer-stdouterr.log && + ipfsi 1 p2p forward /p2p/p2p-test /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 & test_wait_for_file 30 100ms listener.pid && @@ -172,13 +172,13 @@ test_expect_success "'ipfs p2p stream close' closes stream" ' ' test_expect_success "'ipfs p2p close' closes remote handler" ' - ipfsi 0 p2p close -p p2p-test && + ipfsi 0 p2p close -p /p2p/p2p-test && ipfsi 0 p2p ls > actual && test_must_be_empty actual ' test_expect_success "'ipfs p2p close' closes local handler" ' - ipfsi 1 p2p close -p p2p-test && + ipfsi 1 p2p close -p /p2p/p2p-test && ipfsi 1 p2p ls > actual && test_must_be_empty actual ' @@ -188,8 +188,8 @@ 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 p2p-test2 /ipfs /ip4/127.0.0.1/tcp/10101 2>&1 > listener-stdouterr.log && - ipfsi 1 p2p forward p2p-test2 /ip4/127.0.0.1/tcp/10102 /ipfs/$PEERID_0 2>&1 > dialer-stdouterr.log && + ipfsi 0 p2p forward /p2p/p2p-test2 /ipfs /ip4/127.0.0.1/tcp/10101 2>&1 > listener-stdouterr.log && + ipfsi 1 p2p forward /p2p/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 & test_wait_for_file 30 100ms listener.pid && @@ -225,12 +225,18 @@ 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 1234 /ipfs /ip4/127.0.0.1/tcp/10101 && - ipfsi 0 p2p close -p 1234 && + ipfsi 0 p2p forward /p2p/1234 /ipfs /ip4/127.0.0.1/tcp/10101 && + ipfsi 0 p2p close -p /p2p/1234 && ipfsi 0 p2p ls > actual && test_must_be_empty actual ' +test_expect_success "non /p2p/ scoped protocols are not allowed" ' + test_must_fail ipfsi 0 p2p forward /its/not/a/p2p/path /ipfs /ip4/127.0.0.1/tcp/10101 2> actual && + echo "Error: protocol name must be within '"'"'/p2p/'"'"' namespace" > expected + test_cmp expected actual +' + check_test_ports test_expect_success 'stop iptb' '