@cryptotaxi247 / kubo / commits / edfe9c5ef

feat: add peerlog plugin

adds a plugin to log just peerIDs for nodes we connect to, to allow us to see how many unique peers a node connects to over time. It's set up a preloaded plugin, so it would be enabled by default. We may want to extract it to a seperate repo so we can make it an optional add on. **Usage** ```console $ GOLOG_FILE=~/peer.log IPFS_LOGGING_FMT=json ipfs daemon ``` **output** ```json {"level":"info","ts":"2020-02-10T13:54:26.639Z","logger":"plugin/peerlog","caller":"peerlog/peerlog.go:51","msg":"connected","peer":"QmS2H72gdrekXJggGdE9SunXPntBqdkJdkXQJjuxcH8Cbt"} {"level":"info","ts":"2020-02-10T13:54:59.095Z","logger":"plugin/peerlog","caller":"peerlog/peerlog.go:56","msg":"disconnected","peer":"QmS2H72gdrekXJggGdE9SunXPntBqdkJdkXQJjuxcH8Cbt"} ``` License: MIT Signed-off-by: Oli Evans <oli@tableflip.io>

Oli Evans committed Feb 10, 2020 at 13:57 UTC edfe9c5efdbc73087460cd0821714c254b35b3a7
4 files changed +70 -47
plugin/loader/preload.go
+2 -2
@@ -5,7 +5,7 @@ import (
5 pluginflatfs "github.com/ipfs/go-ipfs/plugin/plugins/flatfs"
6 pluginipldgit "github.com/ipfs/go-ipfs/plugin/plugins/git"
7 pluginlevelds "github.com/ipfs/go-ipfs/plugin/plugins/levelds"
8 - pluginpeeridlog "github.com/ipfs/go-ipfs/plugin/plugins/peeridlog"
8 + pluginpeerlog "github.com/ipfs/go-ipfs/plugin/plugins/peerlog"
9 )
10
11 // DO NOT EDIT THIS FILE
@@ -17,5 +17,5 @@ func init() {
17 Preload(pluginbadgerds.Plugins...)
18 Preload(pluginflatfs.Plugins...)
19 Preload(pluginlevelds.Plugins...)
20 - Preload(pluginpeeridlog.Plugins...)
20 + Preload(pluginpeerlog.Plugins...)
21 }
plugin/loader/preload_list
+1 -1
@@ -8,4 +8,4 @@ ipldgit github.com/ipfs/go-ipfs/plugin/plugins/git *
8 badgerds github.com/ipfs/go-ipfs/plugin/plugins/badgerds *
9 flatfs github.com/ipfs/go-ipfs/plugin/plugins/flatfs *
10 levelds github.com/ipfs/go-ipfs/plugin/plugins/levelds *
11 -peeridlog github.com/ipfs/go-ipfs/plugin/plugins/peeridlog *
11 +peerlog github.com/ipfs/go-ipfs/plugin/plugins/peerlog *
plugin/plugins/peeridlog/peeridlog.go deleted
-44
@@ -1,44 +0,0 @@
1 -package peeridlog
2 -
3 -import (
4 - "fmt"
5 -
6 - core "github.com/ipfs/go-ipfs/core"
7 - plugin "github.com/ipfs/go-ipfs/plugin"
8 -)
9 -
10 -// Plugins is exported list of plugins that will be loaded
11 -var Plugins = []plugin.Plugin{
12 - &peerIDLogPlugin{},
13 -}
14 -
15 -// Log all the PeerIDs we connect to.
16 -type peerIDLogPlugin struct{}
17 -
18 -var _ plugin.PluginDaemonInternal = (*peerIDLogPlugin)(nil)
19 -
20 -// Name returns the plugin's name, satisfying the plugin.Plugin interface.
21 -func (*peerIDLogPlugin) Name() string {
22 - return "peeridlog"
23 -}
24 -
25 -// Version returns the plugin's version, satisfying the plugin.Plugin interface.
26 -func (*peerIDLogPlugin) Version() string {
27 - return "0.1.0"
28 -}
29 -
30 -// Init initializes plugin, satisfying the plugin.Plugin interface. Put any
31 -// initialization logic here.
32 -func (*peerIDLogPlugin) Init(*plugin.Environment) error {
33 - return nil
34 -}
35 -
36 -func (*peerIDLogPlugin) Start(*core.IpfsNode) error {
37 - fmt.Println("peerIDLogPlugin HELLO!")
38 - return nil
39 -}
40 -
41 -func (*peerIDLogPlugin) Close() error {
42 - fmt.Println("peerIDLogPlugin GOODBYE!")
43 - return nil
44 -}
plugin/plugins/peerlog/peerlog.go new
+67
@@ -0,0 +1,67 @@
1 +package peerlog
2 +
3 +import (
4 + "fmt"
5 +
6 + core "github.com/ipfs/go-ipfs/core"
7 + plugin "github.com/ipfs/go-ipfs/plugin"
8 + logging "github.com/ipfs/go-log"
9 + network "github.com/libp2p/go-libp2p-core/network"
10 +)
11 +
12 +var log = logging.Logger("plugin/peerlog")
13 +
14 +// Log all the PeerIDs we see
15 +//
16 +// Usage:
17 +// GOLOG_FILE=~/peer.log IPFS_LOGGING_FMT=json ipfs daemon
18 +// Output:
19 +// {"level":"info","ts":"2020-02-10T13:54:26.639Z","logger":"plugin/peerlog","caller":"peerlog/peerlog.go:51","msg":"connected","peer":"QmS2H72gdrekXJggGdE9SunXPntBqdkJdkXQJjuxcH8Cbt"}
20 +// {"level":"info","ts":"2020-02-10T13:54:59.095Z","logger":"plugin/peerlog","caller":"peerlog/peerlog.go:56","msg":"disconnected","peer":"QmS2H72gdrekXJggGdE9SunXPntBqdkJdkXQJjuxcH8Cbt"}
21 +//
22 +type peerLogPlugin struct{}
23 +
24 +var _ plugin.PluginDaemonInternal = (*peerLogPlugin)(nil)
25 +
26 +// Plugins is exported list of plugins that will be loaded
27 +var Plugins = []plugin.Plugin{
28 + &peerLogPlugin{},
29 +}
30 +
31 +// Name returns the plugin's name, satisfying the plugin.Plugin interface.
32 +func (*peerLogPlugin) Name() string {
33 + return "peerlog"
34 +}
35 +
36 +// Version returns the plugin's version, satisfying the plugin.Plugin interface.
37 +func (*peerLogPlugin) Version() string {
38 + return "0.1.0"
39 +}
40 +
41 +// Init initializes plugin
42 +func (*peerLogPlugin) Init(*plugin.Environment) error {
43 + fmt.Println("peerLogPlugin enabled - PeerIDs will be logged")
44 + return nil
45 +}
46 +
47 +func (*peerLogPlugin) Start(node *core.IpfsNode) error {
48 + // Ensure logs from this plugin get printed regardless of global IPFS_LOGGING value
49 + logging.SetLogLevel("plugin/peerlog", "info")
50 + var notifee network.NotifyBundle
51 + notifee.ConnectedF = func(net network.Network, conn network.Conn) {
52 + log.Infow("connected",
53 + "peer", conn.RemotePeer().Pretty(),
54 + )
55 + }
56 + notifee.DisconnectedF = func(net network.Network, conn network.Conn) {
57 + log.Infow("disconnected",
58 + "peer", conn.RemotePeer().Pretty(),
59 + )
60 + }
61 + node.PeerHost.Network().Notify(&notifee)
62 + return nil
63 +}
64 +
65 +func (*peerLogPlugin) Close() error {
66 + return nil
67 +}