diff options
author | lash <nolash@users.noreply.github.com> | 2019-02-20 21:46:00 +0800 |
---|---|---|
committer | Viktor TrĂ³n <viktor.tron@gmail.com> | 2019-02-20 21:46:00 +0800 |
commit | 460d206f309fc0884c666bd191a1b6a4b63462fc (patch) | |
tree | 5a7a5edb620ba69731ab3544992472e094b3779b /swarm/network/protocol.go | |
parent | ba2dfa5ce43d72733c864bc43f7b80b39c674733 (diff) | |
download | go-tangerine-460d206f309fc0884c666bd191a1b6a4b63462fc.tar go-tangerine-460d206f309fc0884c666bd191a1b6a4b63462fc.tar.gz go-tangerine-460d206f309fc0884c666bd191a1b6a4b63462fc.tar.bz2 go-tangerine-460d206f309fc0884c666bd191a1b6a4b63462fc.tar.lz go-tangerine-460d206f309fc0884c666bd191a1b6a4b63462fc.tar.xz go-tangerine-460d206f309fc0884c666bd191a1b6a4b63462fc.tar.zst go-tangerine-460d206f309fc0884c666bd191a1b6a4b63462fc.zip |
swarm/network: Use actual remote peer ip in underlay (#19137)
* swarm/network: Logline to see handshake addr
* swarm/network: Replace remote ip in handshake uaddr
* swarm/network: Add test for enode uaddr rewrite method
* swarm/network: Remove redundance pointer return from sanitize
* swarm/network: Obeying the linting machine
* swarm/network: Add panic comment
(travis trigger take 1)
Diffstat (limited to 'swarm/network/protocol.go')
-rw-r--r-- | swarm/network/protocol.go | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/swarm/network/protocol.go b/swarm/network/protocol.go index 6f8eadad2..ba2a7e5fa 100644 --- a/swarm/network/protocol.go +++ b/swarm/network/protocol.go @@ -21,6 +21,7 @@ import ( "errors" "fmt" "net" + "regexp" "sync" "time" @@ -39,6 +40,8 @@ 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", @@ -214,10 +217,26 @@ func (b *Bzz) performHandshake(p *protocols.Peer, handshake *HandshakeMsg) error return err } handshake.peerAddr = rsh.(*HandshakeMsg).Addr + sanitizeEnodeRemote(p.RemoteAddr(), handshake.peerAddr) handshake.LightNode = rsh.(*HandshakeMsg).LightNode return nil } +// the remote enode string may advertise arbitrary host information (e.g. localhost) +// this method ensures that the addr of the peer will be 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()) + // since we expect nothing else than ipv4 here, a panic on missing submatch is desired + if err == nil && string(hsSubmatch[1]) != ip { + 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)) + } +} + // 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 { @@ -324,7 +343,7 @@ func (b *Bzz) GetOrCreateHandshake(peerID enode.ID) (*HandshakeMsg, bool) { init: make(chan bool, 1), done: make(chan struct{}), } - // when handhsake is first created for a remote peer + // when handshake is first created for a remote peer // it is initialised with the init handshake.init <- true b.handshakes[peerID] = handshake |