From 97be7d6b7b5c4ea65cb497d9bf03c98e0c2a5ef2 Mon Sep 17 00:00:00 2001 From: Will Scott Date: Fri, 10 Apr 2020 12:19:52 -0700 Subject: [PATCH 1/3] Expose option for setting autonat throttling --- config/config.go | 24 +++++++++++++++++------- options.go | 13 +++++++++++++ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/config/config.go b/config/config.go index f491f92df..9b818caec 100644 --- a/config/config.go +++ b/config/config.go @@ -4,6 +4,7 @@ import ( "context" "crypto/rand" "fmt" + "time" "github.com/libp2p/go-libp2p-core/connmgr" "github.com/libp2p/go-libp2p-core/crypto" @@ -83,10 +84,12 @@ type Config struct { Routing RoutingC - EnableAutoRelay bool - Reachability network.Reachability - AutoNATService bool - StaticRelays []peer.AddrInfo + EnableAutoRelay bool + Reachability network.Reachability + AutoNATService bool + AutoNATThrottling bool + AutoNATThrottles [2]int + StaticRelays []peer.AddrInfo } func (cfg *Config) makeSwarm(ctx context.Context) (*swarm.Swarm, error) { @@ -280,9 +283,16 @@ func (cfg *Config) NewNode(ctx context.Context) (host.Host, error) { } } - autonatOpts := []autonat.Option{autonat.UsingAddresses(func() []ma.Multiaddr { - return addrF(h.AllAddrs()) - })} + autonatOpts := []autonat.Option{ + autonat.UsingAddresses(func() []ma.Multiaddr { + return addrF(h.AllAddrs()) + }), + } + if cfg.AutoNATThrottling { + autonatOpts = append(autonatOpts, + autonat.WithThrottling(cfg.AutoNATThrottles[0], 1*time.Minute), + autonat.WithPeerThrottling(cfg.AutoNATThrottles[1])) + } if cfg.AutoNATService { autonatPrivKey, _, err := crypto.GenerateEd25519Key(rand.Reader) if err != nil { diff --git a/options.go b/options.go index 8efe9a834..23cf2b461 100644 --- a/options.go +++ b/options.go @@ -307,6 +307,19 @@ func EnableNATService() Option { } } +// OverrideNATServiceThrottling changes the default rate limiting configured in helping +// other peers determine their reachability status. When set, the host will limit +// the number of requests it responds to in each 60 second period to the set +// numbers. A value of '0' disables throttling. +func OverrideNATServiceThrottling(globalThrottle, perPeerThrottle int) Option { + return func(cfg *Config) error { + cfg.AutoNATThrottling = true + cfg.AutoNATThrottles[0] = globalThrottle + cfg.AutoNATThrottles[1] = perPeerThrottle + return nil + } +} + // FilterAddresses configures libp2p to never dial nor accept connections from // the given addresses. FilterAddresses should be used for cases where the // addresses you want to deny are known ahead of time. From 5473f0ea0ed915f71b9bc2bc84856e3ef8b9deee Mon Sep 17 00:00:00 2001 From: Will Scott Date: Fri, 10 Apr 2020 13:12:19 -0700 Subject: [PATCH 2/3] clarify nat options --- config/config.go | 33 ++++++++++++++++++++------------- options.go | 20 +++++++++++--------- 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/config/config.go b/config/config.go index 9b818caec..36372622b 100644 --- a/config/config.go +++ b/config/config.go @@ -46,7 +46,15 @@ type NATManagerC func(network.Network) bhost.NATManager type RoutingC func(host.Host) (routing.PeerRouting, error) -// AutoNATMode defines the AutoNAT behavior for the libp2p host. +// autoNATConfig defines the AutoNAT behavior for the libp2p host. +type AutoNATConfig struct { + ForceReachabilityPublic bool + ForceReachabilityPrivate bool + EnableService bool + ThrottleGlobalLimit int + ThrottlePeerLimit int + ThrottleInterval time.Duration +} // Config describes a set of settings for a libp2p node // @@ -84,12 +92,9 @@ type Config struct { Routing RoutingC - EnableAutoRelay bool - Reachability network.Reachability - AutoNATService bool - AutoNATThrottling bool - AutoNATThrottles [2]int - StaticRelays []peer.AddrInfo + EnableAutoRelay bool + AutoNATConfig + StaticRelays []peer.AddrInfo } func (cfg *Config) makeSwarm(ctx context.Context) (*swarm.Swarm, error) { @@ -288,12 +293,12 @@ func (cfg *Config) NewNode(ctx context.Context) (host.Host, error) { return addrF(h.AllAddrs()) }), } - if cfg.AutoNATThrottling { + if cfg.AutoNATConfig.ThrottleInterval != 0 { autonatOpts = append(autonatOpts, - autonat.WithThrottling(cfg.AutoNATThrottles[0], 1*time.Minute), - autonat.WithPeerThrottling(cfg.AutoNATThrottles[1])) + autonat.WithThrottling(cfg.AutoNATConfig.ThrottleGlobalLimit, cfg.AutoNATConfig.ThrottleInterval), + autonat.WithPeerThrottling(cfg.AutoNATConfig.ThrottlePeerLimit)) } - if cfg.AutoNATService { + if cfg.AutoNATConfig.EnableService { autonatPrivKey, _, err := crypto.GenerateEd25519Key(rand.Reader) if err != nil { return nil, err @@ -332,8 +337,10 @@ func (cfg *Config) NewNode(ctx context.Context) (host.Host, error) { // closed (as long as we close the underlying network). autonatOpts = append(autonatOpts, autonat.EnableService(dialerHost.Network())) } - if cfg.Reachability != network.ReachabilityUnknown { - autonatOpts = append(autonatOpts, autonat.WithReachability(cfg.Reachability)) + if cfg.AutoNATConfig.ForceReachabilityPublic { + autonatOpts = append(autonatOpts, autonat.WithReachability(network.ReachabilityPublic)) + } else if cfg.AutoNATConfig.ForceReachabilityPrivate { + autonatOpts = append(autonatOpts, autonat.WithReachability(network.ReachabilityPrivate)) } if _, err = autonat.New(ctx, h, autonatOpts...); err != nil { diff --git a/options.go b/options.go index 23cf2b461..70b7901e0 100644 --- a/options.go +++ b/options.go @@ -6,11 +6,11 @@ package libp2p import ( "fmt" "net" + "time" "github.com/libp2p/go-libp2p-core/connmgr" "github.com/libp2p/go-libp2p-core/crypto" "github.com/libp2p/go-libp2p-core/metrics" - "github.com/libp2p/go-libp2p-core/network" "github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/peerstore" "github.com/libp2p/go-libp2p-core/pnet" @@ -283,7 +283,8 @@ func DefaultStaticRelays() Option { // forcing the local node to believe it is reachable externally. func ForceReachabilityPublic() Option { return func(cfg *Config) error { - cfg.Reachability = network.ReachabilityPublic + cfg.AutoNATConfig.ForceReachabilityPublic = true + cfg.AutoNATConfig.ForceReachabilityPrivate = false return nil } } @@ -292,7 +293,8 @@ func ForceReachabilityPublic() Option { // forceing the local node to believe it is behind a NAT and not reachable externally. func ForceReachabilityPrivate() Option { return func(cfg *Config) error { - cfg.Reachability = network.ReachabilityPrivate + cfg.AutoNATConfig.ForceReachabilityPrivate = true + cfg.AutoNATConfig.ForceReachabilityPublic = false return nil } } @@ -302,20 +304,20 @@ func ForceReachabilityPrivate() Option { // to peers, and then tell them if it was successful in making such connections. func EnableNATService() Option { return func(cfg *Config) error { - cfg.AutoNATService = true + cfg.AutoNATConfig.EnableService = true return nil } } -// OverrideNATServiceThrottling changes the default rate limiting configured in helping +// AutoNATServiceRateLimit changes the default rate limiting configured in helping // other peers determine their reachability status. When set, the host will limit // the number of requests it responds to in each 60 second period to the set // numbers. A value of '0' disables throttling. -func OverrideNATServiceThrottling(globalThrottle, perPeerThrottle int) Option { +func AutoNATServiceRateLimit(global, perPeer int, interval time.Duration) Option { return func(cfg *Config) error { - cfg.AutoNATThrottling = true - cfg.AutoNATThrottles[0] = globalThrottle - cfg.AutoNATThrottles[1] = perPeerThrottle + cfg.AutoNATConfig.ThrottleGlobalLimit = global + cfg.AutoNATConfig.ThrottlePeerLimit = perPeer + cfg.AutoNATConfig.ThrottleInterval = interval return nil } } From c08993b4e8521d087d05183f7e557c53bf876632 Mon Sep 17 00:00:00 2001 From: Will Scott Date: Mon, 13 Apr 2020 10:23:22 -0700 Subject: [PATCH 3/3] switch bool to enum --- config/config.go | 17 +++++++---------- options.go | 9 +++++---- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/config/config.go b/config/config.go index 36372622b..f67c117c6 100644 --- a/config/config.go +++ b/config/config.go @@ -48,12 +48,11 @@ type RoutingC func(host.Host) (routing.PeerRouting, error) // autoNATConfig defines the AutoNAT behavior for the libp2p host. type AutoNATConfig struct { - ForceReachabilityPublic bool - ForceReachabilityPrivate bool - EnableService bool - ThrottleGlobalLimit int - ThrottlePeerLimit int - ThrottleInterval time.Duration + ForceReachability *network.Reachability + EnableService bool + ThrottleGlobalLimit int + ThrottlePeerLimit int + ThrottleInterval time.Duration } // Config describes a set of settings for a libp2p node @@ -337,10 +336,8 @@ func (cfg *Config) NewNode(ctx context.Context) (host.Host, error) { // closed (as long as we close the underlying network). autonatOpts = append(autonatOpts, autonat.EnableService(dialerHost.Network())) } - if cfg.AutoNATConfig.ForceReachabilityPublic { - autonatOpts = append(autonatOpts, autonat.WithReachability(network.ReachabilityPublic)) - } else if cfg.AutoNATConfig.ForceReachabilityPrivate { - autonatOpts = append(autonatOpts, autonat.WithReachability(network.ReachabilityPrivate)) + if cfg.AutoNATConfig.ForceReachability != nil { + autonatOpts = append(autonatOpts, autonat.WithReachability(*cfg.AutoNATConfig.ForceReachability)) } if _, err = autonat.New(ctx, h, autonatOpts...); err != nil { diff --git a/options.go b/options.go index 70b7901e0..7dc5cdff9 100644 --- a/options.go +++ b/options.go @@ -11,6 +11,7 @@ import ( "github.com/libp2p/go-libp2p-core/connmgr" "github.com/libp2p/go-libp2p-core/crypto" "github.com/libp2p/go-libp2p-core/metrics" + "github.com/libp2p/go-libp2p-core/network" "github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/peerstore" "github.com/libp2p/go-libp2p-core/pnet" @@ -283,8 +284,8 @@ func DefaultStaticRelays() Option { // forcing the local node to believe it is reachable externally. func ForceReachabilityPublic() Option { return func(cfg *Config) error { - cfg.AutoNATConfig.ForceReachabilityPublic = true - cfg.AutoNATConfig.ForceReachabilityPrivate = false + public := network.Reachability(network.ReachabilityPublic) + cfg.AutoNATConfig.ForceReachability = &public return nil } } @@ -293,8 +294,8 @@ func ForceReachabilityPublic() Option { // forceing the local node to believe it is behind a NAT and not reachable externally. func ForceReachabilityPrivate() Option { return func(cfg *Config) error { - cfg.AutoNATConfig.ForceReachabilityPrivate = true - cfg.AutoNATConfig.ForceReachabilityPublic = false + private := network.Reachability(network.ReachabilityPrivate) + cfg.AutoNATConfig.ForceReachability = &private return nil } }