wl.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // wl.go — último recurso para drivers Broadcom dhd (Asuswrt/Merlin con
  2. // wl): el nl80211 de esos drivers no expone NI la tabla de estaciones NI el
  3. // canal (`iw station dump` vacío, `iw info` sin Channel), así que una radio
  4. // con clientes reales parece vacía: el panel de radios no trae banda y los
  5. // clientes solo existen como hint wireless-side con banda "—". `wl` sí lo
  6. // sabe todo: band (a/b), channel, assoclist y rssi por estación.
  7. package probe
  8. import (
  9. "strconv"
  10. "strings"
  11. )
  12. // CmdWlWireless: un solo spawn. Por interfaz AP de `iw dev` emite
  13. // "==IF== <iface>", la dirección (BSSID), la letra de banda, la línea de
  14. // canal y una línea "STA <mac> <rssi>" por estación asociada. Sin `wl`
  15. // (no-Broadcom) o sin permisos la salida queda vacía: best-effort como el
  16. // resto de sondas. OJO: sin `command -v` (el ash de Merlin no lo tiene).
  17. const CmdWlWireless = `if [ -x /usr/sbin/wl ]; then for i in $(iw dev 2>/dev/null | awk '/^[ \t]*Interface /{n=$2} /^[ \t]*type AP/{print n}'); do echo "==IF== $i"; cat /sys/class/net/$i/address 2>/dev/null; /usr/sbin/wl -i "$i" band 2>/dev/null | head -1; /usr/sbin/wl -i "$i" channel 2>/dev/null | tail -1; echo "CHSPEC $(/usr/sbin/wl -i "$i" chanspec 2>/dev/null)"; for m in $(/usr/sbin/wl -i "$i" assoclist 2>/dev/null | awk '/^assoclist/{print $2}'); do echo "STA $m $(/usr/sbin/wl -i "$i" rssi "$m" 2>/dev/null)"; done; done; fi`
  18. // wlWidthMhz parses the channel width from a `wl chanspec` value: "48/80
  19. // (0xe32a)" -> 80, "11/40" -> 40, a bare "11" -> 20 (20 MHz is the implicit
  20. // default when no "/<width>" is present). Returns 0 only if the token is
  21. // unparseable, so the panel can show "—" instead of a fake number.
  22. func wlWidthMhz(chanspec string) int {
  23. tok := strings.TrimSpace(chanspec)
  24. if i := strings.IndexByte(tok, ' '); i >= 0 {
  25. tok = tok[:i] // drop the "(0x....)" suffix
  26. }
  27. if s := strings.SplitN(tok, "/", 2); len(s) == 2 {
  28. if w, err := strconv.Atoi(strings.TrimSpace(s[1])); err == nil && w > 0 {
  29. return w
  30. }
  31. return 0
  32. }
  33. if tok == "" {
  34. return 0
  35. }
  36. if _, err := strconv.Atoi(tok); err != nil {
  37. return 0 // not a bare channel number: unknown, don't fake 20
  38. }
  39. return 20 // a bare channel number: 20 MHz
  40. }
  41. // wlBand: la letra de `wl band` a banda canónica. "b" = 2.4G (802.11b/g/n),
  42. // "a" = 5G; el resto (n/ax/be y variantes 6G) se decide por canal.
  43. func wlBand(letter string, channel int) string {
  44. switch strings.TrimSpace(strings.ToLower(letter)) {
  45. case "b":
  46. return "2.4 GHz"
  47. case "a":
  48. return "5 GHz"
  49. }
  50. if channel > 0 {
  51. if channel <= 14 {
  52. return "2.4 GHz"
  53. }
  54. return "5 GHz"
  55. }
  56. return ""
  57. }
  58. // ParseWlWireless parsea la salida de CmdWlWireless. Contratos:
  59. // - clientes: MAC upper → WirelessClient{SignalDbm, Band de su interfaz};
  60. // rssi no numérico (estación recién ido) → cliente con señal 0 omitida
  61. // vía SignalDbm=0 igualmente, la presencia es lo que importa.
  62. // - radios: una entrada por interfaz con BSSID (dirección del puerto) y
  63. // banda/canal conocidos; sin banda no se emite radio (mejor nada que
  64. // una banda inventada).
  65. func ParseWlWireless(out string) (clients map[string]WirelessClient, radios []Radio) {
  66. clients = map[string]WirelessClient{}
  67. curBand := ""
  68. curAddr := ""
  69. curCh := 0
  70. curWidth := 0
  71. pending := 0
  72. flush := func() {
  73. if b := wlBand(curBand, curCh); b != "" && curAddr != "" {
  74. radios = append(radios, Radio{Name: b, Channel: curCh, WidthMhz: curWidth, BSSID: strings.ToUpper(curAddr), Clients: pending})
  75. }
  76. }
  77. for _, line := range strings.Split(out, "\n") {
  78. t := strings.TrimSpace(line)
  79. switch {
  80. case t == "":
  81. case strings.HasPrefix(t, "==IF== "):
  82. flush()
  83. curBand, curAddr, curCh, curWidth, pending = "", "", 0, 0, 0
  84. case strings.HasPrefix(t, "CHSPEC"):
  85. curWidth = wlWidthMhz(strings.TrimSpace(strings.TrimPrefix(t, "CHSPEC")))
  86. case len(t) == 17 && !strings.ContainsAny(t, " "):
  87. curAddr = t
  88. case t == "a" || t == "b" || t == "n" || t == "ac" || t == "ax" || t == "be":
  89. curBand = t
  90. case strings.Contains(t, "channel"):
  91. fs := strings.Fields(t)
  92. if len(fs) > 0 {
  93. if ch, err := strconv.Atoi(fs[len(fs)-1]); err == nil {
  94. curCh = ch
  95. }
  96. }
  97. case strings.HasPrefix(t, "STA "):
  98. fs := strings.Fields(t)
  99. if len(fs) < 3 {
  100. continue
  101. }
  102. sig, err := strconv.Atoi(fs[2])
  103. if err != nil || sig >= 0 {
  104. continue // estación recién ida o rssi no numérico: sin señal fiable, no se inventa
  105. }
  106. band := wlBand(curBand, curCh)
  107. if band == "" {
  108. band = "—"
  109. }
  110. clients[strings.ToUpper(fs[1])] = WirelessClient{SignalDbm: sig, Band: band}
  111. pending++
  112. }
  113. }
  114. flush()
  115. if len(clients) == 0 {
  116. clients = nil
  117. }
  118. return clients, radios
  119. }