Merge pull request #378 from upperwal/default_listener

Added a Default listener
This commit is contained in:
Steven Allen 2018-07-27 22:21:46 +00:00 committed by GitHub
commit c984cc61a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 1 deletions

View File

@ -10,6 +10,7 @@ import (
secio "github.com/libp2p/go-libp2p-secio"
tcp "github.com/libp2p/go-tcp-transport"
ws "github.com/libp2p/go-ws-transport"
multiaddr "github.com/multiformats/go-multiaddr"
mplex "github.com/whyrusleeping/go-smux-multiplex"
yamux "github.com/whyrusleeping/go-smux-yamux"
)
@ -20,7 +21,7 @@ import (
// security protocols.
var DefaultSecurity = Security(secio.ID, secio.New)
// DefaultMuxer configures libp2p to use the stream connection multiplexers.
// DefaultMuxers configures libp2p to use the stream connection multiplexers.
//
// Use this option when you want to *extend* the set of multiplexers used by
// libp2p instead of replacing them.
@ -52,6 +53,23 @@ var RandomIdentity = func(cfg *Config) error {
return cfg.Apply(Identity(priv))
}
// DefaultListenAddrs configures libp2p to use default listen address
var DefaultListenAddrs = func(cfg *Config) error {
defaultIP4ListenAddr, err := multiaddr.NewMultiaddr("/ip4/0.0.0.0/tcp/0")
if err != nil {
return err
}
defaultIP6ListenAddr, err := multiaddr.NewMultiaddr("/ip6/::/tcp/0")
if err != nil {
return err
}
return cfg.Apply(ListenAddrs(
defaultIP4ListenAddr,
defaultIP6ListenAddr,
))
}
// Complete list of default options and when to fallback on them.
//
// Please *DON'T* specify default options any other way. Putting this all here
@ -60,6 +78,10 @@ var defaults = []struct {
fallback func(cfg *Config) bool
opt Option
}{
{
fallback: func(cfg *Config) bool { return cfg.Transports == nil && cfg.ListenAddrs == nil },
opt: DefaultListenAddrs,
},
{
fallback: func(cfg *Config) bool { return cfg.Transports == nil },
opt: DefaultTransports,

View File

@ -3,11 +3,13 @@ package libp2p
import (
"context"
"fmt"
"regexp"
"strings"
"testing"
crypto "github.com/libp2p/go-libp2p-crypto"
host "github.com/libp2p/go-libp2p-host"
"github.com/libp2p/go-tcp-transport"
)
func TestNewHost(t *testing.T) {
@ -39,6 +41,39 @@ func TestInsecure(t *testing.T) {
h.Close()
}
func TestDefaultListenAddrs(t *testing.T) {
ctx := context.Background()
re := regexp.MustCompile("/(ip)[4|6]/((0.0.0.0)|(::))/tcp/")
// Test 1: Setting the correct listen addresses if userDefined.Transport == nil && userDefined.ListenAddrs == nil
h, err := New(ctx)
if err != nil {
t.Fatal(err)
}
for _, addr := range h.Network().ListenAddresses() {
if re.FindStringSubmatchIndex(addr.String()) == nil {
t.Error("expected ip4 or ip6 interface")
}
}
h.Close()
// Test 2: Listen addr should not set if user defined transport is passed.
h, err = New(
ctx,
Transport(tcp.NewTCPTransport),
)
if err != nil {
t.Fatal(err)
}
if len(h.Network().ListenAddresses()) != 0 {
t.Error("expected zero listen addrs as none is set with user defined transport")
}
h.Close()
}
func makeRandomHost(t *testing.T, port int) (host.Host, error) {
ctx := context.Background()
priv, _, err := crypto.GenerateKeyPair(crypto.RSA, 2048)