From f978071043167040f13839bd5079747a97232b24 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Thu, 22 Apr 2021 09:07:22 -0400 Subject: [PATCH] add websocket support --- waku/node.go | 12 +++++++++++- waku/v2/node/wakuoptions.go | 9 ++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/waku/node.go b/waku/node.go index c9ec4344..ce6c7025 100644 --- a/waku/node.go +++ b/waku/node.go @@ -17,6 +17,7 @@ import ( logging "github.com/ipfs/go-log" "github.com/libp2p/go-libp2p" "github.com/libp2p/go-libp2p-peerstore/pstoreds" + "github.com/multiformats/go-multiaddr" "github.com/spf13/cobra" "github.com/spf13/viper" "github.com/status-im/go-waku/waku/persistence" @@ -52,6 +53,8 @@ var rootCmd = &cobra.Command{ // has an action associated with it: Run: func(cmd *cobra.Command, args []string) { port, _ := cmd.Flags().GetInt("port") + enableWs, _ := cmd.Flags().GetBool("ws") + wsPort, _ := cmd.Flags().GetInt("ws-port") relay, _ := cmd.Flags().GetBool("relay") key, _ := cmd.Flags().GetString("nodekey") store, _ := cmd.Flags().GetBool("store") @@ -90,6 +93,11 @@ var rootCmd = &cobra.Command{ node.WithHostAddress([]net.Addr{hostAddr}), } + if enableWs { + wsMa, _ := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/0.0.0.0/tcp/%d/ws", wsPort)) + nodeOpts = append(nodeOpts, node.WithMultiaddress([]multiaddr.Multiaddr{wsMa})) + } + libp2pOpts := node.DefaultLibP2POptions if useDB { @@ -174,14 +182,16 @@ func init() { cobra.OnInitialize(initConfig) rootCmd.Flags().Int("port", 9000, "Libp2p TCP listening port (0 for random)") + rootCmd.Flags().Bool("ws", false, "Enable websockets support") + rootCmd.Flags().Int("ws-port", 9001, "Libp2p TCP listening port for websocket connection (0 for random)") rootCmd.Flags().String("nodekey", "", "P2P node private key as hex (default random)") rootCmd.Flags().StringSlice("topics", []string{string(node.DefaultWakuTopic)}, fmt.Sprintf("List of topics to listen (default %s)", node.DefaultWakuTopic)) rootCmd.Flags().StringSlice("staticnodes", []string{}, "Multiaddr of peer to directly connect with. Argument may be repeated") + rootCmd.Flags().Bool("relay", true, "Enable relay protocol") rootCmd.Flags().Bool("store", false, "Enable store protocol") rootCmd.Flags().Bool("use-db", true, "Store messages and peers in a DB, (default: true, use false for in-memory only)") rootCmd.Flags().String("dbpath", "./store.db", "Path to DB file") rootCmd.Flags().String("storenode", "", "Multiaddr of peer to connect with for waku store protocol") - rootCmd.Flags().Bool("relay", true, "Enable relay protocol") } diff --git a/waku/v2/node/wakuoptions.go b/waku/v2/node/wakuoptions.go index b3ba45e3..3b299ac0 100644 --- a/waku/v2/node/wakuoptions.go +++ b/waku/v2/node/wakuoptions.go @@ -39,12 +39,19 @@ func WithHostAddress(hostAddr []net.Addr) WakuNodeOption { multiAddresses = append(multiAddresses, hostAddrMA) } - params.multiAddr = multiAddresses + params.multiAddr = append(params.multiAddr, multiAddresses...) return nil } } +func WithMultiaddress(addresses []ma.Multiaddr) WakuNodeOption { + return func(params *WakuNodeParameters) error { + params.multiAddr = append(params.multiAddr, addresses...) + return nil + } +} + func WithPrivateKey(privKey *ecdsa.PrivateKey) WakuNodeOption { return func(params *WakuNodeParameters) error { privk := crypto.PrivKey((*crypto.Secp256k1PrivateKey)(privKey))