-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: switch gateway code to new API from go-libipfs (#9681)
Co-authored-by: Marcin Rataj <[email protected]> Co-authored-by: Henrique Dias <[email protected]>
- Loading branch information
1 parent
ebdca5c
commit 353dd49
Showing
12 changed files
with
296 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,20 @@ | ||
package node | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
"strings" | ||
"time" | ||
|
||
"github.com/ipfs/boxo/gateway" | ||
config "github.com/ipfs/kubo/config" | ||
doh "github.com/libp2p/go-doh-resolver" | ||
madns "github.com/multiformats/go-multiaddr-dns" | ||
|
||
"github.com/miekg/dns" | ||
) | ||
|
||
var defaultResolvers = map[string]string{ | ||
"eth.": "https://resolver.cloudflare-eth.com/dns-query", | ||
"crypto.": "https://resolver.cloudflare-eth.com/dns-query", | ||
} | ||
|
||
func newResolver(url string, opts ...doh.Option) (madns.BasicResolver, error) { | ||
if !strings.HasPrefix(url, "https://") { | ||
return nil, fmt.Errorf("invalid resolver url: %s", url) | ||
} | ||
|
||
return doh.NewResolver(url, opts...) | ||
} | ||
|
||
func DNSResolver(cfg *config.Config) (*madns.Resolver, error) { | ||
var opts []madns.Option | ||
var err error | ||
|
||
var dohOpts []doh.Option | ||
if !cfg.DNS.MaxCacheTTL.IsDefault() { | ||
dohOpts = append(dohOpts, doh.WithMaxCacheTTL(cfg.DNS.MaxCacheTTL.WithDefault(time.Duration(math.MaxUint32)*time.Second))) | ||
} | ||
|
||
domains := make(map[string]struct{}) // to track overridden default resolvers | ||
rslvrs := make(map[string]madns.BasicResolver) // to reuse resolvers for the same URL | ||
|
||
for domain, url := range cfg.DNS.Resolvers { | ||
if domain != "." && !dns.IsFqdn(domain) { | ||
return nil, fmt.Errorf("invalid domain %s; must be FQDN", domain) | ||
} | ||
|
||
domains[domain] = struct{}{} | ||
if url == "" { | ||
// allow overriding of implicit defaults with the default resolver | ||
continue | ||
} | ||
|
||
rslv, ok := rslvrs[url] | ||
if !ok { | ||
rslv, err = newResolver(url, dohOpts...) | ||
if err != nil { | ||
return nil, fmt.Errorf("bad resolver for %s: %w", domain, err) | ||
} | ||
rslvrs[url] = rslv | ||
} | ||
|
||
if domain != "." { | ||
opts = append(opts, madns.WithDomainResolver(domain, rslv)) | ||
} else { | ||
opts = append(opts, madns.WithDefaultResolver(rslv)) | ||
} | ||
} | ||
|
||
// fill in defaults if not overridden by the user | ||
for domain, url := range defaultResolvers { | ||
_, ok := domains[domain] | ||
if ok { | ||
continue | ||
} | ||
|
||
rslv, ok := rslvrs[url] | ||
if !ok { | ||
rslv, err = newResolver(url) | ||
if err != nil { | ||
return nil, fmt.Errorf("bad resolver for %s: %w", domain, err) | ||
} | ||
rslvrs[url] = rslv | ||
} | ||
|
||
opts = append(opts, madns.WithDomainResolver(domain, rslv)) | ||
} | ||
|
||
return madns.NewResolver(opts...) | ||
return gateway.NewDNSResolver(cfg.DNS.Resolvers, dohOpts...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.