mdns.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package mdns
  2. import (
  3. "context"
  4. "log"
  5. "net"
  6. "strings"
  7. "time"
  8. "github.com/grandcat/zeroconf"
  9. )
  10. type MDNSHost struct {
  11. MDNS *zeroconf.Server
  12. Host *NetworkHost
  13. }
  14. type NetworkHost struct {
  15. HostName string
  16. Port int
  17. IPv4 []net.IP
  18. Domain string
  19. Model string
  20. UUID string
  21. Vendor string
  22. BuildVersion string
  23. MinorVersion string
  24. MacAddr []string
  25. Online bool
  26. }
  27. func NewMDNS(config NetworkHost) (*MDNSHost, error) {
  28. //Get host MAC Address
  29. macAddress, err := getMacAddr()
  30. macAddressBoardcast := ""
  31. if err == nil {
  32. macAddressBoardcast = strings.Join(macAddress, ",")
  33. }
  34. //Register the mds services
  35. server, err := zeroconf.Register(config.HostName, "_http._tcp", "local.", config.Port, []string{"version_build=" + config.BuildVersion, "version_minor=" + config.MinorVersion, "vendor=" + config.Vendor, "model=" + config.Model, "uuid=" + config.UUID, "domain=" + config.Domain, "mac_addr=" + macAddressBoardcast}, nil)
  36. if err != nil {
  37. return &MDNSHost{}, err
  38. }
  39. return &MDNSHost{
  40. MDNS: server,
  41. Host: &config,
  42. }, nil
  43. }
  44. func (m *MDNSHost) Close() {
  45. if m != nil {
  46. m.MDNS.Shutdown()
  47. }
  48. }
  49. //Scan with given timeout and domain filter. Use m.Host.Domain for scanning similar typed devices
  50. func (m *MDNSHost) Scan(timeout int, domainFilter string) []*NetworkHost {
  51. // Discover all services on the network (e.g. _workstation._tcp)
  52. resolver, err := zeroconf.NewResolver(nil)
  53. if err != nil {
  54. log.Fatalln("Failed to initialize resolver:", err.Error())
  55. }
  56. entries := make(chan *zeroconf.ServiceEntry)
  57. //Create go routine to wait for the resolver
  58. discoveredHost := []*NetworkHost{}
  59. go func(results <-chan *zeroconf.ServiceEntry) {
  60. for entry := range results {
  61. if domainFilter == "" {
  62. //This is a ArOZ Online Host
  63. //Split the required information out of the text element
  64. TEXT := entry.Text
  65. properties := map[string]string{}
  66. for _, v := range TEXT {
  67. kv := strings.Split(v, "=")
  68. if len(kv) == 2 {
  69. properties[kv[0]] = kv[1]
  70. }
  71. }
  72. var macAddrs []string
  73. val, ok := properties["mac_addr"]
  74. if !ok || val == "" {
  75. //No MacAddr found. Target node version too old
  76. macAddrs = []string{}
  77. } else {
  78. macAddrs = strings.Split(properties["mac_addr"], ",")
  79. }
  80. //log.Println(properties)
  81. discoveredHost = append(discoveredHost, &NetworkHost{
  82. HostName: entry.HostName,
  83. Port: entry.Port,
  84. IPv4: entry.AddrIPv4,
  85. Domain: properties["domain"],
  86. Model: properties["model"],
  87. UUID: properties["uuid"],
  88. Vendor: properties["vendor"],
  89. BuildVersion: properties["version_build"],
  90. MinorVersion: properties["version_minor"],
  91. MacAddr: macAddrs,
  92. Online: true,
  93. })
  94. } else {
  95. if stringInSlice("domain="+domainFilter, entry.Text) {
  96. //This is a ArOZ Online Host
  97. //Split the required information out of the text element
  98. TEXT := entry.Text
  99. properties := map[string]string{}
  100. for _, v := range TEXT {
  101. kv := strings.Split(v, "=")
  102. if len(kv) == 2 {
  103. properties[kv[0]] = kv[1]
  104. }
  105. }
  106. var macAddrs []string
  107. val, ok := properties["mac_addr"]
  108. if !ok || val == "" {
  109. //No MacAddr found. Target node version too old
  110. macAddrs = []string{}
  111. } else {
  112. macAddrs = strings.Split(properties["mac_addr"], ",")
  113. }
  114. //log.Println(properties)
  115. discoveredHost = append(discoveredHost, &NetworkHost{
  116. HostName: entry.HostName,
  117. Port: entry.Port,
  118. IPv4: entry.AddrIPv4,
  119. Domain: properties["domain"],
  120. Model: properties["model"],
  121. UUID: properties["uuid"],
  122. Vendor: properties["vendor"],
  123. BuildVersion: properties["version_build"],
  124. MinorVersion: properties["version_minor"],
  125. MacAddr: macAddrs,
  126. Online: true,
  127. })
  128. }
  129. }
  130. }
  131. }(entries)
  132. //Resolve each of the mDNS and pipe it back to the log functions
  133. ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(timeout))
  134. defer cancel()
  135. err = resolver.Browse(ctx, "_http._tcp", "local.", entries)
  136. if err != nil {
  137. log.Fatalln("Failed to browse:", err.Error())
  138. }
  139. //Update the master scan record
  140. <-ctx.Done()
  141. return discoveredHost
  142. }