mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
swarm/network: Make sanitize method of bzzaddr
This commit is contained in:
parent
a4df8a56a7
commit
3414632d94
2 changed files with 18 additions and 18 deletions
|
|
@ -214,26 +214,11 @@ func (b *Bzz) performHandshake(p *protocols.Peer, handshake *HandshakeMsg) error
|
|||
return err
|
||||
}
|
||||
handshake.peerAddr = rsh.(*HandshakeMsg).Addr
|
||||
sanitizeEnodeRemote(p.Node(), handshake.peerAddr)
|
||||
handshake.peerAddr.sanitizeEnodeRemote(p.Node())
|
||||
handshake.LightNode = rsh.(*HandshakeMsg).LightNode
|
||||
return nil
|
||||
}
|
||||
|
||||
// if started without the natip argument, the enode string will be localhost
|
||||
// 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 *enode.Node, baddr *BzzAddr) {
|
||||
enod, err := enode.ParseV4(string(baddr.UAddr))
|
||||
if err != nil {
|
||||
log.Error("invalid bzz address", "addr", baddr)
|
||||
return
|
||||
}
|
||||
if enod.IP().IsLoopback() {
|
||||
baddr.UAddr = []byte(paddr.String())
|
||||
}
|
||||
}
|
||||
|
||||
// runBzz is the p2p protocol run function for the bzz base protocol
|
||||
// that negotiates the bzz handshake
|
||||
func (b *Bzz) runBzz(p *p2p.Peer, rw p2p.MsgReadWriter) error {
|
||||
|
|
@ -389,6 +374,21 @@ func (a *BzzAddr) String() string {
|
|||
return fmt.Sprintf("%x <%s>", a.OAddr, a.UAddr)
|
||||
}
|
||||
|
||||
// if started without the natip argument, the enode string will be localhost
|
||||
// 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 (b *BzzAddr) sanitizeEnodeRemote(paddr *enode.Node) {
|
||||
enod, err := enode.ParseV4(string(b.UAddr))
|
||||
if err != nil {
|
||||
log.Error("invalid bzz address", "addr", b)
|
||||
return
|
||||
}
|
||||
if enod.IP().IsLoopback() {
|
||||
b.UAddr = []byte(paddr.String())
|
||||
}
|
||||
}
|
||||
|
||||
// RandomAddr is a utility method generating an address from a public key
|
||||
func RandomAddr() *BzzAddr {
|
||||
key, err := crypto.GenerateKey()
|
||||
|
|
|
|||
|
|
@ -268,14 +268,14 @@ func TestSanitizeEnodeRemote(t *testing.T) {
|
|||
baddr := RandomAddr()
|
||||
oldUAddr := []byte(nodLocal.String())
|
||||
baddr.UAddr = oldUAddr
|
||||
sanitizeEnodeRemote(nodRemote, baddr)
|
||||
baddr.sanitizeEnodeRemote(nodRemote)
|
||||
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)
|
||||
nodRemoteTwo := enode.NewV4(&pk.PublicKey, remoteIP, 30341, 30341)
|
||||
sanitizeEnodeRemote(nodRemoteTwo, baddr)
|
||||
baddr.sanitizeEnodeRemote(nodRemoteTwo)
|
||||
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