Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3
Commits
d34e3c81
Unverified
Commit
d34e3c81
authored
Nov 30, 2018
by
Steven Allen
Committed by
GitHub
Nov 30, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #5771 from ipfs/p2p/report-remote
p2p: report-peer-id option for listen
parents
1dcac25c
d6626a5c
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
63 additions
and
12 deletions
+63
-12
core/commands/p2p.go
core/commands/p2p.go
+5
-11
p2p/remote.go
p2p/remote.go
+15
-1
test/sharness/t0180-p2p.sh
test/sharness/t0180-p2p.sh
+43
-0
No files found.
core/commands/p2p.go
View file @
d34e3c81
...
...
@@ -53,6 +53,7 @@ type P2PStreamsOutput struct {
const
(
allowCustomProtocolOptionName
=
"allow-custom-protocol"
reportPeerIDOptionName
=
"report-peer-id"
)
var
resolveTimeout
=
10
*
time
.
Second
...
...
@@ -183,6 +184,7 @@ Example:
},
Options
:
[]
cmdkit
.
Option
{
cmdkit
.
BoolOption
(
allowCustomProtocolOptionName
,
"Don't require /x/ prefix"
),
cmdkit
.
BoolOption
(
reportPeerIDOptionName
,
"r"
,
"Send remote base58 peerid to target when a new connection is established"
),
},
Run
:
func
(
req
*
cmds
.
Request
,
res
cmds
.
ResponseEmitter
,
env
cmds
.
Environment
)
error
{
n
,
err
:=
p2pGetNode
(
env
)
...
...
@@ -206,15 +208,14 @@ Example:
}
allowCustom
,
_
:=
req
.
Options
[
allowCustomProtocolOptionName
]
.
(
bool
)
if
err
!=
nil
{
return
err
}
reportPeerID
,
_
:=
req
.
Options
[
reportPeerIDOptionName
]
.
(
bool
)
if
!
allowCustom
&&
!
strings
.
HasPrefix
(
string
(
proto
),
P2PProtoPrefix
)
{
return
errors
.
New
(
"protocol name must be within '"
+
P2PProtoPrefix
+
"' namespace"
)
}
return
forwardRemote
(
n
.
Context
(),
n
.
P2P
,
proto
,
target
)
_
,
err
=
n
.
P2P
.
ForwardRemote
(
n
.
Context
(),
proto
,
target
,
reportPeerID
)
return
err
},
}
...
...
@@ -252,13 +253,6 @@ func checkPort(target ma.Multiaddr) error {
return
nil
}
// forwardRemote forwards libp2p service connections to a manet address
func
forwardRemote
(
ctx
context
.
Context
,
p
*
p2p
.
P2P
,
proto
protocol
.
ID
,
target
ma
.
Multiaddr
)
error
{
// TODO: return some info
_
,
err
:=
p
.
ForwardRemote
(
ctx
,
proto
,
target
)
return
err
}
// forwardLocal forwards local connections to a libp2p service
func
forwardLocal
(
ctx
context
.
Context
,
p
*
p2p
.
P2P
,
ps
pstore
.
Peerstore
,
proto
protocol
.
ID
,
bindAddr
ma
.
Multiaddr
,
addrs
[]
ipfsaddr
.
IPFSAddr
)
error
{
for
_
,
addr
:=
range
addrs
{
...
...
p2p/remote.go
View file @
d34e3c81
...
...
@@ -2,6 +2,7 @@ package p2p
import
(
"context"
"fmt"
manet
"gx/ipfs/QmQVUtnrNGtCRkCMpXgpApfzQjc8FDaDVxHqWH8cnZQeh5/go-multiaddr-net"
ma
"gx/ipfs/QmRKLtwMw131aK7ugC3G7ybpumMz78YrJe5dzneyindvG1/go-multiaddr"
...
...
@@ -20,15 +21,21 @@ type remoteListener struct {
// Address to proxy the incoming connections to
addr
ma
.
Multiaddr
// reportRemote if set to true makes the handler send '<base58 remote peerid>\n'
// to target before any data is forwarded
reportRemote
bool
}
// ForwardRemote creates new p2p listener
func
(
p2p
*
P2P
)
ForwardRemote
(
ctx
context
.
Context
,
proto
protocol
.
ID
,
addr
ma
.
Multiaddr
)
(
Listener
,
error
)
{
func
(
p2p
*
P2P
)
ForwardRemote
(
ctx
context
.
Context
,
proto
protocol
.
ID
,
addr
ma
.
Multiaddr
,
reportRemote
bool
)
(
Listener
,
error
)
{
listener
:=
&
remoteListener
{
p2p
:
p2p
,
proto
:
proto
,
addr
:
addr
,
reportRemote
:
reportRemote
,
}
if
err
:=
p2p
.
ListenersP2P
.
Register
(
listener
);
err
!=
nil
{
...
...
@@ -47,6 +54,13 @@ func (l *remoteListener) handleStream(remote net.Stream) {
peer
:=
remote
.
Conn
()
.
RemotePeer
()
if
l
.
reportRemote
{
if
_
,
err
:=
fmt
.
Fprintf
(
local
,
"%s
\n
"
,
peer
.
Pretty
());
err
!=
nil
{
remote
.
Reset
()
return
}
}
peerMa
,
err
:=
ma
.
NewMultiaddr
(
maPrefix
+
peer
.
Pretty
())
if
err
!=
nil
{
remote
.
Reset
()
...
...
test/sharness/t0180-p2p.sh
View file @
d34e3c81
...
...
@@ -299,6 +299,49 @@ test_expect_success "'ipfs p2p close' closes by listen addr" '
test_must_be_empty actual
'
# Peer reporting
test_expect_success
'start p2p listener reporting peer'
'
ipfsi 0 p2p listen /x/p2p-test /ip4/127.0.0.1/tcp/10101 --report-peer-id 2>&1 > listener-stdouterr.log
'
test_expect_success
'C->S Spawn receiving server'
'
ma-pipe-unidir --listen --pidFile=listener.pid recv /ip4/127.0.0.1/tcp/10101 > server.out &
test_wait_for_file 30 100ms listener.pid &&
kill -0 $(cat listener.pid)
'
test_expect_success
'C->S Setup client side'
'
ipfsi 1 p2p forward /x/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'
'
ma-pipe-unidir send /ip4/127.0.0.1/tcp/10102 < test1.bin
'
test_expect_success
'C->S Ensure server finished'
'
go-sleep 250ms &&
test ! -f listener.pid
'
test_expect_success
'C->S Output looks good'
'
echo ${PEERID_1} > expected &&
cat test1.bin >> expected &&
test_cmp server.out expected
'
test_expect_success
'C->S Close listeners'
'
ipfsi 1 p2p close -p /x/p2p-test &&
ipfsi 0 p2p close -p /x/p2p-test &&
ipfsi 0 p2p ls > actual &&
test_must_be_empty actual &&
ipfsi 1 p2p ls > actual &&
test_must_be_empty actual
'
test_expect_success
"non /x/ scoped protocols are not allowed"
'
test_must_fail ipfsi 0 p2p listen /its/not/a/x/path /ip4/127.0.0.1/tcp/10101 2> actual &&
echo "Error: protocol name must be within '
"'"
'/x/'
"'"
' namespace" > expected
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment