|
|
|
@ -16,6 +16,7 @@ import ( |
|
|
|
|
"github.com/libp2p/go-libp2p-core/routing" |
|
|
|
|
dht "github.com/libp2p/go-libp2p-kad-dht" |
|
|
|
|
libp2p_pubsub "github.com/libp2p/go-libp2p-pubsub" |
|
|
|
|
libp2p_config "github.com/libp2p/go-libp2p/config" |
|
|
|
|
libp2p_crypto "github.com/libp2p/go-libp2p/core/crypto" |
|
|
|
|
libp2p_host "github.com/libp2p/go-libp2p/core/host" |
|
|
|
|
libp2p_network "github.com/libp2p/go-libp2p/core/network" |
|
|
|
@ -96,6 +97,7 @@ type HostConfig struct { |
|
|
|
|
DisablePrivateIPScan bool |
|
|
|
|
MaxPeers int64 |
|
|
|
|
WaitForEachPeerToConnect bool |
|
|
|
|
ForceReachabilityPublic bool |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func init() { |
|
|
|
@ -112,6 +114,19 @@ func init() { |
|
|
|
|
libp2p_pubsub.GossipSubMaxIHaveLength = 1000 |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func forceReachabilityPublic(f bool) libp2p_config.Option { |
|
|
|
|
if f { |
|
|
|
|
return func(cfg *libp2p_config.Config) error { |
|
|
|
|
public := libp2p_network.Reachability(libp2p_network.ReachabilityPublic) |
|
|
|
|
cfg.AutoNATConfig.ForceReachability = &public |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return func(p2pConfig *libp2p_config.Config) error { |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// NewHost ..
|
|
|
|
|
func NewHost(cfg HostConfig) (Host, error) { |
|
|
|
|
var ( |
|
|
|
@ -128,8 +143,8 @@ func NewHost(cfg HostConfig) (Host, error) { |
|
|
|
|
ctx, cancel := context.WithCancel(context.Background()) |
|
|
|
|
// TODO: move low and high to configs
|
|
|
|
|
connmgr, err := connmgr.NewConnManager( |
|
|
|
|
int(10), // LowWater
|
|
|
|
|
int(10000)*cfg.MaxConnPerIP, // HighWater,
|
|
|
|
|
int(cfg.MaxConnPerIP), // LowWater
|
|
|
|
|
int(1024)*cfg.MaxConnPerIP, // HighWater,
|
|
|
|
|
connmgr.WithGracePeriod(time.Minute), |
|
|
|
|
) |
|
|
|
|
if err != nil { |
|
|
|
@ -138,16 +153,16 @@ func NewHost(cfg HostConfig) (Host, error) { |
|
|
|
|
} |
|
|
|
|
var idht *dht.IpfsDHT |
|
|
|
|
var opt discovery.DHTConfig |
|
|
|
|
p2pHost, err := libp2p.New( |
|
|
|
|
p2pHostConfig := []libp2p.Option{ |
|
|
|
|
listenAddr, |
|
|
|
|
libp2p.Identity(key), |
|
|
|
|
// support TLS connections
|
|
|
|
|
// Support TLS connections
|
|
|
|
|
libp2p.Security(libp2ptls.ID, libp2ptls.New), |
|
|
|
|
// support noise connections
|
|
|
|
|
// Support noise connections
|
|
|
|
|
libp2p.Security(noise.ID, noise.New), |
|
|
|
|
// support any other default transports (TCP)
|
|
|
|
|
// Support any other default transports (TCP)
|
|
|
|
|
libp2p.DefaultTransports, |
|
|
|
|
// Let's prevent our peer from having too many
|
|
|
|
|
// Prevent the peer from having too many
|
|
|
|
|
// connections by attaching a connection manager.
|
|
|
|
|
libp2p.ConnectionManager(connmgr), |
|
|
|
|
// Attempt to open ports using uPNP for NATed hosts.
|
|
|
|
@ -165,8 +180,7 @@ func NewHost(cfg HostConfig) (Host, error) { |
|
|
|
|
idht, err = dht.New(ctx, h, opts...) |
|
|
|
|
return idht, err |
|
|
|
|
}), |
|
|
|
|
|
|
|
|
|
// to help other peers to figure out if they are behind
|
|
|
|
|
// To help other peers to figure out if they are behind
|
|
|
|
|
// NATs, launch the server-side of AutoNAT too (AutoRelay
|
|
|
|
|
// already runs the client)
|
|
|
|
|
// This service is highly rate-limited and should not cause any
|
|
|
|
@ -174,14 +188,23 @@ func NewHost(cfg HostConfig) (Host, error) { |
|
|
|
|
libp2p.EnableNATService(), |
|
|
|
|
// Bandwidth Reporter
|
|
|
|
|
libp2p.BandwidthReporter(newCounter()), |
|
|
|
|
// ForceReachabilityPublic overrides automatic reachability detection in the AutoNAT subsystem,
|
|
|
|
|
// forcing the local node to believe it is reachable externally.
|
|
|
|
|
// libp2p.ForceReachabilityPublic(),
|
|
|
|
|
// libp2p.DisableRelay(),
|
|
|
|
|
// Enable relay service, to disable relay we can use libp2p.DisableRelay()
|
|
|
|
|
libp2p.EnableRelayService(), |
|
|
|
|
// prevent dialing of public addresses
|
|
|
|
|
// libp2p.ConnectionGater(NewGater(cfg.DisablePrivateIPScan)),
|
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if cfg.ForceReachabilityPublic { |
|
|
|
|
// ForceReachabilityPublic overrides automatic reachability detection in the AutoNAT subsystem,
|
|
|
|
|
// forcing the local node to believe it is reachable externally
|
|
|
|
|
p2pHostConfig = append(p2pHostConfig, libp2p.ForceReachabilityPublic()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if cfg.DisablePrivateIPScan { |
|
|
|
|
// Prevent dialing of public addresses
|
|
|
|
|
p2pHostConfig = append(p2pHostConfig, libp2p.ConnectionGater(NewGater(cfg.DisablePrivateIPScan))) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// create p2p host
|
|
|
|
|
p2pHost, err := libp2p.New(p2pHostConfig...) |
|
|
|
|
if err != nil { |
|
|
|
|
cancel() |
|
|
|
|
return nil, errors.Wrapf(err, "cannot initialize libp2p host") |
|
|
|
@ -190,6 +213,7 @@ func NewHost(cfg HostConfig) (Host, error) { |
|
|
|
|
disc, err := discovery.NewDHTDiscovery(ctx, cancel, p2pHost, idht, opt) |
|
|
|
|
if err != nil { |
|
|
|
|
cancel() |
|
|
|
|
p2pHost.Close() |
|
|
|
|
return nil, errors.Wrap(err, "cannot create DHT discovery") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -230,6 +254,7 @@ func NewHost(cfg HostConfig) (Host, error) { |
|
|
|
|
pubsub, err := libp2p_pubsub.NewGossipSub(ctx, p2pHost, options...) |
|
|
|
|
if err != nil { |
|
|
|
|
cancel() |
|
|
|
|
p2pHost.Close() |
|
|
|
|
return nil, errors.Wrapf(err, "cannot initialize libp2p pub-sub") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|