-
Notifications
You must be signed in to change notification settings - Fork 22
/
config.go
71 lines (59 loc) · 1.35 KB
/
config.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
package main
import (
"fmt"
"io/ioutil"
"strings"
"github.com/miekg/dns"
"gopkg.in/yaml.v2"
)
type PredefinedRecords map[query][]string
func (p *PredefinedRecords) UnmarshalYAML(unmarshal func(interface{}) error) error {
if *p == nil {
*p = make(PredefinedRecords)
}
conf := map[string]map[string][]string{}
if err := unmarshal(&conf); err != nil {
return err
}
for domain, replies := range conf {
if !strings.HasSuffix(domain, ".") {
domain = domain + "."
}
for queryType, rrs := range replies {
t, ok := dns.StringToType[queryType]
if !ok {
return fmt.Errorf("unknown query type: %v", queryType)
}
for _, rr := range rrs {
if _, err := dns.NewRR(rr); err != nil {
return fmt.Errorf("failed to parse %#v for %v %v: %v",
rr, queryType, domain, err)
}
}
q := query{
t: t,
name: domain,
}
(*p)[q] = rrs
}
}
return nil
}
type Config struct {
Domain string `yaml:"domain"`
PredefinedRecords PredefinedRecords `yaml:"predefined_records"`
HTTP struct {
ListenOn []string `yaml:"listen_on"`
} `yaml:"http"`
}
func NewConfig(filename string) (*Config, error) {
data, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
conf := &Config{}
if err := yaml.Unmarshal(data, conf); err != nil {
return nil, err
}
return conf, nil
}