swarm/network: Use enode package for enode rewrite instead of regex

This commit is contained in:
lash 2019-03-01 13:51:59 +01:00
parent 98ad48ae29
commit e489d77d4b
2 changed files with 9 additions and 27 deletions

View file

@ -21,7 +21,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"net" "net"
"regexp"
"sync" "sync"
"time" "time"
@ -40,8 +39,6 @@ const (
bzzHandshakeTimeout = 3000 * time.Millisecond bzzHandshakeTimeout = 3000 * time.Millisecond
) )
var regexpEnodeIP = regexp.MustCompile("@(.+):([0-9]+)")
// BzzSpec is the spec of the generic swarm handshake // BzzSpec is the spec of the generic swarm handshake
var BzzSpec = &protocols.Spec{ var BzzSpec = &protocols.Spec{
Name: "bzz", Name: "bzz",
@ -217,7 +214,7 @@ func (b *Bzz) performHandshake(p *protocols.Peer, handshake *HandshakeMsg) error
return err return err
} }
handshake.peerAddr = rsh.(*HandshakeMsg).Addr handshake.peerAddr = rsh.(*HandshakeMsg).Addr
sanitizeEnodeRemote(p.RemoteAddr(), handshake.peerAddr) sanitizeEnodeRemote(p.Node(), handshake.peerAddr)
handshake.LightNode = rsh.(*HandshakeMsg).LightNode handshake.LightNode = rsh.(*HandshakeMsg).LightNode
return nil return nil
} }
@ -226,20 +223,12 @@ func (b *Bzz) performHandshake(p *protocols.Peer, handshake *HandshakeMsg) error
// this method ensures that if this default is used in a networked environment, we replace // this method ensures that if this default is used in a networked environment, we replace
// the ip with the one applicable on the interface the connection came in on // the ip with the one applicable on the interface the connection came in on
// it modifies the passed bzzaddr in place, and returns the same pointer // it modifies the passed bzzaddr in place, and returns the same pointer
func sanitizeEnodeRemote(paddr net.Addr, baddr *BzzAddr) { func sanitizeEnodeRemote(paddr *enode.Node, baddr *BzzAddr) {
hsSubmatch := regexpEnodeIP.FindSubmatch(baddr.UAddr) enod, err := enode.ParseV4(string(baddr.UAddr))
ip, _, err := net.SplitHostPort(paddr.String()) if err == nil {
if len(hsSubmatch) < 2 { if enod.IP().IsLoopback() {
log.Warn("sanitize found non ipv4 string", "remotestring", paddr.String(), "handshakeaddr", baddr) baddr.UAddr = []byte(paddr.String())
} else if err == nil {
hsip := net.ParseIP(string(hsSubmatch[1]))
if hsip != nil && hsip.IsLoopback() {
remoteStr := fmt.Sprintf("@%s:%s", ip, string(hsSubmatch[2]))
log.Debug("rewrote peer uaddr host/port", "addr", baddr)
baddr.UAddr = regexpEnodeIP.ReplaceAll(baddr.UAddr, []byte(remoteStr))
} }
} else {
log.Trace("passthrough handshake addr rewrite", "submatch", hsSubmatch[1])
} }
} }

View file

@ -263,26 +263,19 @@ func TestSanitizeEnodeRemote(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
remoteIP := net.IPv4(0x80, 0x40, 0x20, 0x10) remoteIP := net.IPv4(0x80, 0x40, 0x20, 0x10)
remoteAddr := net.TCPAddr{
IP: remoteIP,
Port: 30399,
}
nodLocal := enode.NewV4(&pk.PublicKey, net.IPv4(0x7f, 0x00, 0x00, 0x01), 30341, 30341) nodLocal := enode.NewV4(&pk.PublicKey, net.IPv4(0x7f, 0x00, 0x00, 0x01), 30341, 30341)
nodRemote := enode.NewV4(&pk.PublicKey, remoteIP, 30341, 30341) nodRemote := enode.NewV4(&pk.PublicKey, remoteIP, 30341, 30341)
baddr := RandomAddr() baddr := RandomAddr()
oldUAddr := []byte(nodLocal.String()) oldUAddr := []byte(nodLocal.String())
baddr.UAddr = oldUAddr baddr.UAddr = oldUAddr
sanitizeEnodeRemote(&remoteAddr, baddr) sanitizeEnodeRemote(nodRemote, baddr)
if !bytes.Equal(baddr.UAddr, []byte(nodRemote.String())) { if !bytes.Equal(baddr.UAddr, []byte(nodRemote.String())) {
t.Fatalf("insane address. expected %v, got %v", nodRemote.String(), string(baddr.UAddr)) t.Fatalf("insane address. expected %v, got %v", nodRemote.String(), string(baddr.UAddr))
} }
remoteIP = net.IPv4(0x04, 0x04, 0x04, 0x04) remoteIP = net.IPv4(0x04, 0x04, 0x04, 0x04)
remoteAddr = net.TCPAddr{ nodRemoteTwo := enode.NewV4(&pk.PublicKey, remoteIP, 30341, 30341)
IP: remoteIP, sanitizeEnodeRemote(nodRemoteTwo, baddr)
Port: 30399,
}
sanitizeEnodeRemote(&remoteAddr, baddr)
if !bytes.Equal(baddr.UAddr, []byte(nodRemote.String())) { if !bytes.Equal(baddr.UAddr, []byte(nodRemote.String())) {
t.Fatalf("Should not have rewritten non-localhost string. expected %v, got %v", nodRemote.String(), string(baddr.UAddr)) t.Fatalf("Should not have rewritten non-localhost string. expected %v, got %v", nodRemote.String(), string(baddr.UAddr))
} }