classify.go 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package adapters
  2. import "strings"
  3. // ---------------------------------------------------------------------------
  4. // Clasificación de tipo de dispositivo live (2-Ago-2026). El FDB/DHCP no dice
  5. // QUÉ es cada cliente: se estima por patrones de hostname (y en el futuro por
  6. // OUI). Solo afecta a presentación (icono/grupo); ante la duda, "desconocido"
  7. // — nunca afirmar un tipo sin evidencia razonable.
  8. // ---------------------------------------------------------------------------
  9. // typeRule: substrings (minúsculas) del hostname → tipo. El orden importa:
  10. // la primera regla que casa gana (de más específica a más genérica).
  11. var typeRules = []struct {
  12. typ string
  13. subs []string
  14. }{
  15. // Consola antes que tv/iot: "switch" a secas es switch de red, pero
  16. // "switch OLED" no aparece en hostnames reales; Nintendo suele anunciarse
  17. // como "nintendo-switch".
  18. {"consola", []string{"playstation", "ps4", "ps5", "xbox", "nintendo", "steamdeck", "steam-deck", "consola"}},
  19. {"tv", []string{"appletv", "apple-tv", "bravia", "webos", "firetv", "fire-tv", "chromecast", "mibox", "mi-box", "shield", "television", "smart-tv", "smarttv", "-tv", "tv-"}},
  20. {"camara", []string{"camara", "camera", "doorbell", "timbre", "ezviz", "tapo-c", "-cam", "cam-"}},
  21. {"altavoz", []string{"sonos", "heos", "homepod", "echo-", "altavoz", "speaker", "soundbar", "marantz", "denon", "home-mini", "nest-mini", "nest-audio", "amplificador", "receiver"}},
  22. {"tablet", []string{"ipad", "tablet", "kindle", "kobo"}},
  23. {"movil", []string{"iphone", "android", "pixel", "galaxy-s", "galaxy-a", "redmi-note", "oneplus", "xiaomi-1", "mi-1", "phone", "movil"}},
  24. {"portatil", []string{"macbook", "laptop", "thinkpad", "ideapad", "portatil", "notebook", "surface", "hp-laptop", "lenovo-yoga"}},
  25. {"servidor", []string{"proxmox", "pve", "jellyfin", "transmission", "helios", "homeassistant", "home-assistant", "haos", "servidor", "server", "nas", "citadel", "omv", "truenas", "pihole", "pi-hole", "adguard", "raspberry", "rpi", "docker", "keynest", "deltos", "nido", "netpulse"}},
  26. {"ordenador", []string{"imac", "mac-mini", "macstudio", "mac-studio", "desktop", "sobremesa", "workstation", "nuc", "ser9", "pc-", "-pc", "tower", "minipc", "mini-pc"}},
  27. {"switch", []string{"gs308", "gs305", "tl-sg", "switch"}},
  28. {"iot", []string{"roomba", "irobot", "roborock", "aspirador", "robot", "tasmota", "sonoff", "shelly", "esphome", "tuya", "smartlife", "meross", "gosund", "switchbot", "aqara", "lumi", "zigbee", "zhirui", "osram", "ikea", "tradfri", "hue", "wled", "athom", "cargador", "wallbox", "feyree", "tedee", "cerradura", "enchufe", "plug", "bombilla", "downlight", "persiana", "curtain", "riego", "sprinkler", "termo", "termostato", "caldera", "aire", "ac-", "slzb", "impresora", "printer", "epson", "brother", "canon"}},
  29. }
  30. // GuessDeviceType estima el DeviceType a partir del hostname (DHCP) y, si se
  31. // conoce, del fabricante OUI. Vacío/desconocido → "desconocido".
  32. func GuessDeviceType(hostname, manufacturer string) string {
  33. h := strings.ToLower(strings.TrimSpace(hostname))
  34. m := strings.ToLower(strings.TrimSpace(manufacturer))
  35. // Hostnames basura frecuentes (MAC como nombre, "unknown-…", "android-…"
  36. // genérico de DHCP) solo aportan si casan con una regla clara.
  37. for _, r := range typeRules {
  38. for _, s := range r.subs {
  39. if h != "" && strings.Contains(h, s) {
  40. return r.typ
  41. }
  42. }
  43. }
  44. // Fabricante como refuerzo cuando el hostname no dice nada.
  45. ouiIoT := []string{"espressif", "tuya", "shenzhen", "sonoff", "shelly", "tasmota", "meross", "gosund", "lumi", "aqara", "tuya smart"}
  46. for _, s := range ouiIoT {
  47. if m != "" && strings.Contains(m, s) {
  48. return "iot"
  49. }
  50. }
  51. return "desconocido"
  52. }