forked from rancher/rancher-dns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.go
More file actions
56 lines (48 loc) · 1.2 KB
/
Copy pathparse.go
File metadata and controls
56 lines (48 loc) · 1.2 KB
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
package main
import (
"fmt"
"os"
"strings"
log "github.com/PastureStack/internal-dns/internal/logging"
yaml "gopkg.in/yaml.v3"
)
const maxAnswersFileBytes = 64 << 20
func ParseAnswers(path string) (out Answers, err error) {
out = make(Answers)
info, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
log.Warn("Failed to find: ", path)
return out, nil
}
return nil, err
}
if info.Size() > maxAnswersFileBytes {
return nil, fmt.Errorf("answers file exceeds %d bytes", maxAnswersFileBytes)
}
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
if yaml.Unmarshal(data, &out); err != nil {
return nil, err
}
ConvertPtrIps(&out)
return out, nil
}
func ConvertPtrIps(answers *Answers) {
// Convert PTR keys that are IP addresses into "4.3.2.1.in-addr.arpa." form.
for _, client := range *answers {
for origKey, val := range client.Ptr {
if !strings.HasSuffix(origKey, "in-addr.arpa.") {
newKey := "in-addr.arpa."
for _, i := range strings.Split(origKey, ".") {
newKey = i + "." + newKey
}
delete(client.Ptr, origKey)
client.Ptr[newKey] = val
log.Debug("Transformed PTR for ", origKey, " to ", newKey, " => ", val.Answer)
}
}
}
}