package p2p // This file contains all the default configuration options. import ( "crypto/rand" multiaddr "gitlab.dms3.io/mf/go-multiaddr" crypto "gitlab.dms3.io/p2p/go-p2p-core/crypto" mplex "gitlab.dms3.io/p2p/go-p2p-mplex" noise "gitlab.dms3.io/p2p/go-p2p-noise" pstoremem "gitlab.dms3.io/p2p/go-p2p-peerstore/pstoremem" tls "gitlab.dms3.io/p2p/go-p2p-tls" yamux "gitlab.dms3.io/p2p/go-p2p-yamux" tcp "gitlab.dms3.io/p2p/go-tcp-transport" ws "gitlab.dms3.io/p2p/go-ws-transport" ) // DefaultSecurity is the default security option. // // Useful when you want to extend, but not replace, the supported transport // security protocols. var DefaultSecurity = ChainOptions( Security(noise.ID, noise.New), Security(tls.ID, tls.New), ) // DefaultMuxers configures p2p to use the stream connection multiplexers. // // Use this option when you want to *extend* the set of multiplexers used by // p2p instead of replacing them. var DefaultMuxers = ChainOptions( Muxer("/yamux/1.0.0", yamux.DefaultTransport), Muxer("/mplex/6.7.0", mplex.DefaultTransport), ) // DefaultTransports are the default p2p transports. // // Use this option when you want to *extend* the set of transports used by // p2p instead of replacing them. var DefaultTransports = ChainOptions( Transport(tcp.NewTCPTransport), Transport(ws.New), ) // DefaultPeerstore configures p2p to use the default peerstore. var DefaultPeerstore Option = func(cfg *Config) error { return cfg.Apply(Peerstore(pstoremem.NewPeerstore())) } // RandomIdentity generates a random identity. (default behaviour) var RandomIdentity = func(cfg *Config) error { priv, _, err := crypto.GenerateKeyPairWithReader(crypto.RSA, 2048, rand.Reader) if err != nil { return err } return cfg.Apply(Identity(priv)) } // DefaultListenAddrs configures p2p 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, )) } // 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. // // Please *DON'T* specify default options any other way. Putting this all here // makes tracking defaults *much* easier. 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, }, { fallback: func(cfg *Config) bool { return cfg.Muxers == nil }, opt: DefaultMuxers, }, { fallback: func(cfg *Config) bool { return !cfg.Insecure && cfg.SecurityTransports == nil }, opt: DefaultSecurity, }, { fallback: func(cfg *Config) bool { return cfg.PeerKey == nil }, opt: RandomIdentity, }, { fallback: func(cfg *Config) bool { return cfg.Peerstore == nil }, opt: DefaultPeerstore, }, { fallback: func(cfg *Config) bool { return !cfg.RelayCustom }, opt: DefaultEnableRelay, }, } // Defaults configures p2p to use the default options. Can be combined with // other options to *extend* the default options. var Defaults Option = func(cfg *Config) error { for _, def := range defaults { if err := cfg.Apply(def.opt); err != nil { return err } } return nil } // FallbackDefaults applies default options to the p2p node if and only if no // other relevant options have been applied. will be appended to the options // passed into New. var FallbackDefaults Option = func(cfg *Config) error { for _, def := range defaults { if !def.fallback(cfg) { continue } if err := cfg.Apply(def.opt); err != nil { return err } } return nil }