4
0

oui.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Package oui resolves the first 3 bytes of a MAC address (the IEEE OUI) to
  2. // a manufacturer name using an embedded prefix map derived from the Wireshark
  3. // manuf database (which itself is generated from IEEE registration data).
  4. // It also exposes IoT vendor classification for device-type guessing.
  5. package oui
  6. import (
  7. _ "embed"
  8. "strings"
  9. "sync"
  10. "unicode"
  11. )
  12. // The embedded table uses the compact format "aabbcc\tVendor Name" (one OUI
  13. // per line, lowercase hex prefix, tab-separated vendor). Regenerate it from
  14. // the IEEE/Wireshark manuf data when vendor assignments change.
  15. //
  16. //go:embed data/oui.txt
  17. var data string
  18. var (
  19. loadOnce sync.Once
  20. table map[string]string
  21. )
  22. func load() {
  23. loadOnce.Do(func() {
  24. table = make(map[string]string, 40000)
  25. for _, line := range strings.Split(data, "\n") {
  26. prefix, vendor, ok := strings.Cut(line, "\t")
  27. if !ok || prefix == "" || vendor == "" {
  28. continue
  29. }
  30. table[prefix] = vendor
  31. }
  32. })
  33. }
  34. // Lookup returns the manufacturer name for a MAC address (any separator or
  35. // case is accepted), or "" when the OUI is not in the embedded database.
  36. func Lookup(mac string) string {
  37. load()
  38. prefix := normalize(mac)
  39. if len(prefix) < 6 {
  40. return ""
  41. }
  42. return table[prefix[:6]]
  43. }
  44. // normalize lowercases a MAC and strips separators (':', '-', '.'), keeping
  45. // only hex digits.
  46. func normalize(mac string) string {
  47. var b strings.Builder
  48. b.Grow(len(mac))
  49. for _, c := range strings.ToLower(mac) {
  50. if (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') {
  51. b.WriteByte(byte(c))
  52. }
  53. }
  54. return b.String()
  55. }
  56. // iotVendors lists manufacturer substrings (lowercase) that identify IoT
  57. // vendors in the OUI database (sockets, bulbs, locks, vacuums, sensors...).
  58. var iotVendors = []string{
  59. "espressif",
  60. "tuya",
  61. "itead",
  62. "sonoff",
  63. "shelly",
  64. "meross",
  65. "gosund",
  66. "lumi",
  67. "aqara",
  68. "heimgard",
  69. "roborock",
  70. "dreame",
  71. "ecovacs",
  72. "signify",
  73. "ledvance",
  74. "ikea",
  75. "tradfri",
  76. "feeyree",
  77. "nuki",
  78. "tedee",
  79. "wyze",
  80. "broadlink",
  81. "qingping",
  82. "shenzhen",
  83. "tasmota",
  84. // Electrodomésticos y clima conectados: no son "el IoT de siempre" (un
  85. // enchufe, una bombilla), pero para la topología son lo mismo — un
  86. // cacharro de la casa que habla por wifi.
  87. "wiz", // bombillas WiZ (Signify, OUI propio)
  88. "gree", // aire acondicionado
  89. "hausger", // BSH Hausgeräte: horno/lavavajillas Bosch y Siemens
  90. }
  91. // cameraVendors: fabricantes cuyo catálogo es videovigilancia. Un OUI suyo
  92. // es una cámara o un grabador, no un cacharro genérico: hay un tipo propio
  93. // (con su icono) y decir "iot" sería perder información que sí tenemos.
  94. var cameraVendors = []string{
  95. "reolink",
  96. "hikvision",
  97. "dahua",
  98. "ezviz",
  99. "amcrest",
  100. "foscam",
  101. "annke",
  102. }
  103. // vacuumVendors: marcas de aspiradores robots, las mismas que ya asume la
  104. // regla de hostname. Roborock e iRobot solo hacen aspiradores; Ecovacs y
  105. // Dreame son marcas de aspirador primero (igual que la regla de hostname
  106. // los trata). Un OUI suyo con hostname opaco ("ROCK-ROBO") es un aspirador,
  107. // no un cacharro genérico.
  108. var vacuumVendors = []string{
  109. "roborock",
  110. "irobot",
  111. "ecovacs",
  112. "dreame",
  113. }
  114. // IsVacuumVendor reports whether a manufacturer is a robot vacuum brand.
  115. func IsVacuumVendor(manufacturer string) bool {
  116. return vendorMatches(manufacturer, vacuumVendors)
  117. }
  118. // IsCameraVendor reports whether a manufacturer only makes surveillance gear.
  119. func IsCameraVendor(manufacturer string) bool {
  120. return vendorMatches(manufacturer, cameraVendors)
  121. }
  122. // vendorMatches: substring search, except for tokens under 6 characters,
  123. // which have to appear as a whole word. A short token is a liability as a
  124. // substring -- "gree" would swallow Greenwave and Greenliant, "wiz" would
  125. // swallow WIZnet -- and a wrong vendor match hands the device a confident,
  126. // wrong icon. Longer tokens stay substrings so "Dreame Technology" and
  127. // "Shelly Europe Ltd" match whatever suffix the OUI registry carries.
  128. func vendorMatches(manufacturer string, list []string) bool {
  129. m := strings.ToLower(strings.TrimSpace(manufacturer))
  130. if m == "" {
  131. return false
  132. }
  133. var words []string
  134. for _, s := range list {
  135. if len(s) >= 6 {
  136. if strings.Contains(m, s) {
  137. return true
  138. }
  139. continue
  140. }
  141. if words == nil {
  142. words = strings.FieldsFunc(m, func(r rune) bool {
  143. return !unicode.IsLetter(r) && !unicode.IsDigit(r)
  144. })
  145. }
  146. for _, w := range words {
  147. if w == s {
  148. return true
  149. }
  150. }
  151. }
  152. return false
  153. }
  154. // IsIoTVendor reports whether a manufacturer name (as returned by Lookup)
  155. // matches a known IoT vendor.
  156. func IsIoTVendor(manufacturer string) bool {
  157. return vendorMatches(manufacturer, iotVendors)
  158. }