/
other.go
102 lines (83 loc) · 2.72 KB
/
other.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
//
// Last.Backend LLC CONFIDENTIAL
// __________________
//
// [2014] - [2018] Last.Backend LLC
// All Rights Reserved.
//
// NOTICE: All information contained herein is, and remains
// the property of Last.Backend LLC and its suppliers,
// if any. The intellectual and technical concepts contained
// herein are proprietary to Last.Backend LLC
// and its suppliers and may be covered by Russian Federation and Foreign Patents,
// patents in process, and are protected by trade secret or copyright law.
// Dissemination of this information or reproduction of this material
// is strictly forbidden unless prior written permission is obtained
// from Last.Backend LLC.
//
package resources
import (
"time"
"github.com/lastbackend/lastbackend/pkg/log"
"github.com/miekg/dns"
"net"
)
func other(w dns.ResponseWriter, r *dns.Msg) {
log.V(logLevel).Debugf("%s:other:> dns request `.`", logPrefix)
var (
v4 bool
rr dns.RR
)
m := new(dns.Msg)
m.SetReply(r)
m.Compress = false
switch r.Opcode {
case dns.OpcodeQuery:
log.V(logLevel).Debugf("%s:other:> dns.OpcodeQuery", logPrefix)
for _, q := range m.Question {
switch r.Question[0].Qtype {
case dns.TypeTXT:
log.V(logLevel).Debugf("%s:other:> get txt type query", logPrefix)
t := new(dns.TXT)
t.Hdr = dns.RR_Header{Name: q.Name, Rrtype: dns.TypeTXT, Class: dns.ClassINET, Ttl: 0}
m.Answer = append(m.Answer, t)
m.Extra = append(m.Extra, rr)
default:
log.V(logLevel).Debugf("%s:other:> get unknown query type", logPrefix)
fallthrough
case dns.TypeAAAA, dns.TypeA:
log.V(logLevel).Debugf("%s:other:> get A or AAAA type query", logPrefix)
if q.Name[len(q.Name)-1:] != "." {
q.Name += "."
}
// GenerateConfig A and AAAA records
ips := make([]net.IP, 0)
log.V(logLevel).Debugf("%s:other:> ips list: %#v for %s", logPrefix, ips, q.Name)
for _, ip := range ips {
v4 = ip.To4() != nil
if v4 {
rr = new(dns.A)
rr.(*dns.A).Hdr = dns.RR_Header{Name: q.Name, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 0}
rr.(*dns.A).A = ip.To4()
} else {
rr = new(dns.AAAA)
rr.(*dns.AAAA).Hdr = dns.RR_Header{Name: q.Name, Rrtype: dns.TypeAAAA, Class: dns.ClassINET, Ttl: 0}
rr.(*dns.AAAA).AAAA = ip
}
m.Answer = append(m.Answer, rr)
}
}
}
case dns.OpcodeUpdate:
log.V(logLevel).Debugf("%s:other:> dns.OpcodeUpdate", logPrefix)
}
if r.IsTsig() != nil {
if w.TsigStatus() == nil {
m.SetTsig(r.Extra[len(r.Extra)-1].(*dns.TSIG).Hdr.Name, dns.HmacMD5, 300, time.Now().Unix())
} else {
log.V(logLevel).Errorf("%s:other:> tsig status err: %s", logPrefix, w.TsigStatus())
}
}
log.V(logLevel).Debugf("%s:other:> send message info %#v", logPrefix, m)
w.WriteMsg(m)
}