From 7fd82a0e3e87aaf26198a6cf2de242c30fa7c2ab Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Fri, 5 Jul 2019 20:27:13 +0200 Subject: [PATCH] p2p: add address info to peer event reporting (#19716) --- p2p/message.go | 36 ++++++++++++++++++++++-------------- p2p/peer.go | 16 +++++++++------- p2p/server.go | 14 +++++++++----- 3 files changed, 40 insertions(+), 26 deletions(-) diff --git a/p2p/message.go b/p2p/message.go index 9e387ba9b..b98773222 100644 --- a/p2p/message.go +++ b/p2p/message.go @@ -252,19 +252,23 @@ func ExpectMsg(r MsgReader, code uint64, content interface{}) error { type msgEventer struct { MsgReadWriter - feed *event.Feed - peerID enode.ID - Protocol string + feed *event.Feed + peerID enode.ID + Protocol string + localAddress string + remoteAddress string } // newMsgEventer returns a msgEventer which sends message events to the given // feed -func newMsgEventer(rw MsgReadWriter, feed *event.Feed, peerID enode.ID, proto string) *msgEventer { +func newMsgEventer(rw MsgReadWriter, feed *event.Feed, peerID enode.ID, proto, remote, local string) *msgEventer { return &msgEventer{ MsgReadWriter: rw, feed: feed, peerID: peerID, Protocol: proto, + remoteAddress: remote, + localAddress: local, } } @@ -276,11 +280,13 @@ func (ev *msgEventer) ReadMsg() (Msg, error) { return msg, err } ev.feed.Send(&PeerEvent{ - Type: PeerEventTypeMsgRecv, - Peer: ev.peerID, - Protocol: ev.Protocol, - MsgCode: &msg.Code, - MsgSize: &msg.Size, + Type: PeerEventTypeMsgRecv, + Peer: ev.peerID, + Protocol: ev.Protocol, + MsgCode: &msg.Code, + MsgSize: &msg.Size, + LocalAddress: ev.localAddress, + RemoteAddress: ev.remoteAddress, }) return msg, nil } @@ -293,11 +299,13 @@ func (ev *msgEventer) WriteMsg(msg Msg) error { return err } ev.feed.Send(&PeerEvent{ - Type: PeerEventTypeMsgSend, - Peer: ev.peerID, - Protocol: ev.Protocol, - MsgCode: &msg.Code, - MsgSize: &msg.Size, + Type: PeerEventTypeMsgSend, + Peer: ev.peerID, + Protocol: ev.Protocol, + MsgCode: &msg.Code, + MsgSize: &msg.Size, + LocalAddress: ev.localAddress, + RemoteAddress: ev.remoteAddress, }) return nil } diff --git a/p2p/peer.go b/p2p/peer.go index 98ea6835d..d370894f1 100644 --- a/p2p/peer.go +++ b/p2p/peer.go @@ -91,12 +91,14 @@ const ( // PeerEvent is an event emitted when peers are either added or dropped from // a p2p.Server or when a message is sent or received on a peer connection type PeerEvent struct { - Type PeerEventType `json:"type"` - Peer enode.ID `json:"peer"` - Error string `json:"error,omitempty"` - Protocol string `json:"protocol,omitempty"` - MsgCode *uint64 `json:"msg_code,omitempty"` - MsgSize *uint32 `json:"msg_size,omitempty"` + Type PeerEventType `json:"type"` + Peer enode.ID `json:"peer"` + Error string `json:"error,omitempty"` + Protocol string `json:"protocol,omitempty"` + MsgCode *uint64 `json:"msg_code,omitempty"` + MsgSize *uint32 `json:"msg_size,omitempty"` + LocalAddress string `json:"local,omitempty"` + RemoteAddress string `json:"remote,omitempty"` } // Peer represents a connected remote node. @@ -354,7 +356,7 @@ func (p *Peer) startProtocols(writeStart <-chan struct{}, writeErr chan<- error) proto.werr = writeErr var rw MsgReadWriter = proto if p.events != nil { - rw = newMsgEventer(rw, p.events, p.ID(), proto.Name) + rw = newMsgEventer(rw, p.events, p.ID(), proto.Name, p.Info().Network.RemoteAddress, p.Info().Network.LocalAddress) } p.log.Trace(fmt.Sprintf("Starting protocol %s/%d", proto.Name, proto.Version)) go func() { diff --git a/p2p/server.go b/p2p/server.go index b7340a5ea..916298b1b 100644 --- a/p2p/server.go +++ b/p2p/server.go @@ -1016,8 +1016,10 @@ func (srv *Server) runPeer(p *Peer) { // broadcast peer add srv.peerFeed.Send(&PeerEvent{ - Type: PeerEventTypeAdd, - Peer: p.ID(), + Type: PeerEventTypeAdd, + Peer: p.ID(), + RemoteAddress: p.RemoteAddr().String(), + LocalAddress: p.LocalAddr().String(), }) // run the protocol @@ -1025,9 +1027,11 @@ func (srv *Server) runPeer(p *Peer) { // broadcast peer drop srv.peerFeed.Send(&PeerEvent{ - Type: PeerEventTypeDrop, - Peer: p.ID(), - Error: err.Error(), + Type: PeerEventTypeDrop, + Peer: p.ID(), + Error: err.Error(), + RemoteAddress: p.RemoteAddr().String(), + LocalAddress: p.LocalAddr().String(), }) // Note: run waits for existing peers to be sent on srv.delpeer