2021-01-13 17:07:04 +00:00
|
|
|
package mock
|
2020-04-01 22:58:44 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"regexp"
|
2020-12-26 13:11:49 +00:00
|
|
|
|
|
|
|
"github.com/spf13/viper"
|
2020-04-01 22:58:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-10-02 09:56:48 +00:00
|
|
|
fallbackArgsConfigKey = "fallback.args"
|
2020-04-01 22:58:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type resolverRule struct {
|
|
|
|
pattern *regexp.Regexp
|
|
|
|
response net.IP
|
|
|
|
}
|
|
|
|
|
|
|
|
type dnsOptions struct {
|
|
|
|
Rules []resolverRule
|
|
|
|
Fallback ResolverFallback
|
|
|
|
}
|
|
|
|
|
2020-10-02 09:56:48 +00:00
|
|
|
func loadFromConfig(config *viper.Viper) (options dnsOptions, err error) {
|
|
|
|
type rule struct {
|
|
|
|
Pattern string
|
|
|
|
Response string
|
|
|
|
}
|
|
|
|
|
|
|
|
type fallback struct {
|
|
|
|
Strategy string
|
|
|
|
}
|
2020-04-01 22:58:44 +00:00
|
|
|
|
2020-10-02 09:56:48 +00:00
|
|
|
opts := struct {
|
|
|
|
Rules []rule
|
|
|
|
Fallback fallback
|
|
|
|
}{}
|
|
|
|
|
|
|
|
err = config.Unmarshal(&opts)
|
|
|
|
|
|
|
|
for _, rule := range opts.Rules {
|
2020-04-01 22:58:44 +00:00
|
|
|
var err error
|
2020-10-02 09:56:48 +00:00
|
|
|
var rr resolverRule
|
|
|
|
if rr.pattern, err = regexp.Compile(rule.Pattern); err != nil {
|
2020-04-01 22:58:44 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-10-02 09:56:48 +00:00
|
|
|
if rr.response = net.ParseIP(rule.Response); rr.response == nil {
|
2020-04-01 22:58:44 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-10-02 09:56:48 +00:00
|
|
|
options.Rules = append(options.Rules, rr)
|
2020-04-01 22:58:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
options.Fallback = CreateResolverFallback(
|
2020-10-02 09:56:48 +00:00
|
|
|
opts.Fallback.Strategy,
|
2020-04-01 22:58:44 +00:00
|
|
|
config.Sub(fallbackArgsConfigKey),
|
|
|
|
)
|
|
|
|
|
2020-10-02 09:56:48 +00:00
|
|
|
return
|
2020-04-01 22:58:44 +00:00
|
|
|
}
|