-
-
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
Co-Authored-By: Marcin Rataj <lidel@lidel.org> Co-Authored-By: Henrique Dias <hacdias@gmail.com>
- Loading branch information
1 parent
f4d361d
commit 53325b1
Showing
13 changed files
with
266 additions
and
137 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.