/
zone.go
281 lines (259 loc) · 6.46 KB
/
zone.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package mdns
import (
"fmt"
"github.com/miekg/dns"
"net"
"os"
"strings"
"sync/atomic"
)
const (
// defaultTTL 默认dns记录的存活时间
defaultTTL = 120
)
type Zone interface {
// Records 根据dns查询请求,返回一个dns记录。
Records(q dns.Question) []dns.RR
}
// ServiceMDNS mdns服务
type ServiceMDNS struct {
Instance string // 实例的名称 (e.g. "hostService name")
Service string // 服务的名字 (e.g. "_http._tcp.")
Domain string // 域 默认为“local”
HostName string // 主机的dns名称 (e.g. "mymachine.net.")
Port int // 端口
IPs []net.IP // 当前服务使用的ip地址
TXT []string
TTL uint32
serviceAddr string
instanceAddr string
enumAddr string
}
// NewServiceMDNS 创建MDNS服务
// instance
func NewServiceMDNS(instance, service, domain, hostName string, port int, ips []net.IP, txt []string) (*ServiceMDNS, error) {
if instance == "" {
return nil, fmt.Errorf("missing service instance name")
}
if service == "" {
return nil, fmt.Errorf("missing service name")
}
if port == 0 {
return nil, fmt.Errorf("missing service port")
}
if domain == "" {
domain = "local."
}
if err := validateFQDN(domain); err != nil {
return nil, fmt.Errorf("domain %q is not a fully-qualified domain name: %v", domain, err)
}
if hostName == "" {
var err error
hostName, err = os.Hostname()
if err != nil {
return nil, fmt.Errorf("could not determine host: %v", err)
}
hostName = fmt.Sprintf("%s.", hostName)
}
if err := validateFQDN(hostName); err != nil {
return nil, fmt.Errorf("hostName %q is not a fully-qualified domain name: %v", hostName, err)
}
if len(ips) == 0 {
var err error
ips, err = net.LookupIP(trimDot(hostName))
if err != nil {
// Try appending the host domain suffix and lookup again
// (required for Linux-based hosts)
tmpHostName := fmt.Sprintf("%s%s", hostName, domain)
ips, err = net.LookupIP(trimDot(tmpHostName))
if err != nil {
return nil, fmt.Errorf("could not determine host IP addresses for %s", hostName)
}
}
}
for _, ip := range ips {
if ip.To4() == nil && ip.To16() == nil {
return nil, fmt.Errorf("invalid IP address in IPs list: %v", ip)
}
}
return &ServiceMDNS{
Instance: instance,
Service: service,
Domain: domain,
HostName: hostName,
Port: port,
IPs: ips,
TXT: txt,
TTL: defaultTTL,
serviceAddr: fmt.Sprintf("%s.%s.", trimDot(service), trimDot(domain)),
instanceAddr: fmt.Sprintf("%s.%s.%s.", instance, trimDot(service), trimDot(domain)),
enumAddr: fmt.Sprintf("_services._dns-sd._udp.%s.", trimDot(domain)),
}, nil
}
func validateFQDN(s string) error {
if len(s) == 0 {
return fmt.Errorf("FQDN must not be blank")
}
if s[len(s)-1] != '.' {
return fmt.Errorf("FQDN must end in period: %s", s)
}
// TODO(reddaly): Perform full validation.
return nil
}
// 去除.号
func trimDot(s string) string {
return strings.Trim(s, ".")
}
func (that *ServiceMDNS) Records(q dns.Question) []dns.RR {
switch q.Name {
case that.enumAddr:
return that.serviceEnum(q)
case that.serviceAddr:
return that.serviceRecords(q)
case that.instanceAddr:
return that.instanceRecords(q)
case that.HostName:
if q.Qtype == dns.TypeA || q.Qtype == dns.TypeAAAA {
return that.instanceRecords(q)
}
fallthrough
default:
return nil
}
}
func (that *ServiceMDNS) serviceEnum(q dns.Question) []dns.RR {
switch q.Qtype {
case dns.TypeANY:
fallthrough
case dns.TypePTR:
rr := &dns.PTR{
Hdr: dns.RR_Header{
Name: q.Name,
Rrtype: dns.TypePTR,
Class: dns.ClassINET,
Ttl: atomic.LoadUint32(&that.TTL),
},
Ptr: that.serviceAddr,
}
return []dns.RR{rr}
default:
return nil
}
}
func (that *ServiceMDNS) serviceRecords(q dns.Question) []dns.RR {
switch q.Qtype {
case dns.TypeANY:
fallthrough
case dns.TypePTR:
rr := &dns.PTR{
Hdr: dns.RR_Header{
Name: q.Name,
Rrtype: dns.TypePTR,
Class: dns.ClassINET,
Ttl: atomic.LoadUint32(&that.TTL),
},
Ptr: that.instanceAddr,
}
servRec := []dns.RR{rr}
instRecs := that.instanceRecords(dns.Question{
Name: that.instanceAddr,
Qtype: dns.TypeANY,
})
return append(servRec, instRecs...)
default:
return nil
}
}
func (that *ServiceMDNS) instanceRecords(q dns.Question) []dns.RR {
switch q.Qtype {
case dns.TypeANY:
// Get the SRV, which includes A and AAAA
recs := that.instanceRecords(dns.Question{
Name: that.instanceAddr,
Qtype: dns.TypeSRV,
})
// Add the TXT record
recs = append(recs, that.instanceRecords(dns.Question{
Name: that.instanceAddr,
Qtype: dns.TypeTXT,
})...)
return recs
case dns.TypeA:
var rr []dns.RR
for _, ip := range that.IPs {
if ip4 := ip.To4(); ip4 != nil {
rr = append(rr, &dns.A{
Hdr: dns.RR_Header{
Name: that.HostName,
Rrtype: dns.TypeA,
Class: dns.ClassINET,
Ttl: atomic.LoadUint32(&that.TTL),
},
A: ip4,
})
}
}
return rr
case dns.TypeAAAA:
var rr []dns.RR
for _, ip := range that.IPs {
if ip.To4() != nil {
// TODO(reddaly): IPv4 addresses could be encoded in IPv6 format and
// putinto AAAA records, but the current logic puts ipv4-encodable
// addresses into the A records exclusively. Perhaps this should be
// configurable?
continue
}
if ip16 := ip.To16(); ip16 != nil {
rr = append(rr, &dns.AAAA{
Hdr: dns.RR_Header{
Name: that.HostName,
Rrtype: dns.TypeAAAA,
Class: dns.ClassINET,
Ttl: atomic.LoadUint32(&that.TTL),
},
AAAA: ip16,
})
}
}
return rr
case dns.TypeSRV:
// Create the SRV Record
srv := &dns.SRV{
Hdr: dns.RR_Header{
Name: q.Name,
Rrtype: dns.TypeSRV,
Class: dns.ClassINET,
Ttl: atomic.LoadUint32(&that.TTL),
},
Priority: 10,
Weight: 1,
Port: uint16(that.Port),
Target: that.HostName,
}
recs := []dns.RR{srv}
// Add the A record
recs = append(recs, that.instanceRecords(dns.Question{
Name: that.instanceAddr,
Qtype: dns.TypeA,
})...)
// Add the AAAA record
recs = append(recs, that.instanceRecords(dns.Question{
Name: that.instanceAddr,
Qtype: dns.TypeAAAA,
})...)
return recs
case dns.TypeTXT:
txt := &dns.TXT{
Hdr: dns.RR_Header{
Name: q.Name,
Rrtype: dns.TypeTXT,
Class: dns.ClassINET,
Ttl: atomic.LoadUint32(&that.TTL),
},
Txt: that.TXT,
}
return []dns.RR{txt}
}
return nil
}