2022-06-13 18:30:35 +00:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
2023-01-06 22:37:57 +00:00
|
|
|
"context"
|
2022-06-13 18:30:35 +00:00
|
|
|
"errors"
|
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/enr"
|
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
2023-04-20 18:51:13 +00:00
|
|
|
wenr "github.com/waku-org/go-waku/waku/v2/protocol/enr"
|
2022-06-13 18:30:35 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2023-04-20 18:51:13 +00:00
|
|
|
func (w *WakuNode) updateLocalNode(localnode *enode.LocalNode, multiaddrs []ma.Multiaddr, ipAddr *net.TCPAddr, udpPort uint, wakuFlags wenr.WakuEnrBitfield, advertiseAddr []ma.Multiaddr, shouldAutoUpdate bool, log *zap.Logger) error {
|
2023-04-25 16:09:55 +00:00
|
|
|
var options []wenr.ENROption
|
|
|
|
options = append(options, wenr.WithUDPPort(udpPort))
|
|
|
|
options = append(options, wenr.WithWakuBitfield(wakuFlags))
|
|
|
|
options = append(options, wenr.WithMultiaddress(multiaddrs...))
|
2022-06-13 18:30:35 +00:00
|
|
|
|
|
|
|
if advertiseAddr != nil {
|
2023-01-12 02:20:23 +00:00
|
|
|
// An advertised address disables libp2p address updates
|
|
|
|
// and discv5 predictions
|
2023-02-08 16:02:06 +00:00
|
|
|
ipAddr, err := selectMostExternalAddress(advertiseAddr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-25 16:09:55 +00:00
|
|
|
options = append(options, wenr.WithIP(ipAddr))
|
2023-01-12 02:20:23 +00:00
|
|
|
} else if !shouldAutoUpdate {
|
|
|
|
// We received a libp2p address update. Autoupdate is disabled
|
|
|
|
// Using a static ip will disable endpoint prediction.
|
2023-04-25 16:09:55 +00:00
|
|
|
options = append(options, wenr.WithIP(ipAddr))
|
2023-01-12 02:20:23 +00:00
|
|
|
} else {
|
|
|
|
// We received a libp2p address update, but we should still
|
|
|
|
// allow discv5 to update the enr record. We set the localnode
|
|
|
|
// keys manually. It's possible that the ENR record might get
|
|
|
|
// updated automatically
|
|
|
|
ip4 := ipAddr.IP.To4()
|
|
|
|
ip6 := ipAddr.IP.To16()
|
|
|
|
if ip4 != nil && !ip4.IsUnspecified() {
|
|
|
|
localnode.Set(enr.IPv4(ip4))
|
|
|
|
localnode.Set(enr.TCP(uint16(ipAddr.Port)))
|
|
|
|
} else {
|
|
|
|
localnode.Delete(enr.IPv4{})
|
|
|
|
localnode.Delete(enr.TCP(0))
|
|
|
|
}
|
|
|
|
|
2023-02-23 23:43:04 +00:00
|
|
|
if ip4 == nil && ip6 != nil && !ip6.IsUnspecified() {
|
2023-01-12 02:20:23 +00:00
|
|
|
localnode.Set(enr.IPv6(ip6))
|
|
|
|
localnode.Set(enr.TCP6(ipAddr.Port))
|
|
|
|
} else {
|
|
|
|
localnode.Delete(enr.IPv6{})
|
|
|
|
localnode.Delete(enr.TCP6(0))
|
|
|
|
}
|
2022-06-13 18:30:35 +00:00
|
|
|
}
|
|
|
|
|
2023-04-25 16:09:55 +00:00
|
|
|
return wenr.Update(localnode, options...)
|
2022-06-13 18:30:35 +00:00
|
|
|
}
|
|
|
|
|
2023-01-27 20:33:37 +00:00
|
|
|
func isPrivate(addr *net.TCPAddr) bool {
|
|
|
|
return addr.IP.IsPrivate()
|
2022-06-13 18:30:35 +00:00
|
|
|
}
|
|
|
|
|
2023-01-27 20:33:37 +00:00
|
|
|
func isExternal(addr *net.TCPAddr) bool {
|
|
|
|
return !isPrivate(addr) && !addr.IP.IsLoopback() && !addr.IP.IsUnspecified()
|
2022-06-13 18:30:35 +00:00
|
|
|
}
|
|
|
|
|
2023-01-27 20:33:37 +00:00
|
|
|
func isLoopback(addr *net.TCPAddr) bool {
|
|
|
|
return addr.IP.IsLoopback()
|
2022-06-13 18:30:35 +00:00
|
|
|
}
|
|
|
|
|
2023-01-27 20:33:37 +00:00
|
|
|
func filterIP(ss []*net.TCPAddr, fn func(*net.TCPAddr) bool) (ret []*net.TCPAddr) {
|
2022-06-13 18:30:35 +00:00
|
|
|
for _, s := range ss {
|
|
|
|
if fn(s) {
|
|
|
|
ret = append(ret, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-27 20:33:37 +00:00
|
|
|
func extractIPAddressForENR(addr ma.Multiaddr) (*net.TCPAddr, error) {
|
|
|
|
// It's a p2p-circuit address. We shouldnt use these
|
|
|
|
// for building the ENR record default keys
|
|
|
|
_, err := addr.ValueForProtocol(ma.P_CIRCUIT)
|
|
|
|
if err == nil {
|
|
|
|
return nil, errors.New("can't use IP address from a p2p-circuit address")
|
|
|
|
}
|
|
|
|
|
|
|
|
// ws and wss addresses are handled by the multiaddr key
|
|
|
|
// they shouldnt be used for building the ENR record default keys
|
|
|
|
_, err = addr.ValueForProtocol(ma.P_WS)
|
|
|
|
if err == nil {
|
|
|
|
return nil, errors.New("can't use IP address from a ws address")
|
|
|
|
}
|
|
|
|
_, err = addr.ValueForProtocol(ma.P_WSS)
|
|
|
|
if err == nil {
|
|
|
|
return nil, errors.New("can't use IP address from a wss address")
|
|
|
|
}
|
2022-06-13 18:30:35 +00:00
|
|
|
|
|
|
|
var ipStr string
|
|
|
|
dns4, err := addr.ValueForProtocol(ma.P_DNS4)
|
|
|
|
if err != nil {
|
|
|
|
ipStr, err = addr.ValueForProtocol(ma.P_IP4)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
netIP, err := net.ResolveIPAddr("ip4", dns4)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ipStr = netIP.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
portStr, err := addr.ValueForProtocol(ma.P_TCP)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
port, err := strconv.Atoi(portStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &net.TCPAddr{
|
|
|
|
IP: net.ParseIP(ipStr),
|
|
|
|
Port: port,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-01-27 20:33:37 +00:00
|
|
|
func selectMostExternalAddress(addresses []ma.Multiaddr) (*net.TCPAddr, error) {
|
|
|
|
var ipAddrs []*net.TCPAddr
|
2022-06-13 18:30:35 +00:00
|
|
|
for _, addr := range addresses {
|
2023-01-27 20:33:37 +00:00
|
|
|
ipAddr, err := extractIPAddressForENR(addr)
|
2022-06-13 18:30:35 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2023-01-27 20:33:37 +00:00
|
|
|
ipAddrs = append(ipAddrs, ipAddr)
|
2022-06-13 18:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
externalIPs := filterIP(ipAddrs, isExternal)
|
|
|
|
if len(externalIPs) > 0 {
|
2023-01-27 20:33:37 +00:00
|
|
|
return externalIPs[0], nil
|
2022-06-13 18:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
privateIPs := filterIP(ipAddrs, isPrivate)
|
|
|
|
if len(privateIPs) > 0 {
|
2023-01-27 20:33:37 +00:00
|
|
|
return privateIPs[0], nil
|
2022-06-13 18:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
loopback := filterIP(ipAddrs, isLoopback)
|
|
|
|
if len(loopback) > 0 {
|
2023-01-27 20:33:37 +00:00
|
|
|
return loopback[0], nil
|
2022-06-13 18:30:35 +00:00
|
|
|
}
|
|
|
|
|
2023-01-27 20:33:37 +00:00
|
|
|
return nil, errors.New("could not obtain ip address")
|
2022-06-13 18:30:35 +00:00
|
|
|
}
|
|
|
|
|
2023-01-27 20:33:37 +00:00
|
|
|
func decapsulateP2P(addr ma.Multiaddr) (ma.Multiaddr, error) {
|
|
|
|
p2p, err := addr.ValueForProtocol(ma.P_P2P)
|
2022-06-13 18:30:35 +00:00
|
|
|
if err != nil {
|
2023-01-27 20:33:37 +00:00
|
|
|
return nil, err
|
2022-06-13 18:30:35 +00:00
|
|
|
}
|
|
|
|
|
2023-01-27 20:33:37 +00:00
|
|
|
p2pAddr, err := ma.NewMultiaddr("/p2p/" + p2p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
addr = addr.Decapsulate(p2pAddr)
|
|
|
|
|
|
|
|
return addr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func decapsulateCircuitRelayAddr(addr ma.Multiaddr) (ma.Multiaddr, error) {
|
|
|
|
_, err := addr.ValueForProtocol(ma.P_CIRCUIT)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("not a circuit relay address")
|
|
|
|
}
|
|
|
|
|
|
|
|
// We remove the node's multiaddress from the addr
|
|
|
|
addr, _ = ma.SplitFunc(addr, func(c ma.Component) bool {
|
|
|
|
return c.Protocol().Code == ma.P_CIRCUIT
|
|
|
|
})
|
|
|
|
|
|
|
|
return addr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func selectWSSListenAddresses(addresses []ma.Multiaddr) ([]ma.Multiaddr, error) {
|
2022-06-13 18:30:35 +00:00
|
|
|
var result []ma.Multiaddr
|
|
|
|
for _, addr := range addresses {
|
2023-01-27 20:33:37 +00:00
|
|
|
// It's a p2p-circuit address. We dont use these at this stage yet
|
|
|
|
_, err := addr.ValueForProtocol(ma.P_CIRCUIT)
|
|
|
|
if err == nil {
|
|
|
|
continue
|
2022-06-13 18:30:35 +00:00
|
|
|
}
|
|
|
|
|
2023-01-27 20:33:37 +00:00
|
|
|
// Only WSS with a domain name are allowed
|
|
|
|
_, err = addr.ValueForProtocol(ma.P_DNS4)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
2022-06-13 18:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = addr.ValueForProtocol(ma.P_WSS)
|
2023-01-27 20:33:37 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
addr, err = decapsulateP2P(addr)
|
2022-06-13 18:30:35 +00:00
|
|
|
if err == nil {
|
|
|
|
result = append(result, addr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2023-01-27 20:33:37 +00:00
|
|
|
func selectCircuitRelayListenAddresses(addresses []ma.Multiaddr) ([]ma.Multiaddr, error) {
|
|
|
|
var result []ma.Multiaddr
|
|
|
|
for _, addr := range addresses {
|
|
|
|
addr, err := decapsulateCircuitRelayAddr(addr)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
result = append(result, addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WakuNode) getENRAddresses(addrs []ma.Multiaddr) (extAddr *net.TCPAddr, multiaddr []ma.Multiaddr, err error) {
|
|
|
|
|
|
|
|
extAddr, err = selectMostExternalAddress(addrs)
|
2022-06-13 18:30:35 +00:00
|
|
|
if err != nil {
|
2023-01-27 20:33:37 +00:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
wssAddrs, err := selectWSSListenAddresses(addrs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
multiaddr = append(multiaddr, wssAddrs...)
|
2023-02-01 17:37:47 +00:00
|
|
|
|
|
|
|
// to use WSS, you should have a valid certificate with a domain name.
|
|
|
|
// that means you're reachable. So circuit relay addresses are ignored
|
|
|
|
if len(wssAddrs) == 0 {
|
|
|
|
circuitAddrs, err := selectCircuitRelayListenAddresses(addrs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
multiaddr = append(multiaddr, circuitAddrs...)
|
|
|
|
}
|
2023-01-27 20:33:37 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WakuNode) setupENR(ctx context.Context, addrs []ma.Multiaddr) error {
|
|
|
|
ipAddr, multiaddresses, err := w.getENRAddresses(addrs)
|
2022-06-13 18:30:35 +00:00
|
|
|
if err != nil {
|
2023-01-27 20:33:37 +00:00
|
|
|
w.log.Error("obtaining external address", zap.Error(err))
|
2022-06-13 18:30:35 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-08 16:02:06 +00:00
|
|
|
err = w.updateLocalNode(w.localNode, multiaddresses, ipAddr, w.opts.udpPort, w.wakuFlag, w.opts.advertiseAddrs, w.opts.discV5autoUpdate, w.log)
|
2023-01-12 02:20:23 +00:00
|
|
|
if err != nil {
|
2023-01-27 20:33:37 +00:00
|
|
|
w.log.Error("updating localnode ENR record", zap.Error(err))
|
2023-01-12 02:20:23 +00:00
|
|
|
return err
|
2022-06-13 18:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|