Skip to content

Commit

Permalink
p2p: don't automatically prefix proto with /p2p
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
  • Loading branch information
magik6k committed Jun 3, 2018
1 parent 90beabb commit ca4ca83
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 23 deletions.
11 changes: 6 additions & 5 deletions core/commands/p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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())
Expand Down
10 changes: 5 additions & 5 deletions docs/experimental-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,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:***

Expand All @@ -260,7 +260,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
Expand All @@ -274,7 +274,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
Expand Down Expand Up @@ -309,13 +309,13 @@ _you can get `$SERVER_ID` by running `ipfs id -f "<id>\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
Expand Down
32 changes: 19 additions & 13 deletions test/sharness/t0180-p2p.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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' '
Expand All @@ -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
Expand All @@ -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" '
Expand All @@ -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 &&
Expand All @@ -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
'
Expand All @@ -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 &&
Expand Down Expand Up @@ -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' '
Expand Down

0 comments on commit ca4ca83

Please sign in to comment.