feat: ext-ip

This commit is contained in:
Richard Ramos 2023-05-08 11:19:44 -04:00 committed by RichΛrd
parent 38741caca0
commit 9b7ad40b06
5 changed files with 46 additions and 3 deletions

View File

@ -147,9 +147,16 @@ var (
Destination: &options.NAT, // TODO: accept none,any,upnp,extaddr Destination: &options.NAT, // TODO: accept none,any,upnp,extaddr
EnvVars: []string{"WAKUNODE2_NAT"}, EnvVars: []string{"WAKUNODE2_NAT"},
}) })
AdvertiseAddress = cliutils.NewGenericFlagMultiValue(&cli.GenericFlag{ IPAddress = altsrc.NewStringFlag(&cli.StringFlag{
Name: "ext-ip", // This was added so js-waku test don't fail
Usage: "Set external IP address",
Value: "",
Destination: &options.ExtIP,
EnvVars: []string{"WAKUNODE2_EXT_IP"},
})
ExtMultiaddresses = cliutils.NewGenericFlagMultiValue(&cli.GenericFlag{
Name: "ext-multiaddr", Name: "ext-multiaddr",
Usage: "External address to advertise to other nodes. Ooverrides --address and --ws-address flags. Option may be repeated", Usage: "External address to advertise to other nodes. Overrides --address and --ws-address flags. Option may be repeated",
Value: &cliutils.MultiaddrSlice{ Value: &cliutils.MultiaddrSlice{
Values: &options.AdvertiseAddresses, Values: &options.AdvertiseAddresses,
}, },

View File

@ -37,7 +37,8 @@ func main() {
KeepAlive, KeepAlive,
PersistPeers, PersistPeers,
NAT, NAT,
AdvertiseAddress, IPAddress,
ExtMultiaddresses,
ShowAddresses, ShowAddresses,
LogLevel, LogLevel,
LogEncoding, LogEncoding,

View File

@ -5,6 +5,7 @@ import (
"crypto/ecdsa" "crypto/ecdsa"
"database/sql" "database/sql"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"net" "net"
"os" "os"
@ -119,6 +120,14 @@ func Execute(options Options) {
nodeOpts = append(nodeOpts, node.WithDns4Domain(options.Dns4DomainName)) nodeOpts = append(nodeOpts, node.WithDns4Domain(options.Dns4DomainName))
} }
if options.ExtIP != "" {
ip := net.ParseIP(options.ExtIP)
if ip == nil {
failOnErr(errors.New("invalid IP address"), "could not set external IP address")
}
nodeOpts = append(nodeOpts, node.WithExternalIP(ip))
}
libp2pOpts := node.DefaultLibP2POptions libp2pOpts := node.DefaultLibP2POptions
if len(options.AdvertiseAddresses) == 0 { if len(options.AdvertiseAddresses) == 0 {
libp2pOpts = append(libp2pOpts, libp2p.NATPortMap()) // Attempt to open ports using uPNP for NATed hosts.) libp2pOpts = append(libp2pOpts, libp2p.NATPortMap()) // Attempt to open ports using uPNP for NATed hosts.)

View File

@ -159,6 +159,7 @@ type Options struct {
LogEncoding string LogEncoding string
LogOutput string LogOutput string
NAT string NAT string
ExtIP string
PersistPeers bool PersistPeers bool
UserAgent string UserAgent string
PProf bool PProf bool

View File

@ -220,6 +220,31 @@ func WithAdvertiseAddresses(advertiseAddrs ...ma.Multiaddr) WakuNodeOption {
} }
} }
// WithExternalIP is a WakuNodeOption that allows overriding the advertised external IP used in the waku node with custom value
func WithExternalIP(ip net.IP) WakuNodeOption {
return func(params *WakuNodeParameters) error {
params.addressFactory = func(inputAddr []multiaddr.Multiaddr) (addresses []multiaddr.Multiaddr) {
component := "/ip4/"
if ip.To4() == nil && ip.To16() != nil {
component = "/ip6/"
}
hostAddrMA, err := multiaddr.NewMultiaddr(component + ip.String())
if err != nil {
panic("Could not build external IP")
}
for _, addr := range inputAddr {
_, rest := multiaddr.SplitFirst(addr)
addresses = append(addresses, hostAddrMA.Encapsulate(rest))
}
return addresses
}
return nil
}
}
// WithMultiaddress is a WakuNodeOption that configures libp2p to listen on a list of multiaddresses // WithMultiaddress is a WakuNodeOption that configures libp2p to listen on a list of multiaddresses
func WithMultiaddress(addresses []multiaddr.Multiaddr) WakuNodeOption { func WithMultiaddress(addresses []multiaddr.Multiaddr) WakuNodeOption {
return func(params *WakuNodeParameters) error { return func(params *WakuNodeParameters) error {