diff --git a/core/corehttp/hostname.go b/core/corehttp/hostname.go index 0b483c4e90bf..befef8c36738 100644 --- a/core/corehttp/hostname.go +++ b/core/corehttp/hostname.go @@ -1,6 +1,7 @@ package corehttp import ( + "context" "fmt" "net" "net/http" @@ -9,13 +10,16 @@ import ( cid "github.com/ipfs/go-cid" core "github.com/ipfs/go-ipfs/core" + coreapi "github.com/ipfs/go-ipfs/core/coreapi" namesys "github.com/ipfs/go-ipfs/namesys" + isd "github.com/jbenet/go-is-domain" "github.com/libp2p/go-libp2p-core/peer" mbase "github.com/multiformats/go-multibase" config "github.com/ipfs/go-ipfs-config" + iface "github.com/ipfs/interface-go-ipfs-core" + options "github.com/ipfs/interface-go-ipfs-core/options" nsopts "github.com/ipfs/interface-go-ipfs-core/options/namesys" - isd "github.com/jbenet/go-is-domain" ) var defaultPaths = []string{"/ipfs/", "/ipns/", "/api/", "/p2p/", "/version"} @@ -42,6 +46,11 @@ func HostnameOption() ServeOption { return func(n *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) { childMux := http.NewServeMux() + coreApi, err := coreapi.NewCoreAPI(n) + if err != nil { + return nil, err + } + cfg, err := n.Repo.Config() if err != nil { return nil, err @@ -115,7 +124,7 @@ func HostnameOption() ServeOption { // Not a whitelisted path // Try DNSLink, if it was not explicitly disabled for the hostname - if !gw.NoDNSLink && isDNSLinkRequest(r, n) { + if !gw.NoDNSLink && isDNSLinkRequest(n.Context(), coreApi, r) { // rewrite path and handle as DNSLink r.URL.Path = "/ipns/" + stripPort(r.Host) + r.URL.Path childMux.ServeHTTP(w, r) @@ -166,7 +175,7 @@ func HostnameOption() ServeOption { // 1. is wildcard DNSLink enabled (Gateway.NoDNSLink=false)? // 2. does Host header include a fully qualified domain name (FQDN)? // 3. does DNSLink record exist in DNS? - if !cfg.Gateway.NoDNSLink && isDNSLinkRequest(r, n) { + if !cfg.Gateway.NoDNSLink && isDNSLinkRequest(n.Context(), coreApi, r) { // rewrite path and handle as DNSLink r.URL.Path = "/ipns/" + stripPort(r.Host) + r.URL.Path childMux.ServeHTTP(w, r) @@ -221,14 +230,15 @@ func knownSubdomainDetails(hostname string, knownGateways map[string]config.Gate // isDNSLinkRequest returns bool that indicates if request // should return data from content path listed in DNSLink record (if exists) -func isDNSLinkRequest(r *http.Request, n *core.IpfsNode) bool { +func isDNSLinkRequest(ctx context.Context, ipfs iface.CoreAPI, r *http.Request) bool { fqdn := stripPort(r.Host) if len(fqdn) == 0 && !isd.IsDomain(fqdn) { return false } name := "/ipns/" + fqdn // check if DNSLink exists - _, err := n.Namesys.Resolve(n.Context(), name, nsopts.Depth(1)) + depth := options.Name.ResolveOption(nsopts.Depth(1)) + _, err := ipfs.Name().Resolve(ctx, name, depth) return err == nil || err == namesys.ErrResolveRecursion }