-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
115 lines (99 loc) · 2.63 KB
/
api.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"context"
"log"
"net"
"github.com/drekle/dns-server/pkg/lib/go/v1"
impl "github.com/drekle/dns-server/store"
"github.com/drekle/dns-server/store/database"
"github.com/miekg/dns"
)
type addrHandle struct {
Addr []string
Handle func(w dns.ResponseWriter, req *dns.Msg)
rr *[]dns.RR
}
type server struct {
store impl.Store
}
// ********* IMPORTANT ************
// This a one off not intended for long term use
// I will only support A records here with limited options
// *****************************************
type Request struct {
Name string
Address []string `json:",omitempty"`
// TTL int `json:",omitempty"`
// Class int `json:",omitempty"`
// RecordType `json:"type,omitempty"`
}
func (s *server) ServeDNS(w dns.ResponseWriter, req *dns.Msg) {
m := new(dns.Msg)
m.SetReply(req)
q, err := s.store.ReadRecord(context.Background(), req.Question[0].Name)
if err != nil {
log.Println("There was an error reading the resource records during a DNS lookup: ", err)
}
m.Answer = make([]dns.RR, len(q.RR))
for index, rr := range q.RR {
m.Answer[index] = &rr
}
w.WriteMsg(m)
}
func NewServer(dbaddr string) *server {
var s server
s.store = database.NewMongoDBStore(dbaddr)
return &s
}
func newResourceRecordForName(name string, address string) *dns.A {
rr := &dns.A{
Hdr: dns.RR_Header{
Name: name + ".",
Rrtype: dns.TypeA,
Class: dns.ClassINET,
Ttl: 0},
A: net.ParseIP(address).To4(),
}
return rr
}
func (s *server) PostRecord(ctx context.Context, entry *v1.Entry) (*v1.Entry, error) {
a := newResourceRecordForName(entry.Name, entry.Address)
s.store.UpdateRecord(ctx, *a)
return entry, nil
}
func (s *server) GetRecord(ctx context.Context, entry *v1.Entry) (*v1.Entries, error) {
var ret v1.Entries
q, err := s.store.ReadRecord(ctx, entry.Name)
if err == nil {
for _, a := range q.RR {
ret.Entries = append(ret.Entries, &v1.Entry{
Name: a.Header().Name,
Address: a.A.To4().String(),
})
}
}
return &ret, nil
}
//Replace
func (s *server) PutRecord(ctx context.Context, entry *v1.Entry) (*v1.Entry, error) {
rr := newResourceRecordForName(entry.Name, entry.Address)
s.store.CreateRecord(ctx, *rr)
return entry, nil
}
func (s *server) DeleteRecord(ctx context.Context, entry *v1.Entry) (*v1.Entry, error) {
q, err := s.store.ReadRecord(ctx, entry.Name)
if err != nil {
log.Println(err)
return &v1.Entry{}, nil
}
for _, a := range q.RR {
// A record
if entry.Address == a.A.To4().String() {
_, err = s.store.DeleteRecord(ctx, a)
if err != nil {
log.Println("Failed to delete record: ", err)
}
}
}
return &v1.Entry{}, nil
}