Skip to content

Commit

Permalink
Add integrated nopfs-content blocking to the gateway
Browse files Browse the repository at this point in the history
This is pending some go.mod bubbling.
  • Loading branch information
hsanjuan committed Oct 11, 2023
1 parent 05dbe55 commit 1fbb16e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
15 changes: 12 additions & 3 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (

_ "net/http/pprof"

"github.com/ipfs/boxo/coreiface/path"
"github.com/ipfs/boxo/gateway"
"github.com/ipfs/boxo/path"
servertiming "github.com/mitchellh/go-server-timing"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
Expand Down Expand Up @@ -174,7 +174,12 @@ func newKuboRPCHandler(endpoints []string) http.Handler {
// - https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/303
redirectToGateway := func(format string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
path := path.New(r.URL.Query().Get("arg"))
path, err := path.NewPath(r.URL.Query().Get("arg"))

Check failure on line 177 in handlers.go

View workflow job for this annotation

GitHub Actions / go-check / All

undefined: path.NewPath

Check failure on line 177 in handlers.go

View workflow job for this annotation

GitHub Actions / go-check / All

undefined: path.NewPath
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
url := path.String()
if format != "" {
url += "?format=" + format
Expand All @@ -189,7 +194,11 @@ func newKuboRPCHandler(endpoints []string) http.Handler {
mux.HandleFunc("/api/v0/dag/export", redirectToGateway("car"))
mux.HandleFunc("/api/v0/block/get", redirectToGateway("raw"))
mux.HandleFunc("/api/v0/dag/get", func(w http.ResponseWriter, r *http.Request) {
path := path.New(r.URL.Query().Get("arg"))
path, err := path.NewPath(r.URL.Query().Get("arg"))

Check failure on line 197 in handlers.go

View workflow job for this annotation

GitHub Actions / go-check / All

undefined: path.NewPath (compile)

Check failure on line 197 in handlers.go

View workflow job for this annotation

GitHub Actions / go-check / All

undefined: path.NewPath (compile)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
}
codec := r.URL.Query().Get("output-codec")
if codec == "" {
codec = "dag-json"
Expand Down
33 changes: 33 additions & 0 deletions setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ import (

badger "github.com/dgraph-io/badger/v4"
options "github.com/dgraph-io/badger/v4/options"
"github.com/ipfs-shipyard/nopfs"

Check failure on line 13 in setup.go

View workflow job for this annotation

GitHub Actions / go-test / ubuntu (go this)

no required module provides package github.com/ipfs-shipyard/nopfs; to add it:

Check failure on line 13 in setup.go

View workflow job for this annotation

GitHub Actions / go-test / ubuntu (go next)

no required module provides package github.com/ipfs-shipyard/nopfs; to add it:

Check failure on line 13 in setup.go

View workflow job for this annotation

GitHub Actions / go-test / windows (go this)

no required module provides package github.com/ipfs-shipyard/nopfs; to add it:

Check failure on line 13 in setup.go

View workflow job for this annotation

GitHub Actions / go-test / windows (go next)

no required module provides package github.com/ipfs-shipyard/nopfs; to add it:

Check failure on line 13 in setup.go

View workflow job for this annotation

GitHub Actions / go-test / macos (go this)

no required module provides package github.com/ipfs-shipyard/nopfs; to add it:

Check failure on line 13 in setup.go

View workflow job for this annotation

GitHub Actions / go-test / macos (go next)

no required module provides package github.com/ipfs-shipyard/nopfs; to add it:
nopfsipfs "github.com/ipfs-shipyard/nopfs/ipfs"

Check failure on line 14 in setup.go

View workflow job for this annotation

GitHub Actions / go-test / ubuntu (go this)

no required module provides package github.com/ipfs-shipyard/nopfs/ipfs; to add it:

Check failure on line 14 in setup.go

View workflow job for this annotation

GitHub Actions / go-test / ubuntu (go next)

no required module provides package github.com/ipfs-shipyard/nopfs/ipfs; to add it:

Check failure on line 14 in setup.go

View workflow job for this annotation

GitHub Actions / go-test / windows (go this)

no required module provides package github.com/ipfs-shipyard/nopfs/ipfs; to add it:

Check failure on line 14 in setup.go

View workflow job for this annotation

GitHub Actions / go-test / windows (go next)

no required module provides package github.com/ipfs-shipyard/nopfs/ipfs; to add it:

Check failure on line 14 in setup.go

View workflow job for this annotation

GitHub Actions / go-test / macos (go this)

no required module provides package github.com/ipfs-shipyard/nopfs/ipfs; to add it:

Check failure on line 14 in setup.go

View workflow job for this annotation

GitHub Actions / go-test / macos (go next)

no required module provides package github.com/ipfs-shipyard/nopfs/ipfs; to add it:
bsclient "github.com/ipfs/boxo/bitswap/client"
bsnet "github.com/ipfs/boxo/bitswap/network"
"github.com/ipfs/boxo/blockservice"
"github.com/ipfs/boxo/blockstore"
bsfetcher "github.com/ipfs/boxo/fetcher/impl/blockservice"
"github.com/ipfs/boxo/gateway"
"github.com/ipfs/boxo/ipns"
"github.com/ipfs/boxo/namesys"
"github.com/ipfs/boxo/path/resolver"
routingv1client "github.com/ipfs/boxo/routing/http/client"
httpcontentrouter "github.com/ipfs/boxo/routing/http/contentrouter"
"github.com/ipfs/go-cid"
Expand All @@ -25,6 +29,11 @@ import (
levelds "github.com/ipfs/go-ds-leveldb"
metri "github.com/ipfs/go-metrics-interface"
mprome "github.com/ipfs/go-metrics-prometheus"
"github.com/ipfs/go-unixfsnode"
dagpb "github.com/ipld/go-codec-dagpb"
"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/node/basicnode"
"github.com/ipld/go-ipld-prime/schema"
"github.com/libp2p/go-libp2p"
dht "github.com/libp2p/go-libp2p-kad-dht"
"github.com/libp2p/go-libp2p-kad-dht/fullrt"
Expand Down Expand Up @@ -57,6 +66,7 @@ type Node struct {
blockstore blockstore.Blockstore
bsClient *bsclient.Client
bsrv blockservice.BlockService
resolver resolver.Resolver

ns namesys.NameSystem
kuboRPCs []string
Expand Down Expand Up @@ -260,6 +270,15 @@ func Setup(ctx context.Context, cfg Config) (*Node, error) {
bswap := bsclient.New(bsctx, bn, blkst)
bn.Start(bswap)

files, err := nopfs.GetDenylistFiles()
if err != nil {
return nil, err
}
blocker, err := nopfs.NewBlocker(files)
if err != nil {
return nil, err
}

bsrv := blockservice.New(blkst, bswap,
// if we are doing things right, our bitswap wantlists should
// not have blocks that we already have (see
Expand All @@ -269,6 +288,7 @@ func Setup(ctx context.Context, cfg Config) (*Node, error) {
// before writing new blocks.
blockservice.WriteThrough(),
)
bsrv = nopfsipfs.WrapBlockService(bsrv, blocker)

dns, err := gateway.NewDNSResolver(nil)
if err != nil {
Expand All @@ -278,6 +298,18 @@ func Setup(ctx context.Context, cfg Config) (*Node, error) {
if err != nil {
return nil, err
}
ns = nopfsipfs.WrapNameSystem(ns, blocker)

fetcherConfig := bsfetcher.NewFetcherConfig(bsrv)
fetcherConfig.PrototypeChooser = dagpb.AddSupportToChooser(func(lnk ipld.Link, lnkCtx ipld.LinkContext) (ipld.NodePrototype, error) {
if tlnkNd, ok := lnkCtx.LinkNode.(schema.TypedLinkNode); ok {
return tlnkNd.LinkTargetNodePrototype(), nil
}
return basicnode.Prototype.Any, nil
})
fetcher := fetcherConfig.WithReifier(unixfsnode.Reify)
r := resolver.NewBasicResolver(fetcher)
r = nopfsipfs.WrapResolver(r, blocker)

return &Node{
host: h,
Expand All @@ -287,6 +319,7 @@ func Setup(ctx context.Context, cfg Config) (*Node, error) {
ns: ns,
vs: vs,
bsrv: bsrv,
resolver: r,
bwc: bwc,
kuboRPCs: cfg.KuboRPCURLs,
}, nil
Expand Down

0 comments on commit 1fbb16e

Please sign in to comment.