-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdb.go
100 lines (88 loc) · 2.65 KB
/
db.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
package yesdns
// Depends on:
// resolver.go/
import (
"github.com/nanobox-io/golang-scribble"
"log"
"strconv"
"encoding/json"
"bytes"
)
//
// Database interface
//
type Database struct {
db *scribble.Driver
}
func NewDatabase(scribbleDbDir string) (error, *Database) {
db, err := scribble.New(scribbleDbDir, nil)
if err != nil {
return err, nil
}
database := Database{db: db}
return nil, &database
}
func (d Database) WriteDnsMessage(dnsRecord DnsMessage) error {
log.Printf("DEBUG Saving %s to db\n", dnsRecord)
// We create records for every resolver
for _, resolverId := range dnsRecord.Resolvers {
// We have 1 document in the db for every entry in Question section
for _, question := range dnsRecord.Question {
key := resolverId + "/" + strconv.Itoa(int(question.Qtype))
err := d.db.Write(key, question.Qname, dnsRecord)
if err != nil {
return err
}
}
}
return nil
}
func (d Database) WriteResolver(resolver Resolver) error {
err := d.db.Write("resolvers", resolver.Id, resolver)
return err
}
func (d Database) ReadDnsMessage(dnsRecord DnsMessage) (error, DnsMessage) {
log.Printf("DEBUG Querying DNS Record %s\n", dnsRecord)
question := dnsRecord.Question[0]
returnDnsRecord := DnsMessage{}
// TODO look up by resolver.id/question.qtype
err := d.db.Read(strconv.Itoa(int(question.Qtype)), question.Qname, &returnDnsRecord)
return err, returnDnsRecord
}
func (d Database) ReadResolverDnsMessage(resolverId string, qtype uint16, qname string) (error, *DnsMessage) {
returnDnsRecord := DnsMessage{}
key := resolverId + "/" + strconv.Itoa(int(qtype))
err := d.db.Read(key, qname, &returnDnsRecord)
return err, &returnDnsRecord
}
func (d *Database) ReadAllResolvers() (error, []*Resolver) {
jsonStrings, err := d.db.ReadAll("resolvers")
if len(jsonStrings) == 0 {
return err, nil
}
var resolvers []*Resolver
for _, jsonString := range jsonStrings {
var resolver *Resolver
if err := json.NewDecoder(bytes.NewBufferString(jsonString)).Decode(&resolver); err != nil {
log.Printf("WARN Could not decode json: %s\n", err)
} else {
resolver.Database = d
resolvers = append(resolvers, resolver)
}
}
return err, resolvers
}
func (d Database) DeleteDnsMessage(dnsRecord DnsMessage) error {
log.Printf("DEBUG Deleting DNS Record %s\n", dnsRecord)
var err error
for _, resolverId := range dnsRecord.Resolvers {
key := resolverId + "/" + strconv.Itoa(int(dnsRecord.Question[0].Qtype))
err = d.db.Delete(key, dnsRecord.Question[0].Qname)
}
return err
}
func (d Database) DeleteResolver(resolver Resolver) error {
log.Printf("DEBUG Deleting resolver %s\n", resolver.Id)
err := d.db.Delete("resolvers", resolver.Id)
return err
}