-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfunctions.go
78 lines (66 loc) · 1.78 KB
/
functions.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
package gtm
import (
"fmt"
"github.com/miekg/dns"
)
// LoadBalancing define how to load balance
type LoadBalancing func([]IP) string
type GetAnswer func(dns.Question) []dns.RR
type ServeDNS func(dns.ResponseWriter, *dns.Msg)
type SelectRecord func(q dns.Question, records []*Record, domain string) *Record
//LBAnswer Given ips and ttl configuration, return a Get Answer func
func LBAnswer(records []*Record, getRecord SelectRecord, domain string) func(loadBalancer LoadBalancing) GetAnswer {
return func(loadBalancer LoadBalancing) GetAnswer {
return func(q dns.Question) []dns.RR {
record := getRecord(q, records, domain)
if record == nil {
return make([]dns.RR, 0)
}
ip := loadBalancer(record.HealthCheck.Receive())
if ip == "" {
return make([]dns.RR, 0)
}
rr, err := dns.NewRR(fmt.Sprintf("%s %d A %s", q.Name, record.TTL, ip))
if err != nil {
return make([]dns.RR, 0)
}
return []dns.RR{rr}
}
}
}
const WILD_CARD string = "*"
func DefaultSelectRecord(q dns.Question, records []*Record, domain string) *Record {
var wildCard *Record
for _, record := range records {
if record.Name == WILD_CARD {
wildCard = record
continue
}
fqdn := fmt.Sprintf("%s.%s", record.Name, domain)
if q.Name == fqdn {
return record
}
}
return wildCard
}
// func (lb LoadBalancing) withHealthCheck(frequency time.Duration, hk HealthCheck) LoadBalancing {
//
// }
// DNSRequest Serve DNS Request
func DNSRequest(answerFunc GetAnswer) ServeDNS {
return func(w dns.ResponseWriter, r *dns.Msg) {
m := new(dns.Msg)
m.SetReply(r)
m.Compress = false
switch r.Opcode {
case dns.OpcodeQuery:
for _, q := range m.Question {
switch q.Qtype {
case dns.TypeA:
m.Answer = append(m.Answer, answerFunc(q)...)
}
}
}
w.WriteMsg(m)
}
}