-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
plugin: Add support for content blocking in Kubo
Fixes #8492. This introduces "nopfs" as a preloaded plugin into Kubo. It automatically make Kubo watch *.deny files found in: - /etc/ipfs/denylists - $XDG_CONFIG_HOME/ipfs/denylists - $IPFS_PATH/denylists (files need to be present before boot in order to be watched). Debug logging can be enabled with `GOLOG_LOG_LEVEL="nopfs=debug"`. All blocks are logged to "nopfs-blocks", so logging requests for blocked content can be achieved with `GOLOG_LOG_LEVEL="nopfs-blocks=warn"`: ``` WARN (...) QmRFniDxwxoG2n4AcnGhRdjqDjCM5YeUcBE75K8WXmioH3: blocked (test.deny:9) ``` Interactive/gateway users will also receive errors as responses but with less details: ``` Error: /ipfs/QmQvjk82hPkSaZsyJ8vNER5cmzKW7HyGX5XVusK7EAenCN is blocked and cannot be provided ``` One particularity to keep in mind is that GetMany() will silently drop blocked blocks from the response (a warnings are logged). AddMany() will act similarly and avoid adding blocked blocks. The code implementing all this is actually in nopfs: - https://github.com/ipfs-shipyard/nopfs (main library) - https://github.com/ipfs-shipyard/nopfs/tree/master/ipfs (wrappers) The interpretation of the list rules and block detection is well tested, but a general review might be in order.
- Loading branch information
Showing
6 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package nopfs | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/ipfs-shipyard/nopfs" | ||
"github.com/ipfs-shipyard/nopfs/ipfs" | ||
"github.com/ipfs/kubo/config" | ||
"github.com/ipfs/kubo/core" | ||
"github.com/ipfs/kubo/core/node" | ||
"github.com/ipfs/kubo/plugin" | ||
"go.uber.org/fx" | ||
) | ||
|
||
// Plugins sets the list of plugins to be loaded. | ||
var Plugins = []plugin.Plugin{ | ||
&nopfsPlugin{}, | ||
} | ||
|
||
// fxtestPlugin is used for testing the fx plugin. | ||
// It merely adds an fx option that logs a debug statement, so we can verify that it works in tests. | ||
type nopfsPlugin struct{} | ||
|
||
var _ plugin.PluginFx = (*nopfsPlugin)(nil) | ||
|
||
func (p *nopfsPlugin) Name() string { | ||
return "nopfs" | ||
} | ||
|
||
func (p *nopfsPlugin) Version() string { | ||
return "0.0.10" | ||
} | ||
|
||
func (p *nopfsPlugin) Init(env *plugin.Environment) error { | ||
return nil | ||
} | ||
|
||
// MakeBlocker is a factory for the blocker so that it can be provided with Fx. | ||
func MakeBlocker() (*nopfs.Blocker, error) { | ||
ipfsPath, err := config.PathRoot() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defaultFiles, err := nopfs.GetDenylistFiles() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
kuboFiles, err := nopfs.GetDenylistFilesInDir(filepath.Join(ipfsPath, "denylists")) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
files := append(defaultFiles, kuboFiles...) | ||
|
||
return nopfs.NewBlocker(files) | ||
} | ||
|
||
// PathResolvers returns wrapped PathResolvers for Kubo. | ||
func PathResolvers(fetchers node.FetchersIn, blocker *nopfs.Blocker) node.PathResolversOut { | ||
res := node.PathResolverConfig(fetchers) | ||
return node.PathResolversOut{ | ||
IPLDPathResolver: ipfs.WrapResolver(res.IPLDPathResolver, blocker), | ||
UnixFSPathResolver: ipfs.WrapResolver(res.UnixFSPathResolver, blocker), | ||
} | ||
} | ||
|
||
func (p *nopfsPlugin) Options(info core.FXNodeInfo) ([]fx.Option, error) { | ||
if os.Getenv("IPFS_CONTENT_BLOCKING_DISABLE") != "" { | ||
return info.FXOptions, nil | ||
} | ||
|
||
opts := append( | ||
info.FXOptions, | ||
fx.Provide(MakeBlocker), | ||
fx.Decorate(ipfs.WrapBlockService), | ||
fx.Decorate(ipfs.WrapNameSystem), | ||
fx.Decorate(PathResolvers), | ||
) | ||
return opts, nil | ||
} |