2021-04-13 18:54:06 +00:00
|
|
|
package waku
|
2021-03-18 23:21:45 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-10-04 22:38:27 +00:00
|
|
|
"crypto/ecdsa"
|
2021-04-19 00:03:16 +00:00
|
|
|
"database/sql"
|
2022-07-25 12:24:42 +00:00
|
|
|
"encoding/json"
|
2021-04-13 18:54:06 +00:00
|
|
|
"errors"
|
2021-03-18 23:21:45 +00:00
|
|
|
"fmt"
|
2021-10-04 22:38:27 +00:00
|
|
|
"io/ioutil"
|
2021-03-18 23:21:45 +00:00
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2021-06-24 13:02:53 +00:00
|
|
|
"time"
|
2021-03-18 23:21:45 +00:00
|
|
|
|
2022-07-25 12:24:42 +00:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/keystore"
|
2022-07-28 14:04:33 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2021-03-23 14:46:16 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2021-09-30 23:03:19 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2021-04-13 18:54:06 +00:00
|
|
|
dssql "github.com/ipfs/go-ds-sql"
|
2022-02-21 15:23:52 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2022-01-18 18:17:06 +00:00
|
|
|
"go.uber.org/zap"
|
2021-10-03 21:45:07 +00:00
|
|
|
|
2021-04-13 18:54:06 +00:00
|
|
|
"github.com/libp2p/go-libp2p"
|
2021-10-15 02:15:02 +00:00
|
|
|
"github.com/libp2p/go-libp2p/config"
|
2022-10-19 19:39:32 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/discovery"
|
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
2022-08-25 20:36:04 +00:00
|
|
|
"github.com/libp2p/go-libp2p/p2p/transport/tcp"
|
2021-10-04 22:38:27 +00:00
|
|
|
|
2021-10-08 13:50:56 +00:00
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
2022-10-19 19:39:32 +00:00
|
|
|
"github.com/libp2p/go-libp2p/p2p/host/peerstore/pstoreds"
|
2022-08-25 20:36:04 +00:00
|
|
|
ws "github.com/libp2p/go-libp2p/p2p/transport/websocket"
|
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"
|
2022-05-27 13:25:06 +00:00
|
|
|
"github.com/status-im/go-waku/logging"
|
2021-06-28 13:20:23 +00:00
|
|
|
"github.com/status-im/go-waku/waku/metrics"
|
2021-04-13 18:54:06 +00:00
|
|
|
"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"
|
2021-03-18 23:21:45 +00:00
|
|
|
"github.com/status-im/go-waku/waku/v2/node"
|
2021-09-30 16:01:53 +00:00
|
|
|
"github.com/status-im/go-waku/waku/v2/protocol/filter"
|
|
|
|
"github.com/status-im/go-waku/waku/v2/protocol/lightpush"
|
2021-04-28 20:10:44 +00:00
|
|
|
"github.com/status-im/go-waku/waku/v2/protocol/relay"
|
2021-09-30 16:01:53 +00:00
|
|
|
"github.com/status-im/go-waku/waku/v2/protocol/store"
|
2022-07-24 20:51:42 +00:00
|
|
|
"github.com/status-im/go-waku/waku/v2/rest"
|
2021-11-02 09:54:34 +00:00
|
|
|
"github.com/status-im/go-waku/waku/v2/rpc"
|
2022-01-18 18:17:06 +00:00
|
|
|
"github.com/status-im/go-waku/waku/v2/utils"
|
2021-03-18 23:21:45 +00:00
|
|
|
)
|
|
|
|
|
2021-10-03 21:45:07 +00:00
|
|
|
func failOnErr(err error, msg string) {
|
2021-04-13 18:54:06 +00:00
|
|
|
if err != nil {
|
2022-01-18 18:17:06 +00:00
|
|
|
utils.Logger().Fatal(msg, zap.Error(err))
|
2021-04-13 18:54:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-08-26 00:18:03 +00:00
|
|
|
const dialTimeout = 7 * time.Second
|
|
|
|
|
2021-10-09 18:18:53 +00:00
|
|
|
// Execute starts a go-waku node with settings determined by the Options parameter
|
2021-10-03 21:45:07 +00:00
|
|
|
func Execute(options Options) {
|
2021-10-04 22:38:27 +00:00
|
|
|
if options.GenerateKey {
|
2022-07-25 12:24:42 +00:00
|
|
|
if err := writePrivateKeyToFile(options.KeyFile, []byte(options.KeyPasswd), options.Overwrite); err != nil {
|
2021-10-04 22:38:27 +00:00
|
|
|
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")
|
2021-03-18 23:21:45 +00:00
|
|
|
|
2021-10-04 22:38:27 +00:00
|
|
|
prvKey, err := getPrivKey(options)
|
|
|
|
failOnErr(err, "nodekey error")
|
2021-04-13 18:54:06 +00:00
|
|
|
|
2022-05-27 19:55:35 +00:00
|
|
|
p2pPrvKey := utils.EcdsaPrivKeyToSecp256k1PrivKey(prvKey)
|
|
|
|
id, err := peer.IDFromPublicKey(p2pPrvKey.GetPublic())
|
2022-05-27 13:25:06 +00:00
|
|
|
failOnErr(err, "deriving peer ID from private key")
|
|
|
|
logger := utils.Logger().With(logging.HostID("node", id))
|
|
|
|
|
2021-10-03 21:45:07 +00:00
|
|
|
if options.DBPath == "" && options.UseDB {
|
|
|
|
failOnErr(errors.New("dbpath can't be null"), "")
|
|
|
|
}
|
2021-06-28 13:20:23 +00:00
|
|
|
|
2021-10-03 21:45:07 +00:00
|
|
|
var db *sql.DB
|
|
|
|
if options.UseDB {
|
|
|
|
db, err = sqlite.NewDB(options.DBPath)
|
|
|
|
failOnErr(err, "Could not connect to DB")
|
2022-05-30 18:48:22 +00:00
|
|
|
logger.Debug("using database: ", zap.String("path", options.DBPath))
|
|
|
|
|
|
|
|
} else {
|
|
|
|
db, err = sqlite.NewDB(":memory:")
|
|
|
|
failOnErr(err, "Could not create in-memory DB")
|
|
|
|
logger.Debug("using in-memory database")
|
2021-10-03 21:45:07 +00:00
|
|
|
}
|
2021-04-22 13:07:22 +00:00
|
|
|
|
2021-10-03 21:45:07 +00:00
|
|
|
ctx := context.Background()
|
2021-04-19 00:03:16 +00:00
|
|
|
|
2021-10-03 21:45:07 +00:00
|
|
|
var metricsServer *metrics.Server
|
|
|
|
if options.Metrics.Enable {
|
2022-05-30 15:55:30 +00:00
|
|
|
metricsServer = metrics.NewMetricsServer(options.Metrics.Address, options.Metrics.Port, logger)
|
2021-10-03 21:45:07 +00:00
|
|
|
go metricsServer.Start()
|
|
|
|
}
|
2021-04-19 00:03:16 +00:00
|
|
|
|
2021-10-03 21:45:07 +00:00
|
|
|
nodeOpts := []node.WakuNodeOption{
|
2022-05-27 13:25:06 +00:00
|
|
|
node.WithLogger(logger),
|
2021-10-03 21:45:07 +00:00
|
|
|
node.WithPrivateKey(prvKey),
|
2021-11-17 16:19:42 +00:00
|
|
|
node.WithHostAddress(hostAddr),
|
2022-08-15 17:13:45 +00:00
|
|
|
node.WithKeepAlive(options.KeepAlive),
|
2021-10-03 21:45:07 +00:00
|
|
|
}
|
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 00:48:46 +00:00
|
|
|
nodeOpts = append(nodeOpts, node.WithAdvertiseAddress(advertiseAddr))
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.Dns4DomainName != "" {
|
|
|
|
nodeOpts = append(nodeOpts, node.WithDns4Domain(options.Dns4DomainName))
|
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 {
|
2022-08-25 20:36:04 +00:00
|
|
|
nodeOpts = append(nodeOpts, node.WithWebsockets(options.Websocket.Address, options.Websocket.WSPort))
|
2022-03-22 13:12:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if options.Websocket.Secure {
|
2022-08-25 20:36:04 +00:00
|
|
|
nodeOpts = append(nodeOpts, node.WithSecureWebsockets(options.Websocket.Address, options.Websocket.WSSPort, options.Websocket.CertPath, options.Websocket.KeyPath))
|
2021-10-03 21:45:07 +00:00
|
|
|
}
|
2021-04-19 00:03:16 +00:00
|
|
|
|
2021-10-14 18:17:01 +00:00
|
|
|
if options.ShowAddresses {
|
2021-10-15 02:15:02 +00:00
|
|
|
printListeningAddresses(ctx, nodeOpts, options)
|
2021-10-14 18:17:01 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-19 21:47:39 +00:00
|
|
|
if options.Version {
|
2022-08-09 14:15:16 +00:00
|
|
|
fmt.Printf("version / git commit hash: %s-%s\n", node.Version, node.GitCommit)
|
2022-06-19 21:47:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-03 21:45:07 +00:00
|
|
|
if options.UseDB {
|
2022-06-19 21:47:39 +00:00
|
|
|
if options.PersistPeers {
|
|
|
|
// Create persistent peerstore
|
|
|
|
queries, err := sqlite.NewQueries("peerstore", db)
|
|
|
|
failOnErr(err, "Peerstore")
|
2021-10-01 17:43:03 +00:00
|
|
|
|
2022-06-19 21:47:39 +00:00
|
|
|
datastore := dssql.NewDatastore(db, queries)
|
|
|
|
opts := pstoreds.DefaultOpts()
|
|
|
|
peerStore, err := pstoreds.NewPeerstore(ctx, datastore, opts)
|
|
|
|
failOnErr(err, "Peerstore")
|
2021-10-01 17:43:03 +00:00
|
|
|
|
2022-06-19 21:47:39 +00:00
|
|
|
libp2pOpts = append(libp2pOpts, libp2p.Peerstore(peerStore))
|
|
|
|
}
|
2021-10-03 21:45:07 +00:00
|
|
|
}
|
2021-03-18 23:21:45 +00:00
|
|
|
|
2021-10-03 21:45:07 +00:00
|
|
|
nodeOpts = append(nodeOpts, node.WithLibP2POptions(libp2pOpts...))
|
2021-10-01 17:49:50 +00:00
|
|
|
|
2022-02-21 15:23:52 +00:00
|
|
|
if options.Relay.Enable {
|
2021-10-03 21:45:07 +00:00
|
|
|
var wakurelayopts []pubsub.Option
|
|
|
|
wakurelayopts = append(wakurelayopts, pubsub.WithPeerExchange(options.Relay.PeerExchange))
|
2021-12-06 08:43:00 +00:00
|
|
|
nodeOpts = append(nodeOpts, node.WithWakuRelayAndMinPeers(options.Relay.MinRelayPeersToPublish, wakurelayopts...))
|
2021-10-03 21:45:07 +00:00
|
|
|
}
|
2021-06-10 12:59:51 +00:00
|
|
|
|
2021-10-03 21:45:07 +00:00
|
|
|
if options.RendezvousServer.Enable {
|
2021-10-14 14:14:55 +00:00
|
|
|
db, err := persistence.NewRendezVousLevelDB(options.RendezvousServer.DBPath)
|
2021-10-03 21:45:07 +00:00
|
|
|
failOnErr(err, "RendezvousDB")
|
|
|
|
storage := rendezvous.NewStorage(db)
|
|
|
|
nodeOpts = append(nodeOpts, node.WithRendezvousServer(storage))
|
|
|
|
}
|
2021-04-13 18:54:06 +00:00
|
|
|
|
2021-10-03 21:45:07 +00:00
|
|
|
if options.Filter.Enable {
|
2022-08-15 17:13:45 +00:00
|
|
|
nodeOpts = append(nodeOpts, node.WithWakuFilter(!options.Filter.DisableFullNode, filter.WithTimeout(options.Filter.Timeout)))
|
2021-10-03 21:45:07 +00:00
|
|
|
}
|
2021-06-28 14:14:28 +00:00
|
|
|
|
2021-10-03 21:45:07 +00:00
|
|
|
if options.Store.Enable {
|
2022-06-13 18:30:35 +00:00
|
|
|
if options.Store.PersistMessages {
|
2022-08-03 13:32:52 +00:00
|
|
|
nodeOpts = append(nodeOpts, node.WithWakuStore(true, options.Store.ShouldResume))
|
2022-08-15 17:13:45 +00:00
|
|
|
dbStore, err := persistence.NewDBStore(logger, persistence.WithDB(db), persistence.WithRetentionPolicy(options.Store.RetentionMaxMessages, options.Store.RetentionTime))
|
2022-06-13 18:30:35 +00:00
|
|
|
failOnErr(err, "DBStore")
|
|
|
|
nodeOpts = append(nodeOpts, node.WithMessageProvider(dbStore))
|
|
|
|
} else {
|
|
|
|
nodeOpts = append(nodeOpts, node.WithWakuStore(false, false))
|
|
|
|
}
|
2021-10-03 21:45:07 +00:00
|
|
|
}
|
2021-09-30 16:01:53 +00:00
|
|
|
|
2021-10-03 21:45:07 +00:00
|
|
|
if options.LightPush.Enable {
|
|
|
|
nodeOpts = append(nodeOpts, node.WithLightPush())
|
|
|
|
}
|
2021-04-12 17:59:41 +00:00
|
|
|
|
2021-10-03 21:45:07 +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-10-03 21:45:07 +00:00
|
|
|
}
|
2021-03-18 23:21:45 +00:00
|
|
|
|
2022-05-05 19:17:35 +00:00
|
|
|
var discoveredNodes []dnsdisc.DiscoveredNode
|
|
|
|
if options.DNSDiscovery.Enable {
|
2022-09-14 19:19:11 +00:00
|
|
|
if len(options.DNSDiscovery.URLs.Value()) != 0 {
|
|
|
|
for _, url := range options.DNSDiscovery.URLs.Value() {
|
|
|
|
logger.Info("attempting DNS discovery with ", zap.String("URL", url))
|
|
|
|
nodes, err := dnsdisc.RetrieveNodes(ctx, url, dnsdisc.WithNameserver(options.DNSDiscovery.Nameserver))
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn("dns discovery error ", zap.Error(err))
|
|
|
|
} else {
|
|
|
|
var discAddresses []multiaddr.Multiaddr
|
|
|
|
for _, n := range nodes {
|
|
|
|
discAddresses = append(discAddresses, n.Addresses...)
|
|
|
|
}
|
|
|
|
logger.Info("found dns entries ", logging.MultiAddrs("nodes", discAddresses...))
|
|
|
|
discoveredNodes = nodes
|
2022-08-26 00:18:03 +00:00
|
|
|
}
|
2022-05-05 19:17:35 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-05-27 13:25:06 +00:00
|
|
|
logger.Fatal("DNS discovery URL is required")
|
2022-05-05 19:17:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-17 16:19:42 +00:00
|
|
|
if options.DiscV5.Enable {
|
|
|
|
var bootnodes []*enode.Node
|
2022-02-21 15:23:52 +00:00
|
|
|
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 {
|
2022-05-27 13:25:06 +00:00
|
|
|
logger.Fatal("parsing ENR", zap.Error(err))
|
2021-11-17 16:19:42 +00:00
|
|
|
}
|
|
|
|
bootnodes = append(bootnodes, bootnode)
|
|
|
|
}
|
2022-05-05 19:17:35 +00:00
|
|
|
|
|
|
|
for _, n := range discoveredNodes {
|
|
|
|
if n.ENR != nil {
|
|
|
|
bootnodes = append(bootnodes, n.ENR)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-17 16:19:42 +00:00
|
|
|
nodeOpts = append(nodeOpts, node.WithDiscoveryV5(options.DiscV5.Port, bootnodes, options.DiscV5.AutoUpdate, pubsub.WithDiscoveryOpts(discovery.Limit(45), discovery.TTL(time.Duration(20)*time.Second))))
|
|
|
|
}
|
|
|
|
|
2022-09-11 21:08:58 +00:00
|
|
|
checkForRLN(logger, options, &nodeOpts)
|
2022-07-05 21:28:34 +00:00
|
|
|
|
2021-10-03 21:45:07 +00:00
|
|
|
wakuNode, err := node.New(ctx, nodeOpts...)
|
2021-04-15 21:23:07 +00:00
|
|
|
|
2021-10-03 21:45:07 +00:00
|
|
|
failOnErr(err, "Wakunode")
|
2021-09-30 16:01:53 +00:00
|
|
|
|
2022-08-15 17:13:45 +00:00
|
|
|
addPeers(wakuNode, options.Rendezvous.Nodes, string(rendezvous.RendezvousID_v001))
|
|
|
|
addPeers(wakuNode, options.Store.Nodes, string(store.StoreID_v20beta4))
|
|
|
|
addPeers(wakuNode, options.LightPush.Nodes, string(lightpush.LightPushID_v20beta1))
|
|
|
|
addPeers(wakuNode, options.Filter.Nodes, 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 {
|
2022-05-27 13:25:06 +00:00
|
|
|
logger.Fatal("starting waku node", zap.Error(err))
|
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 {
|
2022-05-27 13:25:06 +00:00
|
|
|
logger.Fatal("starting discovery v5", zap.Error(err))
|
2021-11-17 16:19:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-21 15:23:52 +00:00
|
|
|
if len(options.Relay.Topics.Value()) == 0 {
|
|
|
|
options.Relay.Topics = *cli.NewStringSlice(relay.DefaultWakuTopic)
|
2021-10-03 21:45:07 +00:00
|
|
|
}
|
2021-09-30 16:01:53 +00:00
|
|
|
|
2022-02-21 15:23:52 +00:00
|
|
|
if options.Relay.Enable {
|
|
|
|
for _, nodeTopic := range options.Relay.Topics.Value() {
|
2022-04-25 19:31:26 +00:00
|
|
|
nodeTopic := nodeTopic
|
2022-02-18 17:49:11 +00:00
|
|
|
sub, err := wakuNode.Relay().SubscribeToTopic(ctx, nodeTopic)
|
2021-10-30 14:29:34 +00:00
|
|
|
failOnErr(err, "Error subscring to topic")
|
2022-04-25 19:31:26 +00:00
|
|
|
wakuNode.Broadcaster().Unregister(&nodeTopic, sub.C)
|
2021-10-30 14:29:34 +00:00
|
|
|
}
|
2021-10-03 21:45:07 +00:00
|
|
|
}
|
2021-03-18 23:21:45 +00:00
|
|
|
|
2022-08-15 17:13:45 +00:00
|
|
|
for _, n := range options.StaticNodes {
|
2022-08-26 00:18:03 +00:00
|
|
|
go func(ctx context.Context, node multiaddr.Multiaddr) {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, dialTimeout)
|
|
|
|
defer cancel()
|
2022-08-15 17:13:45 +00:00
|
|
|
err = wakuNode.DialPeerWithMultiAddress(ctx, node)
|
2021-10-03 21:45:07 +00:00
|
|
|
if err != nil {
|
2022-05-27 13:25:06 +00:00
|
|
|
logger.Error("dialing peer", zap.Error(err))
|
2021-03-18 23:21:45 +00:00
|
|
|
}
|
2022-08-26 00:18:03 +00:00
|
|
|
}(ctx, n)
|
2021-10-03 21:45:07 +00:00
|
|
|
}
|
2021-03-18 23:21:45 +00:00
|
|
|
|
2022-05-05 19:17:35 +00:00
|
|
|
if len(discoveredNodes) != 0 {
|
|
|
|
for _, n := range discoveredNodes {
|
|
|
|
for _, m := range n.Addresses {
|
|
|
|
go func(ctx context.Context, m multiaddr.Multiaddr) {
|
2022-08-26 00:18:03 +00:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, dialTimeout)
|
2022-05-05 19:17:35 +00:00
|
|
|
defer cancel()
|
|
|
|
err = wakuNode.DialPeerWithMultiAddress(ctx, m)
|
|
|
|
if err != nil {
|
2022-08-26 00:18:03 +00:00
|
|
|
logger.Error("dialing peer", logging.MultiAddrs("peer", m), zap.Error(err))
|
2022-05-05 19:17:35 +00:00
|
|
|
}
|
|
|
|
}(ctx, m)
|
2021-09-30 23:03:19 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-03 21:45:07 +00:00
|
|
|
}
|
2021-09-30 23:03:19 +00:00
|
|
|
|
2022-08-12 12:44:13 +00:00
|
|
|
onStartRLN(wakuNode, options)
|
2022-08-09 00:02:08 +00:00
|
|
|
|
2021-11-02 09:54:34 +00:00
|
|
|
var rpcServer *rpc.WakuRpc
|
|
|
|
if options.RPCServer.Enable {
|
2022-05-30 15:55:30 +00:00
|
|
|
rpcServer = rpc.NewWakuRpc(wakuNode, options.RPCServer.Address, options.RPCServer.Port, options.RPCServer.Admin, options.RPCServer.Private, logger)
|
2021-11-19 14:17:38 +00:00
|
|
|
rpcServer.Start()
|
2021-11-02 09:54:34 +00:00
|
|
|
}
|
|
|
|
|
2022-07-24 20:51:42 +00:00
|
|
|
var restServer *rest.WakuRest
|
|
|
|
if options.RESTServer.Enable {
|
|
|
|
restServer = rest.NewWakuRest(wakuNode, options.RESTServer.Address, options.RESTServer.Port, options.RESTServer.Admin, options.RESTServer.Private, options.RESTServer.RelayCacheCapacity, logger)
|
|
|
|
restServer.Start()
|
|
|
|
}
|
|
|
|
|
2022-05-30 18:48:22 +00:00
|
|
|
logger.Info("Node setup complete")
|
2022-05-26 12:23:10 +00:00
|
|
|
|
2021-10-03 21:45:07 +00:00
|
|
|
// Wait for a SIGINT or SIGTERM signal
|
|
|
|
ch := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
<-ch
|
2022-05-27 13:25:06 +00:00
|
|
|
logger.Info("Received signal, shutting down...")
|
2021-04-19 00:03:16 +00:00
|
|
|
|
2021-10-03 21:45:07 +00:00
|
|
|
// shut the node down
|
|
|
|
wakuNode.Stop()
|
2021-06-28 13:20:23 +00:00
|
|
|
|
2021-11-02 09:54:34 +00:00
|
|
|
if options.RPCServer.Enable {
|
|
|
|
err := rpcServer.Stop(ctx)
|
|
|
|
failOnErr(err, "RPCClose")
|
|
|
|
}
|
|
|
|
|
2022-07-24 20:51:42 +00:00
|
|
|
if options.RESTServer.Enable {
|
|
|
|
err := restServer.Stop(ctx)
|
|
|
|
failOnErr(err, "RESTClose")
|
|
|
|
}
|
|
|
|
|
2021-10-03 21:45:07 +00:00
|
|
|
if options.Metrics.Enable {
|
2021-10-18 12:43:17 +00:00
|
|
|
err = metricsServer.Stop(ctx)
|
|
|
|
failOnErr(err, "MetricsClose")
|
2021-10-03 21:45:07 +00:00
|
|
|
}
|
2021-03-18 23:21:45 +00:00
|
|
|
|
2021-10-03 21:45:07 +00:00
|
|
|
if options.UseDB {
|
|
|
|
err = db.Close()
|
|
|
|
failOnErr(err, "DBClose")
|
|
|
|
}
|
2021-03-18 23:21:45 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 17:13:45 +00:00
|
|
|
func addPeers(wakuNode *node.WakuNode, addresses []multiaddr.Multiaddr, protocols ...string) {
|
|
|
|
for _, addr := range addresses {
|
|
|
|
_, err := wakuNode.AddPeer(addr, protocols...)
|
2021-10-03 21:45:07 +00:00
|
|
|
failOnErr(err, "error adding peer")
|
2021-09-30 16:01:53 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-04 22:38:27 +00:00
|
|
|
|
2022-07-25 12:24:42 +00:00
|
|
|
func loadPrivateKeyFromFile(path string, passwd string) (*ecdsa.PrivateKey, error) {
|
2021-10-04 22:38:27 +00:00
|
|
|
src, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-07-25 12:24:42 +00:00
|
|
|
var encryptedK keystore.CryptoJSON
|
|
|
|
err = json.Unmarshal(src, &encryptedK)
|
2021-10-04 22:38:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-07-25 12:24:42 +00:00
|
|
|
pKey, err := keystore.DecryptDataV3(encryptedK, passwd)
|
2022-05-27 19:55:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-10-04 22:38:27 +00:00
|
|
|
|
2022-07-25 12:24:42 +00:00
|
|
|
return crypto.ToECDSA(pKey)
|
2021-10-04 22:38:27 +00:00
|
|
|
}
|
|
|
|
|
2022-08-09 00:02:08 +00:00
|
|
|
func checkForFileExistence(path string, overwrite bool) error {
|
2021-10-04 22:38:27 +00:00
|
|
|
_, err := os.Stat(path)
|
|
|
|
|
2021-12-08 14:21:30 +00:00
|
|
|
if err == nil && !overwrite {
|
2021-10-04 22:38:27 +00:00
|
|
|
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) {
|
2021-10-04 22:38:27 +00:00
|
|
|
key, err := crypto.GenerateKey()
|
|
|
|
if err != nil {
|
2021-12-08 14:21:30 +00:00
|
|
|
return nil, err
|
2021-10-04 22:38:27 +00:00
|
|
|
}
|
|
|
|
|
2022-07-25 12:24:42 +00:00
|
|
|
return key.D.Bytes(), nil
|
2021-12-08 14:21:30 +00:00
|
|
|
}
|
|
|
|
|
2022-07-25 12:24:42 +00:00
|
|
|
func writePrivateKeyToFile(path string, passwd []byte, overwrite bool) error {
|
2022-08-09 00:02:08 +00:00
|
|
|
if err := checkForFileExistence(path, overwrite); err != nil {
|
2021-12-08 14:21:30 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-25 12:24:42 +00:00
|
|
|
key, err := generatePrivateKey()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
encryptedK, err := keystore.EncryptDataV3(key, passwd, keystore.StandardScryptN, keystore.StandardScryptP)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
output, err := json.Marshal(encryptedK)
|
2021-12-08 14:21:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-04 22:38:27 +00:00
|
|
|
return ioutil.WriteFile(path, output, 0600)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPrivKey(options Options) (*ecdsa.PrivateKey, error) {
|
|
|
|
var prvKey *ecdsa.PrivateKey
|
|
|
|
var err error
|
2022-08-15 17:13:45 +00:00
|
|
|
|
|
|
|
if options.NodeKey != nil {
|
|
|
|
prvKey = options.NodeKey
|
2021-10-04 22:38:27 +00:00
|
|
|
} else {
|
2022-08-15 17:13:45 +00:00
|
|
|
// TODO: once https://github.com/urfave/cli/issues/1272 is fixed, remove env variable logic
|
2021-10-04 22:38:27 +00:00
|
|
|
keyString := os.Getenv("GOWAKU-NODEKEY")
|
|
|
|
if keyString != "" {
|
2022-08-04 21:39:12 +00:00
|
|
|
if prvKey, err = crypto.ToECDSA(common.FromHex(keyString)); err != nil {
|
2021-10-04 22:38:27 +00:00
|
|
|
return nil, fmt.Errorf("error converting key into valid ecdsa key: %w", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if _, err := os.Stat(options.KeyFile); err == nil {
|
2022-07-25 12:24:42 +00:00
|
|
|
if prvKey, err = loadPrivateKeyFromFile(options.KeyFile, options.KeyPasswd); err != nil {
|
2021-10-04 22:38:27 +00:00
|
|
|
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 {
|
2021-10-04 22:38:27 +00:00
|
|
|
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-14 18:17:01 +00:00
|
|
|
|
2021-10-15 02:15:02 +00:00
|
|
|
func printListeningAddresses(ctx context.Context, nodeOpts []node.WakuNodeOption, options Options) {
|
2021-10-14 18:17:01 +00:00
|
|
|
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
|
|
|
|
2022-08-25 20:36:04 +00:00
|
|
|
if options.Websocket.Secure {
|
|
|
|
transports := libp2p.ChainOptions(
|
|
|
|
libp2p.Transport(tcp.NewTCPTransport),
|
|
|
|
libp2p.Transport(ws.New, ws.WithTLSConfig(params.TLSConfig())),
|
|
|
|
)
|
|
|
|
libp2pOpts = append(libp2pOpts, transports)
|
|
|
|
}
|
|
|
|
|
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...)
|
2021-10-14 18:17:01 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
hostInfo, _ := multiaddr.NewMultiaddr(fmt.Sprintf("/p2p/%s", h.ID().Pretty()))
|
2021-10-15 02:15:02 +00:00
|
|
|
|
2021-10-14 18:17:01 +00:00
|
|
|
for _, addr := range h.Addrs() {
|
|
|
|
fmt.Println(addr.Encapsulate(hostInfo))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|