-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
117 lines (100 loc) · 2.73 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"time"
"github.com/miekg/dns"
)
type ZoneAggregator struct {
IP string `json:"ip"`
UDPPort int `json:"udp_port"`
TCPPort int `json:"tcp_port"`
ZoneAggregates []ZoneAggregate `json:"zone_aggregates"`
}
type ZoneAggregate struct {
Zone string `json:"zone"`
TTL uint32 `json:"ttl"`
Peers []Peer `json:"peers"`
}
type Peer struct {
Address string `json:"address"`
Zones []string `json:"zones"`
}
func NewZoneAggregator() (*ZoneAggregator, error) {
configPath := os.Getenv("CONFIG_PATH")
configFile, err := ioutil.ReadFile(configPath)
if err != nil {
return nil, err
}
var za *ZoneAggregator
err = json.Unmarshal(configFile, &za)
if err != nil {
return nil, err
}
dns.HandleFunc(".", za.RequestHandler)
tcp := dns.Server{Addr: za.IP + ":" + strconv.Itoa(za.TCPPort), Net: "tcp"}
udp := dns.Server{Addr: za.IP + ":" + strconv.Itoa(za.UDPPort), Net: "udp"}
// Spin Up Servers
go tcp.ListenAndServe()
go udp.ListenAndServe()
return za, nil
}
func (za *ZoneAggregator) RequestHandler(w dns.ResponseWriter, r *dns.Msg) {
fmt.Println(r.Question)
m := za.doAggregation(r)
w.WriteMsg(m)
}
func (za *ZoneAggregator) doAggregation(r *dns.Msg) *dns.Msg {
m := new(dns.Msg)
var answer []dns.RR
// See if our query matches any of our aggregate zones
for _, aggr := range za.ZoneAggregates {
// ToLower the record... Microsoft CNames come in ".NET"
// Even if .net is the cname ./facepalm.
r.Question[0].Name = strings.ToLower(r.Question[0].Name)
if strings.Contains(r.Question[0].Name, aggr.Zone) {
for _, peer := range aggr.Peers {
for _, zone := range peer.Zones {
q := r.Copy()
// Convert the names from AggrZone to PeerZone
newName := strings.ReplaceAll(q.Question[0].Name, aggr.Zone, zone)
q.Question[0].Name = newName
c := new(dns.Client)
in, _, err := c.Exchange(q, peer.Address)
if err != nil {
fmt.Printf("Error Received during Query: %s", err.Error())
}
if len(in.Answer) > 0 {
// Convert the names from PeerZone to AggrZone
for _, a := range in.Answer {
newName := strings.ReplaceAll(a.Header().Name, zone, aggr.Zone)
a.Header().Name = newName
a.Header().Ttl = aggr.TTL
answer = append(answer, a)
}
}
}
}
}
}
m.Id = r.Id
m.SetReply(r)
m.Answer = answer
m.Authoritative = true
return m
}
func main() {
za, err := NewZoneAggregator()
if err != nil {
fmt.Println(err)
}
fmt.Printf("ZoneAggregator is Running on: %s, TCP: %s, UDP: %s\n", za.IP, strconv.Itoa(za.TCPPort), strconv.Itoa(za.UDPPort))
// Don't Exit Main
for {
time.Sleep(5 * time.Minute)
}
}