go-waku/waku/node.go

456 lines
13 KiB
Go
Raw Normal View History

package waku
import (
"context"
"crypto/ecdsa"
2021-04-19 00:03:16 +00:00
"database/sql"
"encoding/hex"
"errors"
"fmt"
"io/ioutil"
"net"
"os"
"os/signal"
"syscall"
"time"
2021-03-23 14:46:16 +00:00
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/p2p/enode"
dssql "github.com/ipfs/go-ds-sql"
"github.com/urfave/cli/v2"
"go.uber.org/zap"
"github.com/libp2p/go-libp2p"
libp2pcrypto "github.com/libp2p/go-libp2p-core/crypto"
2021-11-16 14:22:01 +00:00
"github.com/libp2p/go-libp2p-core/discovery"
2021-10-15 02:15:02 +00:00
"github.com/libp2p/go-libp2p/config"
"github.com/libp2p/go-libp2p-peerstore/pstoreds"
pubsub "github.com/libp2p/go-libp2p-pubsub"
2021-04-22 13:07:22 +00:00
"github.com/multiformats/go-multiaddr"
2021-10-05 02:13:54 +00:00
rendezvous "github.com/status-im/go-waku-rendezvous"
"github.com/status-im/go-waku/tests"
"github.com/status-im/go-waku/waku/metrics"
"github.com/status-im/go-waku/waku/persistence"
"github.com/status-im/go-waku/waku/persistence/sqlite"
2021-11-16 14:22:01 +00:00
"github.com/status-im/go-waku/waku/v2/dnsdisc"
"github.com/status-im/go-waku/waku/v2/node"
"github.com/status-im/go-waku/waku/v2/protocol/filter"
"github.com/status-im/go-waku/waku/v2/protocol/lightpush"
"github.com/status-im/go-waku/waku/v2/protocol/relay"
"github.com/status-im/go-waku/waku/v2/protocol/store"
"github.com/status-im/go-waku/waku/v2/rpc"
"github.com/status-im/go-waku/waku/v2/utils"
)
func failOnErr(err error, msg string) {
if err != nil {
if msg != "" {
msg = msg + ": "
}
utils.Logger().Fatal(msg, zap.Error(err))
}
}
2021-11-17 16:19:42 +00:00
func freePort() (int, error) {
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
if err != nil {
return 0, err
}
l, err := net.ListenTCP("tcp", addr)
if err != nil {
return 0, err
}
port := l.Addr().(*net.TCPAddr).Port
err = l.Close()
if err != nil {
return 0, err
}
return port, nil
}
2021-10-09 18:18:53 +00:00
// Execute starts a go-waku node with settings determined by the Options parameter
func Execute(options Options) {
if options.GenerateKey {
if err := writePrivateKeyToFile(options.KeyFile, options.Overwrite); err != nil {
failOnErr(err, "nodekey error")
}
return
}
2021-10-15 02:15:02 +00:00
hostAddr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", options.Address, options.Port))
failOnErr(err, "invalid host address")
prvKey, err := getPrivKey(options)
failOnErr(err, "nodekey error")
if options.DBPath == "" && options.UseDB {
failOnErr(errors.New("dbpath can't be null"), "")
}
var db *sql.DB
if options.UseDB {
db, err = sqlite.NewDB(options.DBPath)
failOnErr(err, "Could not connect to DB")
}
2021-04-22 13:07:22 +00:00
ctx := context.Background()
2021-04-19 00:03:16 +00:00
var metricsServer *metrics.Server
if options.Metrics.Enable {
metricsServer = metrics.NewMetricsServer(options.Metrics.Address, options.Metrics.Port, tests.Logger())
go metricsServer.Start()
}
2021-04-19 00:03:16 +00:00
nodeOpts := []node.WakuNodeOption{
node.WithLogger(utils.Logger()),
node.WithPrivateKey(prvKey),
2021-11-17 16:19:42 +00:00
node.WithHostAddress(hostAddr),
node.WithKeepAlive(time.Duration(options.KeepAlive) * time.Second),
}
2021-04-19 00:03:16 +00:00
2021-10-15 02:15:02 +00:00
if options.AdvertiseAddress != "" {
advertiseAddr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", options.AdvertiseAddress, options.Port))
2021-11-17 16:19:42 +00:00
failOnErr(err, "Invalid advertise address")
if advertiseAddr.Port == 0 {
for {
p, err := freePort()
if err == nil {
advertiseAddr.Port = p
break
}
}
}
2022-03-22 13:12:58 +00:00
nodeOpts = append(nodeOpts, node.WithAdvertiseAddress(advertiseAddr, options.Websocket.Enable, options.Websocket.Secure, options.Websocket.Port))
2021-10-15 02:15:02 +00:00
}
2022-03-22 13:12:58 +00:00
libp2pOpts := node.DefaultLibP2POptions
if options.AdvertiseAddress == "" {
libp2pOpts = append(libp2pOpts, libp2p.NATPortMap()) // Attempt to open ports using uPNP for NATed hosts.)
}
if options.Websocket.Enable {
nodeOpts = append(nodeOpts, node.WithWebsockets(options.Websocket.Address, options.Websocket.Port))
}
if options.Websocket.Secure {
nodeOpts = append(nodeOpts, node.WithSecureWebsockets(options.Websocket.Address, options.Websocket.Port, options.Websocket.CertPath, options.Websocket.KeyPath))
}
2021-04-19 00:03:16 +00:00
if options.ShowAddresses {
2021-10-15 02:15:02 +00:00
printListeningAddresses(ctx, nodeOpts, options)
return
}
if options.UseDB {
// Create persistent peerstore
queries, err := sqlite.NewQueries("peerstore", db)
failOnErr(err, "Peerstore")
datastore := dssql.NewDatastore(db, queries)
opts := pstoreds.DefaultOpts()
peerStore, err := pstoreds.NewPeerstore(ctx, datastore, opts)
failOnErr(err, "Peerstore")
libp2pOpts = append(libp2pOpts, libp2p.Peerstore(peerStore))
}
nodeOpts = append(nodeOpts, node.WithLibP2POptions(libp2pOpts...))
2021-10-01 17:49:50 +00:00
if options.Relay.Enable {
var wakurelayopts []pubsub.Option
wakurelayopts = append(wakurelayopts, pubsub.WithPeerExchange(options.Relay.PeerExchange))
nodeOpts = append(nodeOpts, node.WithWakuRelayAndMinPeers(options.Relay.MinRelayPeersToPublish, wakurelayopts...))
}
2021-06-10 12:59:51 +00:00
if options.RendezvousServer.Enable {
db, err := persistence.NewRendezVousLevelDB(options.RendezvousServer.DBPath)
failOnErr(err, "RendezvousDB")
storage := rendezvous.NewStorage(db)
nodeOpts = append(nodeOpts, node.WithRendezvousServer(storage))
}
if options.Filter.Enable {
nodeOpts = append(nodeOpts, node.WithWakuFilter(!options.Filter.DisableFullNode, filter.WithTimeout(time.Duration(options.Filter.Timeout)*time.Second)))
}
2021-06-28 14:14:28 +00:00
if options.Store.Enable {
nodeOpts = append(nodeOpts, node.WithWakuStoreAndRetentionPolicy(options.Store.ShouldResume, options.Store.RetentionMaxDaysDuration(), options.Store.RetentionMaxMessages))
if options.UseDB {
dbStore, err := persistence.NewDBStore(tests.Logger(), persistence.WithDB(db), persistence.WithRetentionPolicy(options.Store.RetentionMaxMessages, options.Store.RetentionMaxDaysDuration()))
failOnErr(err, "DBStore")
nodeOpts = append(nodeOpts, node.WithMessageProvider(dbStore))
} else {
nodeOpts = append(nodeOpts, node.WithMessageProvider(nil))
}
}
if options.LightPush.Enable {
nodeOpts = append(nodeOpts, node.WithLightPush())
}
2021-04-12 17:59:41 +00:00
if options.Rendezvous.Enable {
2021-11-16 14:22:01 +00:00
nodeOpts = append(nodeOpts, node.WithRendezvous(pubsub.WithDiscoveryOpts(discovery.Limit(45), discovery.TTL(time.Duration(20)*time.Second))))
}
2021-11-17 16:19:42 +00:00
if options.DiscV5.Enable {
var bootnodes []*enode.Node
for _, addr := range options.DiscV5.Nodes.Value() {
2021-11-17 16:19:42 +00:00
bootnode, err := enode.Parse(enode.ValidSchemes, addr)
if err != nil {
utils.Logger().Fatal("could not parse enr: ", zap.Error(err))
2021-11-17 16:19:42 +00:00
}
bootnodes = append(bootnodes, bootnode)
}
nodeOpts = append(nodeOpts, node.WithDiscoveryV5(options.DiscV5.Port, bootnodes, options.DiscV5.AutoUpdate, pubsub.WithDiscoveryOpts(discovery.Limit(45), discovery.TTL(time.Duration(20)*time.Second))))
}
wakuNode, err := node.New(ctx, nodeOpts...)
2021-04-15 21:23:07 +00:00
failOnErr(err, "Wakunode")
addPeers(wakuNode, options.Rendezvous.Nodes.Value(), string(rendezvous.RendezvousID_v001))
addPeers(wakuNode, options.Store.Nodes.Value(), string(store.StoreID_v20beta4))
addPeers(wakuNode, options.LightPush.Nodes.Value(), string(lightpush.LightPushID_v20beta1))
addPeers(wakuNode, options.Filter.Nodes.Value(), string(filter.FilterID_v20beta1))
2021-10-05 02:13:54 +00:00
2021-10-06 15:42:57 +00:00
if err = wakuNode.Start(); err != nil {
utils.Logger().Fatal(fmt.Errorf("could not start waku node, %w", err).Error())
2021-10-06 15:42:57 +00:00
}
2021-10-05 02:13:54 +00:00
2021-11-17 16:19:42 +00:00
if options.DiscV5.Enable {
if err = wakuNode.DiscV5().Start(); err != nil {
utils.Logger().Fatal(fmt.Errorf("could not start discovery v5, %w", err).Error())
2021-11-17 16:19:42 +00:00
}
}
if len(options.Relay.Topics.Value()) == 0 {
options.Relay.Topics = *cli.NewStringSlice(relay.DefaultWakuTopic)
}
if options.Relay.Enable {
for _, nodeTopic := range options.Relay.Topics.Value() {
nodeTopic := nodeTopic
sub, err := wakuNode.Relay().SubscribeToTopic(ctx, nodeTopic)
failOnErr(err, "Error subscring to topic")
wakuNode.Broadcaster().Unregister(&nodeTopic, sub.C)
}
}
for _, n := range options.StaticNodes.Value() {
go func(node string) {
err = wakuNode.DialPeer(ctx, node)
if err != nil {
utils.Logger().Error("error dialing peer ", zap.Error(err))
}
}(n)
}
if options.DNSDiscovery.Enable {
if options.DNSDiscovery.URL != "" {
utils.Logger().Info("attempting DNS discovery with ", zap.String("URL", options.DNSDiscovery.URL))
2021-11-16 14:22:01 +00:00
multiaddresses, err := dnsdisc.RetrieveNodes(ctx, options.DNSDiscovery.URL, dnsdisc.WithNameserver(options.DNSDiscovery.Nameserver))
if err != nil {
utils.Logger().Warn("dns discovery error ", zap.Error(err))
} else {
utils.Logger().Info("found dns entries ", zap.Any("multiaddresses", multiaddresses))
for _, m := range multiaddresses {
go func(ctx context.Context, m multiaddr.Multiaddr) {
ctx, cancel := context.WithTimeout(ctx, time.Duration(3)*time.Second)
defer cancel()
err = wakuNode.DialPeerWithMultiAddress(ctx, m)
if err != nil {
utils.Logger().Error("error dialing peer ", zap.Error(err))
}
}(ctx, m)
}
}
} else {
utils.Logger().Fatal("DNS discovery URL is required")
}
}
var rpcServer *rpc.WakuRpc
if options.RPCServer.Enable {
rpcServer = rpc.NewWakuRpc(wakuNode, options.RPCServer.Address, options.RPCServer.Port, tests.Logger())
rpcServer.Start()
}
// Wait for a SIGINT or SIGTERM signal
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
<-ch
utils.Logger().Info("Received signal, shutting down...")
2021-04-19 00:03:16 +00:00
// shut the node down
wakuNode.Stop()
if options.RPCServer.Enable {
err := rpcServer.Stop(ctx)
failOnErr(err, "RPCClose")
}
if options.Metrics.Enable {
2021-10-18 12:43:17 +00:00
err = metricsServer.Stop(ctx)
failOnErr(err, "MetricsClose")
}
if options.UseDB {
err = db.Close()
failOnErr(err, "DBClose")
}
}
func addPeers(wakuNode *node.WakuNode, addresses []string, protocols ...string) {
for _, addrString := range addresses {
if addrString == "" {
continue
}
addr, err := multiaddr.NewMultiaddr(addrString)
failOnErr(err, "invalid multiaddress")
_, err = wakuNode.AddPeer(addr, protocols...)
failOnErr(err, "error adding peer")
}
}
func loadPrivateKeyFromFile(path string) (*ecdsa.PrivateKey, error) {
src, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
dst := make([]byte, hex.DecodedLen(len(src)))
_, err = hex.Decode(dst, src)
if err != nil {
return nil, err
}
p, err := libp2pcrypto.UnmarshalSecp256k1PrivateKey(dst)
if err != nil {
return nil, err
}
privKey := (*ecdsa.PrivateKey)(p.(*libp2pcrypto.Secp256k1PrivateKey))
privKey.Curve = crypto.S256()
return privKey, nil
}
2021-12-08 14:21:30 +00:00
func checkForPrivateKeyFile(path string, overwrite bool) error {
_, err := os.Stat(path)
2021-12-08 14:21:30 +00:00
if err == nil && !overwrite {
return fmt.Errorf("%s already exists. Use --overwrite to overwrite the file", path)
}
if err := os.Remove(path); err != nil && !os.IsNotExist(err) {
return err
}
2021-12-08 14:21:30 +00:00
return nil
}
func generatePrivateKey() ([]byte, error) {
key, err := crypto.GenerateKey()
if err != nil {
2021-12-08 14:21:30 +00:00
return nil, err
}
privKey := libp2pcrypto.PrivKey((*libp2pcrypto.Secp256k1PrivateKey)(key))
b, err := privKey.Raw()
if err != nil {
2021-12-08 14:21:30 +00:00
return nil, err
}
output := make([]byte, hex.EncodedLen(len(b)))
hex.Encode(output, b)
2021-12-08 14:21:30 +00:00
return output, nil
}
func writePrivateKeyToFile(path string, overwrite bool) error {
if err := checkForPrivateKeyFile(path, overwrite); err != nil {
return err
}
output, err := generatePrivateKey()
if err != nil {
return err
}
return ioutil.WriteFile(path, output, 0600)
}
func getPrivKey(options Options) (*ecdsa.PrivateKey, error) {
var prvKey *ecdsa.PrivateKey
var err error
2021-10-05 02:13:54 +00:00
if options.NodeKey != "" {
if prvKey, err = crypto.HexToECDSA(options.NodeKey); err != nil {
return nil, fmt.Errorf("error converting key into valid ecdsa key: %w", err)
}
} else {
keyString := os.Getenv("GOWAKU-NODEKEY")
if keyString != "" {
2021-10-05 02:13:54 +00:00
if prvKey, err = crypto.HexToECDSA(keyString); err != nil {
return nil, fmt.Errorf("error converting key into valid ecdsa key: %w", err)
}
} else {
if _, err := os.Stat(options.KeyFile); err == nil {
2021-10-05 02:13:54 +00:00
if prvKey, err = loadPrivateKeyFromFile(options.KeyFile); err != nil {
return nil, fmt.Errorf("could not read keyfile: %w", err)
}
} else {
if os.IsNotExist(err) {
2021-10-05 02:13:54 +00:00
if prvKey, err = crypto.GenerateKey(); err != nil {
return nil, fmt.Errorf("error generating key: %w", err)
}
} else {
return nil, fmt.Errorf("could not read keyfile: %w", err)
}
}
}
}
return prvKey, nil
}
2021-10-15 02:15:02 +00:00
func printListeningAddresses(ctx context.Context, nodeOpts []node.WakuNodeOption, options Options) {
params := new(node.WakuNodeParameters)
for _, opt := range nodeOpts {
err := opt(params)
if err != nil {
panic(err)
}
}
2021-10-15 02:15:02 +00:00
var libp2pOpts []config.Option
2021-11-17 16:19:42 +00:00
libp2pOpts = append(libp2pOpts,
params.Identity(),
libp2p.ListenAddrs(params.MultiAddresses()...),
)
2021-10-15 02:15:02 +00:00
2021-11-17 16:19:42 +00:00
addrFactory := params.AddressFactory()
if addrFactory != nil {
libp2pOpts = append(libp2pOpts, libp2p.AddrsFactory(addrFactory))
2021-10-15 02:15:02 +00:00
}
2022-03-22 13:12:58 +00:00
h, err := libp2p.New(libp2pOpts...)
if err != nil {
panic(err)
}
hostInfo, _ := multiaddr.NewMultiaddr(fmt.Sprintf("/p2p/%s", h.ID().Pretty()))
2021-10-15 02:15:02 +00:00
for _, addr := range h.Addrs() {
fmt.Println(addr.Encapsulate(hostInfo))
}
}