-
Notifications
You must be signed in to change notification settings - Fork 3
/
setup.go
186 lines (161 loc) · 4.03 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package https
import (
"crypto/tls"
"fmt"
"net/http"
"net/url"
"github.com/coredns/caddy"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
pkgtls "github.com/coredns/coredns/plugin/pkg/tls"
)
const maxUpstreams = 15
func init() { plugin.Register("https", setup) }
func setup(c *caddy.Controller) error {
conf, err := parseConfig(c)
if err != nil {
return plugin.Error("https", err)
}
dnsClient := setupDNSClient(conf)
h := newHTTPS(conf.from, dnsClient, withExcept(conf.except))
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
h.Next = next
return h
})
return nil
}
func setupDNSClient(conf *httpsConfig) dnsClient {
tr := &http.Transport{
TLSClientConfig: conf.tlsConfig,
ForceAttemptHTTP2: true,
}
httpClient := &http.Client{
Transport: tr,
}
clients := make([]dnsClient, len(conf.toURLs))
for i, toURL := range conf.toURLs {
clients[i] = newMetricDNSClient(newDoHDNSClient(httpClient, toURL), toURL)
}
var opts []lbDNSClientOption
if conf.policy != nil {
opts = append(opts, withLbPolicy(conf.policy))
}
// TODO request timeout, max_fail options
return newLoadBalanceDNSClient(clients, opts...)
}
type httpsConfig struct {
from string
toURLs []string
except []string
tlsConfig *tls.Config
tlsServerName string
policy policy
}
func parseConfig(c *caddy.Controller) (conf *httpsConfig, err error) {
conf = &httpsConfig{}
if !c.Next() {
return conf, c.ArgErr()
}
if !c.Args(&conf.from) {
return conf, c.ArgErr()
}
conf.from, err = parseHost(conf.from)
if err != nil {
return conf, err
}
toURLs := c.RemainingArgs()
if len(toURLs) == 0 {
return conf, c.ArgErr()
}
if len(toURLs) > maxUpstreams {
return conf, fmt.Errorf("more than %d TOs configured: %d", maxUpstreams, len(toURLs))
}
conf.toURLs = make([]string, 0, len(toURLs))
for _, to := range toURLs {
toURL := "https://" + to
if _, err := url.ParseRequestURI(toURL); err != nil {
return conf, err
}
conf.toURLs = append(conf.toURLs, toURL)
}
for c.NextBlock() {
if err := parseBlock(c, conf); err != nil {
return conf, err
}
}
if conf.tlsServerName != "" {
if conf.tlsConfig == nil {
conf.tlsConfig = new(tls.Config)
}
conf.tlsConfig.ServerName = conf.tlsServerName
}
return conf, nil
}
func parseBlock(c *caddy.Controller, conf *httpsConfig) (err error) {
f, ok := parseBlockMap[c.Val()]
if !ok {
return c.Errf("unknown property '%s'", c.Val())
}
return f(c, conf)
}
type parseBlockFunc func(*caddy.Controller, *httpsConfig) error
var parseBlockMap = map[string]parseBlockFunc{
"except": parseExcept,
"tls": parseTLS,
"tls_servername": parseTLSServerName,
"policy": parsePolicy,
}
func parseExcept(c *caddy.Controller, conf *httpsConfig) (err error) {
except := c.RemainingArgs()
if len(except) == 0 {
return c.ArgErr()
}
for i := 0; i < len(except); i++ {
if except[i], err = parseHost(except[i]); err != nil {
return
}
}
conf.except = except
return
}
func parseHost(hostAddr string) (string, error) {
hosts := plugin.Host(hostAddr).NormalizeExact()
if len(hosts) == 0 {
return "", fmt.Errorf("unable to normalize '%s'", hostAddr)
}
return plugin.Name(hosts[0]).Normalize(), nil
}
func parseTLS(c *caddy.Controller, conf *httpsConfig) error {
args := c.RemainingArgs()
tlsConfig, err := pkgtls.NewTLSConfigFromArgs(args...)
if err != nil {
return err
}
conf.tlsConfig = tlsConfig
return nil
}
func parseTLSServerName(c *caddy.Controller, conf *httpsConfig) error {
args := c.RemainingArgs()
if len(args) != 1 {
return c.ArgErr()
}
conf.tlsServerName = args[0]
return nil
}
func parsePolicy(c *caddy.Controller, conf *httpsConfig) error {
args := c.RemainingArgs()
if len(args) != 1 {
return c.ArgErr()
}
switch args[0] {
case "random":
conf.policy = newRandomPolicy()
case "round_robin":
conf.policy = newRoundRobinPolicy()
case "sequential":
conf.policy = newSequentialPolicy()
default:
return c.Errf("unknown policy '%s'", args[0])
}
return nil
}