feat: DNS.MaxCacheTTL for DNS-over-HTTPS resolvers (#8615)
* feat: Add MaxCacheTTL option for DNS-over-HTTPS resolvers * Update libp2p/go-doh-resolver dependencies to support Options * feat: Add `DNS.MaxCacheTTL` config documentation * docs: DNS.MaxCacheTTL * chore: go-ipfs-config@v0.19.0 Co-authored-by: Gus Eggert <gus@gus.dev> Co-authored-by: Marcin Rataj <lidel@lidel.org>
Thibault Meunier committed
Feb 10, 2022 at 21:26 UTC
a93907a40f8e19d73cb81c8612beef3b63a5abc0
4 files changed
+38
-13
core/node/dns.go
+10
-3
@@ -2,7 +2,9 @@ package node
2
3
import (
4
"fmt"
5
+ "math"
6
"strings"
7
+ "time"
8
9
config "github.com/ipfs/go-ipfs-config"
10
doh "github.com/libp2p/go-doh-resolver"
@@ -16,18 +18,23 @@ var defaultResolvers = map[string]string{
18
"crypto.": "https://resolver.cloudflare-eth.com/dns-query",
19
}
20
19
-func newResolver(url string) (madns.BasicResolver, error) {
21
+func newResolver(url string, opts ...doh.Option) (madns.BasicResolver, error) {
22
if !strings.HasPrefix(url, "https://") {
23
return nil, fmt.Errorf("invalid resolver url: %s", url)
24
}
25
24
- return doh.NewResolver(url), nil
26
+ return doh.NewResolver(url, opts...)
27
}
28
29
func DNSResolver(cfg *config.Config) (*madns.Resolver, error) {
30
var opts []madns.Option
31
var err error
32
33
+ var dohOpts []doh.Option
34
+ if !cfg.DNS.MaxCacheTTL.IsDefault() {
35
+ dohOpts = append(dohOpts, doh.WithMaxCacheTTL(cfg.DNS.MaxCacheTTL.WithDefault(time.Duration(math.MaxUint32)*time.Second)))
36
+ }
37
+
38
domains := make(map[string]struct{}) // to track overridden default resolvers
39
rslvrs := make(map[string]madns.BasicResolver) // to reuse resolvers for the same URL
40
@@ -44,7 +51,7 @@ func DNSResolver(cfg *config.Config) (*madns.Resolver, error) {
51
52
rslv, ok := rslvrs[url]
53
if !ok {
47
- rslv, err = newResolver(url)
54
+ rslv, err = newResolver(url, dohOpts...)
55
if err != nil {
56
return nil, fmt.Errorf("bad resolver for %s: %w", domain, err)
57
}
docs/config.md
+22
-4
@@ -140,7 +140,8 @@ config file at runtime.
140
- [`Swarm.Transports.Multiplexers.Yamux`](#swarmtransportsmultiplexersyamux)
141
- [`Swarm.Transports.Multiplexers.Mplex`](#swarmtransportsmultiplexersmplex)
142
- [`DNS`](#dns)
143
- - [`DNS.Resolvers`](#dnsresolvers)
143
+ - [`DNS.Resolvers`](#dnsresolvers)
144
+ - [`DNS.MaxCacheTTL`](#dnsmaxcachettl)
145
146
147
@@ -1756,7 +1757,7 @@ Type: `priority`
1757
1758
Options for configuring DNS resolution for [DNSLink](https://docs.ipfs.io/concepts/dnslink/) and `/dns*` [Multiaddrs](https://github.com/multiformats/multiaddr/).
1759
1759
-## `DNS.Resolvers`
1760
+### `DNS.Resolvers`
1761
1762
Map of [FQDNs](https://en.wikipedia.org/wiki/Fully_qualified_domain_name) to custom resolver URLs.
1763
@@ -1771,7 +1772,7 @@ Example:
1772
"eth.": "https://eth.link/dns-query",
1773
"crypto.": "https://resolver.unstoppable.io/dns-query",
1774
"libre.": "https://ns1.iriseden.fr/dns-query",
1774
- ".": "https://doh-ch.blahdns.com:4443/dns-query"
1775
+ ".": "https://cloudflare-dns.com/dns-query"
1776
}
1777
}
1778
}
@@ -1784,7 +1785,7 @@ Be mindful that:
1785
```json
1786
{
1787
"eth.": "https://resolver.cloudflare-eth.com/dns-query",
1787
- "crypto.": "https://resolver.cloudflare-eth.com/dns-query
1788
+ "crypto.": "https://resolver.cloudflare-eth.com/dns-query"
1789
}
1790
```
1791
To get all the benefits of a decentralized naming system we strongly suggest setting DoH endpoint to an empty string and running own decentralized resolver as catch-all one on localhost.
@@ -1792,3 +1793,20 @@ Be mindful that:
1793
Default: `{}`
1794
1795
Type: `object[string -> string]`
1796
+
1797
+### `DNS.MaxCacheTTL`
1798
+
1799
+Maximum duration for which entries are valid in the DoH cache.
1800
+
1801
+This allows you to cap the Time-To-Live suggested by the DNS response ([RFC2181](https://datatracker.ietf.org/doc/html/rfc2181#section-8)).
1802
+If present, the upper bound is applied to DoH resolvers in [`DNS.Resolvers`](#dnsresolvers).
1803
+
1804
+Note: this does NOT work with Go's default DNS resolver. To make this a global setting, add a `.` entry to `DNS.Resolvers` first.
1805
+
1806
+**Examples:**
1807
+* `"5m"` DNS entries are kept for 5 minutes or less.
1808
+* `"0s"` DNS entries expire as soon as they are retrieved.
1809
+
1810
+Default: Respect DNS Response TTL
1811
+
1812
+Type: `optionalDuration`
go.mod
+2
-2
@@ -31,7 +31,7 @@ require (
31
github.com/ipfs/go-ipfs-blockstore v1.1.2
32
github.com/ipfs/go-ipfs-chunker v0.0.5
33
github.com/ipfs/go-ipfs-cmds v0.6.0
34
- github.com/ipfs/go-ipfs-config v0.18.0
34
+ github.com/ipfs/go-ipfs-config v0.19.0
35
github.com/ipfs/go-ipfs-exchange-interface v0.1.0
36
github.com/ipfs/go-ipfs-exchange-offline v0.1.1
37
github.com/ipfs/go-ipfs-files v0.0.9
@@ -65,7 +65,7 @@ require (
65
github.com/jbenet/go-random v0.0.0-20190219211222-123a90aedc0c
66
github.com/jbenet/go-temp-err-catcher v0.1.0
67
github.com/jbenet/goprocess v0.1.4
68
- github.com/libp2p/go-doh-resolver v0.3.1
68
+ github.com/libp2p/go-doh-resolver v0.4.0
69
github.com/libp2p/go-libp2p v0.16.0
70
github.com/libp2p/go-libp2p-connmgr v0.2.4
71
github.com/libp2p/go-libp2p-core v0.11.0
go.sum
+4
-4
@@ -475,8 +475,8 @@ github.com/ipfs/go-ipfs-chunker v0.0.5 h1:ojCf7HV/m+uS2vhUGWcogIIxiO5ubl5O57Q7Na
475
github.com/ipfs/go-ipfs-chunker v0.0.5/go.mod h1:jhgdF8vxRHycr00k13FM8Y0E+6BoalYeobXmUyTreP8=
476
github.com/ipfs/go-ipfs-cmds v0.6.0 h1:yAxdowQZzoFKjcLI08sXVNnqVj3jnABbf9smrPQmBsw=
477
github.com/ipfs/go-ipfs-cmds v0.6.0/go.mod h1:ZgYiWVnCk43ChwoH8hAmI1IRbuVtq3GSTHwtRB/Kqhk=
478
-github.com/ipfs/go-ipfs-config v0.18.0 h1:Ta1aNGNEq6RIvzbw7dqzCVZJKb7j+Dd35JFnAOCpT8g=
479
-github.com/ipfs/go-ipfs-config v0.18.0/go.mod h1:wz2lKzOjgJeYJa6zx8W9VT7mz+iSd0laBMqS/9wmX6A=
478
+github.com/ipfs/go-ipfs-config v0.19.0 h1:OuKIL+BkOZgJ+hb4Wg/9ynCtE/BaZBWcGy8hgdMepAo=
479
+github.com/ipfs/go-ipfs-config v0.19.0/go.mod h1:wz2lKzOjgJeYJa6zx8W9VT7mz+iSd0laBMqS/9wmX6A=
480
github.com/ipfs/go-ipfs-delay v0.0.0-20181109222059-70721b86a9a8/go.mod h1:8SP1YXK1M1kXuc4KJZINY3TQQ03J2rwBG9QfXmbRPrw=
481
github.com/ipfs/go-ipfs-delay v0.0.1 h1:r/UXYyRcddO6thwOnhiznIAiSvxMECGgtv35Xs1IeRQ=
482
github.com/ipfs/go-ipfs-delay v0.0.1/go.mod h1:8SP1YXK1M1kXuc4KJZINY3TQQ03J2rwBG9QfXmbRPrw=
@@ -673,8 +673,8 @@ github.com/libp2p/go-conn-security-multistream v0.2.0/go.mod h1:hZN4MjlNetKD3Rq5
673
github.com/libp2p/go-conn-security-multistream v0.2.1/go.mod h1:cR1d8gA0Hr59Fj6NhaTpFhJZrjSYuNmhpT2r25zYR70=
674
github.com/libp2p/go-conn-security-multistream v0.3.0 h1:9UCIKlBL1hC9u7nkMXpD1nkc/T53PKMAn3/k9ivBAVc=
675
github.com/libp2p/go-conn-security-multistream v0.3.0/go.mod h1:EEP47t4fw/bTelVmEzIDqSe69hO/ip52xBEhZMLWAHM=
676
-github.com/libp2p/go-doh-resolver v0.3.1 h1:1wbVGkB4Tdj4WEvjAuYknOPyt4vSSDn9thnj13pKPaY=
677
-github.com/libp2p/go-doh-resolver v0.3.1/go.mod h1:y5go1ZppAq9N2eppbX0xON01CyPBeUg2yS6BTssssog=
676
+github.com/libp2p/go-doh-resolver v0.4.0 h1:gUBa1f1XsPwtpE1du0O+nnZCUqtG7oYi7Bb+0S7FQqw=
677
+github.com/libp2p/go-doh-resolver v0.4.0/go.mod h1:v1/jwsFusgsWIGX/c6vCRrnJ60x7bhTiq/fs2qt0cAg=
678
github.com/libp2p/go-eventbus v0.1.0/go.mod h1:vROgu5cs5T7cv7POWlWxBaVLxfSegC5UGQf8A2eEmx4=
679
github.com/libp2p/go-eventbus v0.2.1 h1:VanAdErQnpTioN2TowqNcOijf6YwhuODe4pPKSDpxGc=
680
github.com/libp2p/go-eventbus v0.2.1/go.mod h1:jc2S4SoEVPP48H9Wpzm5aiGwUCBMfGhVhhBjyhhCJs8=