@cryptotaxi247 / kubo / commits / 830ed4870

p2p: refactor first review

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

Łukasz Magiera committed Jun 2, 2018 at 17:10 UTC 830ed487033f1d4b52d0a1305a7153a72a37529d
7 files changed +36 -30
core/commands/p2p.go
+10 -8
@@ -68,10 +68,12 @@ var p2pForwardCmd = &cmds.Command{
68 Helptext: cmdkit.HelpText{
69 Tagline: "Forward connections to or from libp2p services",
70 ShortDescription: `
71 -Forward connections to <listen-address> to <target-address>. Protocol specifies
72 -the libp2p protocol to use.
71 +Forward connections made to <listen-address> to <target-address>.
72
74 -To create libp2p service listener, specify '/ipfs' as <listen-address>
73 +<protocol> specifies the libp2p protocol name to use for libp2p
74 +connections and/or handlers.
75 +
76 +To create a libp2p service listener, specify '/ipfs' as <listen-address>
77
78 Examples:
79 ipfs p2p forward myproto /ipfs /ip4/127.0.0.1/tcp/1234
@@ -83,8 +85,8 @@ Examples:
85 `,
86 },
87 Arguments: []cmdkit.Argument{
86 - cmdkit.StringArg("protocol", true, false, "Protocol identifier."),
87 - cmdkit.StringArg("listen-address", true, false, "Listening endpoint"),
88 + cmdkit.StringArg("protocol", true, false, "Protocol name."),
89 + cmdkit.StringArg("listen-address", true, false, "Listening endpoint."),
90 cmdkit.StringArg("target-address", true, false, "Target endpoint."),
91 },
92 Run: func(req cmds.Request, res cmds.Response) {
@@ -311,7 +313,7 @@ var p2pStreamLsCmd = &cmds.Command{
313 Tagline: "List active p2p streams.",
314 },
315 Options: []cmdkit.Option{
314 - cmdkit.BoolOption("headers", "v", "Print table headers (HagndlerID, Protocol, Local, Remote)."),
316 + cmdkit.BoolOption("headers", "v", "Print table headers (ID, Protocol, Local, Remote)."),
317 },
318 Run: func(req cmds.Request, res cmds.Response) {
319 n, err := p2pGetNode(req)
@@ -326,7 +328,7 @@ var p2pStreamLsCmd = &cmds.Command{
328 output.Streams = append(output.Streams, P2PStreamInfoOutput{
329 HandlerID: strconv.FormatUint(id, 10),
330
329 - Protocol: s.Protocol,
331 + Protocol: string(s.Protocol),
332
333 OriginAddress: s.OriginAddr.String(),
334 TargetAddress: s.TargetAddr.String(),
@@ -349,7 +351,7 @@ var p2pStreamLsCmd = &cmds.Command{
351 w := tabwriter.NewWriter(buf, 1, 2, 1, ' ', 0)
352 for _, stream := range list.Streams {
353 if headers {
352 - fmt.Fprintln(w, "Id\tProtocol\tOrigin\tTarget")
354 + fmt.Fprintln(w, "ID\tProtocol\tOrigin\tTarget")
355 }
356
357 fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", stream.HandlerID, stream.Protocol, stream.OriginAddress, stream.TargetAddress)
docs/experimental-features.md
+11 -8
@@ -282,9 +282,12 @@ going to use `/kickass/1.0`.
282 1. A "server" node with peer ID `$SERVER_ID`
283 2. A "client" node.
284
285 -**On the "server" node:**
285 +**Netcat example:**
286
287 -First, start your application and have it listen on `$APP_PORT`.
287 +***On the "server" node:***
288 +
289 +First, start your application and have it listen for TCP connections on
290 +port `$APP_PORT`.
291
292 Then, configure the p2p listener by running:
293
@@ -296,11 +299,11 @@ This will configure IPFS to forward all incoming `/p2p/kickass/1.0` streams to
299 `127.0.0.1:$APP_PORT` (opening a new connection to `127.0.0.1:$APP_PORT` per
300 incoming stream.
301
299 -**On the "client" node:**
302 +***On the "client" node:***
303
301 -First, configure the p2p dialer to forward all inbound connections on
302 -`127.0.0.1:SOME_PORT` to the listener behind `/p2p/kickass/1.0` on the server
303 -node.
304 +First, configure the client p2p dialer, so that it forwards all inbound
305 +connections on `127.0.0.1:SOME_PORT` to the server node listening
306 +on `/p2p/kickass/1.0`.
307
308 ```sh
309 > ipfs p2p forward /kickass/1.0 /ip4/127.0.0.1/tcp/$SOME_PORT /ipfs/$SERVER_ID
@@ -310,12 +313,12 @@ Next, have your application open a connection to `127.0.0.1:$SOME_PORT`. This
313 connection will be forwarded to the service running on `127.0.0.1:$APP_PORT` on
314 the remote machine. You can test it with netcat:
315
313 -**On "server" node:**
316 +***On "server" node:***
317 ```sh
318 > nc -v -l -p $APP_PORT
319 ```
320
318 -**On "client" node:**
321 +***On "client" node:***
322 ```sh
323 > nc -v 127.0.0.1 $SOME_PORT
324 ```
p2p/listener.go
+1 -1
@@ -24,7 +24,7 @@ type listenerKey struct {
24 // ListenerRegistry is a collection of local application proto listeners.
25 type ListenerRegistry struct {
26 Listeners map[listenerKey]Listener
27 - lk *sync.Mutex
27 + lk sync.Mutex
28 }
29
30 func (r *ListenerRegistry) lock(l Listener) error {
p2p/local.go
+4 -4
@@ -19,7 +19,7 @@ type localListener struct {
19 p2p *P2P
20 id peer.ID
21
22 - proto string
22 + proto protocol.ID
23 laddr ma.Multiaddr
24 peer peer.ID
25
@@ -34,7 +34,7 @@ func (p2p *P2P) ForwardLocal(ctx context.Context, peer peer.ID, proto string, bi
34 p2p: p2p,
35 id: p2p.identity,
36
37 - proto: proto,
37 + proto: protocol.ID(proto),
38 laddr: bindAddr,
39 peer: peer,
40 }
@@ -66,7 +66,7 @@ func (l *localListener) dial() (net.Stream, error) {
66 return nil, err
67 }
68
69 - return l.p2p.peerHost.NewStream(l.ctx, l.peer, protocol.ID(l.proto))
69 + return l.p2p.peerHost.NewStream(l.ctx, l.peer, l.proto)
70 }
71
72 func (l *localListener) acceptConns() {
@@ -112,7 +112,7 @@ func (l *localListener) Close() error {
112 }
113
114 func (l *localListener) Protocol() string {
115 - return l.proto
115 + return string(l.proto)
116 }
117
118 func (l *localListener) ListenAddress() string {
p2p/p2p.go
+2 -2
@@ -27,11 +27,11 @@ func NewP2P(identity peer.ID, peerHost p2phost.Host, peerstore pstore.Peerstore)
27
28 Listeners: &ListenerRegistry{
29 Listeners: map[listenerKey]Listener{},
30 - lk: &sync.Mutex{},
30 + lk: sync.Mutex{},
31 },
32 Streams: &StreamRegistry{
33 Streams: map[uint64]*Stream{},
34 - lk: &sync.Mutex{},
34 + lk: sync.Mutex{},
35 },
36 }
37 }
p2p/remote.go
+5 -5
@@ -14,7 +14,7 @@ type remoteListener struct {
14 p2p *P2P
15
16 // Application proto identifier.
17 - proto string
17 + proto protocol.ID
18
19 // Address to proxy the incoming connections to
20 addr ma.Multiaddr
@@ -25,7 +25,7 @@ func (p2p *P2P) ForwardRemote(ctx context.Context, proto string, addr ma.Multiad
25 listener := &remoteListener{
26 p2p: p2p,
27
28 - proto: proto,
28 + proto: protocol.ID(proto),
29 addr: addr,
30 }
31
@@ -33,7 +33,7 @@ func (p2p *P2P) ForwardRemote(ctx context.Context, proto string, addr ma.Multiad
33 return nil, err
34 }
35
36 - p2p.peerHost.SetStreamHandler(protocol.ID(proto), func(remote net.Stream) {
36 + p2p.peerHost.SetStreamHandler(listener.proto, func(remote net.Stream) {
37 local, err := manet.Dial(addr)
38 if err != nil {
39 remote.Reset()
@@ -48,7 +48,7 @@ func (p2p *P2P) ForwardRemote(ctx context.Context, proto string, addr ma.Multiad
48 }
49
50 stream := &Stream{
51 - Protocol: proto,
51 + Protocol: listener.proto,
52
53 OriginAddr: peerMa,
54 TargetAddr: addr,
@@ -69,7 +69,7 @@ func (p2p *P2P) ForwardRemote(ctx context.Context, proto string, addr ma.Multiad
69 }
70
71 func (l *remoteListener) Protocol() string {
72 - return l.proto
72 + return string(l.proto)
73 }
74
75 func (l *remoteListener) ListenAddress() string {
p2p/stream.go
+3 -2
@@ -6,6 +6,7 @@ import (
6
7 ma "gx/ipfs/QmWWQ2Txc2c6tqjsBpzg5Ar652cHPGNsQQp2SejkNmkUMb/go-multiaddr"
8 net "gx/ipfs/QmYj8wdn5sZEHX2XMDWGBvcXJNdzVbaVpHmXvhHBVZepen/go-libp2p-net"
9 + "gx/ipfs/QmZNkThpqfVXs9GNbexPrfBbXSLNYeKrE7jwFM2oqHbyqN/go-libp2p-protocol"
10 manet "gx/ipfs/QmcGXGdw9BWDysPJQHxJinjGHha3eEg4vzFETre4woNwcX/go-multiaddr-net"
11 )
12
@@ -13,7 +14,7 @@ import (
14 type Stream struct {
15 id uint64
16
16 - Protocol string
17 + Protocol protocol.ID
18
19 OriginAddr ma.Multiaddr
20 TargetAddr ma.Multiaddr
@@ -59,7 +60,7 @@ func (s *Stream) startStreaming() {
60 // StreamRegistry is a collection of active incoming and outgoing proto app streams.
61 type StreamRegistry struct {
62 Streams map[uint64]*Stream
62 - lk *sync.Mutex
63 + lk sync.Mutex
64
65 nextID uint64
66 }