| 1 | package nopfs |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | |
| 7 | "github.com/ipfs-shipyard/nopfs" |
| 8 | "github.com/ipfs-shipyard/nopfs/ipfs" |
| 9 | "github.com/ipfs/kubo/core" |
| 10 | "github.com/ipfs/kubo/core/node" |
| 11 | "github.com/ipfs/kubo/plugin" |
| 12 | "go.uber.org/fx" |
| 13 | ) |
| 14 | |
| 15 | // Plugins sets the list of plugins to be loaded. |
| 16 | var Plugins = []plugin.Plugin{ |
| 17 | &nopfsPlugin{}, |
| 18 | } |
| 19 | |
| 20 | // fxtestPlugin is used for testing the fx plugin. |
| 21 | // It merely adds an fx option that logs a debug statement, so we can verify that it works in tests. |
| 22 | type nopfsPlugin struct { |
| 23 | // Path to the IPFS repo. |
| 24 | repo string |
| 25 | } |
| 26 | |
| 27 | var _ plugin.PluginFx = (*nopfsPlugin)(nil) |
| 28 | |
| 29 | func (p *nopfsPlugin) Name() string { |
| 30 | return "nopfs" |
| 31 | } |
| 32 | |
| 33 | func (p *nopfsPlugin) Version() string { |
| 34 | return "0.0.10" |
| 35 | } |
| 36 | |
| 37 | func (p *nopfsPlugin) Init(env *plugin.Environment) error { |
| 38 | p.repo = env.Repo |
| 39 | |
| 40 | return nil |
| 41 | } |
| 42 | |
| 43 | // MakeBlocker is a factory for the blocker so that it can be provided with Fx. |
| 44 | func MakeBlocker(repoPath string) func() (*nopfs.Blocker, error) { |
| 45 | return func() (*nopfs.Blocker, error) { |
| 46 | defaultFiles, err := nopfs.GetDenylistFiles() |
| 47 | if err != nil { |
| 48 | return nil, err |
| 49 | } |
| 50 | |
| 51 | kuboFiles, err := nopfs.GetDenylistFilesInDir(filepath.Join(repoPath, "denylists")) |
| 52 | if err != nil { |
| 53 | return nil, err |
| 54 | } |
| 55 | |
| 56 | files := append(defaultFiles, kuboFiles...) |
| 57 | |
| 58 | return nopfs.NewBlocker(files) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // PathResolvers returns wrapped PathResolvers for Kubo. |
| 63 | func PathResolvers(fetchers node.FetchersIn, blocker *nopfs.Blocker) node.PathResolversOut { |
| 64 | res := node.PathResolverConfig(fetchers) |
| 65 | return node.PathResolversOut{ |
| 66 | IPLDPathResolver: ipfs.WrapResolver(res.IPLDPathResolver, blocker), |
| 67 | UnixFSPathResolver: ipfs.WrapResolver(res.UnixFSPathResolver, blocker), |
| 68 | OfflineIPLDPathResolver: ipfs.WrapResolver(res.OfflineIPLDPathResolver, blocker), |
| 69 | OfflineUnixFSPathResolver: ipfs.WrapResolver(res.OfflineUnixFSPathResolver, blocker), |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | func (p *nopfsPlugin) Options(info core.FXNodeInfo) ([]fx.Option, error) { |
| 74 | if os.Getenv("IPFS_CONTENT_BLOCKING_DISABLE") != "" { |
| 75 | return info.FXOptions, nil |
| 76 | } |
| 77 | |
| 78 | opts := append( |
| 79 | info.FXOptions, |
| 80 | fx.Provide(MakeBlocker(p.repo)), |
| 81 | fx.Decorate(ipfs.WrapBlockService), |
| 82 | fx.Decorate(ipfs.WrapNameSystem), |
| 83 | fx.Decorate(PathResolvers), |
| 84 | ) |
| 85 | return opts, nil |
| 86 | } |