2018-07-17 18:39:04 +00:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-05-29 19:43:23 +00:00
|
|
|
"io/ioutil"
|
2019-05-28 12:54:49 +00:00
|
|
|
"net"
|
2019-05-29 19:43:23 +00:00
|
|
|
"os"
|
2018-07-17 18:39:04 +00:00
|
|
|
|
2019-05-09 21:02:24 +00:00
|
|
|
"github.com/libp2p/go-libp2p"
|
2019-05-29 19:43:23 +00:00
|
|
|
crypto "github.com/libp2p/go-libp2p-crypto"
|
|
|
|
peer "github.com/libp2p/go-libp2p-peer"
|
2019-05-28 12:54:49 +00:00
|
|
|
filter "github.com/libp2p/go-maddr-filter"
|
2018-07-17 18:39:04 +00:00
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
2018-08-14 16:16:21 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/iputils"
|
2018-07-17 18:39:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// buildOptions for the libp2p host.
|
2018-09-20 04:14:31 +00:00
|
|
|
// TODO(287): Expand on these options and provide the option configuration via flags.
|
2019-05-28 12:54:49 +00:00
|
|
|
func buildOptions(cfg *ServerConfig) []libp2p.Option {
|
2019-05-29 19:43:23 +00:00
|
|
|
|
2018-08-14 16:16:21 +00:00
|
|
|
ip, err := iputils.ExternalIPv4()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Could not get IPv4 address: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-05-28 12:54:49 +00:00
|
|
|
listen, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", ip, cfg.Port))
|
2018-07-29 00:44:24 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to p2p listen: %v", err)
|
|
|
|
}
|
2018-07-17 18:39:04 +00:00
|
|
|
|
2019-06-26 19:59:37 +00:00
|
|
|
options := []libp2p.Option{
|
2018-07-17 18:39:04 +00:00
|
|
|
libp2p.ListenAddrs(listen),
|
2018-11-25 16:55:02 +00:00
|
|
|
libp2p.EnableRelay(), // Allows dialing to peers via relay.
|
2019-05-28 12:54:49 +00:00
|
|
|
optionConnectionManager(cfg.MaxPeers),
|
|
|
|
whitelistSubnet(cfg.WhitelistCIDR),
|
2019-05-29 19:43:23 +00:00
|
|
|
privKey(cfg.PrvKey),
|
2019-05-28 12:54:49 +00:00
|
|
|
}
|
2019-06-26 19:59:37 +00:00
|
|
|
|
|
|
|
if cfg.EnableUPnP {
|
|
|
|
options = append(options, libp2p.NATPortMap()) //Allow to use UPnP
|
|
|
|
}
|
|
|
|
|
|
|
|
return options
|
2019-05-28 12:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// whitelistSubnet adds a whitelist multiaddress filter for a given CIDR subnet.
|
|
|
|
// Example: 192.168.0.0/16 may be used to accept only connections on your local
|
|
|
|
// network.
|
|
|
|
func whitelistSubnet(cidr string) libp2p.Option {
|
|
|
|
if cidr == "" {
|
|
|
|
return func(_ *libp2p.Config) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(cfg *libp2p.Config) error {
|
|
|
|
_, ipnet, err := net.ParseCIDR(cidr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.Filters == nil {
|
|
|
|
cfg.Filters = filter.NewFilters()
|
|
|
|
}
|
|
|
|
cfg.Filters.AddFilter(*ipnet, filter.ActionAccept)
|
|
|
|
|
|
|
|
return nil
|
2018-07-17 18:39:04 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-29 19:43:23 +00:00
|
|
|
|
|
|
|
// Adds a private key to the libp2p option if the option was provided.
|
|
|
|
// If the private key file is missing or cannot be read, or if the
|
|
|
|
// private key contents cannot be marshaled, an exception is thrown.
|
|
|
|
func privKey(prvKey string) libp2p.Option {
|
|
|
|
if prvKey == "" {
|
|
|
|
return func(_ *libp2p.Config) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(cfg *libp2p.Config) error {
|
|
|
|
if _, err := os.Stat(prvKey); os.IsNotExist(err) {
|
|
|
|
log.WithField("private key file", prvKey).Warn("Could not read private key, file is missing or unreadable")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
bytes, err := ioutil.ReadFile(prvKey)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Error reading private key from file")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
keyBytes, err := crypto.ConfigDecodeKey(string(bytes))
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Error decoding private key")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
key, err := crypto.UnmarshalPrivateKey(keyBytes)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Error unmarshalling private key")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pubKey, err := peer.IDFromPrivateKey(key)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Could not print public key: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.WithField("public key", pubKey.Pretty()).Info("Private key loaded. Announcing public key.")
|
|
|
|
|
|
|
|
return cfg.Apply(libp2p.Identity(key))
|
|
|
|
}
|
|
|
|
}
|