swarm/network: Use net.ParseIP to check for loopback address

This commit is contained in:
lash 2019-03-01 13:04:24 +01:00
parent 407f429723
commit 98ad48ae29

View file

@ -17,7 +17,6 @@
package network package network
import ( import (
"bytes"
"context" "context"
"errors" "errors"
"fmt" "fmt"
@ -232,10 +231,13 @@ func sanitizeEnodeRemote(paddr net.Addr, baddr *BzzAddr) {
ip, _, err := net.SplitHostPort(paddr.String()) ip, _, err := net.SplitHostPort(paddr.String())
if len(hsSubmatch) < 2 { if len(hsSubmatch) < 2 {
log.Warn("sanitize found non ipv4 string", "remotestring", paddr.String(), "handshakeaddr", baddr) log.Warn("sanitize found non ipv4 string", "remotestring", paddr.String(), "handshakeaddr", baddr)
} else if err == nil && bytes.Equal(hsSubmatch[1], []byte("127.0.0.1")) { } else if err == nil {
hsip := net.ParseIP(string(hsSubmatch[1]))
if hsip != nil && hsip.IsLoopback() {
remoteStr := fmt.Sprintf("@%s:%s", ip, string(hsSubmatch[2])) remoteStr := fmt.Sprintf("@%s:%s", ip, string(hsSubmatch[2]))
log.Debug("rewrote peer uaddr host/port", "addr", baddr) log.Debug("rewrote peer uaddr host/port", "addr", baddr)
baddr.UAddr = regexpEnodeIP.ReplaceAll(baddr.UAddr, []byte(remoteStr)) baddr.UAddr = regexpEnodeIP.ReplaceAll(baddr.UAddr, []byte(remoteStr))
}
} else { } else {
log.Trace("passthrough handshake addr rewrite", "submatch", hsSubmatch[1]) log.Trace("passthrough handshake addr rewrite", "submatch", hsSubmatch[1])
} }