Commit 3a709b0c authored by Janos Guljas's avatar Janos Guljas

add p2p listen options

parent b67e4c9a
......@@ -18,11 +18,11 @@ Docker image `janos/bee`.
Execute the commands in two terminals to start `node 1` and `node 2`:
```sh
bee start --listen :8501
bee start --api-addr :8501 --p2p-addr :30401
```
```sh
bee start --listen :8502
bee start --api-addr :8502 --p2p-addr :30402
```
Copy one of the multiaddresses from one running instance.
......
......@@ -16,7 +16,10 @@ import (
func (c *command) initStartCmd() (err error) {
const (
optionNameListen = "listen"
optionNameAPIAddr = "api-addr"
optionNameP2PAddr = "p2p-addr"
optionNameP2PDisableWS = "p2p-disable-ws"
optionNameP2PDisableQUIC = "p2p-disable-quic"
)
cmd := &cobra.Command{
......@@ -30,6 +33,9 @@ func (c *command) initStartCmd() (err error) {
// Construct P2P service.
p2ps, err := libp2p.New(ctx, libp2p.Options{
Addr: c.config.GetString(optionNameP2PAddr),
DisableWS: c.config.GetBool(optionNameP2PDisableWS),
DisableQUIC: c.config.GetBool(optionNameP2PDisableQUIC),
// Routing: func(h host.Host) (r routing.PeerRouting, err error) {
// idht, err = dht.New(ctx, h)
// return idht, err
......@@ -61,7 +67,7 @@ func (c *command) initStartCmd() (err error) {
Pingpong: pingPong,
})
l, err := net.Listen("tcp", c.config.GetString(optionNameListen))
l, err := net.Listen("tcp", c.config.GetString(optionNameAPIAddr))
if err != nil {
return fmt.Errorf("listen TCP: %w", err)
}
......@@ -72,7 +78,10 @@ func (c *command) initStartCmd() (err error) {
},
}
cmd.Flags().String(optionNameListen, ":8500", "HTTP API listen address")
cmd.Flags().String(optionNameAPIAddr, ":8500", "HTTP API listen address")
cmd.Flags().String(optionNameP2PAddr, ":30399", "P2P listen address")
cmd.Flags().Bool(optionNameP2PDisableWS, false, "Disable P2P WebSocket protocol")
cmd.Flags().Bool(optionNameP2PDisableQUIC, false, "Disable P2P QUIC protocol")
if err := c.config.BindPFlags(cmd.Flags()); err != nil {
return err
......
......@@ -3,10 +3,9 @@ package libp2p
import (
"context"
"fmt"
"net"
"time"
"github.com/multiformats/go-multistream"
"github.com/janos/bee/pkg/p2p"
"github.com/libp2p/go-libp2p"
......@@ -18,11 +17,11 @@ import (
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/peerstore"
protocol "github.com/libp2p/go-libp2p-core/protocol"
"github.com/libp2p/go-libp2p-core/routing"
libp2pquic "github.com/libp2p/go-libp2p-quic-transport"
secio "github.com/libp2p/go-libp2p-secio"
libp2ptls "github.com/libp2p/go-libp2p-tls"
ma "github.com/multiformats/go-multiaddr"
"github.com/multiformats/go-multistream"
)
var _ p2p.Service = new(Service)
......@@ -32,25 +31,60 @@ type Service struct {
}
type Options struct {
Port int
ListenIPv4 string
PrivKey []byte
Routing func(host.Host) (routing.PeerRouting, error)
Addr string
DisableWS bool
DisableQUIC bool
// PrivKey []byte
// Routing func(host.Host) (routing.PeerRouting, error)
}
func New(ctx context.Context, o Options) (*Service, error) {
ipV4Addr := o.ListenIPv4
if ipV4Addr == "" {
ipV4Addr = "0.0.0.0"
host, port, err := net.SplitHostPort(o.Addr)
if err != nil {
return nil, fmt.Errorf("address: %w", err)
}
ip4Addr := "0.0.0.0"
ip6Addr := "::1"
if host != "" {
ip := net.ParseIP(host)
if ip4 := ip.To4(); ip4 != nil {
ip4Addr = ip4.String()
ip6Addr = ""
} else if ip6 := ip.To16(); ip6 != nil {
ip6Addr = ip6.String()
ip4Addr = ""
}
}
var listenAddrs []string
if ip4Addr != "" {
listenAddrs = append(listenAddrs, fmt.Sprintf("/ip4/%s/tcp/%s", ip4Addr, port))
if !o.DisableWS {
listenAddrs = append(listenAddrs, fmt.Sprintf("/ip4/%s/tcp/%s/ws", ip4Addr, port))
}
if !o.DisableQUIC {
listenAddrs = append(listenAddrs, fmt.Sprintf("/ip4/%s/udp/%s/quic", ip4Addr, port))
}
}
if ip6Addr != "" {
listenAddrs = append(listenAddrs, fmt.Sprintf("/ip6/%s/tcp/%s", ip6Addr, port))
if !o.DisableWS {
listenAddrs = append(listenAddrs, fmt.Sprintf("/ip6/%s/tcp/%s/ws", ip6Addr, port))
}
if !o.DisableQUIC {
listenAddrs = append(listenAddrs, fmt.Sprintf("/ip6/%s/udp/%s/quic", ip6Addr, port))
}
}
opts := []libp2p.Option{
// Use the keypair we generated
//libp2p.Identity(priv),
// Multiple listen addresses
libp2p.ListenAddrStrings(
fmt.Sprintf("/ip4/%s/tcp/%v", ipV4Addr, o.Port), // regular tcp connections
fmt.Sprintf("/ip4/%s/udp/%v/quic", ipV4Addr, o.Port), // a UDP endpoint for the QUIC transport
),
libp2p.ListenAddrStrings(listenAddrs...),
// support TLS connections
libp2p.Security(libp2ptls.ID, libp2ptls.New),
// support secio connections
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment