| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- // wl.go — último recurso para drivers Broadcom dhd (Asuswrt/Merlin con
- // wl): el nl80211 de esos drivers no expone NI la tabla de estaciones NI el
- // canal (`iw station dump` vacío, `iw info` sin Channel), así que una radio
- // con clientes reales parece vacía: el panel de radios no trae banda y los
- // clientes solo existen como hint wireless-side con banda "—". `wl` sí lo
- // sabe todo: band (a/b), channel, assoclist y rssi por estación.
- package probe
- import (
- "strconv"
- "strings"
- )
- // CmdWlWireless: un solo spawn. Por interfaz AP de `iw dev` emite
- // "==IF== <iface>", la dirección (BSSID), la letra de banda, la línea de
- // canal y una línea "STA <mac> <rssi>" por estación asociada. Sin `wl`
- // (no-Broadcom) o sin permisos la salida queda vacía: best-effort como el
- // resto de sondas. OJO: sin `command -v` (el ash de Merlin no lo tiene).
- 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`
- // wlWidthMhz parses the channel width from a `wl chanspec` value: "48/80
- // (0xe32a)" -> 80, "11/40" -> 40, a bare "11" -> 20 (20 MHz is the implicit
- // default when no "/<width>" is present). Returns 0 only if the token is
- // unparseable, so the panel can show "—" instead of a fake number.
- func wlWidthMhz(chanspec string) int {
- tok := strings.TrimSpace(chanspec)
- if i := strings.IndexByte(tok, ' '); i >= 0 {
- tok = tok[:i] // drop the "(0x....)" suffix
- }
- if s := strings.SplitN(tok, "/", 2); len(s) == 2 {
- if w, err := strconv.Atoi(strings.TrimSpace(s[1])); err == nil && w > 0 {
- return w
- }
- return 0
- }
- if tok == "" {
- return 0
- }
- if _, err := strconv.Atoi(tok); err != nil {
- return 0 // not a bare channel number: unknown, don't fake 20
- }
- return 20 // a bare channel number: 20 MHz
- }
- // wlBand: la letra de `wl band` a banda canónica. "b" = 2.4G (802.11b/g/n),
- // "a" = 5G; el resto (n/ax/be y variantes 6G) se decide por canal.
- func wlBand(letter string, channel int) string {
- switch strings.TrimSpace(strings.ToLower(letter)) {
- case "b":
- return "2.4 GHz"
- case "a":
- return "5 GHz"
- }
- if channel > 0 {
- if channel <= 14 {
- return "2.4 GHz"
- }
- return "5 GHz"
- }
- return ""
- }
- // ParseWlWireless parsea la salida de CmdWlWireless. Contratos:
- // - clientes: MAC upper → WirelessClient{SignalDbm, Band de su interfaz};
- // rssi no numérico (estación recién ido) → cliente con señal 0 omitida
- // vía SignalDbm=0 igualmente, la presencia es lo que importa.
- // - radios: una entrada por interfaz con BSSID (dirección del puerto) y
- // banda/canal conocidos; sin banda no se emite radio (mejor nada que
- // una banda inventada).
- func ParseWlWireless(out string) (clients map[string]WirelessClient, radios []Radio) {
- clients = map[string]WirelessClient{}
- curBand := ""
- curAddr := ""
- curCh := 0
- curWidth := 0
- pending := 0
- flush := func() {
- if b := wlBand(curBand, curCh); b != "" && curAddr != "" {
- radios = append(radios, Radio{Name: b, Channel: curCh, WidthMhz: curWidth, BSSID: strings.ToUpper(curAddr), Clients: pending})
- }
- }
- for _, line := range strings.Split(out, "\n") {
- t := strings.TrimSpace(line)
- switch {
- case t == "":
- case strings.HasPrefix(t, "==IF== "):
- flush()
- curBand, curAddr, curCh, curWidth, pending = "", "", 0, 0, 0
- case strings.HasPrefix(t, "CHSPEC"):
- curWidth = wlWidthMhz(strings.TrimSpace(strings.TrimPrefix(t, "CHSPEC")))
- case len(t) == 17 && !strings.ContainsAny(t, " "):
- curAddr = t
- case t == "a" || t == "b" || t == "n" || t == "ac" || t == "ax" || t == "be":
- curBand = t
- case strings.Contains(t, "channel"):
- fs := strings.Fields(t)
- if len(fs) > 0 {
- if ch, err := strconv.Atoi(fs[len(fs)-1]); err == nil {
- curCh = ch
- }
- }
- case strings.HasPrefix(t, "STA "):
- fs := strings.Fields(t)
- if len(fs) < 3 {
- continue
- }
- sig, err := strconv.Atoi(fs[2])
- if err != nil || sig >= 0 {
- continue // estación recién ida o rssi no numérico: sin señal fiable, no se inventa
- }
- band := wlBand(curBand, curCh)
- if band == "" {
- band = "—"
- }
- clients[strings.ToUpper(fs[1])] = WirelessClient{SignalDbm: sig, Band: band}
- pending++
- }
- }
- flush()
- if len(clients) == 0 {
- clients = nil
- }
- return clients, radios
- }
|