swarm/network: Replace remote ip in handshake uaddr

This commit is contained in:
lash 2019-02-15 10:09:45 +01:00
parent dcb872952c
commit 2766ccb4c0

View file

@ -21,6 +21,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"net" "net"
"regexp"
"sync" "sync"
"time" "time"
@ -39,6 +40,8 @@ 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",
@ -213,8 +216,15 @@ func (b *Bzz) performHandshake(p *protocols.Peer, handshake *HandshakeMsg) error
handshake.err = err handshake.err = err
return err return err
} }
log.Warn("have hs", "remote", p.RemoteAddr(), "hs", rsh.(*HandshakeMsg).Addr) log.Warn("have hs before", "remote", p.LocalAddr(), "hs", rsh.(*HandshakeMsg).Addr)
handshake.peerAddr = rsh.(*HandshakeMsg).Addr handshake.peerAddr = rsh.(*HandshakeMsg).Addr
ip, port, err := net.SplitHostPort(p.LocalAddr().String())
if err == nil {
remoteStr := fmt.Sprintf("@%s:%s", ip, port)
log.Warn("remoteaddr", "a", p.LocalAddr(), "s", remoteStr)
handshake.peerAddr.UAddr = regexpEnodeIP.ReplaceAll(handshake.peerAddr.UAddr, []byte(remoteStr))
}
log.Warn("have hs after", "remote", p.LocalAddr(), "hs", handshake.peerAddr)
handshake.LightNode = rsh.(*HandshakeMsg).LightNode handshake.LightNode = rsh.(*HandshakeMsg).LightNode
return nil return nil
} }