mdns.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. IfaceOverride *net.Interface
  14. }
  15. type NetworkHost struct {
  16. HostName string
  17. Port int
  18. IPv4 []net.IP
  19. Domain string
  20. Model string
  21. UUID string
  22. Vendor string
  23. BuildVersion string
  24. MinorVersion string
  25. MacAddr []string
  26. Online bool
  27. }
  28. // Create a new MDNS discoverer, set MacOverride to empty string for using the first NIC discovered
  29. func NewMDNS(config NetworkHost, MacOverride string) (*MDNSHost, error) {
  30. //Get host MAC Address
  31. macAddress, err := getMacAddr()
  32. if err != nil {
  33. return nil, err
  34. }
  35. macAddressBoardcast := ""
  36. if err == nil {
  37. macAddressBoardcast = strings.Join(macAddress, ",")
  38. } else {
  39. log.Println("[mDNS] Unable to get MAC Address: ", err.Error())
  40. }
  41. //Register the mds services
  42. 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)
  43. if err != nil {
  44. log.Println("[mDNS] Error when registering zeroconf broadcast message", err.Error())
  45. return &MDNSHost{}, err
  46. }
  47. //Discover the iface to override if exists
  48. var overrideIface *net.Interface = nil
  49. if MacOverride != "" {
  50. ifaceIp := ""
  51. ifaces, err := net.Interfaces()
  52. if err != nil {
  53. log.Println("[mDNS] Unable to override iface MAC: " + err.Error() + ". Resuming with default iface")
  54. }
  55. foundMatching := false
  56. for _, iface := range ifaces {
  57. thisIfaceMac := iface.HardwareAddr.String()
  58. thisIfaceMac = strings.ReplaceAll(thisIfaceMac, ":", "-")
  59. MacOverride = strings.ReplaceAll(MacOverride, ":", "-")
  60. if strings.EqualFold(thisIfaceMac, strings.TrimSpace(MacOverride)) {
  61. //This is the correct iface to use
  62. overrideIface = &iface
  63. addr, err := iface.Addrs()
  64. if err == nil && len(addr) > 0 {
  65. ifaceIp = addr[0].String()
  66. }
  67. foundMatching = true
  68. break
  69. }
  70. }
  71. if !foundMatching {
  72. log.Println("[mDNS] Unable to find the target iface with MAC address: " + MacOverride + ". Resuming with default iface")
  73. } else {
  74. log.Println("[mDNS] Entering force MAC address mode, listening on: " + MacOverride + "( IP address: " + ifaceIp + ")")
  75. }
  76. }
  77. return &MDNSHost{
  78. MDNS: server,
  79. Host: &config,
  80. IfaceOverride: overrideIface,
  81. }, nil
  82. }
  83. func (m *MDNSHost) Close() {
  84. if m != nil {
  85. m.MDNS.Shutdown()
  86. }
  87. }
  88. // Scan with given timeout and domain filter. Use m.Host.Domain for scanning similar typed devices
  89. func (m *MDNSHost) Scan(timeout int, domainFilter string) []*NetworkHost {
  90. // Discover all services on the network (e.g. _workstation._tcp)
  91. var zcoption zeroconf.ClientOption = nil
  92. if m.IfaceOverride != nil {
  93. zcoption = zeroconf.SelectIfaces([]net.Interface{*m.IfaceOverride})
  94. }
  95. resolver, err := zeroconf.NewResolver(zcoption)
  96. if err != nil {
  97. log.Fatalln("Failed to initialize resolver:", err.Error())
  98. }
  99. entries := make(chan *zeroconf.ServiceEntry)
  100. //Create go routine to wait for the resolver
  101. discoveredHost := []*NetworkHost{}
  102. go func(results <-chan *zeroconf.ServiceEntry) {
  103. for entry := range results {
  104. if domainFilter == "" {
  105. //This is a ArOZ Online Host
  106. //Split the required information out of the text element
  107. TEXT := entry.Text
  108. properties := map[string]string{}
  109. for _, v := range TEXT {
  110. kv := strings.Split(v, "=")
  111. if len(kv) == 2 {
  112. properties[kv[0]] = kv[1]
  113. }
  114. }
  115. var macAddrs []string
  116. val, ok := properties["mac_addr"]
  117. if !ok || val == "" {
  118. //No MacAddr found. Target node version too old
  119. macAddrs = []string{}
  120. } else {
  121. macAddrs = strings.Split(properties["mac_addr"], ",")
  122. }
  123. //log.Println(properties)
  124. discoveredHost = append(discoveredHost, &NetworkHost{
  125. HostName: entry.HostName,
  126. Port: entry.Port,
  127. IPv4: entry.AddrIPv4,
  128. Domain: properties["domain"],
  129. Model: properties["model"],
  130. UUID: properties["uuid"],
  131. Vendor: properties["vendor"],
  132. BuildVersion: properties["version_build"],
  133. MinorVersion: properties["version_minor"],
  134. MacAddr: macAddrs,
  135. Online: true,
  136. })
  137. } else {
  138. if stringInSlice("domain="+domainFilter, entry.Text) {
  139. //This is a ArOZ Online Host
  140. //Split the required information out of the text element
  141. TEXT := entry.Text
  142. properties := map[string]string{}
  143. for _, v := range TEXT {
  144. kv := strings.Split(v, "=")
  145. if len(kv) == 2 {
  146. properties[kv[0]] = kv[1]
  147. }
  148. }
  149. var macAddrs []string
  150. val, ok := properties["mac_addr"]
  151. if !ok || val == "" {
  152. //No MacAddr found. Target node version too old
  153. macAddrs = []string{}
  154. } else {
  155. macAddrs = strings.Split(properties["mac_addr"], ",")
  156. }
  157. //log.Println(properties)
  158. discoveredHost = append(discoveredHost, &NetworkHost{
  159. HostName: entry.HostName,
  160. Port: entry.Port,
  161. IPv4: entry.AddrIPv4,
  162. Domain: properties["domain"],
  163. Model: properties["model"],
  164. UUID: properties["uuid"],
  165. Vendor: properties["vendor"],
  166. BuildVersion: properties["version_build"],
  167. MinorVersion: properties["version_minor"],
  168. MacAddr: macAddrs,
  169. Online: true,
  170. })
  171. }
  172. }
  173. }
  174. }(entries)
  175. //Resolve each of the mDNS and pipe it back to the log functions
  176. ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(timeout))
  177. defer cancel()
  178. err = resolver.Browse(ctx, "_http._tcp", "local.", entries)
  179. if err != nil {
  180. log.Fatalln("Failed to browse:", err.Error())
  181. }
  182. //Update the master scan record
  183. <-ctx.Done()
  184. return discoveredHost
  185. }