提交 c0a1e80f 作者: Łukasz Magiera 提交者: Steven Allen

p2p: report-peer-id option for listen

License: MIT
Signed-off-by: 's avatarŁukasz Magiera <magik6k@gmail.com>
上级 1dcac25c
......@@ -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 {
......
......@@ -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()
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论