@cryptotaxi247 / kubo / commits / c0a1e80f3

p2p: report-peer-id option for listen

License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>

Łukasz Magiera committed Nov 13, 2018 at 03:07 UTC c0a1e80f3629984944079bdc71d7f97f2a0c57e2
2 files changed +20 -12
core/commands/p2p.go
+5 -11
@@ -53,6 +53,7 @@ type P2PStreamsOutput struct {
53
54 const (
55 allowCustomProtocolOptionName = "allow-custom-protocol"
56 + reportPeerIDOptionName = "report-peer-id"
57 )
58
59 var resolveTimeout = 10 * time.Second
@@ -183,6 +184,7 @@ Example:
184 },
185 Options: []cmdkit.Option{
186 cmdkit.BoolOption(allowCustomProtocolOptionName, "Don't require /x/ prefix"),
187 + cmdkit.BoolOption(reportPeerIDOptionName, "r", "Send remote base58 peerid to target when a new connection is established"),
188 },
189 Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
190 n, err := p2pGetNode(env)
@@ -206,15 +208,14 @@ Example:
208 }
209
210 allowCustom, _ := req.Options[allowCustomProtocolOptionName].(bool)
209 - if err != nil {
210 - return err
211 - }
211 + reportPeerID, _ := req.Options[reportPeerIDOptionName].(bool)
212
213 if !allowCustom && !strings.HasPrefix(string(proto), P2PProtoPrefix) {
214 return errors.New("protocol name must be within '" + P2PProtoPrefix + "' namespace")
215 }
216
217 - return forwardRemote(n.Context(), n.P2P, proto, target)
217 + _, err = n.P2P.ForwardRemote(n.Context(), proto, target, reportPeerID)
218 + return err
219 },
220 }
221
@@ -252,13 +253,6 @@ func checkPort(target ma.Multiaddr) error {
253 return nil
254 }
255
255 -// forwardRemote forwards libp2p service connections to a manet address
256 -func forwardRemote(ctx context.Context, p *p2p.P2P, proto protocol.ID, target ma.Multiaddr) error {
257 - // TODO: return some info
258 - _, err := p.ForwardRemote(ctx, proto, target)
259 - return err
260 -}
261 -
256 // forwardLocal forwards local connections to a libp2p service
257 func forwardLocal(ctx context.Context, p *p2p.P2P, ps pstore.Peerstore, proto protocol.ID, bindAddr ma.Multiaddr, addrs []ipfsaddr.IPFSAddr) error {
258 for _, addr := range addrs {
p2p/remote.go
+15 -1
@@ -2,6 +2,7 @@ package p2p
2
3 import (
4 "context"
5 + "fmt"
6
7 manet "gx/ipfs/QmQVUtnrNGtCRkCMpXgpApfzQjc8FDaDVxHqWH8cnZQeh5/go-multiaddr-net"
8 ma "gx/ipfs/QmRKLtwMw131aK7ugC3G7ybpumMz78YrJe5dzneyindvG1/go-multiaddr"
@@ -20,15 +21,21 @@ type remoteListener struct {
21
22 // Address to proxy the incoming connections to
23 addr ma.Multiaddr
24 +
25 + // reportRemote if set to true makes the handler send '<base58 remote peerid>\n'
26 + // to target before any data is forwarded
27 + reportRemote bool
28 }
29
30 // ForwardRemote creates new p2p listener
26 -func (p2p *P2P) ForwardRemote(ctx context.Context, proto protocol.ID, addr ma.Multiaddr) (Listener, error) {
31 +func (p2p *P2P) ForwardRemote(ctx context.Context, proto protocol.ID, addr ma.Multiaddr, reportRemote bool) (Listener, error) {
32 listener := &remoteListener{
33 p2p: p2p,
34
35 proto: proto,
36 addr: addr,
37 +
38 + reportRemote: reportRemote,
39 }
40
41 if err := p2p.ListenersP2P.Register(listener); err != nil {
@@ -47,6 +54,13 @@ func (l *remoteListener) handleStream(remote net.Stream) {
54
55 peer := remote.Conn().RemotePeer()
56
57 + if l.reportRemote {
58 + if _, err := fmt.Fprintf(local, "%s\n", peer.Pretty()); err != nil {
59 + remote.Reset()
60 + return
61 + }
62 + }
63 +
64 peerMa, err := ma.NewMultiaddr(maPrefix + peer.Pretty())
65 if err != nil {
66 remote.Reset()