-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.go
153 lines (146 loc) · 4.62 KB
/
setup.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package recursor
import (
"context"
"fmt"
"github.com/coredns/caddy"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
"github.com/prometheus/client_golang/prometheus"
"net"
"net/url"
"strconv"
"strings"
"time"
)
// init registers this plugin.
func init() { plugin.Register(pluginName, setup) }
func setup(c *caddy.Controller) error {
cfg := dnsserver.GetConfig(c)
log.Infof("Setup plugin %s/%s, zone: %s, port: %s", pluginName, pluginVersion, cfg.Zone, cfg.Port)
rcuCfg, err := readCaddyControllerConfig(c)
if err != nil {
return plugin.Error(pluginName, fmt.Errorf("%s/%s read config error: %w", pluginName, pluginVersion, err))
}
rcu, err := createRecursor(rcuCfg)
if err != nil {
return plugin.Error(pluginName, fmt.Errorf("%s/%s create recursor error: %w", pluginName, pluginVersion, err))
}
updateInfoMetrics(cfg.Port, rcu)
if rcu.verbose > 1 {
log.Infof("Plugin %s/%s created (zone '%s'): %s", pluginName, pluginVersion, rcu.zone, rcu.String())
}
cfg.AddPlugin(func(next plugin.Handler) plugin.Handler {
rcu.Next = next
if rcu.verbose > 0 {
log.Infof("Plugin %s/%s added (zone '%s')", pluginName, pluginVersion, rcu.zone)
}
return rcu
})
return nil
}
func updateInfoMetrics(port string, rcu recursor) {
promBuildInfo.With(prometheus.Labels{"version": pluginVersion}).Set(0)
for name, def := range rcu.resolvers {
promResolvesInfo.With(prometheus.Labels{"port": port, "zone": rcu.zone, "resolver": name, "urls": strings.Join(def.urls, ",")}).Set(1)
}
for name, def := range rcu.aliases {
promAliasesInfo.With(prometheus.Labels{"port": port, "zone": rcu.zone, "alias": name, "resolver": def.resolverDefRef.name, "ttl": strconv.Itoa(int(def.ttl))}).Set(1)
for _, host := range def.hosts {
promAliasesEntriesInfo.With(prometheus.Labels{"port": port, "zone": rcu.zone, "alias": name, "resolver": def.resolverDefRef.name, "type": "host", "entry": host}).Set(1)
}
for _, ip := range def.ips {
promAliasesEntriesInfo.With(prometheus.Labels{"port": port, "zone": rcu.zone, "alias": name, "resolver": def.resolverDefRef.name, "type": "ip", "entry": ip.String()}).Set(1)
}
}
}
func createRecursor(cfg recursorCfg) (recursor, error) {
r := recursor{
verbose: cfg.Verbose,
zone: cfg.Zone,
resolvers: map[string]resolverDef{},
aliases: map[string]aliasDef{},
}
r.resolvers[defaultResolverName] = resolverDef{
name: defaultResolverName,
resolverRefs: []*net.Resolver{net.DefaultResolver},
urls: []string{"://" + defaultResolverName},
}
for key, rslCfg := range cfg.Resolvers {
rsl, err := createResolver(key, rslCfg)
if err != nil {
return r, fmt.Errorf("creating resolver '%s' error: %w, config: %s", key, err, rslCfg.String())
}
r.resolvers[key] = rsl
}
for key, aCfg := range cfg.Aliases {
a, err := createAlias(key, aCfg, r.resolvers)
if err != nil {
return r, fmt.Errorf("creating alias '%s' error: %w, config: %s", key, err, aCfg.String())
}
r.aliases[key] = a
}
return r, nil
}
func createResolver(name string, p resolverCfg) (resolverDef, error) {
rslDef := resolverDef{
name: name,
}
if len(p.Urls) < 1 {
return rslDef, fmt.Errorf("resolver urls can't be empty")
} else {
for _, urlStr := range p.Urls {
u, err := url.Parse(urlStr)
if err != nil {
return resolverDef{}, fmt.Errorf("parsing resolver urls '%v' error: %w", urlStr, err)
}
var resolverRef = &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
d := net.Dialer{}
if p.TimeoutMs > 0 {
d = net.Dialer{
Timeout: time.Duration(p.TimeoutMs) * time.Millisecond,
}
}
return d.DialContext(ctx, u.Scheme, u.Host)
},
}
rslDef.resolverRefs = append(rslDef.resolverRefs, resolverRef)
rslDef.urls = append(rslDef.urls, urlStr)
}
return rslDef, nil
}
}
func createAlias(aName string, aCfg aliasCfg, resolvers map[string]resolverDef) (aliasDef, error) {
a := aliasDef{
ips: []net.IP{},
hosts: aCfg.Hosts,
shuffleIps: aCfg.ShuffleIps,
ttl: aCfg.Ttl,
}
var ips []net.IP
for _, ip := range aCfg.Ips {
addr := net.ParseIP(ip)
if addr != nil {
ips = append(ips, addr)
} else {
return a, fmt.Errorf("wrong alias ip '%s'", ip)
}
}
a.ips = ips
if aName != "*" {
if len(a.ips) == 0 && len(a.hosts) == 0 {
return a, fmt.Errorf("alias ips and hosts are empty")
}
}
rslName := defaultResolverName
if len(aCfg.ResolverName) > 0 {
rslName = aCfg.ResolverName
}
if r, ok := resolvers[rslName]; ok {
a.resolverDefRef = &r
} else {
return a, fmt.Errorf("alias resolver '%s' not found", rslName)
}
return a, nil
}