Skip to content

Commit

Permalink
fix(gw): use real FQDN in DNSLink errors
Browse files Browse the repository at this point in the history
The old version was not very smart and wasted DNS lookup per
every inlined DNSLink label (my-v--long-example-com) before
retrying with un-inlined one (my.v-long.example.com).

This version is optimized to avoid unnecessary DNS lookup for
the golden path majority of inlined DNS names on subdomain will hit.

It also ensures that in case of missing DNSLink, the un-inlined domain
name is used in error path, which makes better UX.
  • Loading branch information
lidel authored and hacdias committed Oct 19, 2023
1 parent a50f784 commit a7e134e
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions gateway/hostname.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,32 @@ func NewHostnameHandler(c Config, backend IPFSBackend, next http.Handler) http.H
// can be loaded from a subdomain gateway with a wildcard
// TLS cert if represented as a single DNS label:
// https://my-v--long-example-com.ipns.dweb.link
if ns == "ipns" && !strings.Contains(rootID, ".") {
// if there is no TXT recordfor rootID
if !hasDNSLinkRecord(r.Context(), backend, rootID) {
// my-v--long-example-com → my.v-long.example.com
dnslinkFQDN := toDNSLinkFQDN(rootID)
if hasDNSLinkRecord(r.Context(), backend, dnslinkFQDN) {
// update path prefix to use real FQDN with DNSLink
pathPrefix = "/ipns/" + dnslinkFQDN
}
if ns == "ipns" && !strings.Contains(rootID, ".") && strings.Contains(rootID, "-") {
// If there are no '.' but '-' is present in rootID, we most
// likely have an inlined DNSLink (like my-v--long-example-com)

// We un-inline and check for DNSLink presence on domain with '.'
// first to minimize the amount of DNS lookups:
// my-v--long-example-com → my.v-long.example.com
dnslinkFQDN := toDNSLinkFQDN(rootID)

// Does _dnslink.my.v-long.example.com exist?
if hasDNSLinkRecord(r.Context(), backend, dnslinkFQDN) {
// Un-inlined DNS name has a valid DNSLink record.
// Update path prefix to use un-inlined FQDN in gateway processing.
pathPrefix = "/ipns/" + dnslinkFQDN // → /ipns/my.v-long.example.com

} else if !hasDNSLinkRecord(r.Context(), backend, rootID) {
// Inspected _dnslink.my-v--long-example-com as a
// fallback, but it had no DNSLink record either.

// At this point it is more likely the un-inlined
// dnslinkFQDN is what the end user wanted to load, so
// we switch to that. This ensures the error message
// about missing DNSLink will use the un-inlined FQDN,
// and not the inlined one.
pathPrefix = "/ipns/" + dnslinkFQDN

}
}
}
Expand Down

0 comments on commit a7e134e

Please sign in to comment.