-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
149 lines (135 loc) · 3.6 KB
/
main.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"context"
"fmt"
"log"
"os"
"strings"
"time"
"net/http"
"github.com/miekg/dns"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/x/mongo/driver/connstring"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type Elem struct {
Ip []string
}
var mongoUri string = os.Getenv("MONGO_URI")
var collectionName string = os.Getenv("GOREDNS_COLLECTION")
func appendResults(etype string, name string, m *dns.Msg, cur *mongo.Cursor) int {
count := 0
for cur.Next(context.TODO()) {
var elem Elem
err := cur.Decode(&elem)
if err != nil {
log.Fatal(err)
}
for _, ip := range elem.Ip {
tp := "A"
if strings.Contains(ip, ":") {
tp = "AAAA"
}
if etype != tp {
continue
}
log.Printf("Appending: %s %s %s\n", name, tp, ip)
rr, err := dns.NewRR(fmt.Sprintf("%s. %s %s", name, tp, ip))
if err == nil {
m.Answer = append(m.Answer, rr)
count += 1
}
}
}
return count
}
func query(tp string, name string, m *dns.Msg, coll *mongo.Collection) {
// TODO: Validate `name` against RE_FQDN
log.Printf("Query %s for %s\n", tp, name)
cur, err := coll.Find(context.TODO(), bson.M{"dns.fqdn": name, "disabled": false})
if err != nil {
log.Fatal(err)
}
if appendResults(tp, name, m, cur) == 0 {
cur, err := coll.Find(context.TODO(), bson.M{"dns.san": name, "disabled": false})
if err != nil {
log.Fatal(err)
}
if appendResults(tp, name, m, cur) == 0 {
m.Rcode = dns.RcodeNameError
counterNoResults.Inc()
} else {
counterAlternativeNames.Inc()
}
} else {
counterExactMatches.Inc()
}
}
func wrapper(coll *mongo.Collection) func(dns.ResponseWriter, *dns.Msg) {
return func(w dns.ResponseWriter, r *dns.Msg) {
counterQueries.Inc()
m := new(dns.Msg)
m.SetReply(r)
m.Compress = false
m.Authoritative = true
switch r.Opcode {
case dns.OpcodeQuery:
for _, q := range m.Question {
switch q.Qtype {
case dns.TypeA:
query("A", q.Name[:len(q.Name)-1], m, coll)
case dns.TypeAAAA:
query("AAAA", q.Name[:len(q.Name)-1], m, coll)
}
}
}
w.WriteMsg(m)
}
}
var (
counterQueries = promauto.NewCounter(prometheus.CounterOpts{
Name: "goredns_queries",
Help: "The total number of queries.",
})
counterNoResults = promauto.NewCounter(prometheus.CounterOpts{
Name: "goredns_no_results",
Help: "The total number of queries that had no results.",
})
counterExactMatches = promauto.NewCounter(prometheus.CounterOpts{
Name: "goredns_exact_matches",
Help: "The total number of queries that matched FQDN exactly.",
})
counterAlternativeNames = promauto.NewCounter(prometheus.CounterOpts{
Name: "goredns_alternative_names",
Help: "The total number of queries that matched SAN record.",
})
)
func main() {
cs, err := connstring.ParseAndValidate(mongoUri)
client, err := mongo.NewClient(options.Client().ApplyURI(mongoUri))
if err != nil {
log.Fatal(err)
}
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
err = client.Connect(ctx)
if err != nil {
log.Fatal(err)
}
coll := client.Database(cs.Database).Collection(collectionName)
defer client.Disconnect(ctx)
dns.HandleFunc(".", wrapper(coll))
http.Handle("/metrics", promhttp.Handler())
server := &dns.Server{Addr: ":53", Net: "udp"}
go func() {
http.ListenAndServe("127.0.0.1:9001", nil)
}()
err2 := server.ListenAndServe()
defer server.Shutdown()
if err2 != nil {
log.Fatalf("Failed to start server: %s\n ", err2.Error())
}
}