fix: port on discv5 (#224)

This commit is contained in:
Richard Ramos 2022-03-31 21:17:14 -04:00
parent a20a128fb7
commit b294ee6f6f
No known key found for this signature in database
GPG Key ID: BD36D48BC9FFC88C
2 changed files with 42 additions and 15 deletions

View File

@ -43,6 +43,12 @@ type DiscoveryV5 struct {
wg *sync.WaitGroup wg *sync.WaitGroup
peerCache peerCache peerCache peerCache
// Used for those weird cases where updateAddress
// receives the same external address twice both with the original port
// and the nat port. Ideally this atribute should be removed by doing
// hole punching before starting waku
ogTCPPort int
} }
type peerCache struct { type peerCache struct {
@ -146,6 +152,7 @@ func NewDiscoveryV5(host host.Host, addresses []ma.Multiaddr, priv *ecdsa.Privat
Port: params.udpPort, Port: params.udpPort,
}, },
log: logger, log: logger,
ogTCPPort: ipAddr.Port,
}, nil }, nil
} }
@ -241,7 +248,7 @@ func (d *DiscoveryV5) Stop() {
d.wg.Wait() d.wg.Wait()
} }
func (d *DiscoveryV5) UpdateAddr(addr net.IP) error { func (d *DiscoveryV5) UpdateAddr(addr *net.TCPAddr) error {
if !d.params.autoUpdate { if !d.params.autoUpdate {
return nil return nil
} }
@ -249,18 +256,21 @@ func (d *DiscoveryV5) UpdateAddr(addr net.IP) error {
d.Lock() d.Lock()
defer d.Unlock() defer d.Unlock()
if addr.IsUnspecified() || d.localnode.Node().IP().Equal(addr) { // TODO: This code is not elegant and should be improved
if !isExternal(addr) && !isExternal(&net.TCPAddr{IP: d.localnode.Node().IP()}) {
if !((d.localnode.Node().IP().IsLoopback() && isPrivate(addr)) || (isPrivate(&net.TCPAddr{IP: d.localnode.Node().IP()}) && isExternal(addr))) {
return nil
}
}
if addr.IP.IsUnspecified() || (d.localnode.Node().IP().Equal(addr.IP) && addr.Port == d.ogTCPPort) {
return nil return nil
} }
// TODO: improve this logic to determine if an address should be replaced or not d.localnode.SetStaticIP(addr.IP)
if !((d.localnode.Node().IP().IsLoopback() && isPrivate(&net.TCPAddr{IP: addr})) || d.localnode.Set(enr.TCP(uint16(addr.Port))) // lgtm [go/incorrect-integer-conversion]
(isPrivate(&net.TCPAddr{IP: d.localnode.Node().IP()}) && isExternal(&net.TCPAddr{IP: addr}))) { d.log.Info(fmt.Sprintf("Updated Discovery V5 node: %s:%d", d.localnode.Node().IP(), d.localnode.Node().TCP()))
return nil
}
d.localnode.SetStaticIP(addr)
d.log.Info(fmt.Sprintf("Updated Discovery V5 node IP: %s", d.localnode.Node().IP()))
d.log.Info("Discovery V5 ", d.localnode.Node()) d.log.Info("Discovery V5 ", d.localnode.Node())
return nil return nil

View File

@ -5,6 +5,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"net" "net"
"strconv"
"sync" "sync"
"time" "time"
@ -195,13 +196,29 @@ func (w *WakuNode) onAddrChange() {
w.log.Error(fmt.Sprintf("could not extract ip from ma %s: %s", m, err.Error())) w.log.Error(fmt.Sprintf("could not extract ip from ma %s: %s", m, err.Error()))
continue continue
} }
ip := net.ParseIP(ipStr)
if !ip.IsLoopback() && !ip.IsUnspecified() { portStr, err := m.ValueForProtocol(ma.P_TCP)
if w.opts.enableDiscV5 {
err := w.discoveryV5.UpdateAddr(ip)
if err != nil { if err != nil {
w.log.Error(fmt.Sprintf("could not update DiscV5 address with IP %s: %s", ip, err.Error())) w.log.Error(fmt.Sprintf("could not extract port from ma %s: %s", m, err.Error()))
continue
}
port, err := strconv.Atoi(portStr)
if err != nil {
w.log.Error(fmt.Sprintf("could not convert port to int: %s", err.Error()))
continue
}
addr := &net.TCPAddr{
IP: net.ParseIP(ipStr),
Port: port,
}
if !addr.IP.IsLoopback() && !addr.IP.IsUnspecified() {
if w.opts.enableDiscV5 {
err := w.discoveryV5.UpdateAddr(addr)
if err != nil {
w.log.Error(fmt.Sprintf("could not update DiscV5 address with IP %s:%d %s", addr.IP, addr.Port, err.Error()))
continue continue
} }
} }