mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
swarm/network: Use enode package for enode rewrite instead of regex
This commit is contained in:
parent
98ad48ae29
commit
e489d77d4b
2 changed files with 9 additions and 27 deletions
|
|
@ -21,7 +21,6 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"regexp"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
|
|
@ -40,8 +39,6 @@ const (
|
|||
bzzHandshakeTimeout = 3000 * time.Millisecond
|
||||
)
|
||||
|
||||
var regexpEnodeIP = regexp.MustCompile("@(.+):([0-9]+)")
|
||||
|
||||
// BzzSpec is the spec of the generic swarm handshake
|
||||
var BzzSpec = &protocols.Spec{
|
||||
Name: "bzz",
|
||||
|
|
@ -217,7 +214,7 @@ func (b *Bzz) performHandshake(p *protocols.Peer, handshake *HandshakeMsg) error
|
|||
return err
|
||||
}
|
||||
handshake.peerAddr = rsh.(*HandshakeMsg).Addr
|
||||
sanitizeEnodeRemote(p.RemoteAddr(), handshake.peerAddr)
|
||||
sanitizeEnodeRemote(p.Node(), handshake.peerAddr)
|
||||
handshake.LightNode = rsh.(*HandshakeMsg).LightNode
|
||||
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
|
||||
// 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
|
||||
func sanitizeEnodeRemote(paddr net.Addr, baddr *BzzAddr) {
|
||||
hsSubmatch := regexpEnodeIP.FindSubmatch(baddr.UAddr)
|
||||
ip, _, err := net.SplitHostPort(paddr.String())
|
||||
if len(hsSubmatch) < 2 {
|
||||
log.Warn("sanitize found non ipv4 string", "remotestring", paddr.String(), "handshakeaddr", baddr)
|
||||
} 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))
|
||||
func sanitizeEnodeRemote(paddr *enode.Node, baddr *BzzAddr) {
|
||||
enod, err := enode.ParseV4(string(baddr.UAddr))
|
||||
if err == nil {
|
||||
if enod.IP().IsLoopback() {
|
||||
baddr.UAddr = []byte(paddr.String())
|
||||
}
|
||||
} else {
|
||||
log.Trace("passthrough handshake addr rewrite", "submatch", hsSubmatch[1])
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -263,26 +263,19 @@ func TestSanitizeEnodeRemote(t *testing.T) {
|
|||
t.Fatal(err)
|
||||
}
|
||||
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)
|
||||
nodRemote := enode.NewV4(&pk.PublicKey, remoteIP, 30341, 30341)
|
||||
baddr := RandomAddr()
|
||||
oldUAddr := []byte(nodLocal.String())
|
||||
baddr.UAddr = oldUAddr
|
||||
sanitizeEnodeRemote(&remoteAddr, baddr)
|
||||
sanitizeEnodeRemote(nodRemote, baddr)
|
||||
if !bytes.Equal(baddr.UAddr, []byte(nodRemote.String())) {
|
||||
t.Fatalf("insane address. expected %v, got %v", nodRemote.String(), string(baddr.UAddr))
|
||||
}
|
||||
|
||||
remoteIP = net.IPv4(0x04, 0x04, 0x04, 0x04)
|
||||
remoteAddr = net.TCPAddr{
|
||||
IP: remoteIP,
|
||||
Port: 30399,
|
||||
}
|
||||
sanitizeEnodeRemote(&remoteAddr, baddr)
|
||||
nodRemoteTwo := enode.NewV4(&pk.PublicKey, remoteIP, 30341, 30341)
|
||||
sanitizeEnodeRemote(nodRemoteTwo, baddr)
|
||||
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))
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue