fix: enodeToMultiaddress when using ipv6

Fixes #560
This commit is contained in:
Richard Ramos 2023-05-30 10:34:34 -04:00 committed by RichΛrd
parent 1c75c89790
commit 1bffa35dfa
3 changed files with 32 additions and 5 deletions

View File

@ -230,12 +230,12 @@ func WithExternalIP(ip net.IP) WakuNodeOption {
params.addressFactory = func(inputAddr []multiaddr.Multiaddr) (addresses []multiaddr.Multiaddr) {
addresses = append(addresses, inputAddr...)
component := "/ip4/"
if ip.To4() == nil && ip.To16() != nil {
component = "/ip6/"
ipType := "/ip4/"
if utils.IsIPv6(ip.String()) {
ipType = "/ip6/"
}
hostAddrMA, err := multiaddr.NewMultiaddr(component + ip.String())
hostAddrMA, err := multiaddr.NewMultiaddr(ipType + ip.String())
if err != nil {
panic("Could not build external IP")
}

View File

@ -57,7 +57,18 @@ func enodeToMultiAddr(node *enode.Node) (multiaddr.Multiaddr, error) {
return nil, err
}
return multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d/p2p/%s", node.IP(), node.TCP(), peerID))
ipType := "ip4"
portNumber := node.TCP()
if utils.IsIPv6(node.IP().String()) {
ipType = "ip6"
var port enr.TCP6
if err := node.Record().Load(&port); err != nil {
return nil, err
}
portNumber = int(port)
}
return multiaddr.NewMultiaddr(fmt.Sprintf("/%s/%s/tcp/%d/p2p/%s", ipType, node.IP(), portNumber, peerID))
}
// Multiaddress is used to extract all the multiaddresses that are part of a ENR record

16
waku/v2/utils/ip.go Normal file
View File

@ -0,0 +1,16 @@
package utils
import (
"net"
"strings"
)
func IsIPv4(str string) bool {
ip := net.ParseIP(str)
return ip != nil && !strings.Contains(str, ":")
}
func IsIPv6(str string) bool {
ip := net.ParseIP(str)
return ip != nil && strings.Contains(str, ":")
}