-
Notifications
You must be signed in to change notification settings - Fork 384
/
dns.go
139 lines (122 loc) · 4.55 KB
/
dns.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package models
import (
"encoding/json"
"fmt"
"strings"
)
// DefaultTTL is applied to any DNS record without an explicit TTL.
const DefaultTTL = uint32(300)
// DNSConfig describes the desired DNS configuration, usually loaded from dnsconfig.js.
type DNSConfig struct {
Registrars []*RegistrarConfig `json:"registrars"`
DNSProviders []*DNSProviderConfig `json:"dns_providers"`
Domains []*DomainConfig `json:"domains"`
RegistrarsByName map[string]*RegistrarConfig `json:"-"`
DNSProvidersByName map[string]*DNSProviderConfig `json:"-"`
SkipRecordAudit bool `json:"skiprecordaudit,omitempty"`
}
// FindDomain returns the *DomainConfig for domain query in config.
func (config *DNSConfig) FindDomain(query string) *DomainConfig {
for _, b := range config.Domains {
if b.Name == query {
return b
}
}
return nil
}
// RegistrarConfig describes a registrar.
type RegistrarConfig struct {
Name string `json:"name"`
Type string `json:"type"`
Metadata json.RawMessage `json:"meta,omitempty"`
}
// DNSProviderConfig describes a DNS service provider.
type DNSProviderConfig struct {
Name string `json:"name"`
Type string `json:"type"`
Metadata json.RawMessage `json:"meta,omitempty"`
}
// FIXME(tal): In hindsight, the Nameserver struct is overkill. We
// could have just used []string. Now every provider calls StringsToNameservers
// and ever user calls StringsToNameservers. We should refactor this
// some day. https://github.com/StackExchange/dnscontrol/issues/577
// Nameserver describes a nameserver.
type Nameserver struct {
Name string `json:"name"` // Normalized to a FQDN with NO trailing "."
// NB(tlim): DomainConfig.Nameservers are stored WITH a trailing "." (Sorry!)
}
func (n *Nameserver) String() string {
return n.Name
}
// StringsToNameservers constructs a list of *Nameserver structs using a list of FQDNs.
// Deprecated. Please use ToNameservers, or maybe ToNameserversStripTD instead.
// See https://github.com/StackExchange/dnscontrol/issues/491
func StringsToNameservers(nss []string) []*Nameserver {
nservers := []*Nameserver{}
for _, ns := range nss {
nservers = append(nservers, &Nameserver{Name: ns})
}
return nservers
}
// ToNameservers turns a list of strings into a list of Nameservers.
// It is an error if any string has a trailing dot. Either remove the
// trailing dot before you call this or (much preferred) use ToNameserversStripTD.
func ToNameservers(nss []string) ([]*Nameserver, error) {
nservers := []*Nameserver{}
for _, ns := range nss {
if strings.HasSuffix(ns, ".") {
return nil, fmt.Errorf("provider code leaves trailing dot on nameserver")
// If you see this error, maybe the provider should call
// ToNameserversStripTD instead.
}
nservers = append(nservers, &Nameserver{Name: ns})
}
return nservers, nil
}
// ToNameserversStripTD is like ToNameservers but strips the trailing
// dot from each item. It is an error if there is no trailing dot.
func ToNameserversStripTD(nss []string) ([]*Nameserver, error) {
nservers := []*Nameserver{}
for _, ns := range nss {
if !strings.HasSuffix(ns, ".") {
return nil, fmt.Errorf("provider code already removed nameserver trailing dot (%v)", ns)
// If you see this error, maybe the provider should call ToNameservers instead.
}
nservers = append(nservers, &Nameserver{Name: ns[0 : len(ns)-1]})
}
return nservers, nil
}
// NameserversToStrings constructs a list of strings from *Nameserver structs
func NameserversToStrings(nss []*Nameserver) (s []string) {
for _, ns := range nss {
s = append(s, ns.Name)
}
return s
}
// Correction is anything that can be run. Implementation is up to the specific provider.
type Correction struct {
F func() error `json:"-"`
Msg string
}
// DomainContainingFQDN finds the best domain from the dns config for the given record fqdn.
// It will chose the domain whose name is the longest suffix match for the fqdn.
func (config *DNSConfig) DomainContainingFQDN(fqdn string) *DomainConfig {
fqdn = strings.TrimSuffix(fqdn, ".")
longestLength := 0
var d *DomainConfig
for _, dom := range config.Domains {
if (dom.Name == fqdn || strings.HasSuffix(fqdn, "."+dom.Name)) && len(dom.Name) > longestLength {
longestLength = len(dom.Name)
d = dom
}
}
return d
}
// IgnoreTarget describes an IGNORE_TARGET rule.
type IgnoreTarget struct {
Pattern string `json:"pattern"` // Glob pattern
Type string `json:"type"` // All caps rtype name.
}
func (i *IgnoreTarget) String() string {
return i.Pattern
}