clarify nat options

This commit is contained in:
Will Scott 2020-04-10 13:12:19 -07:00
parent 97be7d6b7b
commit 5473f0ea0e
No known key found for this signature in database
GPG Key ID: 67AD690C0FB4AAC7
2 changed files with 31 additions and 22 deletions

View File

@ -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 {

View File

@ -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
}
}