mdns.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. }
  26. func NewMDNS(config NetworkHost) (*MDNSHost, error) {
  27. //Get host MAC Address
  28. macAddress, err := getMacAddr()
  29. macAddressBoardcast := ""
  30. if err == nil {
  31. macAddressBoardcast = strings.Join(macAddress, ",")
  32. }
  33. //Register the mds services
  34. //server, err := zeroconf.Register("ArOZ", "_http._tcp", "local.", *listen_port, []string{"version_build=" + build_version, "version_minor=" + internal_version, "vendor=" + deviceVendor, "model=" + deviceModel, "uuid=" + deviceUUID, "domain=aroz.online"}, nil)
  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. })
  93. } else {
  94. if stringInSlice("domain="+domainFilter, entry.Text) {
  95. //This is a ArOZ Online Host
  96. //Split the required information out of the text element
  97. TEXT := entry.Text
  98. properties := map[string]string{}
  99. for _, v := range TEXT {
  100. kv := strings.Split(v, "=")
  101. if len(kv) == 2 {
  102. properties[kv[0]] = kv[1]
  103. }
  104. }
  105. var macAddrs []string
  106. val, ok := properties["mac_addr"]
  107. if !ok || val == "" {
  108. //No MacAddr found. Target node version too old
  109. macAddrs = []string{}
  110. } else {
  111. macAddrs = strings.Split(properties["mac_addr"], ",")
  112. }
  113. //log.Println(properties)
  114. discoveredHost = append(discoveredHost, &NetworkHost{
  115. HostName: entry.HostName,
  116. Port: entry.Port,
  117. IPv4: entry.AddrIPv4,
  118. Domain: properties["domain"],
  119. Model: properties["model"],
  120. UUID: properties["uuid"],
  121. Vendor: properties["vendor"],
  122. BuildVersion: properties["version_build"],
  123. MinorVersion: properties["version_minor"],
  124. MacAddr: macAddrs,
  125. })
  126. }
  127. }
  128. }
  129. }(entries)
  130. //Resolve each of the mDNS and pipe it back to the log functions
  131. ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(timeout))
  132. defer cancel()
  133. err = resolver.Browse(ctx, "_http._tcp", "local.", entries)
  134. if err != nil {
  135. log.Fatalln("Failed to browse:", err.Error())
  136. }
  137. <-ctx.Done()
  138. return discoveredHost
  139. }