提交 830ed487 作者: Łukasz Magiera

p2p: refactor first review

License: MIT
Signed-off-by: 's avatarŁukasz Magiera <magik6k@gmail.com>
上级 3e0184bd
......@@ -68,10 +68,12 @@ var p2pForwardCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "Forward connections to or from libp2p services",
ShortDescription: `
Forward connections to <listen-address> to <target-address>. Protocol specifies
the libp2p protocol to use.
Forward connections made to <listen-address> to <target-address>.
To create libp2p service listener, specify '/ipfs' as <listen-address>
<protocol> specifies the libp2p protocol name to use for libp2p
connections and/or handlers.
To create a libp2p service listener, specify '/ipfs' as <listen-address>
Examples:
ipfs p2p forward myproto /ipfs /ip4/127.0.0.1/tcp/1234
......@@ -83,8 +85,8 @@ Examples:
`,
},
Arguments: []cmdkit.Argument{
cmdkit.StringArg("protocol", true, false, "Protocol identifier."),
cmdkit.StringArg("listen-address", true, false, "Listening endpoint"),
cmdkit.StringArg("protocol", true, false, "Protocol name."),
cmdkit.StringArg("listen-address", true, false, "Listening endpoint."),
cmdkit.StringArg("target-address", true, false, "Target endpoint."),
},
Run: func(req cmds.Request, res cmds.Response) {
......@@ -311,7 +313,7 @@ var p2pStreamLsCmd = &cmds.Command{
Tagline: "List active p2p streams.",
},
Options: []cmdkit.Option{
cmdkit.BoolOption("headers", "v", "Print table headers (HagndlerID, Protocol, Local, Remote)."),
cmdkit.BoolOption("headers", "v", "Print table headers (ID, Protocol, Local, Remote)."),
},
Run: func(req cmds.Request, res cmds.Response) {
n, err := p2pGetNode(req)
......@@ -326,7 +328,7 @@ var p2pStreamLsCmd = &cmds.Command{
output.Streams = append(output.Streams, P2PStreamInfoOutput{
HandlerID: strconv.FormatUint(id, 10),
Protocol: s.Protocol,
Protocol: string(s.Protocol),
OriginAddress: s.OriginAddr.String(),
TargetAddress: s.TargetAddr.String(),
......@@ -349,7 +351,7 @@ var p2pStreamLsCmd = &cmds.Command{
w := tabwriter.NewWriter(buf, 1, 2, 1, ' ', 0)
for _, stream := range list.Streams {
if headers {
fmt.Fprintln(w, "Id\tProtocol\tOrigin\tTarget")
fmt.Fprintln(w, "ID\tProtocol\tOrigin\tTarget")
}
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", stream.HandlerID, stream.Protocol, stream.OriginAddress, stream.TargetAddress)
......
......@@ -282,9 +282,12 @@ going to use `/kickass/1.0`.
1. A "server" node with peer ID `$SERVER_ID`
2. A "client" node.
**On the "server" node:**
**Netcat example:**
First, start your application and have it listen on `$APP_PORT`.
***On the "server" node:***
First, start your application and have it listen for TCP connections on
port `$APP_PORT`.
Then, configure the p2p listener by running:
......@@ -296,11 +299,11 @@ This will configure IPFS to forward all incoming `/p2p/kickass/1.0` streams to
`127.0.0.1:$APP_PORT` (opening a new connection to `127.0.0.1:$APP_PORT` per
incoming stream.
**On the "client" node:**
***On the "client" node:***
First, configure the p2p dialer to forward all inbound connections on
`127.0.0.1:SOME_PORT` to the listener behind `/p2p/kickass/1.0` on the server
node.
First, configure the client p2p dialer, so that it forwards all inbound
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
......@@ -310,12 +313,12 @@ Next, have your application open a connection to `127.0.0.1:$SOME_PORT`. This
connection will be forwarded to the service running on `127.0.0.1:$APP_PORT` on
the remote machine. You can test it with netcat:
**On "server" node:**
***On "server" node:***
```sh
> nc -v -l -p $APP_PORT
```
**On "client" node:**
***On "client" node:***
```sh
> nc -v 127.0.0.1 $SOME_PORT
```
......
......@@ -24,7 +24,7 @@ type listenerKey struct {
// ListenerRegistry is a collection of local application proto listeners.
type ListenerRegistry struct {
Listeners map[listenerKey]Listener
lk *sync.Mutex
lk sync.Mutex
}
func (r *ListenerRegistry) lock(l Listener) error {
......
......@@ -19,7 +19,7 @@ type localListener struct {
p2p *P2P
id peer.ID
proto string
proto protocol.ID
laddr ma.Multiaddr
peer peer.ID
......@@ -34,7 +34,7 @@ func (p2p *P2P) ForwardLocal(ctx context.Context, peer peer.ID, proto string, bi
p2p: p2p,
id: p2p.identity,
proto: proto,
proto: protocol.ID(proto),
laddr: bindAddr,
peer: peer,
}
......@@ -66,7 +66,7 @@ func (l *localListener) dial() (net.Stream, error) {
return nil, err
}
return l.p2p.peerHost.NewStream(l.ctx, l.peer, protocol.ID(l.proto))
return l.p2p.peerHost.NewStream(l.ctx, l.peer, l.proto)
}
func (l *localListener) acceptConns() {
......@@ -112,7 +112,7 @@ func (l *localListener) Close() error {
}
func (l *localListener) Protocol() string {
return l.proto
return string(l.proto)
}
func (l *localListener) ListenAddress() string {
......
......@@ -27,11 +27,11 @@ func NewP2P(identity peer.ID, peerHost p2phost.Host, peerstore pstore.Peerstore)
Listeners: &ListenerRegistry{
Listeners: map[listenerKey]Listener{},
lk: &sync.Mutex{},
lk: sync.Mutex{},
},
Streams: &StreamRegistry{
Streams: map[uint64]*Stream{},
lk: &sync.Mutex{},
lk: sync.Mutex{},
},
}
}
......
......@@ -14,7 +14,7 @@ type remoteListener struct {
p2p *P2P
// Application proto identifier.
proto string
proto protocol.ID
// Address to proxy the incoming connections to
addr ma.Multiaddr
......@@ -25,7 +25,7 @@ func (p2p *P2P) ForwardRemote(ctx context.Context, proto string, addr ma.Multiad
listener := &remoteListener{
p2p: p2p,
proto: proto,
proto: protocol.ID(proto),
addr: addr,
}
......@@ -33,7 +33,7 @@ func (p2p *P2P) ForwardRemote(ctx context.Context, proto string, addr ma.Multiad
return nil, err
}
p2p.peerHost.SetStreamHandler(protocol.ID(proto), func(remote net.Stream) {
p2p.peerHost.SetStreamHandler(listener.proto, func(remote net.Stream) {
local, err := manet.Dial(addr)
if err != nil {
remote.Reset()
......@@ -48,7 +48,7 @@ func (p2p *P2P) ForwardRemote(ctx context.Context, proto string, addr ma.Multiad
}
stream := &Stream{
Protocol: proto,
Protocol: listener.proto,
OriginAddr: peerMa,
TargetAddr: addr,
......@@ -69,7 +69,7 @@ func (p2p *P2P) ForwardRemote(ctx context.Context, proto string, addr ma.Multiad
}
func (l *remoteListener) Protocol() string {
return l.proto
return string(l.proto)
}
func (l *remoteListener) ListenAddress() string {
......
......@@ -6,6 +6,7 @@ import (
ma "gx/ipfs/QmWWQ2Txc2c6tqjsBpzg5Ar652cHPGNsQQp2SejkNmkUMb/go-multiaddr"
net "gx/ipfs/QmYj8wdn5sZEHX2XMDWGBvcXJNdzVbaVpHmXvhHBVZepen/go-libp2p-net"
"gx/ipfs/QmZNkThpqfVXs9GNbexPrfBbXSLNYeKrE7jwFM2oqHbyqN/go-libp2p-protocol"
manet "gx/ipfs/QmcGXGdw9BWDysPJQHxJinjGHha3eEg4vzFETre4woNwcX/go-multiaddr-net"
)
......@@ -13,7 +14,7 @@ import (
type Stream struct {
id uint64
Protocol string
Protocol protocol.ID
OriginAddr ma.Multiaddr
TargetAddr ma.Multiaddr
......@@ -59,7 +60,7 @@ func (s *Stream) startStreaming() {
// StreamRegistry is a collection of active incoming and outgoing proto app streams.
type StreamRegistry struct {
Streams map[uint64]*Stream
lk *sync.Mutex
lk sync.Mutex
nextID uint64
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论