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"
|
2021-03-18 23:21:45 +00:00
|
|
|
"encoding/hex"
|
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
|
|
|
|
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"
|
2021-10-03 21:45:07 +00:00
|
|
|
|
2021-04-13 18:54:06 +00:00
|
|
|
logging "github.com/ipfs/go-log"
|
|
|
|
"github.com/libp2p/go-libp2p"
|
2021-10-04 22:38:27 +00:00
|
|
|
libp2pcrypto "github.com/libp2p/go-libp2p-core/crypto"
|
2021-10-03 21:45:07 +00:00
|
|
|
libp2pdisc "github.com/libp2p/go-libp2p-core/discovery"
|
2021-10-04 22:38:27 +00:00
|
|
|
|
2021-09-30 16:01:53 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/protocol"
|
2021-04-13 18:54:06 +00:00
|
|
|
"github.com/libp2p/go-libp2p-peerstore/pstoreds"
|
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"
|
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-09-30 23:03:19 +00:00
|
|
|
"github.com/status-im/go-waku/waku/v2/discovery"
|
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"
|
2021-10-03 21:45:07 +00:00
|
|
|
pubsub "github.com/status-im/go-wakurelay-pubsub"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/opt"
|
2021-03-18 23:21:45 +00:00
|
|
|
)
|
|
|
|
|
2021-04-13 18:54:06 +00:00
|
|
|
var log = logging.Logger("wakunode")
|
|
|
|
|
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 {
|
|
|
|
if msg != "" {
|
|
|
|
msg = msg + ": "
|
|
|
|
}
|
|
|
|
log.Fatal(msg, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-03 21:45:07 +00:00
|
|
|
func Execute(options Options) {
|
2021-10-04 22:38:27 +00:00
|
|
|
if options.GenerateKey {
|
|
|
|
if err := writePrivateKeyToFile(options.KeyFile, options.Overwrite); err != nil {
|
|
|
|
failOnErr(err, "nodekey error")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-03 21:45:07 +00:00
|
|
|
hostAddr, _ := net.ResolveTCPAddr("tcp", fmt.Sprint("0.0.0.0:", options.Port))
|
2021-03-18 23:21:45 +00:00
|
|
|
|
2021-10-03 21:45:07 +00:00
|
|
|
var err error
|
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
|
|
|
|
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
|
2021-04-13 18:54:06 +00:00
|
|
|
|
2021-10-03 21:45:07 +00:00
|
|
|
if options.UseDB {
|
|
|
|
db, err = sqlite.NewDB(options.DBPath)
|
|
|
|
failOnErr(err, "Could not connect to DB")
|
|
|
|
}
|
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 {
|
|
|
|
metricsServer = metrics.NewMetricsServer(options.Metrics.Address, options.Metrics.Port)
|
|
|
|
go metricsServer.Start()
|
|
|
|
}
|
2021-04-19 00:03:16 +00:00
|
|
|
|
2021-10-03 21:45:07 +00:00
|
|
|
nodeOpts := []node.WakuNodeOption{
|
|
|
|
node.WithPrivateKey(prvKey),
|
|
|
|
node.WithHostAddress([]net.Addr{hostAddr}),
|
|
|
|
node.WithKeepAlive(time.Duration(options.KeepAlive) * time.Second),
|
|
|
|
}
|
2021-04-19 00:03:16 +00:00
|
|
|
|
2021-10-03 21:45:07 +00:00
|
|
|
if options.EnableWS {
|
|
|
|
wsMa, _ := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/0.0.0.0/tcp/%d/ws", options.WSPort))
|
|
|
|
nodeOpts = append(nodeOpts, node.WithMultiaddress([]multiaddr.Multiaddr{wsMa}))
|
|
|
|
}
|
2021-04-19 00:03:16 +00:00
|
|
|
|
2021-10-03 21:45:07 +00:00
|
|
|
libp2pOpts := node.DefaultLibP2POptions
|
2021-04-19 00:03:16 +00:00
|
|
|
|
2021-10-03 21:45:07 +00:00
|
|
|
if options.UseDB {
|
|
|
|
// Create persistent peerstore
|
|
|
|
queries, err := sqlite.NewQueries("peerstore", db)
|
|
|
|
failOnErr(err, "Peerstore")
|
2021-10-01 17:43:03 +00:00
|
|
|
|
2021-10-03 21:45:07 +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
|
|
|
|
2021-10-03 21:45:07 +00:00
|
|
|
libp2pOpts = append(libp2pOpts, libp2p.Peerstore(peerStore))
|
|
|
|
}
|
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
|
|
|
|
2021-10-03 21:45:07 +00:00
|
|
|
if !options.Relay.Disable {
|
|
|
|
var wakurelayopts []pubsub.Option
|
|
|
|
wakurelayopts = append(wakurelayopts, pubsub.WithPeerExchange(options.Relay.PeerExchange))
|
|
|
|
nodeOpts = append(nodeOpts, node.WithWakuRelay(wakurelayopts...))
|
|
|
|
}
|
2021-06-10 12:59:51 +00:00
|
|
|
|
2021-10-03 21:45:07 +00:00
|
|
|
if options.RendezvousServer.Enable {
|
|
|
|
db, err := leveldb.OpenFile(options.RendezvousServer.DBPath, &opt.Options{OpenFilesCacheCapacity: 3})
|
|
|
|
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 {
|
|
|
|
nodeOpts = append(nodeOpts, node.WithWakuFilter())
|
|
|
|
}
|
2021-06-28 14:14:28 +00:00
|
|
|
|
2021-10-03 21:45:07 +00:00
|
|
|
if options.Store.Enable {
|
|
|
|
nodeOpts = append(nodeOpts, node.WithWakuStore(true, true))
|
|
|
|
if options.UseDB {
|
|
|
|
dbStore, err := persistence.NewDBStore(persistence.WithDB(db))
|
|
|
|
failOnErr(err, "DBStore")
|
|
|
|
nodeOpts = append(nodeOpts, node.WithMessageProvider(dbStore))
|
|
|
|
} else {
|
|
|
|
nodeOpts = append(nodeOpts, node.WithMessageProvider(nil))
|
2021-09-30 16:01:53 +00:00
|
|
|
}
|
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 {
|
|
|
|
nodeOpts = append(nodeOpts, node.WithRendezvous(pubsub.WithDiscoveryOpts(libp2pdisc.Limit(45), libp2pdisc.TTL(time.Duration(20)*time.Second))))
|
|
|
|
}
|
2021-03-18 23:21:45 +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
|
|
|
|
2021-10-05 02:13:54 +00:00
|
|
|
addPeers(wakuNode, options.Rendezvous.Nodes, rendezvous.RendezvousID_v001)
|
|
|
|
addPeers(wakuNode, options.Store.Nodes, store.StoreID_v20beta3)
|
|
|
|
addPeers(wakuNode, options.LightPush.Nodes, lightpush.LightPushID_v20beta1)
|
|
|
|
addPeers(wakuNode, options.Filter.Nodes, filter.FilterID_v20beta1)
|
|
|
|
|
2021-10-06 15:42:57 +00:00
|
|
|
if err = wakuNode.Start(); err != nil {
|
|
|
|
log.Fatal(fmt.Errorf("could not start waku node, %w", err))
|
|
|
|
}
|
2021-10-05 02:13:54 +00:00
|
|
|
|
2021-10-03 21:45:07 +00:00
|
|
|
if len(options.Relay.Topics) == 0 {
|
|
|
|
options.Relay.Topics = []string{string(relay.DefaultWakuTopic)}
|
|
|
|
}
|
2021-09-30 16:01:53 +00:00
|
|
|
|
2021-10-03 21:45:07 +00:00
|
|
|
for _, t := range options.Relay.Topics {
|
|
|
|
nodeTopic := relay.Topic(t)
|
|
|
|
_, err := wakuNode.Subscribe(&nodeTopic)
|
|
|
|
failOnErr(err, "Error subscring to topic")
|
|
|
|
}
|
2021-03-18 23:21:45 +00:00
|
|
|
|
2021-10-03 21:45:07 +00:00
|
|
|
for _, n := range options.StaticNodes {
|
|
|
|
go func(node string) {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, time.Duration(3)*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
err = wakuNode.DialPeer(ctx, node)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("error dialing peer ", err)
|
2021-03-18 23:21:45 +00:00
|
|
|
}
|
2021-10-03 21:45:07 +00:00
|
|
|
}(n)
|
|
|
|
}
|
2021-03-18 23:21:45 +00:00
|
|
|
|
2021-10-03 21:45:07 +00:00
|
|
|
if options.DNSDiscovery.Enable {
|
|
|
|
for _, addr := range wakuNode.ListenAddresses() {
|
|
|
|
ip, _ := addr.ValueForProtocol(multiaddr.P_IP4)
|
|
|
|
enr := enode.NewV4(&prvKey.PublicKey, net.ParseIP(ip), hostAddr.Port, 0)
|
|
|
|
log.Info("ENR: ", enr)
|
|
|
|
}
|
2021-09-30 23:03:19 +00:00
|
|
|
|
2021-10-03 21:45:07 +00:00
|
|
|
if options.DNSDiscovery.URL != "" {
|
|
|
|
log.Info("attempting DNS discovery with ", options.DNSDiscovery.URL)
|
|
|
|
multiaddresses, err := discovery.RetrieveNodes(ctx, options.DNSDiscovery.URL, discovery.WithNameserver(options.DNSDiscovery.Nameserver))
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("dns discovery error ", err)
|
2021-09-30 23:03:19 +00:00
|
|
|
} else {
|
2021-10-03 21:45:07 +00:00
|
|
|
log.Info("found dns entries ", 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 {
|
|
|
|
log.Error("error dialing peer ", err)
|
|
|
|
}
|
|
|
|
}(ctx, m)
|
|
|
|
}
|
2021-09-30 23:03:19 +00:00
|
|
|
}
|
2021-10-03 21:45:07 +00:00
|
|
|
} else {
|
|
|
|
log.Fatal("DNS discovery URL is required")
|
2021-09-30 23:03:19 +00:00
|
|
|
}
|
2021-10-03 21:45:07 +00:00
|
|
|
}
|
2021-09-30 23:03:19 +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
|
|
|
|
fmt.Println("\n\n\nReceived 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-10-03 21:45:07 +00:00
|
|
|
if options.Metrics.Enable {
|
|
|
|
metricsServer.Stop(ctx)
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2021-09-30 16:01:53 +00:00
|
|
|
func addPeers(wakuNode *node.WakuNode, addresses []string, protocol protocol.ID) {
|
|
|
|
for _, addrString := range addresses {
|
|
|
|
if addrString == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
addr, err := multiaddr.NewMultiaddr(addrString)
|
2021-10-03 21:45:07 +00:00
|
|
|
failOnErr(err, "invalid multiaddress")
|
2021-09-30 16:01:53 +00:00
|
|
|
|
|
|
|
_, err = wakuNode.AddPeer(addr, protocol)
|
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
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func writePrivateKeyToFile(path string, force bool) error {
|
|
|
|
_, err := os.Stat(path)
|
|
|
|
|
|
|
|
if err == nil && !force {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := crypto.GenerateKey()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
privKey := libp2pcrypto.PrivKey((*libp2pcrypto.Secp256k1PrivateKey)(key))
|
|
|
|
|
|
|
|
b, err := privKey.Raw()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
output := make([]byte, hex.EncodedLen(len(b)))
|
|
|
|
hex.Encode(output, b)
|
|
|
|
|
|
|
|
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 {
|
2021-10-04 22:38:27 +00:00
|
|
|
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 {
|
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 {
|
2021-10-05 02:13:54 +00:00
|
|
|
if prvKey, err = loadPrivateKeyFromFile(options.KeyFile); 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
|
|
|
|
}
|