-
Notifications
You must be signed in to change notification settings - Fork 34
/
ec2_server.go
168 lines (138 loc) · 3.49 KB
/
ec2_server.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"github.com/miekg/dns"
"log"
"strconv"
"strings"
"time"
)
type EC2Server struct {
domain string
hostname string
cache *EC2Cache
}
type response struct {
*dns.Msg
}
func NewEC2Server(domain string, hostname string, cache *EC2Cache) *EC2Server {
if !strings.HasSuffix(domain, ".") {
domain += "."
}
if !strings.HasSuffix(hostname, ".") {
hostname += "."
}
server := &EC2Server{
domain: domain,
hostname: hostname,
cache: cache,
}
dns.HandleFunc(server.domain, server.handleRequest)
return server
}
func (s *EC2Server) listenAndServe(port string, net string) {
server := &dns.Server{Addr: port, Net: net}
if err := server.ListenAndServe(); err != nil {
if strings.Contains(err.Error(), "permission denied") {
log.Printf(CAPABILITIES)
}
log.Fatalf("%s", err)
}
}
func (s *EC2Server) handleRequest(w dns.ResponseWriter, request *dns.Msg) {
r := new(dns.Msg)
r.SetReply(request)
r.Authoritative = true
for _, msg := range request.Question {
log.Printf("%v %#v %v (id=%v)", dns.TypeToString[msg.Qtype], msg.Name, w.RemoteAddr(), request.Id)
answers := s.Answer(msg)
if len(answers) > 0 {
r.Answer = append(r.Answer, answers...)
} else {
r.Ns = append(r.Ns, s.SOA(msg))
}
}
w.WriteMsg(r)
}
func (s *EC2Server) Answer(msg dns.Question) (answers []dns.RR) {
if msg.Qtype == dns.TypeNS {
if msg.Name == s.domain {
answers = append(answers, &dns.NS{
Hdr: dns.RR_Header{Name: msg.Name, Rrtype: dns.TypeNS, Class: dns.ClassINET, Ttl: 300},
Ns: s.hostname,
})
}
return answers
}
if msg.Qtype == dns.TypeSOA {
if msg.Name == s.domain {
answers = append(answers, s.SOA(msg))
}
return answers
}
for _, record := range s.Lookup(msg) {
ttl := uint32(record.TTL(time.Now()) / time.Second)
if record.CName != "" {
answers = append(answers, &dns.CNAME{
Hdr: dns.RR_Header{Name: msg.Name, Rrtype: dns.TypeCNAME, Class: dns.ClassINET, Ttl: ttl},
Target: record.CName,
})
} else if msg.Qtype == dns.TypeA {
if record.PublicIP != nil {
answers = append(answers, &dns.A{
Hdr: dns.RR_Header{Name: msg.Name, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: ttl},
A: record.PublicIP,
})
} else {
answers = append(answers, &dns.A{
Hdr: dns.RR_Header{Name: msg.Name, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: ttl},
A: record.PrivateIP,
})
}
}
}
return answers
}
func (s *EC2Server) Lookup(msg dns.Question) []*Record {
parts := strings.Split(strings.TrimSuffix(msg.Name, "."+s.domain), ".")
nth := 0
tag := LOOKUP_NAME
// handle role lookup, e.g. web.role.internal
if len(parts) > 1 {
if parts[len(parts)-1] == "role" {
tag = LOOKUP_ROLE
parts = parts[:len(parts)-1]
}
}
// handle nth lookup, e.g. 1.web.internal
if len(parts) > 1 {
if i, err := strconv.Atoi(parts[0]); err == nil && i > 0 {
nth = i
parts = parts[1:]
}
}
if len(parts) != 1 || parts[0] == "" {
log.Printf("ERROR: badly formed: %s %#v", msg.Name, parts)
return nil
}
results := s.cache.Lookup(tag, parts[0])
if nth != 0 {
if nth > len(results) {
results = results[0:0]
} else {
results = results[nth-1 : nth]
}
}
return results
}
func (s *EC2Server) SOA(msg dns.Question) dns.RR {
return &dns.SOA{
Hdr: dns.RR_Header{Name: s.domain, Rrtype: dns.TypeSOA, Class: dns.ClassINET, Ttl: 60},
Ns: s.hostname,
Mbox: "me.cirw.in.",
Serial: uint32(time.Now().Unix()),
Refresh: 86400,
Retry: 7200,
Expire: 86400,
Minttl: 60,
}
}