mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-21 18:37:45 +08:00
update resolver config parsing, add eth. resolver by default
This commit is contained in:
parent
b6a1ba9617
commit
0ef2f8c9ea
@ -7,21 +7,48 @@ import (
|
||||
config "github.com/ipfs/go-ipfs-config"
|
||||
doh "github.com/libp2p/go-doh-resolver"
|
||||
madns "github.com/multiformats/go-multiaddr-dns"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
const ethResolverURL = "https://eth.link/dns-query"
|
||||
|
||||
func newResolver(url string) (madns.BasicResolver, error) {
|
||||
if !strings.HasPrefix(url, "https://") {
|
||||
return nil, fmt.Errorf("invalid resolver url: %s", url)
|
||||
}
|
||||
|
||||
return doh.NewResolver(url), nil
|
||||
}
|
||||
|
||||
func DNSResolver(cfg *config.Config) (*madns.Resolver, error) {
|
||||
var opts []madns.Option
|
||||
if url := cfg.DNS.DefaultResolver; url != "" {
|
||||
if !strings.HasPrefix(url, "https://") {
|
||||
return nil, fmt.Errorf("invalid default resolver url: %s", url)
|
||||
|
||||
hasEth := false
|
||||
for domain, url := range cfg.DNS.Resolvers {
|
||||
if !dns.IsFqdn(domain) {
|
||||
return nil, fmt.Errorf("invalid domain %s; must be FQDN", domain)
|
||||
}
|
||||
opts = append(opts, madns.WithDefaultResolver(doh.NewResolver(url)))
|
||||
}
|
||||
for domain, url := range cfg.DNS.CustomResolvers {
|
||||
if !strings.HasPrefix(url, "https://") {
|
||||
return nil, fmt.Errorf("invalid domain resolver url for %s: %s", domain, url)
|
||||
|
||||
rslv, err := newResolver(url)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("bad resolver for %s: %w", domain, err)
|
||||
}
|
||||
|
||||
if domain != "." {
|
||||
opts = append(opts, madns.WithDomainResolver(domain, rslv))
|
||||
} else {
|
||||
opts = append(opts, madns.WithDefaultResolver(rslv))
|
||||
}
|
||||
|
||||
if domain == "eth." {
|
||||
hasEth = true
|
||||
}
|
||||
opts = append(opts, madns.WithDomainResolver(domain, doh.NewResolver(url)))
|
||||
}
|
||||
|
||||
if !hasEth {
|
||||
opts = append(opts, madns.WithDomainResolver("eth.", doh.NewResolver(ethResolverURL)))
|
||||
}
|
||||
|
||||
return madns.NewResolver(opts...)
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user