2016-04-27 20:21:05 +03:00
|
|
|
package mdns
|
|
|
|
|
|
|
|
import (
|
2020-07-27 15:22:00 +03:00
|
|
|
"bytes"
|
|
|
|
"compress/zlib"
|
2019-09-09 15:11:25 +03:00
|
|
|
"context"
|
2020-07-27 15:22:00 +03:00
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2019-09-09 15:11:25 +03:00
|
|
|
|
2020-07-27 15:22:00 +03:00
|
|
|
"github.com/google/uuid"
|
2021-02-19 23:12:48 +03:00
|
|
|
util "github.com/unistack-org/micro-register-mdns/v3/util"
|
|
|
|
"github.com/unistack-org/micro/v3/logger"
|
|
|
|
"github.com/unistack-org/micro/v3/register"
|
2016-04-27 20:21:05 +03:00
|
|
|
)
|
|
|
|
|
2020-07-27 15:22:00 +03:00
|
|
|
const (
|
|
|
|
// every service is written to the global domain so * domain queries work, e.g.
|
|
|
|
// calling mdns.List(registry.ListDomain("*")) will list the services across all
|
|
|
|
// domains
|
|
|
|
globalDomain = "global"
|
|
|
|
)
|
|
|
|
|
|
|
|
type mdnsTxt struct {
|
|
|
|
Service string
|
|
|
|
Version string
|
2021-02-19 23:12:48 +03:00
|
|
|
Endpoints []*register.Endpoint
|
2020-07-27 15:22:00 +03:00
|
|
|
Metadata map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
type mdnsEntry struct {
|
|
|
|
id string
|
2021-02-19 23:12:48 +03:00
|
|
|
node *util.Server
|
2020-07-27 15:22:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// services are a key/value map, with the service name as a key and the value being a
|
|
|
|
// slice of mdns entries, representing the nodes with a single _services entry to be
|
|
|
|
// used for listing
|
|
|
|
type services map[string][]*mdnsEntry
|
|
|
|
|
|
|
|
// mdsRegistry is a multicast dns registry
|
2021-02-19 23:12:48 +03:00
|
|
|
type mdnsRegister struct {
|
|
|
|
opts register.Options
|
2020-07-27 15:22:00 +03:00
|
|
|
|
|
|
|
// the top level domains, these can be overriden using options
|
|
|
|
defaultDomain string
|
|
|
|
globalDomain string
|
|
|
|
|
|
|
|
sync.Mutex
|
|
|
|
domains map[string]services
|
|
|
|
|
|
|
|
mtx sync.RWMutex
|
|
|
|
|
|
|
|
// watchers
|
|
|
|
watchers map[string]*mdnsWatcher
|
|
|
|
|
|
|
|
// listener
|
2021-02-19 23:12:48 +03:00
|
|
|
listener chan *util.ServiceEntry
|
2016-04-27 20:21:05 +03:00
|
|
|
}
|
2019-09-09 15:11:25 +03:00
|
|
|
|
2020-07-27 15:22:00 +03:00
|
|
|
type mdnsWatcher struct {
|
|
|
|
id string
|
2021-02-19 23:12:48 +03:00
|
|
|
wo register.WatchOptions
|
|
|
|
ch chan *util.ServiceEntry
|
2020-07-27 15:22:00 +03:00
|
|
|
exit chan struct{}
|
|
|
|
// the mdns domain
|
|
|
|
domain string
|
|
|
|
// the registry
|
2021-02-19 23:12:48 +03:00
|
|
|
registry *mdnsRegister
|
2020-07-27 15:22:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
defer func() {
|
|
|
|
if closeErr := w.Close(); closeErr != nil {
|
2021-02-19 23:12:48 +03:00
|
|
|
if logger.V(logger.ErrorLevel) {
|
|
|
|
logger.Errorf(context.TODO(), "[mdns] registry close encoding writer err: %v", closeErr)
|
2020-07-27 15:22:00 +03:00
|
|
|
}
|
2019-09-09 15:11:25 +03:00
|
|
|
}
|
2020-07-27 15:22:00 +03:00
|
|
|
}()
|
|
|
|
if _, err := w.Write(b); err != nil {
|
|
|
|
return nil, err
|
2019-09-09 15:11:25 +03:00
|
|
|
}
|
2020-07-27 15:22:00 +03:00
|
|
|
|
|
|
|
if err = w.Close(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
encoded := hex.EncodeToString(buf.Bytes())
|
|
|
|
// individual txt limit
|
|
|
|
if len(encoded) <= 255 {
|
|
|
|
return []string{encoded}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// split encoded string
|
|
|
|
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
|
|
|
|
}
|
|
|
|
defer zr.Close()
|
|
|
|
|
|
|
|
rbuf, err := ioutil.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
|
|
|
|
}
|
|
|
|
|
2021-02-19 23:12:48 +03:00
|
|
|
func newRegister(opts ...register.Option) register.Register {
|
|
|
|
options := register.Options{
|
2020-07-27 15:22:00 +03:00
|
|
|
Context: context.Background(),
|
|
|
|
Timeout: time.Millisecond * 100,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the domain
|
2021-02-19 23:12:48 +03:00
|
|
|
defaultDomain := register.DefaultDomain
|
2020-07-27 15:22:00 +03:00
|
|
|
if d, ok := options.Context.Value("mdns.domain").(string); ok {
|
|
|
|
defaultDomain = d
|
|
|
|
}
|
|
|
|
|
2021-02-19 23:12:48 +03:00
|
|
|
return &mdnsRegister{
|
2020-07-27 15:22:00 +03:00
|
|
|
defaultDomain: defaultDomain,
|
|
|
|
globalDomain: globalDomain,
|
|
|
|
opts: options,
|
|
|
|
domains: make(map[string]services),
|
|
|
|
watchers: make(map[string]*mdnsWatcher),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-19 23:12:48 +03:00
|
|
|
func (m *mdnsRegister) Init(opts ...register.Option) error {
|
2020-07-27 15:22:00 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&m.opts)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-19 23:12:48 +03:00
|
|
|
func (m *mdnsRegister) Options() register.Options {
|
2020-07-27 15:22:00 +03:00
|
|
|
return m.opts
|
|
|
|
}
|
|
|
|
|
2021-02-19 23:12:48 +03:00
|
|
|
func (m *mdnsRegister) Connect(ctx context.Context) error {
|
|
|
|
// TODO: real connect
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mdnsRegister) Disconnect(ctx context.Context) error {
|
|
|
|
// TODO: real disconnect
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-27 15:22:00 +03:00
|
|
|
// createServiceMDNSEntry will create a new wildcard mdns entry for the service in the
|
|
|
|
// given domain. This wildcard mdns entry is used when listing services.
|
|
|
|
func createServiceMDNSEntry(name, domain string) (*mdnsEntry, error) {
|
|
|
|
ip := net.ParseIP("0.0.0.0")
|
|
|
|
|
2021-02-19 23:12:48 +03:00
|
|
|
s, err := util.NewMDNSService(name, "_services", domain+".", "", 9999, []net.IP{ip}, nil)
|
2020-07-27 15:22:00 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-02-19 23:12:48 +03:00
|
|
|
srv, err := util.NewServer(&util.Config{Zone: &util.DNSSDService{MDNSService: s} /*, LocalhostChecking: true*/})
|
2020-07-27 15:22:00 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &mdnsEntry{id: "*", node: srv}, nil
|
|
|
|
}
|
|
|
|
|
2021-02-19 23:12:48 +03:00
|
|
|
func (m *mdnsRegister) createMDNSEntries(domain, serviceName string) ([]*mdnsEntry, error) {
|
2020-08-10 23:58:35 +03:00
|
|
|
// if it already exists don't reegister it again
|
2020-07-27 15:22:00 +03:00
|
|
|
entries, ok := m.domains[domain][serviceName]
|
|
|
|
if ok {
|
|
|
|
return entries, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// create the wildcard entry used for list queries in this domain
|
|
|
|
entry, err := createServiceMDNSEntry(serviceName, domain)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return []*mdnsEntry{entry}, nil
|
|
|
|
}
|
|
|
|
|
2021-02-19 23:12:48 +03:00
|
|
|
func registerService(service *register.Service, entries []*mdnsEntry, options register.RegisterOptions) ([]*mdnsEntry, error) {
|
2020-07-27 15:22:00 +03:00
|
|
|
var lastError error
|
|
|
|
for _, node := range service.Nodes {
|
|
|
|
var seen bool
|
|
|
|
|
|
|
|
for _, entry := range entries {
|
2021-06-05 17:16:58 +03:00
|
|
|
if node.ID == entry.id {
|
2020-07-27 15:22:00 +03:00
|
|
|
seen = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// this node has already been registered, continue
|
|
|
|
if seen {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
txt, err := encode(&mdnsTxt{
|
|
|
|
Service: service.Name,
|
|
|
|
Version: service.Version,
|
|
|
|
Endpoints: service.Endpoints,
|
|
|
|
Metadata: node.Metadata,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
lastError = err
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
host, pt, err := net.SplitHostPort(node.Address)
|
|
|
|
if err != nil {
|
|
|
|
lastError = err
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
port, _ := strconv.Atoi(pt)
|
|
|
|
|
2021-02-19 23:12:48 +03:00
|
|
|
if logger.V(logger.DebugLevel) {
|
|
|
|
logger.Debugf(context.TODO(), "[mdns] registry create new service with ip: %s for: %s", net.ParseIP(host).String(), host)
|
2020-07-27 15:22:00 +03:00
|
|
|
}
|
|
|
|
// we got here, new node
|
2021-02-19 23:12:48 +03:00
|
|
|
s, err := util.NewMDNSService(
|
2021-06-05 17:16:58 +03:00
|
|
|
node.ID,
|
2020-07-27 15:22:00 +03:00
|
|
|
service.Name,
|
|
|
|
options.Domain+".",
|
|
|
|
"",
|
|
|
|
port,
|
|
|
|
[]net.IP{net.ParseIP(host)},
|
|
|
|
txt,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
lastError = err
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-02-19 23:12:48 +03:00
|
|
|
srv, err := util.NewServer(&util.Config{Zone: s /*, LocalhostChecking: true*/})
|
2020-07-27 15:22:00 +03:00
|
|
|
if err != nil {
|
|
|
|
lastError = err
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-06-05 17:16:58 +03:00
|
|
|
entries = append(entries, &mdnsEntry{id: node.ID, node: srv})
|
2020-07-27 15:22:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return entries, lastError
|
|
|
|
}
|
|
|
|
|
2021-02-19 23:12:48 +03:00
|
|
|
func createGlobalDomainService(service *register.Service, options register.RegisterOptions) *register.Service {
|
2020-07-27 15:22:00 +03:00
|
|
|
srv := *service
|
|
|
|
srv.Nodes = nil
|
|
|
|
|
|
|
|
for _, n := range service.Nodes {
|
|
|
|
node := n
|
|
|
|
|
|
|
|
// set the original domain in node metadata
|
|
|
|
if node.Metadata == nil {
|
|
|
|
node.Metadata = map[string]string{"domain": options.Domain}
|
|
|
|
} else {
|
|
|
|
node.Metadata["domain"] = options.Domain
|
|
|
|
}
|
|
|
|
|
|
|
|
srv.Nodes = append(srv.Nodes, node)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &srv
|
|
|
|
}
|
|
|
|
|
2021-02-19 23:12:48 +03:00
|
|
|
func (m *mdnsRegister) Register(ctx context.Context, service *register.Service, opts ...register.RegisterOption) error {
|
2020-07-27 15:22:00 +03:00
|
|
|
m.Lock()
|
|
|
|
|
|
|
|
// parse the options
|
2021-02-19 23:12:48 +03:00
|
|
|
var options register.RegisterOptions
|
2020-07-27 15:22:00 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
if len(options.Domain) == 0 {
|
|
|
|
options.Domain = m.defaultDomain
|
|
|
|
}
|
|
|
|
|
|
|
|
// create the domain in the memory store if it doesn't yet exist
|
|
|
|
if _, ok := m.domains[options.Domain]; !ok {
|
|
|
|
m.domains[options.Domain] = make(services)
|
|
|
|
}
|
|
|
|
|
2020-08-10 23:58:35 +03:00
|
|
|
entries, err := m.createMDNSEntries(options.Domain, service.Name)
|
2020-07-27 15:22:00 +03:00
|
|
|
if err != nil {
|
|
|
|
m.Unlock()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
entries, gerr := registerService(service, entries, options)
|
|
|
|
|
|
|
|
// save the mdns entry
|
|
|
|
m.domains[options.Domain][service.Name] = entries
|
|
|
|
m.Unlock()
|
|
|
|
|
|
|
|
// register in the global Domain so it can be queried as one
|
|
|
|
if options.Domain != m.globalDomain {
|
|
|
|
srv := createGlobalDomainService(service, options)
|
2021-02-19 23:12:48 +03:00
|
|
|
if err := m.Register(ctx, srv, append(opts, register.RegisterDomain(m.globalDomain))...); err != nil {
|
2020-07-27 15:22:00 +03:00
|
|
|
gerr = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return gerr
|
|
|
|
}
|
|
|
|
|
2021-02-19 23:12:48 +03:00
|
|
|
func (m *mdnsRegister) Deregister(ctx context.Context, service *register.Service, opts ...register.DeregisterOption) error {
|
2020-07-27 15:22:00 +03:00
|
|
|
// parse the options
|
2021-02-19 23:12:48 +03:00
|
|
|
var options register.DeregisterOptions
|
2020-07-27 15:22:00 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
if len(options.Domain) == 0 {
|
|
|
|
options.Domain = m.defaultDomain
|
|
|
|
}
|
|
|
|
|
|
|
|
// register in the global Domain
|
|
|
|
var err error
|
|
|
|
if options.Domain != m.globalDomain {
|
|
|
|
defer func() {
|
2021-02-19 23:12:48 +03:00
|
|
|
err = m.Deregister(ctx, service, append(opts, register.DeregisterDomain(m.globalDomain))...)
|
2020-07-27 15:22:00 +03:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// we want to unlock before we call deregister on the global domain, so it's important this unlock
|
|
|
|
// is applied after the defer m.Deregister is called above
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
|
|
|
// the service wasn't registered, we can safely exist
|
|
|
|
if _, ok := m.domains[options.Domain]; !ok {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// loop existing entries, check if any match, shutdown those that do
|
|
|
|
var newEntries []*mdnsEntry
|
|
|
|
for _, entry := range m.domains[options.Domain][service.Name] {
|
|
|
|
var remove bool
|
|
|
|
|
|
|
|
for _, node := range service.Nodes {
|
2021-06-05 17:16:58 +03:00
|
|
|
if node.ID == entry.id {
|
2020-07-27 15:22:00 +03:00
|
|
|
entry.node.Shutdown()
|
|
|
|
remove = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// keep it?
|
|
|
|
if !remove {
|
|
|
|
newEntries = append(newEntries, entry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// we have no new entries, we can exit
|
|
|
|
if len(newEntries) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// we have more than one entry remaining, we can exit
|
|
|
|
if len(newEntries) > 1 {
|
|
|
|
m.domains[options.Domain][service.Name] = newEntries
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// our remaining entry is not a wildcard, we can exit
|
|
|
|
if len(newEntries) == 1 && newEntries[0].id != "*" {
|
|
|
|
m.domains[options.Domain][service.Name] = newEntries
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// last entry is the wildcard for list queries. Remove it.
|
|
|
|
newEntries[0].node.Shutdown()
|
|
|
|
delete(m.domains[options.Domain], service.Name)
|
|
|
|
|
|
|
|
// check to see if we can delete the domain entry
|
|
|
|
if len(m.domains[options.Domain]) == 0 {
|
|
|
|
delete(m.domains, options.Domain)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-19 23:12:48 +03:00
|
|
|
func (m *mdnsRegister) LookupService(ctx context.Context, service string, opts ...register.LookupOption) ([]*register.Service, error) {
|
2020-07-27 15:22:00 +03:00
|
|
|
// parse the options
|
2021-02-19 23:12:48 +03:00
|
|
|
var options register.LookupOptions
|
2020-07-27 15:22:00 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
if len(options.Domain) == 0 {
|
|
|
|
options.Domain = m.defaultDomain
|
|
|
|
}
|
2021-02-19 23:12:48 +03:00
|
|
|
if options.Domain == register.WildcardDomain {
|
2020-07-27 15:22:00 +03:00
|
|
|
options.Domain = m.globalDomain
|
|
|
|
}
|
|
|
|
|
2021-02-19 23:12:48 +03:00
|
|
|
serviceMap := make(map[string]*register.Service)
|
|
|
|
entries := make(chan *util.ServiceEntry, 10)
|
2020-07-27 15:22:00 +03:00
|
|
|
done := make(chan bool)
|
|
|
|
|
2021-02-19 23:12:48 +03:00
|
|
|
p := util.DefaultParams(service)
|
2020-07-27 15:22:00 +03:00
|
|
|
// set context with timeout
|
2021-02-19 23:12:48 +03:00
|
|
|
//var cancel context.CancelFunc
|
|
|
|
//p.Context, cancel = context.WithTimeout(context.Background(), m.opts.Timeout)
|
|
|
|
//defer cancel()
|
2020-07-27 15:22:00 +03:00
|
|
|
// set entries channel
|
|
|
|
p.Entries = entries
|
|
|
|
// set the domain
|
|
|
|
p.Domain = options.Domain
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case e := <-entries:
|
|
|
|
// list record so skip
|
|
|
|
if e.Name == "_services" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
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 {
|
2021-02-19 23:12:48 +03:00
|
|
|
s = ®ister.Service{
|
2020-07-27 15:22:00 +03:00
|
|
|
Name: txt.Service,
|
|
|
|
Version: txt.Version,
|
|
|
|
Endpoints: txt.Endpoints,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
addr := ""
|
|
|
|
// prefer ipv4 addrs
|
|
|
|
if len(e.AddrV4) > 0 {
|
|
|
|
addr = e.AddrV4.String()
|
|
|
|
// else use ipv6
|
|
|
|
} else if len(e.AddrV6) > 0 {
|
|
|
|
addr = "[" + e.AddrV6.String() + "]"
|
|
|
|
} else {
|
2021-02-19 23:12:48 +03:00
|
|
|
if logger.V(logger.InfoLevel) {
|
|
|
|
logger.Infof(context.TODO(), "[mdns]: invalid endpoint received: %v", e)
|
2020-07-27 15:22:00 +03:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2021-02-19 23:12:48 +03:00
|
|
|
s.Nodes = append(s.Nodes, ®ister.Node{
|
2021-06-05 17:16:58 +03:00
|
|
|
ID: strings.TrimSuffix(e.Name, "."+p.Service+"."+p.Domain+"."),
|
2020-07-27 15:22:00 +03:00
|
|
|
Address: fmt.Sprintf("%s:%d", addr, e.Port),
|
|
|
|
Metadata: txt.Metadata,
|
|
|
|
})
|
|
|
|
|
|
|
|
serviceMap[txt.Version] = s
|
2021-02-19 23:12:48 +03:00
|
|
|
//case <-p.Context.Done():
|
|
|
|
// close(done)
|
|
|
|
// return
|
2020-07-27 15:22:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// execute the query
|
2021-02-19 23:12:48 +03:00
|
|
|
if err := util.Query(ctx, p); err != nil {
|
2020-07-27 15:22:00 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for completion
|
|
|
|
<-done
|
|
|
|
|
|
|
|
// create list and return
|
2021-02-19 23:12:48 +03:00
|
|
|
services := make([]*register.Service, 0, len(serviceMap))
|
2020-07-27 15:22:00 +03:00
|
|
|
|
|
|
|
for _, service := range serviceMap {
|
|
|
|
services = append(services, service)
|
|
|
|
}
|
|
|
|
|
|
|
|
return services, nil
|
|
|
|
}
|
|
|
|
|
2021-02-19 23:12:48 +03:00
|
|
|
func (m *mdnsRegister) ListServices(ctx context.Context, opts ...register.ListOption) ([]*register.Service, error) {
|
2020-07-27 15:22:00 +03:00
|
|
|
// parse the options
|
2021-02-19 23:12:48 +03:00
|
|
|
var options register.ListOptions
|
2020-07-27 15:22:00 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
if len(options.Domain) == 0 {
|
|
|
|
options.Domain = m.defaultDomain
|
|
|
|
}
|
2021-02-19 23:12:48 +03:00
|
|
|
if options.Domain == register.WildcardDomain {
|
2020-07-27 15:22:00 +03:00
|
|
|
options.Domain = m.globalDomain
|
|
|
|
}
|
|
|
|
|
|
|
|
serviceMap := make(map[string]bool)
|
2021-02-19 23:12:48 +03:00
|
|
|
entries := make(chan *util.ServiceEntry, 10)
|
2020-07-27 15:22:00 +03:00
|
|
|
done := make(chan bool)
|
|
|
|
|
2021-02-19 23:12:48 +03:00
|
|
|
p := util.DefaultParams("_services")
|
2020-07-27 15:22:00 +03:00
|
|
|
// set context with timeout
|
2021-02-19 23:12:48 +03:00
|
|
|
//var cancel context.CancelFunc
|
|
|
|
//p.Context, cancel = context.WithTimeout(context.Background(), m.opts.Timeout)
|
|
|
|
//defer cancel()
|
2020-07-27 15:22:00 +03:00
|
|
|
// set entries channel
|
|
|
|
p.Entries = entries
|
|
|
|
// set domain
|
|
|
|
p.Domain = options.Domain
|
|
|
|
|
2021-02-19 23:12:48 +03:00
|
|
|
var services []*register.Service
|
2020-07-27 15:22:00 +03:00
|
|
|
|
|
|
|
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
|
2021-02-19 23:12:48 +03:00
|
|
|
services = append(services, ®ister.Service{Name: name})
|
2020-07-27 15:22:00 +03:00
|
|
|
}
|
2021-02-19 23:12:48 +03:00
|
|
|
//case <-p.Context.Done():
|
|
|
|
// close(done)
|
|
|
|
//return
|
2020-07-27 15:22:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// execute query
|
2021-02-19 23:12:48 +03:00
|
|
|
if err := util.Query(ctx, p); err != nil {
|
2020-07-27 15:22:00 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait till done
|
|
|
|
<-done
|
|
|
|
|
|
|
|
return services, nil
|
|
|
|
}
|
|
|
|
|
2021-02-19 23:12:48 +03:00
|
|
|
func (m *mdnsRegister) Watch(ctx context.Context, opts ...register.WatchOption) (register.Watcher, error) {
|
|
|
|
var wo register.WatchOptions
|
2020-07-27 15:22:00 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&wo)
|
|
|
|
}
|
|
|
|
if len(wo.Domain) == 0 {
|
|
|
|
wo.Domain = m.defaultDomain
|
|
|
|
}
|
2021-02-19 23:12:48 +03:00
|
|
|
if wo.Domain == register.WildcardDomain {
|
2020-07-27 15:22:00 +03:00
|
|
|
wo.Domain = m.globalDomain
|
|
|
|
}
|
|
|
|
|
|
|
|
md := &mdnsWatcher{
|
|
|
|
id: uuid.New().String(),
|
|
|
|
wo: wo,
|
2021-02-19 23:12:48 +03:00
|
|
|
ch: make(chan *util.ServiceEntry, 32),
|
2020-07-27 15:22:00 +03:00
|
|
|
exit: make(chan struct{}),
|
|
|
|
domain: wo.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
|
|
|
|
}
|
|
|
|
|
|
|
|
// start the listener
|
|
|
|
go func() {
|
|
|
|
// 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{})
|
2021-02-19 23:12:48 +03:00
|
|
|
ch := make(chan *util.ServiceEntry, 32)
|
2020-07-27 15:22:00 +03:00
|
|
|
m.listener = ch
|
|
|
|
|
|
|
|
m.mtx.Unlock()
|
|
|
|
|
|
|
|
// send messages to the watchers
|
|
|
|
go func() {
|
2021-02-19 23:12:48 +03:00
|
|
|
send := func(w *mdnsWatcher, e *util.ServiceEntry) {
|
2020-07-27 15:22:00 +03:00
|
|
|
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
|
2021-02-19 23:12:48 +03:00
|
|
|
util.Listen(ch, exit)
|
2020-07-27 15:22:00 +03:00
|
|
|
|
2021-02-19 23:12:48 +03:00
|
|
|
// util.Listen has unblocked
|
2020-07-27 15:22:00 +03:00
|
|
|
// kill the saved listener
|
|
|
|
m.mtx.Lock()
|
|
|
|
m.listener = nil
|
|
|
|
close(ch)
|
|
|
|
m.mtx.Unlock()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return md, nil
|
|
|
|
}
|
|
|
|
|
2021-02-19 23:12:48 +03:00
|
|
|
func (m *mdnsRegister) String() string {
|
2020-07-27 15:22:00 +03:00
|
|
|
return "mdns"
|
|
|
|
}
|
|
|
|
|
2021-02-19 23:12:48 +03:00
|
|
|
func (m *mdnsWatcher) Next() (*register.Result, error) {
|
2020-07-27 15:22:00 +03:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case e := <-m.ch:
|
|
|
|
txt, err := decode(e.InfoFields)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(txt.Service) == 0 || len(txt.Version) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter watch options
|
|
|
|
// wo.Service: Only keep services we care about
|
|
|
|
if len(m.wo.Service) > 0 && txt.Service != m.wo.Service {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var action string
|
|
|
|
if e.TTL == 0 {
|
|
|
|
action = "delete"
|
|
|
|
} else {
|
|
|
|
action = "create"
|
|
|
|
}
|
|
|
|
|
2021-02-19 23:12:48 +03:00
|
|
|
service := ®ister.Service{
|
2020-07-27 15:22:00 +03:00
|
|
|
Name: txt.Service,
|
|
|
|
Version: txt.Version,
|
|
|
|
Endpoints: txt.Endpoints,
|
|
|
|
Metadata: txt.Metadata,
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip anything without the domain we care about
|
|
|
|
suffix := fmt.Sprintf(".%s.%s.", service.Name, m.domain)
|
|
|
|
if !strings.HasSuffix(e.Name, suffix) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var addr string
|
|
|
|
if len(e.AddrV4) > 0 {
|
|
|
|
addr = e.AddrV4.String()
|
|
|
|
} else if len(e.AddrV6) > 0 {
|
|
|
|
addr = "[" + e.AddrV6.String() + "]"
|
2021-02-19 23:12:48 +03:00
|
|
|
// } else {
|
|
|
|
// addr = e.Addr.String()
|
2020-07-27 15:22:00 +03:00
|
|
|
}
|
|
|
|
|
2021-02-19 23:12:48 +03:00
|
|
|
service.Nodes = append(service.Nodes, ®ister.Node{
|
2021-06-05 17:16:58 +03:00
|
|
|
ID: strings.TrimSuffix(e.Name, suffix),
|
2020-07-27 15:22:00 +03:00
|
|
|
Address: fmt.Sprintf("%s:%d", addr, e.Port),
|
|
|
|
Metadata: txt.Metadata,
|
|
|
|
})
|
|
|
|
|
2021-02-19 23:12:48 +03:00
|
|
|
return ®ister.Result{
|
2020-07-27 15:22:00 +03:00
|
|
|
Action: action,
|
|
|
|
Service: service,
|
|
|
|
}, nil
|
|
|
|
case <-m.exit:
|
2021-02-19 23:12:48 +03:00
|
|
|
return nil, register.ErrWatcherStopped
|
2020-07-27 15:22:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mdnsWatcher) Stop() {
|
|
|
|
select {
|
|
|
|
case <-m.exit:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
close(m.exit)
|
|
|
|
// remove self from the registry
|
|
|
|
m.registry.mtx.Lock()
|
|
|
|
delete(m.registry.watchers, m.id)
|
|
|
|
m.registry.mtx.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-19 23:12:48 +03:00
|
|
|
func (m *mdnsRegister) Name() string {
|
|
|
|
return m.opts.Name
|
|
|
|
}
|
|
|
|
|
2020-07-27 15:22:00 +03:00
|
|
|
// NewRegistry returns a new default registry which is mdns
|
2021-02-19 23:12:48 +03:00
|
|
|
func NewRegister(opts ...register.Option) register.Register {
|
|
|
|
return newRegister(opts...)
|
2019-09-09 15:11:25 +03:00
|
|
|
}
|