forked from folbricht/routedns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathttl-modifier.go
68 lines (59 loc) · 1.53 KB
/
ttl-modifier.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package rdns
import (
"github.com/miekg/dns"
)
// TTLModifier passes queries to upstream resolvers and then modifies
// the TTL in response RRs according to limits.
type TTLModifier struct {
id string
TTLModifierOptions
resolver Resolver
}
var _ Resolver = &TTLModifier{}
type TTLModifierOptions struct {
// Minimum TTL, any RR with a TTL below will be updated to this value.
MinTTL uint32
// Maximum TTL, any RR with a TTL higher than this will have their value
// set to the max. A value of 0 disables the limit. Default 0.
MaxTTL uint32
}
// NewTTLModifier returns a new instance of a TTL modifier.
func NewTTLModifier(id string, resolver Resolver, opt TTLModifierOptions) *TTLModifier {
return &TTLModifier{
id: id,
TTLModifierOptions: opt,
resolver: resolver,
}
}
// Resolve a DNS query by first resoling it upstream, then applying TTL limits
// on the response.
func (r *TTLModifier) Resolve(q *dns.Msg, ci ClientInfo) (*dns.Msg, error) {
a, err := r.resolver.Resolve(q, ci)
if err != nil || a == nil {
return a, err
}
var modified bool
for _, rrs := range [][]dns.RR{a.Answer, a.Ns, a.Extra} {
for _, rr := range rrs {
if _, ok := rr.(*dns.OPT); ok {
continue
}
h := rr.Header()
if h.Ttl < r.MinTTL {
h.Ttl = r.MinTTL
modified = true
}
if r.MaxTTL > 0 && h.Ttl > r.MaxTTL {
h.Ttl = r.MaxTTL
modified = true
}
}
}
if modified {
logger(r.id, q, ci).Debug("modified response ttl")
}
return a, nil
}
func (r *TTLModifier) String() string {
return r.id
}