mirror of https://github.com/status-im/go-waku.git
fix: add --show-addresses and allow setting the listening address
This commit is contained in:
parent
54a93a60c5
commit
8c590851e3
31
waku/node.go
31
waku/node.go
|
@ -61,7 +61,7 @@ func Execute(options Options) {
|
|||
return
|
||||
}
|
||||
|
||||
hostAddr, _ := net.ResolveTCPAddr("tcp", fmt.Sprint("0.0.0.0:", options.Port))
|
||||
hostAddr, _ := net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", options.Address, options.Port))
|
||||
|
||||
var err error
|
||||
|
||||
|
@ -94,10 +94,15 @@ func Execute(options Options) {
|
|||
}
|
||||
|
||||
if options.EnableWS {
|
||||
wsMa, _ := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/0.0.0.0/tcp/%d/ws", options.WSPort))
|
||||
wsMa, _ := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d/ws", options.WSAddress, options.WSPort))
|
||||
nodeOpts = append(nodeOpts, node.WithMultiaddress([]multiaddr.Multiaddr{wsMa}))
|
||||
}
|
||||
|
||||
if options.ShowAddresses {
|
||||
printListeningAddresses(ctx, nodeOpts)
|
||||
return
|
||||
}
|
||||
|
||||
libp2pOpts := node.DefaultLibP2POptions
|
||||
|
||||
if options.UseDB {
|
||||
|
@ -330,3 +335,25 @@ func getPrivKey(options Options) (*ecdsa.PrivateKey, error) {
|
|||
}
|
||||
return prvKey, nil
|
||||
}
|
||||
|
||||
func printListeningAddresses(ctx context.Context, nodeOpts []node.WakuNodeOption) {
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
params := new(node.WakuNodeParameters)
|
||||
for _, opt := range nodeOpts {
|
||||
err := opt(params)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
h, err := libp2p.New(ctx, libp2p.ListenAddrs(params.MultiAddresses()...))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
hostInfo, _ := multiaddr.NewMultiaddr(fmt.Sprintf("/p2p/%s", h.ID().Pretty()))
|
||||
for _, addr := range h.Addrs() {
|
||||
fmt.Println(addr.Encapsulate(hostInfo))
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -60,18 +60,21 @@ type MetricsOptions struct {
|
|||
// Options contains all the available features and settings that can be
|
||||
// configured via flags when executing go-waku as a service.
|
||||
type Options struct {
|
||||
Port int `short:"p" long:"port" description:"Libp2p TCP listening port (0 for random)" default:"9000"`
|
||||
EnableWS bool `long:"ws" description:"Enable websockets support"`
|
||||
WSPort int `long:"ws-port" description:"Libp2p TCP listening port for websocket connection (0 for random)" default:"9001"`
|
||||
NodeKey string `long:"nodekey" description:"P2P node private key as hex. Can also be set with GOWAKU-NODEKEY env variable (default random)"`
|
||||
KeyFile string `long:"key-file" description:"Path to a file containing the private key for the P2P node" default:"./nodekey"`
|
||||
GenerateKey bool `long:"generate-key" description:"Generate private key file at path specified in --key-file"`
|
||||
Overwrite bool `long:"overwrite" description:"When generating a keyfile, overwrite the nodekey file if it already exists"`
|
||||
StaticNodes []string `long:"static-node" description:"Multiaddr of peer to directly connect with. Option may be repeated"`
|
||||
KeepAlive int `long:"keep-alive" default:"20" description:"Interval in seconds for pinging peers to keep the connection alive."`
|
||||
UseDB bool `long:"use-db" description:"Use SQLiteDB to persist information"`
|
||||
DBPath string `long:"dbpath" default:"./store.db" description:"Path to DB file"`
|
||||
LogLevel string `short:"l" long:"log-level" description:"Define the logging level, supported strings are: DEBUG, INFO, WARN, ERROR, DPANIC, PANIC, FATAL, and their lower-case forms." default:"INFO"`
|
||||
Port int `short:"p" long:"port" description:"Libp2p TCP listening port (0 for random)" default:"9000"`
|
||||
Address string `long:"address" description:"Listening address" default:"0.0.0.0"`
|
||||
EnableWS bool `long:"ws" description:"Enable websockets support"`
|
||||
WSPort int `long:"ws-port" description:"Libp2p TCP listening port for websocket connection (0 for random)" default:"9001"`
|
||||
WSAddress string `long:"ws-address" description:"Listening address for websocket connections" default:"0.0.0.0"`
|
||||
NodeKey string `long:"nodekey" description:"P2P node private key as hex. Can also be set with GOWAKU-NODEKEY env variable (default random)"`
|
||||
KeyFile string `long:"key-file" description:"Path to a file containing the private key for the P2P node" default:"./nodekey"`
|
||||
GenerateKey bool `long:"generate-key" description:"Generate private key file at path specified in --key-file"`
|
||||
Overwrite bool `long:"overwrite" description:"When generating a keyfile, overwrite the nodekey file if it already exists"`
|
||||
StaticNodes []string `long:"static-node" description:"Multiaddr of peer to directly connect with. Option may be repeated"`
|
||||
KeepAlive int `long:"keep-alive" default:"20" description:"Interval in seconds for pinging peers to keep the connection alive."`
|
||||
UseDB bool `long:"use-db" description:"Use SQLiteDB to persist information"`
|
||||
DBPath string `long:"dbpath" default:"./store.db" description:"Path to DB file"`
|
||||
ShowAddresses bool `long:"show-addresses" description:"Display listening addresses according to current configuration"`
|
||||
LogLevel string `short:"l" long:"log-level" description:"Define the logging level, supported strings are: DEBUG, INFO, WARN, ERROR, DPANIC, PANIC, FATAL, and their lower-case forms." default:"INFO"`
|
||||
|
||||
Relay RelayOptions `group:"Relay Options"`
|
||||
Store StoreOptions `group:"Store Options"`
|
||||
|
|
|
@ -47,6 +47,10 @@ type WakuNodeParameters struct {
|
|||
|
||||
type WakuNodeOption func(*WakuNodeParameters) error
|
||||
|
||||
func (w WakuNodeParameters) MultiAddresses() []ma.Multiaddr {
|
||||
return w.multiAddr
|
||||
}
|
||||
|
||||
// WithHostAddress is a WakuNodeOption that configures libp2p to listen on a list of net endpoint addresses
|
||||
func WithHostAddress(hostAddr []net.Addr) WakuNodeOption {
|
||||
return func(params *WakuNodeParameters) error {
|
||||
|
|
Loading…
Reference in New Issue