...
1 package dns
2
3 import (
4 "bufio"
5 "io"
6 "os"
7 "strconv"
8 "strings"
9 )
10
11
12 type ClientConfig struct {
13 Servers []string
14 Search []string
15 Port string
16 Ndots int
17 Timeout int
18 Attempts int
19 }
20
21
22
23 func ClientConfigFromFile(resolvconf string) (*ClientConfig, error) {
24 file, err := os.Open(resolvconf)
25 if err != nil {
26 return nil, err
27 }
28 defer file.Close()
29 return ClientConfigFromReader(file)
30 }
31
32
33 func ClientConfigFromReader(resolvconf io.Reader) (*ClientConfig, error) {
34 c := new(ClientConfig)
35 scanner := bufio.NewScanner(resolvconf)
36 c.Servers = make([]string, 0)
37 c.Search = make([]string, 0)
38 c.Port = "53"
39 c.Ndots = 1
40 c.Timeout = 5
41 c.Attempts = 2
42
43 for scanner.Scan() {
44 if err := scanner.Err(); err != nil {
45 return nil, err
46 }
47 line := scanner.Text()
48 f := strings.Fields(line)
49 if len(f) < 1 {
50 continue
51 }
52 switch f[0] {
53 case "nameserver":
54 if len(f) > 1 {
55
56
57
58 name := f[1]
59 c.Servers = append(c.Servers, name)
60 }
61
62 case "domain":
63 if len(f) > 1 {
64 c.Search = make([]string, 1)
65 c.Search[0] = f[1]
66 } else {
67 c.Search = make([]string, 0)
68 }
69
70 case "search":
71 c.Search = cloneSlice(f[1:])
72
73 case "options":
74 for _, s := range f[1:] {
75 switch {
76 case len(s) >= 6 && s[:6] == "ndots:":
77 n, _ := strconv.Atoi(s[6:])
78 if n < 0 {
79 n = 0
80 } else if n > 15 {
81 n = 15
82 }
83 c.Ndots = n
84 case len(s) >= 8 && s[:8] == "timeout:":
85 n, _ := strconv.Atoi(s[8:])
86 if n < 1 {
87 n = 1
88 }
89 c.Timeout = n
90 case len(s) >= 9 && s[:9] == "attempts:":
91 n, _ := strconv.Atoi(s[9:])
92 if n < 1 {
93 n = 1
94 }
95 c.Attempts = n
96 case s == "rotate":
97
98 }
99 }
100 }
101 }
102 return c, nil
103 }
104
105
106
107
108 func (c *ClientConfig) NameList(name string) []string {
109
110 if IsFqdn(name) {
111 return []string{name}
112 }
113
114
115
116 hasNdots := CountLabel(name) > c.Ndots
117
118 name = Fqdn(name)
119
120
121 names := []string{}
122
123
124 if hasNdots {
125 names = append(names, name)
126 }
127 for _, s := range c.Search {
128 names = append(names, Fqdn(name+s))
129 }
130
131 if !hasNdots {
132 names = append(names, name)
133 }
134 return names
135 }
136
View as plain text