@cryptotaxi247 / kubo / commits / 255fefb2a

feat(peerlog): log protocols/versions

Steven Allen committed Mar 10, 2020 at 09:31 UTC 255fefb2aaa268df1fa0f4f8bc7450825355d136
1 file changed +35
plugin/plugins/peerlog/peerlog.go
+35
@@ -6,6 +6,8 @@ import (
6 core "github.com/ipfs/go-ipfs/core"
7 plugin "github.com/ipfs/go-ipfs/plugin"
8 logging "github.com/ipfs/go-log"
9 + eventbus "github.com/libp2p/go-eventbus"
10 + event "github.com/libp2p/go-libp2p-core/event"
11 network "github.com/libp2p/go-libp2p-core/network"
12 )
13
@@ -50,6 +52,7 @@ func (*peerLogPlugin) Start(node *core.IpfsNode) error {
52 }
53 var notifee network.NotifyBundle
54 notifee.ConnectedF = func(net network.Network, conn network.Conn) {
55 + // TODO: Log transport, country, etc?
56 log.Infow("connected",
57 "peer", conn.RemotePeer().Pretty(),
58 )
@@ -60,6 +63,38 @@ func (*peerLogPlugin) Start(node *core.IpfsNode) error {
63 )
64 }
65 node.PeerHost.Network().Notify(&notifee)
66 +
67 + sub, err := node.PeerHost.EventBus().Subscribe(
68 + new(event.EvtPeerIdentificationCompleted),
69 + eventbus.BufSize(1024),
70 + )
71 + if err != nil {
72 + return fmt.Errorf("failed to subscribe to identify notifications")
73 + }
74 + go func() {
75 + defer sub.Close()
76 + for e := range sub.Out() {
77 + switch e := e.(type) {
78 + case event.EvtPeerIdentificationCompleted:
79 + protocols, err := node.Peerstore.GetProtocols(e.Peer)
80 + if err != nil {
81 + log.Errorw("failed to get protocols", "error", err)
82 + continue
83 + }
84 + agent, err := node.Peerstore.Get(e.Peer, "AgentVersion")
85 + if err != nil {
86 + log.Errorw("failed to get agent version", "error", err)
87 + continue
88 + }
89 + log.Infow(
90 + "identified",
91 + "peer", e.Peer.Pretty(),
92 + "agent", agent,
93 + "protocols", protocols,
94 + )
95 + }
96 + }
97 + }()
98 return nil
99 }
100