Skip to content

Commit

Permalink
feat: custom dnslink resolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
aschmahmann committed Dec 23, 2024
1 parent aca9ee3 commit 6ca2e4d
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ The following emojis are used to highlight certain changes:

### Added

- Add support for custom DNSLink resolvers (e.g. to support .eth, .crypto, etc.)

### Changed

### Fixed
Expand Down
33 changes: 33 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"errors"
"fmt"
"github.com/ipfs/boxo/gateway"
madns "github.com/multiformats/go-multiaddr-dns"
"io"
"io/fs"
"net/http"
Expand Down Expand Up @@ -399,6 +401,12 @@ Generate an identity seed and launch a gateway:
EnvVars: []string{"ROUTING_MAX_TIMEOUT"},
Usage: "Maximum time for routing to find the maximum number of providers",
},
&cli.StringSliceFlag{
Name: "dnslink-resolvers",
Value: cli.NewStringSlice(),
EnvVars: []string{"RAINBOW_DNSLINK_RESOLVERS"},
Usage: "The DNSLink resolvers to use (comma-separated tuples that each look like `eth. : https://dns.eth.limo/dns-query`)",
},
}

app.Commands = []*cli.Command{
Expand Down Expand Up @@ -513,6 +521,12 @@ share the same seed as long as the indexes are different.
}
}

customDNSResolvers := cctx.StringSlice("dnslink-resolvers")
dns, err := parseCustomDNSLinkResolvers(customDNSResolvers)
if err != nil {
return err
}

cfg := Config{
DataDir: ddir,
BlockstoreType: cctx.String("blockstore"),
Expand Down Expand Up @@ -546,6 +560,7 @@ share the same seed as long as the indexes are different.
GCThreshold: cctx.Float64("gc-threshold"),
ListenAddrs: cctx.StringSlice("libp2p-listen-addrs"),
TracingAuthToken: cctx.String("tracing-auth"),
DNSLinkResolver: dns,

// Pebble config
BytesPerSync: cctx.Int("pebble-bytes-per-sync"),
Expand Down Expand Up @@ -766,3 +781,21 @@ func replaceRainbowSeedWithPeer(addr string, seed string) (string, error) {

return strings.Replace(addr, match[0], "/p2p/"+pid.String(), 1), nil
}

func parseCustomDNSLinkResolvers(customDNSResolvers []string) (madns.BasicResolver, error) {
customDNSResolverMap := make(map[string]string)
for _, s := range customDNSResolvers {
split := strings.SplitN(s, ":", 2)
if len(split) != 2 {
return nil, fmt.Errorf("invalid DNS resolver: %s", s)
}
domain := strings.TrimSpace(split[0])
resolverUrl := strings.TrimSpace(split[1])
customDNSResolverMap[domain] = resolverUrl
}
dns, err := gateway.NewDNSResolver(customDNSResolverMap)
if err != nil {
return nil, err
}
return dns, nil
}
7 changes: 7 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"crypto/rand"
"github.com/ipfs/boxo/gateway"
"net/http/httptest"
"testing"

Expand Down Expand Up @@ -63,6 +64,12 @@ func mustTestNodeWithKey(t *testing.T, cfg Config, sk ic.PrivKey) *Node {
_ = cdns.Close()
})

if cfg.DNSLinkResolver == nil {
dnslinkResovler, err := gateway.NewDNSResolver(nil)
require.NoError(t, err)
cfg.DNSLinkResolver = dnslinkResovler
}

nd, err := SetupWithLibp2p(ctx, cfg, sk, cdns)
require.NoError(t, err)
return nd
Expand Down
15 changes: 7 additions & 8 deletions setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
crand "crypto/rand"
"errors"
"fmt"
madns "github.com/multiformats/go-multiaddr-dns"
"io/fs"
"os"
"path/filepath"
Expand Down Expand Up @@ -112,6 +113,8 @@ type Config struct {
IpnsMaxCacheTTL time.Duration
Bitswap bool

DNSLinkResolver madns.BasicResolver

// BitswapWantHaveReplaceSize tells the bitswap server to replace WantHave
// with WantBlock responses when the block size less then or equal to this
// value. Set to zero to disable replacement and avoid block size lookup
Expand Down Expand Up @@ -194,7 +197,7 @@ func SetupNoLibp2p(ctx context.Context, cfg Config, dnsCache *cachedDNS) (*Node,
bsrv = nopfsipfs.WrapBlockService(bsrv, blocker)
}

ns, err := setupNamesys(cfg, vs, blocker)
ns, err := setupNamesys(cfg, vs, blocker, cfg.DNSLinkResolver)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -357,7 +360,7 @@ func SetupWithLibp2p(ctx context.Context, cfg Config, key crypto.PrivKey, dnsCac
n.bsrv = bsrv
n.resolver = r

ns, err := setupNamesys(cfg, vs, blocker)
ns, err := setupNamesys(cfg, vs, blocker, cfg.DNSLinkResolver)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -521,12 +524,8 @@ func setupDenylists(cfg Config) ([]*nopfs.HTTPSubscriber, *nopfs.Blocker, error)
return denylists, blocker, nil
}

func setupNamesys(cfg Config, vs routing.ValueStore, blocker *nopfs.Blocker) (namesys.NameSystem, error) {
dns, err := gateway.NewDNSResolver(nil)
if err != nil {
return nil, err
}
nsOptions := []namesys.Option{namesys.WithDNSResolver(dns)}
func setupNamesys(cfg Config, vs routing.ValueStore, blocker *nopfs.Blocker, dnslinkResolver madns.BasicResolver) (namesys.NameSystem, error) {
nsOptions := []namesys.Option{namesys.WithDNSResolver(dnslinkResolver)}
if cfg.IpnsMaxCacheTTL > 0 {
nsOptions = append(nsOptions, namesys.WithMaxCacheTTL(cfg.IpnsMaxCacheTTL))
}
Expand Down
4 changes: 4 additions & 0 deletions setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
"github.com/ipfs/boxo/gateway"
"net"
"testing"
"time"
Expand Down Expand Up @@ -173,6 +174,8 @@ func testSeedPeering(t *testing.T, n int, dhtRouting DHTRouting, dhtSharedHost b
nodes := make([]*Node, n)

for i := 0; i < n; i++ {
dnslinkResolver, err := gateway.NewDNSResolver(nil)
require.NoError(t, err)
cfgs[i] = Config{
DataDir: t.TempDir(),
BlockstoreType: "flatfs",
Expand All @@ -183,6 +186,7 @@ func testSeedPeering(t *testing.T, n int, dhtRouting DHTRouting, dhtSharedHost b
SeedIndex: i,
SeedPeering: true,
SeedPeeringMaxIndex: n,
DNSLinkResolver: dnslinkResolver,
}

nodes[i], err = SetupWithLibp2p(ctx, cfgs[i], keys[i], cdns)
Expand Down

0 comments on commit 6ca2e4d

Please sign in to comment.