-
Notifications
You must be signed in to change notification settings - Fork 33
/
mdns.go
546 lines (488 loc) · 11.1 KB
/
mdns.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
package registry
import (
"bytes"
"compress/zlib"
"context"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/gogf/gf/v2/util/guid"
"github.com/osgochina/dmicro/logger"
"github.com/osgochina/dmicro/utils/mdns"
"io"
"net"
"strconv"
"strings"
"sync"
"time"
)
var (
mdnsDomain = "dmicro"
)
type mdnsTxt struct {
Service string
Version string
Metadata map[string]string
}
type mdnsEntry struct {
id string
node *mdns.Server
}
type mdnsRegistry struct {
sync.Mutex
opts Options
domain string
services map[string][]*mdnsEntry
mtx sync.RWMutex
watchers map[string]*mdnsWatcher
// listener
listener chan *mdns.ServiceEntry
}
type mdnsWatcher struct {
id string
wo WatchOptions
ch chan *mdns.ServiceEntry
exit chan struct{}
domain string
registry *mdnsRegistry
}
func newRegistry(opts ...Option) Registry {
options := Options{
Context: context.Background(),
Timeout: time.Millisecond * 100,
}
for _, o := range opts {
o(&options)
}
domain := mdnsDomain
d, ok := options.Context.Value("mdns.domain").(string)
if ok {
domain = d
}
return &mdnsRegistry{
opts: options,
domain: domain,
services: make(map[string][]*mdnsEntry),
watchers: make(map[string]*mdnsWatcher),
}
}
func (that *mdnsRegistry) Init(opts ...Option) error {
for _, o := range opts {
o(&that.opts)
}
return nil
}
func (that *mdnsRegistry) Options() Options {
return that.opts
}
// Register 注册服务节点
func (that *mdnsRegistry) Register(service *Service, _ ...RegisterOption) error {
that.Lock()
defer that.Unlock()
entries, ok := that.services[service.Name]
if !ok {
s, err := mdns.NewServiceMDNS(service.Name, "_services", that.domain+".", "", 9999, []net.IP{net.ParseIP("0.0.0.0")}, nil)
if err != nil {
return err
}
svr, err := mdns.NewServer(&mdns.Config{Zone: &mdns.DNSSDService{ServiceMDNS: s}})
if err != nil {
return err
}
entries = append(entries, &mdnsEntry{id: "*", node: svr})
}
var gerr error
for _, node := range service.Nodes {
var seen bool
var e *mdnsEntry
//判断要添加的节点是否已经存在
for _, entry := range entries {
if node.Id == entry.id {
seen = true
e = entry
break
}
}
if seen {
continue
} else {
e = &mdnsEntry{}
}
// 把节点数据编码
txt, err := encode(&mdnsTxt{
Service: service.Name,
Version: service.Version,
Metadata: node.Metadata,
})
if err != nil {
gerr = err
continue
}
// 分割地址端口
host, pt, err := net.SplitHostPort(node.Address)
if err != nil {
gerr = err
continue
}
port, _ := strconv.Atoi(pt)
logger.Debugf(context.TODO(), "[mdns] registry create new service with ip: %s:%d for: %s", net.ParseIP(host).String(), port, host)
//
s, err := mdns.NewServiceMDNS(node.Id, service.Name, that.domain+".", "", port, []net.IP{net.ParseIP(host)}, txt)
if err != nil {
gerr = err
continue
}
srv, err := mdns.NewServer(&mdns.Config{Zone: s, LocalhostChecking: true})
if err != nil {
gerr = err
continue
}
e.id = node.Id
e.node = srv
entries = append(entries, e)
}
that.services[service.Name] = entries
return gerr
}
// Deregister 取消注册节点
func (that *mdnsRegistry) Deregister(service *Service, _ ...DeregisterOption) error {
that.Lock()
defer that.Unlock()
var newEntries []*mdnsEntry
for _, entry := range that.services[service.Name] {
var remove bool
for _, node := range service.Nodes {
if node.Id == entry.id {
_ = entry.node.Shutdown()
remove = true
continue
}
}
// 如果不需要移除,则重新加入
if !remove {
newEntries = append(newEntries, entry)
}
}
//如果最后节点只剩下一个,并且是通配符*,则删除它
if len(newEntries) == 1 && newEntries[0].id == "*" {
_ = newEntries[0].node.Shutdown()
delete(that.services, service.Name)
} else {
that.services[service.Name] = newEntries
}
return nil
}
// GetService 获取指定的服务信息
func (that *mdnsRegistry) GetService(service string, _ ...GetOption) ([]*Service, error) {
serviceMap := make(map[string]*Service)
entries := make(chan *mdns.ServiceEntry, 10)
done := make(chan bool)
p := mdns.DefaultParams(service)
// 设置查询时间,因为是查询是异步执行的,所以实际查询时间就是设置的超时时间
var cancel context.CancelFunc
p.Context, cancel = context.WithTimeout(context.Background(), that.opts.Timeout)
defer cancel()
// set entries channel
p.Entries = entries
// set the domain
p.Domain = that.domain
// 等待查询请求的响应
go func() {
for {
select {
case e := <-entries:
// 跳过列表记录
if p.Service == "_services" {
continue
}
// 不是当前域
if p.Domain != that.domain {
continue
}
// 已过期
if e.TTL == 0 {
continue
}
txt, err := decode(e.InfoFields)
if err != nil {
continue
}
// 收到信息不是当前要获取的service
if txt.Service != service {
continue
}
// 版本不存在则新建
s, ok := serviceMap[txt.Version]
if !ok {
s = &Service{
Name: txt.Service,
Version: txt.Version,
}
}
addr := ""
if len(e.AddrV4) > 0 {
addr = net.JoinHostPort(e.AddrV4.String(), fmt.Sprint(e.Port))
} else if len(e.AddrV6) > 0 {
addr = net.JoinHostPort(e.AddrV6.String(), fmt.Sprint(e.Port))
} else {
logger.Debugf(context.TODO(), "[mdns]: invalid endpoint received: %v", e)
continue
}
s.Nodes = append(s.Nodes, &Node{
Id: strings.TrimSuffix(e.Name, "."+p.Service+"."+p.Domain+"."),
Address: addr,
Metadata: txt.Metadata,
})
serviceMap[txt.Version] = s
case <-p.Context.Done():
close(done)
return
}
}
}()
// 发送查询请求
if err := mdns.Query(p); err != nil {
return nil, err
}
// 等待响应
<-done
services := make([]*Service, 0, len(serviceMap))
for _, s := range serviceMap {
services = append(services, s)
}
return services, nil
}
// ListServices 获取服务列表
func (that *mdnsRegistry) ListServices(_ ...ListOption) ([]*Service, error) {
serviceMap := make(map[string]bool)
entries := make(chan *mdns.ServiceEntry, 10)
done := make(chan bool)
p := mdns.DefaultParams("_services")
// set context with timeout
var cancel context.CancelFunc
p.Context, cancel = context.WithTimeout(context.Background(), that.opts.Timeout)
defer cancel()
// set entries channel
p.Entries = entries
// set domain
p.Domain = that.domain
var services []*Service
go func() {
for {
select {
case e := <-entries:
if e.TTL == 0 {
continue
}
if !strings.HasSuffix(e.Name, p.Domain+".") {
continue
}
name := strings.TrimSuffix(e.Name, "."+p.Service+"."+p.Domain+".")
if !serviceMap[name] {
serviceMap[name] = true
services = append(services, &Service{Name: name})
}
case <-p.Context.Done():
close(done)
return
}
}
}()
// execute query
if err := mdns.Query(p); err != nil {
return nil, err
}
// wait till done
<-done
return services, nil
}
// Watch 监听
func (that *mdnsRegistry) Watch(opts ...WatchOption) (Watcher, error) {
var wo WatchOptions
for _, o := range opts {
o(&wo)
}
md := &mdnsWatcher{
id: guid.S(),
wo: wo,
ch: make(chan *mdns.ServiceEntry, 32),
exit: make(chan struct{}),
domain: that.domain,
registry: that,
}
that.mtx.Lock()
defer that.mtx.Unlock()
that.watchers[md.id] = md
if that.listener != nil {
return md, nil
}
go func() {
for {
that.mtx.Lock()
// 如果监听列表为空,则不执行操作
if len(that.watchers) == 0 {
that.listener = nil
that.mtx.Unlock()
return
}
//如果已存在监听,则退出
if that.listener != nil {
that.mtx.Unlock()
return
}
exit := make(chan struct{})
ch := make(chan *mdns.ServiceEntry, 32)
that.listener = ch
that.mtx.Unlock()
go func() {
send := func(w *mdnsWatcher, e *mdns.ServiceEntry) {
select {
case w.ch <- e:
default:
}
}
for {
select {
case <-exit:
return
case e, ok := <-ch:
if !ok {
return
}
that.mtx.RLock()
// 每次接收到了事件,都需要给所有的watcher发送一遍
for _, w := range that.watchers {
send(w, e)
}
that.mtx.RUnlock()
}
}
}()
// 阻塞启动监听
_ = mdns.Listen(ch, exit)
// 监听结束
that.mtx.Lock()
that.listener = nil
close(ch)
that.mtx.Unlock()
}
}()
return md, nil
}
func (that *mdnsRegistry) String() string {
return "mdns"
}
// Next 获取监听的节点变化
func (that *mdnsWatcher) Next() (*Result, error) {
for {
select {
case e := <-that.ch:
txt, err := decode(e.InfoFields)
if err != nil {
continue
}
if len(txt.Service) == 0 || len(txt.Version) == 0 {
continue
}
// 如果服务不是要监听的服务,则跳过
if len(that.wo.Service) > 0 && txt.Service != that.wo.Service {
continue
}
var action EventType
if e.TTL == 0 {
action = Delete
} else {
action = Create
}
service := &Service{
Name: txt.Service,
Version: txt.Version,
}
suffix := fmt.Sprintf(".%s.%s.", service.Name, that.domain)
if !strings.HasSuffix(e.Name, suffix) {
continue
}
var addr string
if len(e.AddrV4) > 0 {
addr = net.JoinHostPort(e.AddrV4.String(), fmt.Sprint(e.Port))
} else if len(e.AddrV6) > 0 {
addr = net.JoinHostPort(e.AddrV6.String(), fmt.Sprint(e.Port))
} else {
addr = e.Addr.String()
}
service.Nodes = append(service.Nodes, &Node{
Id: strings.TrimSuffix(e.Name, suffix),
Address: addr,
Metadata: txt.Metadata,
})
return &Result{
Action: action,
Service: service,
}, nil
case <-that.exit:
return nil, ErrWatcherStopped
}
}
}
func (that *mdnsWatcher) Stop() {
select {
case <-that.exit:
return
default:
close(that.exit)
// remove self from the registry
that.registry.mtx.Lock()
delete(that.registry.watchers, that.id)
that.registry.mtx.Unlock()
}
}
func NewRegistry(opts ...Option) Registry {
return newRegistry(opts...)
}
// 编码
func encode(txt *mdnsTxt) ([]string, error) {
b, err := json.Marshal(txt)
if err != nil {
return nil, err
}
var buf bytes.Buffer
defer buf.Reset()
w := zlib.NewWriter(&buf)
if _, err := w.Write(b); err != nil {
return nil, err
}
_ = w.Close()
encoded := hex.EncodeToString(buf.Bytes())
// individual txt limit
if len(encoded) <= 255 {
return []string{encoded}, nil
}
var record []string
for len(encoded) > 255 {
record = append(record, encoded[:255])
encoded = encoded[255:]
}
record = append(record, encoded)
return record, nil
}
func decode(record []string) (*mdnsTxt, error) {
encoded := strings.Join(record, "")
hr, err := hex.DecodeString(encoded)
if err != nil {
return nil, err
}
br := bytes.NewReader(hr)
zr, err := zlib.NewReader(br)
if err != nil {
return nil, err
}
rbuf, err := io.ReadAll(zr)
if err != nil {
return nil, err
}
var txt *mdnsTxt
if err := json.Unmarshal(rbuf, &txt); err != nil {
return nil, err
}
return txt, nil
}