iw.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // iw.go — fallback nl80211 portable (#1481): `iw dev` + `iw station dump`
  2. // por interfaz para targets sin ubus hostapd (Asuswrt con driver compatible
  3. // nl80211, Linux genérico). Mismo shape que el camino hostapd/iwinfo:
  4. // clientes keyed por MAC mayúscula y radios agrupadas por phy.
  5. package probe
  6. import (
  7. "strconv"
  8. "strings"
  9. )
  10. // CmdIwStationDump: un SOLO spawn: el bucle de shell itera las interfaces de
  11. // `iw dev` y vuelca sus estaciones. La sección ==DEV== lleva la topología
  12. // (phy → interfaces, banda, BSSID) y cada ==IF== su station dump.
  13. const CmdIwStationDump = `echo "==DEV=="; iw dev 2>/dev/null; for i in $(iw dev 2>/dev/null | awk '/^[ \t]*Interface /{print $2}'); do echo "==IF==$i"; iw dev "$i" station dump 2>/dev/null; done`
  14. // bandFromMHz convierte una frecuencia MHz al nombre de banda del payload
  15. // ("2.4 GHz"|"5 GHz"|"6 GHz"); "" si no cae en ninguna conocida.
  16. func bandFromMHz(mhz int) string {
  17. switch {
  18. case mhz >= 2300 && mhz <= 2500:
  19. return "2.4 GHz"
  20. case mhz >= 4900 && mhz < 5950:
  21. return "5 GHz"
  22. case mhz >= 5950 && mhz <= 7125:
  23. return "6 GHz"
  24. }
  25. return ""
  26. }
  27. // iwIface: una interfaz de `iw dev` con lo que hace falta para bandas y
  28. // radios (solo interesan las tipo AP para las radios del equipo).
  29. type iwIface struct {
  30. phy string
  31. name string
  32. addr string
  33. kind string
  34. band string
  35. channel int
  36. width int
  37. }
  38. // ParseIwWireless parsea la salida combinada de CmdIwStationDump: clientes
  39. // asociados (signal, contadores, banda de su interfaz) y radios por phy
  40. // (una por phy con interfaces AP: banda/canal/ancho/BSSID y conteo de
  41. // estaciones). Defensivo: líneas desconocidas se ignoran; sin ==DEV== los
  42. // clientes salen sin banda y sin radios.
  43. func ParseIwWireless(out string) (map[string]WirelessClient, []Radio) {
  44. clients := map[string]WirelessClient{}
  45. stationIface := map[string]string{}
  46. ifaces := map[string]iwIface{}
  47. phyOrder := []string{}
  48. phyIfaces := map[string][]iwIface{}
  49. section := "dev" // nombre de interfaz del ==IF== actual
  50. curPhy := ""
  51. var cur *iwIface
  52. flush := func() {
  53. if cur != nil && cur.name != "" {
  54. ifaces[cur.name] = *cur
  55. phyIfaces[cur.phy] = append(phyIfaces[cur.phy], *cur)
  56. }
  57. cur = nil
  58. }
  59. station := ""
  60. for _, line := range strings.Split(out, "\n") {
  61. trim := strings.TrimSpace(line)
  62. switch {
  63. case trim == "==DEV==":
  64. flush()
  65. section = "dev"
  66. station = ""
  67. continue
  68. case strings.HasPrefix(trim, "==IF=="):
  69. flush()
  70. section = strings.TrimPrefix(trim, "==IF==")
  71. station = ""
  72. continue
  73. }
  74. if section == "dev" {
  75. switch {
  76. case strings.HasPrefix(trim, "phy#"):
  77. curPhy = trim
  78. phyOrder = append(phyOrder, curPhy)
  79. case strings.HasPrefix(trim, "Interface "):
  80. flush()
  81. cur = &iwIface{phy: curPhy, name: strings.TrimPrefix(trim, "Interface ")}
  82. case cur != nil && strings.HasPrefix(trim, "addr "):
  83. cur.addr = strings.ToUpper(strings.TrimPrefix(trim, "addr "))
  84. case cur != nil && strings.HasPrefix(trim, "type "):
  85. cur.kind = strings.TrimPrefix(trim, "type ")
  86. case cur != nil && strings.HasPrefix(trim, "channel "):
  87. f := strings.Fields(trim)
  88. if len(f) >= 2 {
  89. cur.channel, _ = strconv.Atoi(f[1])
  90. }
  91. if open := strings.Index(trim, "("); open >= 0 {
  92. rest := trim[open+1:]
  93. if close := strings.Index(rest, ")"); close > 0 {
  94. if mhz, err := strconv.Atoi(strings.Fields(rest[:close])[0]); err == nil {
  95. cur.band = bandFromMHz(mhz)
  96. }
  97. }
  98. }
  99. if w := strings.Index(trim, "width:"); w >= 0 {
  100. if wf := strings.Fields(trim[w:]); len(wf) >= 2 {
  101. cur.width, _ = strconv.Atoi(wf[1])
  102. }
  103. }
  104. }
  105. continue
  106. }
  107. // Sección de station dump.
  108. if strings.HasPrefix(trim, "Station ") {
  109. // Station 24:9e:7d:0a:c3:1a (on wl3.1)
  110. f := strings.Fields(trim)
  111. if len(f) >= 4 {
  112. station = strings.ToUpper(f[1])
  113. iface := strings.TrimSuffix(strings.TrimPrefix(f[3], "("), ")")
  114. stationIface[station] = iface
  115. if _, ok := clients[station]; !ok {
  116. clients[station] = WirelessClient{Band: ifaces[iface].band}
  117. }
  118. }
  119. continue
  120. }
  121. if station == "" {
  122. continue
  123. }
  124. c := clients[station]
  125. switch {
  126. case strings.HasPrefix(trim, "signal:"):
  127. // signal: -56 [-62] dBm (el primero es el agregado)
  128. if f := strings.Fields(trim); len(f) >= 2 {
  129. if dbm, err := strconv.Atoi(f[1]); err == nil {
  130. c.SignalDbm = dbm
  131. }
  132. }
  133. case strings.HasPrefix(trim, "rx bytes:"):
  134. if f := strings.Fields(trim); len(f) >= 3 {
  135. c.RxBytes, _ = strconv.ParseUint(f[2], 10, 64)
  136. }
  137. case strings.HasPrefix(trim, "tx bytes:"):
  138. if f := strings.Fields(trim); len(f) >= 3 {
  139. c.TxBytes, _ = strconv.ParseUint(f[2], 10, 64)
  140. }
  141. }
  142. clients[station] = c
  143. }
  144. flush()
  145. radios := []Radio{}
  146. for _, phy := range phyOrder {
  147. aps := []iwIface{}
  148. for _, ifc := range phyIfaces[phy] {
  149. if ifc.kind == "AP" {
  150. aps = append(aps, ifc)
  151. }
  152. }
  153. if len(aps) == 0 {
  154. continue
  155. }
  156. inPhy := map[string]bool{}
  157. for _, ifc := range aps {
  158. inPhy[ifc.name] = true
  159. }
  160. n := 0
  161. for mac, iface := range stationIface {
  162. if _, ok := clients[mac]; ok && inPhy[iface] {
  163. n++
  164. }
  165. }
  166. radios = append(radios, Radio{
  167. Name: aps[0].band, Channel: aps[0].channel,
  168. WidthMhz: aps[0].width, BSSID: aps[0].addr, Clients: n,
  169. })
  170. }
  171. return clients, radios
  172. }