Commit 0600392e authored by vyzo's avatar vyzo
Browse files

enable relay by default in New

parent 2787133b
...@@ -44,8 +44,9 @@ type Config struct { ...@@ -44,8 +44,9 @@ type Config struct {
Insecure bool Insecure bool
Protector pnet.Protector Protector pnet.Protector
Relay bool RelayCustom bool
RelayOpts []circuit.RelayOpt Relay bool
RelayOpts []circuit.RelayOpt
ListenAddrs []ma.Multiaddr ListenAddrs []ma.Multiaddr
AddrsFactory bhost.AddrsFactory AddrsFactory bhost.AddrsFactory
......
...@@ -70,6 +70,11 @@ var DefaultListenAddrs = func(cfg *Config) error { ...@@ -70,6 +70,11 @@ var DefaultListenAddrs = func(cfg *Config) error {
)) ))
} }
// DefaultEnableRelay enables relay dialing and listening by default
var DefaultEnableRelay = func(cfg *Config) error {
return cfg.Apply(EnableRelay())
}
// Complete list of default options and when to fallback on them. // Complete list of default options and when to fallback on them.
// //
// Please *DON'T* specify default options any other way. Putting this all here // Please *DON'T* specify default options any other way. Putting this all here
...@@ -102,6 +107,10 @@ var defaults = []struct { ...@@ -102,6 +107,10 @@ var defaults = []struct {
fallback: func(cfg *Config) bool { return cfg.Peerstore == nil }, fallback: func(cfg *Config) bool { return cfg.Peerstore == nil },
opt: DefaultPeerstore, opt: DefaultPeerstore,
}, },
{
fallback: func(cfg *Config) bool { return !cfg.RelayCustom },
opt: DefaultEnableRelay,
},
} }
// Defaults configures libp2p to use the default options. Can be combined with // Defaults configures libp2p to use the default options. Can be combined with
......
...@@ -81,6 +81,7 @@ func TestDefaultListenAddrs(t *testing.T) { ...@@ -81,6 +81,7 @@ func TestDefaultListenAddrs(t *testing.T) {
ctx := context.Background() ctx := context.Background()
re := regexp.MustCompile("/(ip)[4|6]/((0.0.0.0)|(::))/tcp/") re := regexp.MustCompile("/(ip)[4|6]/((0.0.0.0)|(::))/tcp/")
re2 := regexp.MustCompile("/p2p-circuit")
// Test 1: Setting the correct listen addresses if userDefined.Transport == nil && userDefined.ListenAddrs == nil // Test 1: Setting the correct listen addresses if userDefined.Transport == nil && userDefined.ListenAddrs == nil
h, err := New(ctx) h, err := New(ctx)
...@@ -88,14 +89,15 @@ func TestDefaultListenAddrs(t *testing.T) { ...@@ -88,14 +89,15 @@ func TestDefaultListenAddrs(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
for _, addr := range h.Network().ListenAddresses() { for _, addr := range h.Network().ListenAddresses() {
if re.FindStringSubmatchIndex(addr.String()) == nil { if re.FindStringSubmatchIndex(addr.String()) == nil &&
t.Error("expected ip4 or ip6 interface") re2.FindStringSubmatchIndex(addr.String()) == nil {
t.Error("expected ip4 or ip6 or relay interface")
} }
} }
h.Close() h.Close()
// Test 2: Listen addr should not set if user defined transport is passed. // Test 2: Listen addr only include relay if user defined transport is passed.
h, err = New( h, err = New(
ctx, ctx,
Transport(tcp.NewTCPTransport), Transport(tcp.NewTCPTransport),
...@@ -104,8 +106,11 @@ func TestDefaultListenAddrs(t *testing.T) { ...@@ -104,8 +106,11 @@ func TestDefaultListenAddrs(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
if len(h.Network().ListenAddresses()) != 0 { if len(h.Network().ListenAddresses()) != 1 {
t.Error("expected zero listen addrs as none is set with user defined transport") t.Error("expected one listen addr with user defined transport")
}
if re2.FindStringSubmatchIndex(h.Network().ListenAddresses()[0].String()) == nil {
t.Error("expected relay address")
} }
h.Close() h.Close()
} }
......
...@@ -201,15 +201,25 @@ func AddrsFactory(factory config.AddrsFactory) Option { ...@@ -201,15 +201,25 @@ func AddrsFactory(factory config.AddrsFactory) Option {
} }
} }
// EnableRelay configures libp2p to enable the relay transport. // EnableRelay configures libp2p to enable the relay transport with configuration options.
func EnableRelay(options ...circuit.RelayOpt) Option { func EnableRelay(options ...circuit.RelayOpt) Option {
return func(cfg *Config) error { return func(cfg *Config) error {
cfg.RelayCustom = true
cfg.Relay = true cfg.Relay = true
cfg.RelayOpts = options cfg.RelayOpts = options
return nil return nil
} }
} }
// DisableRelay configures libp2p to disable the relay transport
func DisableRelay() Option {
return func(cfg *Config) error {
cfg.RelayCustom = true
cfg.Relay = false
return nil
}
}
// FilterAddresses configures libp2p to never dial nor accept connections from // FilterAddresses configures libp2p to never dial nor accept connections from
// the given addresses. // the given addresses.
func FilterAddresses(addrs ...*net.IPNet) Option { func FilterAddresses(addrs ...*net.IPNet) Option {
...@@ -245,9 +255,15 @@ func NATManager(nm config.NATManagerC) Option { ...@@ -245,9 +255,15 @@ func NATManager(nm config.NATManagerC) Option {
// NoListenAddrs will configure libp2p to not listen by default. // NoListenAddrs will configure libp2p to not listen by default.
// //
// This will both clear any configured listen addrs and prevent libp2p from // This will both clear any configured listen addrs and prevent libp2p from
// applying the default listen address option. // applying the default listen address option. It also disables relay, unless the
// user explicitly specifies with an option, as the transport creates an implicit
// listen address that would make the node diable through any relay it was connected to.
var NoListenAddrs = func(cfg *Config) error { var NoListenAddrs = func(cfg *Config) error {
cfg.ListenAddrs = []ma.Multiaddr{} cfg.ListenAddrs = []ma.Multiaddr{}
if !cfg.RelayCustom {
cfg.RelayCustom = true
cfg.Relay = false
}
return nil return nil
} }
......
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