2019-01-15 19:50:37 +03:00
|
|
|
// Package mdns is a multicast dns registry
|
|
|
|
package registry
|
|
|
|
|
|
|
|
import (
|
2019-02-01 16:41:11 +03:00
|
|
|
"context"
|
2019-07-08 10:01:42 +03:00
|
|
|
"fmt"
|
2019-01-15 19:50:37 +03:00
|
|
|
"net"
|
2019-07-08 10:01:42 +03:00
|
|
|
"strconv"
|
2019-01-15 19:50:37 +03:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2019-11-06 18:49:40 +03:00
|
|
|
"github.com/google/uuid"
|
2019-01-15 19:50:37 +03:00
|
|
|
"github.com/micro/mdns"
|
|
|
|
)
|
|
|
|
|
2019-09-09 15:11:25 +03:00
|
|
|
var (
|
|
|
|
// use a .micro domain rather than .local
|
|
|
|
mdnsDomain = "micro"
|
|
|
|
)
|
|
|
|
|
2019-01-15 19:50:37 +03:00
|
|
|
type mdnsTxt struct {
|
|
|
|
Service string
|
|
|
|
Version string
|
|
|
|
Endpoints []*Endpoint
|
|
|
|
Metadata map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
type mdnsEntry struct {
|
|
|
|
id string
|
|
|
|
node *mdns.Server
|
|
|
|
}
|
|
|
|
|
|
|
|
type mdnsRegistry struct {
|
|
|
|
opts Options
|
2019-09-09 15:11:25 +03:00
|
|
|
// the mdns domain
|
|
|
|
domain string
|
2019-01-15 19:50:37 +03:00
|
|
|
|
|
|
|
sync.Mutex
|
|
|
|
services map[string][]*mdnsEntry
|
2019-11-06 18:49:40 +03:00
|
|
|
|
|
|
|
mtx sync.RWMutex
|
|
|
|
|
|
|
|
// watchers
|
|
|
|
watchers map[string]*mdnsWatcher
|
|
|
|
|
|
|
|
// listener
|
|
|
|
listener chan *mdns.ServiceEntry
|
2019-01-15 19:50:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func newRegistry(opts ...Option) Registry {
|
|
|
|
options := Options{
|
2019-09-09 15:11:25 +03:00
|
|
|
Context: context.Background(),
|
2019-11-30 04:16:32 +03:00
|
|
|
Timeout: time.Millisecond * 100,
|
2019-01-15 19:50:37 +03:00
|
|
|
}
|
|
|
|
|
2019-09-09 15:11:25 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the domain
|
|
|
|
domain := mdnsDomain
|
|
|
|
|
|
|
|
d, ok := options.Context.Value("mdns.domain").(string)
|
|
|
|
if ok {
|
|
|
|
domain = d
|
|
|
|
}
|
|
|
|
|
2019-01-15 19:50:37 +03:00
|
|
|
return &mdnsRegistry{
|
|
|
|
opts: options,
|
2019-09-09 15:11:25 +03:00
|
|
|
domain: domain,
|
2019-01-15 19:50:37 +03:00
|
|
|
services: make(map[string][]*mdnsEntry),
|
2019-11-06 18:49:40 +03:00
|
|
|
watchers: make(map[string]*mdnsWatcher),
|
2019-01-15 19:50:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mdnsRegistry) Init(opts ...Option) error {
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&m.opts)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mdnsRegistry) Options() Options {
|
|
|
|
return m.opts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mdnsRegistry) Register(service *Service, opts ...RegisterOption) error {
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
|
|
|
entries, ok := m.services[service.Name]
|
|
|
|
// first entry, create wildcard used for list queries
|
|
|
|
if !ok {
|
|
|
|
s, err := mdns.NewMDNSService(
|
|
|
|
service.Name,
|
|
|
|
"_services",
|
2019-09-09 15:11:25 +03:00
|
|
|
m.domain+".",
|
2019-01-15 19:50:37 +03:00
|
|
|
"",
|
|
|
|
9999,
|
|
|
|
[]net.IP{net.ParseIP("0.0.0.0")},
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-05 19:10:49 +03:00
|
|
|
srv, err := mdns.NewServer(&mdns.Config{Zone: &mdns.DNSSDService{MDNSService: s}})
|
2019-01-15 19:50:37 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// append the wildcard entry
|
|
|
|
entries = append(entries, &mdnsEntry{id: "*", node: srv})
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// already registered, continue
|
2019-09-26 18:46:09 +03:00
|
|
|
if seen {
|
2019-01-15 19:50:37 +03:00
|
|
|
continue
|
|
|
|
// doesn't exist
|
|
|
|
} else {
|
2019-09-26 18:46:09 +03:00
|
|
|
e = &mdnsEntry{}
|
2019-01-15 19:50:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
txt, err := encode(&mdnsTxt{
|
|
|
|
Service: service.Name,
|
|
|
|
Version: service.Version,
|
|
|
|
Endpoints: service.Endpoints,
|
|
|
|
Metadata: node.Metadata,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
gerr = err
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-07-08 10:01:42 +03:00
|
|
|
host, pt, err := net.SplitHostPort(node.Address)
|
|
|
|
if err != nil {
|
|
|
|
gerr = err
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
port, _ := strconv.Atoi(pt)
|
|
|
|
|
2019-01-15 19:50:37 +03:00
|
|
|
// we got here, new node
|
|
|
|
s, err := mdns.NewMDNSService(
|
|
|
|
node.Id,
|
|
|
|
service.Name,
|
2019-09-09 15:11:25 +03:00
|
|
|
m.domain+".",
|
2019-01-15 19:50:37 +03:00
|
|
|
"",
|
2019-07-08 10:01:42 +03:00
|
|
|
port,
|
|
|
|
[]net.IP{net.ParseIP(host)},
|
2019-01-15 19:50:37 +03:00
|
|
|
txt,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
gerr = err
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
srv, err := mdns.NewServer(&mdns.Config{Zone: s})
|
|
|
|
if err != nil {
|
|
|
|
gerr = err
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
e.id = node.Id
|
|
|
|
e.node = srv
|
|
|
|
entries = append(entries, e)
|
|
|
|
}
|
|
|
|
|
|
|
|
// save
|
|
|
|
m.services[service.Name] = entries
|
|
|
|
|
|
|
|
return gerr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mdnsRegistry) Deregister(service *Service) error {
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
|
|
|
var newEntries []*mdnsEntry
|
|
|
|
|
|
|
|
// loop existing entries, check if any match, shutdown those that do
|
|
|
|
for _, entry := range m.services[service.Name] {
|
|
|
|
var remove bool
|
|
|
|
|
|
|
|
for _, node := range service.Nodes {
|
|
|
|
if node.Id == entry.id {
|
|
|
|
entry.node.Shutdown()
|
|
|
|
remove = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// keep it?
|
|
|
|
if !remove {
|
|
|
|
newEntries = append(newEntries, entry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// last entry is the wildcard for list queries. Remove it.
|
|
|
|
if len(newEntries) == 1 && newEntries[0].id == "*" {
|
|
|
|
newEntries[0].node.Shutdown()
|
|
|
|
delete(m.services, service.Name)
|
|
|
|
} else {
|
|
|
|
m.services[service.Name] = newEntries
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mdnsRegistry) GetService(service string) ([]*Service, error) {
|
|
|
|
serviceMap := make(map[string]*Service)
|
2019-02-01 16:41:11 +03:00
|
|
|
entries := make(chan *mdns.ServiceEntry, 10)
|
|
|
|
done := make(chan bool)
|
|
|
|
|
|
|
|
p := mdns.DefaultParams(service)
|
|
|
|
// set context with timeout
|
2019-12-03 10:25:58 +03:00
|
|
|
var cancel context.CancelFunc
|
|
|
|
p.Context, cancel = context.WithTimeout(context.Background(), m.opts.Timeout)
|
|
|
|
defer cancel()
|
2019-02-01 16:41:11 +03:00
|
|
|
// set entries channel
|
|
|
|
p.Entries = entries
|
2019-09-09 15:11:25 +03:00
|
|
|
// set the domain
|
|
|
|
p.Domain = m.domain
|
2019-01-15 19:50:37 +03:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
2019-02-01 16:41:11 +03:00
|
|
|
case e := <-entries:
|
2019-01-15 19:50:37 +03:00
|
|
|
// list record so skip
|
|
|
|
if p.Service == "_services" {
|
|
|
|
continue
|
|
|
|
}
|
2019-09-09 15:11:25 +03:00
|
|
|
if p.Domain != m.domain {
|
|
|
|
continue
|
|
|
|
}
|
2019-01-15 19:50:37 +03:00
|
|
|
if e.TTL == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
txt, err := decode(e.InfoFields)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if txt.Service != service {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
s, ok := serviceMap[txt.Version]
|
|
|
|
if !ok {
|
|
|
|
s = &Service{
|
|
|
|
Name: txt.Service,
|
|
|
|
Version: txt.Version,
|
|
|
|
Endpoints: txt.Endpoints,
|
|
|
|
}
|
|
|
|
}
|
2020-03-07 21:28:48 +03:00
|
|
|
addr := ""
|
|
|
|
// prefer ipv4 addrs
|
|
|
|
if e.AddrV4 != nil {
|
|
|
|
addr = e.AddrV4.String()
|
|
|
|
// else use ipv6
|
|
|
|
} else if e.AddrV6 != nil {
|
|
|
|
addr = "[" + e.AddrV6.String() + "]"
|
|
|
|
} else {
|
|
|
|
// broken endpoint
|
|
|
|
continue
|
|
|
|
}
|
2019-01-15 19:50:37 +03:00
|
|
|
s.Nodes = append(s.Nodes, &Node{
|
|
|
|
Id: strings.TrimSuffix(e.Name, "."+p.Service+"."+p.Domain+"."),
|
2020-03-07 21:28:48 +03:00
|
|
|
Address: fmt.Sprintf("%s:%d", addr, e.Port),
|
2019-01-15 19:50:37 +03:00
|
|
|
Metadata: txt.Metadata,
|
|
|
|
})
|
|
|
|
|
|
|
|
serviceMap[txt.Version] = s
|
2019-02-01 16:41:11 +03:00
|
|
|
case <-p.Context.Done():
|
|
|
|
close(done)
|
2019-01-15 19:50:37 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-02-01 16:41:11 +03:00
|
|
|
// execute the query
|
2019-01-15 19:50:37 +03:00
|
|
|
if err := mdns.Query(p); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-02-01 16:41:11 +03:00
|
|
|
// wait for completion
|
|
|
|
<-done
|
|
|
|
|
2019-01-15 19:50:37 +03:00
|
|
|
// create list and return
|
2019-11-11 03:03:51 +03:00
|
|
|
services := make([]*Service, 0, len(serviceMap))
|
2019-01-15 19:50:37 +03:00
|
|
|
|
|
|
|
for _, service := range serviceMap {
|
|
|
|
services = append(services, service)
|
|
|
|
}
|
|
|
|
|
|
|
|
return services, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mdnsRegistry) ListServices() ([]*Service, error) {
|
2019-02-01 16:41:11 +03:00
|
|
|
serviceMap := make(map[string]bool)
|
|
|
|
entries := make(chan *mdns.ServiceEntry, 10)
|
|
|
|
done := make(chan bool)
|
2019-01-15 19:50:37 +03:00
|
|
|
|
2019-02-01 16:41:11 +03:00
|
|
|
p := mdns.DefaultParams("_services")
|
|
|
|
// set context with timeout
|
2019-12-03 10:25:58 +03:00
|
|
|
var cancel context.CancelFunc
|
|
|
|
p.Context, cancel = context.WithTimeout(context.Background(), m.opts.Timeout)
|
|
|
|
defer cancel()
|
2019-02-01 16:41:11 +03:00
|
|
|
// set entries channel
|
|
|
|
p.Entries = entries
|
2019-09-09 15:11:25 +03:00
|
|
|
// set domain
|
|
|
|
p.Domain = m.domain
|
2019-01-15 19:50:37 +03:00
|
|
|
|
|
|
|
var services []*Service
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
2019-02-01 16:41:11 +03:00
|
|
|
case e := <-entries:
|
2019-01-15 19:50:37 +03:00
|
|
|
if e.TTL == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2019-09-09 15:11:25 +03:00
|
|
|
if !strings.HasSuffix(e.Name, p.Domain+".") {
|
|
|
|
continue
|
|
|
|
}
|
2019-01-15 19:50:37 +03:00
|
|
|
name := strings.TrimSuffix(e.Name, "."+p.Service+"."+p.Domain+".")
|
|
|
|
if !serviceMap[name] {
|
|
|
|
serviceMap[name] = true
|
|
|
|
services = append(services, &Service{Name: name})
|
|
|
|
}
|
2019-02-01 16:41:11 +03:00
|
|
|
case <-p.Context.Done():
|
|
|
|
close(done)
|
2019-01-15 19:50:37 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-02-01 16:41:11 +03:00
|
|
|
// execute query
|
2019-01-15 19:50:37 +03:00
|
|
|
if err := mdns.Query(p); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-02-01 16:41:11 +03:00
|
|
|
// wait till done
|
|
|
|
<-done
|
|
|
|
|
2019-01-15 19:50:37 +03:00
|
|
|
return services, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mdnsRegistry) Watch(opts ...WatchOption) (Watcher, error) {
|
|
|
|
var wo WatchOptions
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&wo)
|
|
|
|
}
|
|
|
|
|
|
|
|
md := &mdnsWatcher{
|
2019-11-06 18:49:40 +03:00
|
|
|
id: uuid.New().String(),
|
|
|
|
wo: wo,
|
|
|
|
ch: make(chan *mdns.ServiceEntry, 32),
|
|
|
|
exit: make(chan struct{}),
|
|
|
|
domain: m.domain,
|
|
|
|
registry: m,
|
|
|
|
}
|
|
|
|
|
|
|
|
m.mtx.Lock()
|
|
|
|
defer m.mtx.Unlock()
|
|
|
|
|
|
|
|
// save the watcher
|
|
|
|
m.watchers[md.id] = md
|
|
|
|
|
|
|
|
// check of the listener exists
|
|
|
|
if m.listener != nil {
|
|
|
|
return md, nil
|
2019-01-15 19:50:37 +03:00
|
|
|
}
|
|
|
|
|
2019-11-06 18:49:40 +03:00
|
|
|
// start the listener
|
2019-01-15 19:50:37 +03:00
|
|
|
go func() {
|
2019-11-06 18:49:40 +03:00
|
|
|
// go to infinity
|
|
|
|
for {
|
|
|
|
m.mtx.Lock()
|
|
|
|
|
|
|
|
// just return if there are no watchers
|
|
|
|
if len(m.watchers) == 0 {
|
|
|
|
m.listener = nil
|
|
|
|
m.mtx.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// check existing listener
|
|
|
|
if m.listener != nil {
|
|
|
|
m.mtx.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// reset the listener
|
|
|
|
exit := make(chan struct{})
|
|
|
|
ch := make(chan *mdns.ServiceEntry, 32)
|
|
|
|
m.listener = ch
|
|
|
|
|
|
|
|
m.mtx.Unlock()
|
|
|
|
|
|
|
|
// send messages to the watchers
|
|
|
|
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
|
|
|
|
}
|
|
|
|
m.mtx.RLock()
|
|
|
|
// send service entry to all watchers
|
|
|
|
for _, w := range m.watchers {
|
|
|
|
send(w, e)
|
|
|
|
}
|
|
|
|
m.mtx.RUnlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
// start listening, blocking call
|
|
|
|
mdns.Listen(ch, exit)
|
|
|
|
|
|
|
|
// mdns.Listen has unblocked
|
|
|
|
// kill the saved listener
|
|
|
|
m.mtx.Lock()
|
|
|
|
m.listener = nil
|
|
|
|
close(ch)
|
|
|
|
m.mtx.Unlock()
|
2019-01-15 19:50:37 +03:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return md, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mdnsRegistry) String() string {
|
|
|
|
return "mdns"
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRegistry returns a new default registry which is mdns
|
|
|
|
func NewRegistry(opts ...Option) Registry {
|
|
|
|
return newRegistry(opts...)
|
|
|
|
}
|