فهرست منبع

fix(roaming): parse the most common hostapd AP-STA line format (#908) (#913)

The parser only handled the iface-twice variant ("wlan0: AP-STA-X
wlan0 <mac>") or no iface at all, but the most common OpenWrt hostapd
line has the iface once: "wlan0: AP-STA-CONNECTED 04:95:...". That
variant matched neither regex, so connect events silently never made
it into the feed. Accept an optional repeated iface and tolerate
trailing reason suffixes ((wpa2_psk), reason=N) with word-boundary
MAC matching; cover all variants with tests.
Nacho 2 ماه پیش
والد
کامیت
7737f6c213
2فایلهای تغییر یافته به همراه32 افزوده شده و 6 حذف شده
  1. 13 6
      server-go/internal/roamevents/collector.go
  2. 19 0
      server-go/internal/roamevents/collector_test.go

+ 13 - 6
server-go/internal/roamevents/collector.go

@@ -207,9 +207,16 @@ var (
 	// Variante sin año: "Sat Aug  8 19:21:45 daemon.notice hostapd: <resto>".
 	syslogNoYearRe = regexp.MustCompile(`^(?:\w{3}\s+)?(\w{3})\s+(\d+)\s+(\d{2}):(\d{2}):(\d{2})\s+(?:daemon|user)\.\w+\s+(\w+):\s+(.+)$`)
 
-	// hostapd: "<iface>: AP-STA-CONNECTED <iface2> <mac>"
-	hostapdConnRe = regexp.MustCompile(`^(\S+):\s+AP-STA-(CONNECTED|DISCONNECTED)\s+\S+\s+([0-9a-fA-F:]{17})`)
-	// hostapd variants sin segundo iface: "AP-STA-CONNECTED <mac>"
+	// hostapd: variantes reales observadas en OpenWrt:
+	//   "wlan0: AP-STA-CONNECTED 04:95:e6:76:55:a1"            (iface una vez, la más común)
+	//   "wlan0: AP-STA-CONNECTED wlan0 04:95:e6:76:55:a1"      (iface repetida)
+	//   "wlan0: AP-STA-CONNECTED 04:95:e6:76:55:a1 (wpa2_psk)" (sufijo de motivo)
+	//   "wlan0: AP-STA-DISCONNECTED 04:95:e6:76:55:a1 reason=3" (sufijo reason)
+	// La iface interior es opcional y el sufijo lo ignora la MAC con \b (#908:
+	// la variante de iface única no casaba con ninguna regex y los CONNECTED
+	// desaparecían del feed de eventos).
+	hostapdRe = regexp.MustCompile(`^(\S+):\s+AP-STA-(CONNECTED|DISCONNECTED)\s+(?:(\S+)\s+)?([0-9a-fA-F:]{17})\b`)
+	// hostapd sin prefijo de iface: "AP-STA-CONNECTED <mac>"
 	hostapdConnShortRe = regexp.MustCompile(`^AP-STA-(CONNECTED|DISCONNECTED)\s+([0-9a-fA-F:]{17})`)
 
 	// dawn: "Client / BSSID = <mac> / <bssid>: <action>"
@@ -265,10 +272,10 @@ func ParseLogreadLine(line, routerID string) (Event, bool) {
 }
 
 func parseHostapd(rest, routerID string, ts int64) (Event, bool) {
-	// Caso largo: "wlan0: AP-STA-CONNECTED wlan0 04:95:..."
-	if m := hostapdConnRe.FindStringSubmatch(rest); m != nil {
+	// Caso general (iface opcional repetida + sufijos de motivo tolerados).
+	if m := hostapdRe.FindStringSubmatch(rest); m != nil {
 		iface := m[1]
-		mac := m[3]
+		mac := m[4]
 		typ := TypeConnected
 		if m[2] == "DISCONNECTED" {
 			typ = TypeDisconnected

+ 19 - 0
server-go/internal/roamevents/collector_test.go

@@ -101,6 +101,25 @@ func TestParseHostapdConnected(t *testing.T) {
 			want:  TypeDisconnected,
 			iface: "wlan1",
 		},
+		{
+			// #908: variante más común en OpenWrt (iface una sola vez).
+			name:  "single-iface-connect",
+			line:  "Sat Aug  8 19:21:45 2026 daemon.notice hostapd: wlan0: AP-STA-CONNECTED 04:95:e6:76:55:a1",
+			want:  TypeConnected,
+			iface: "wlan0",
+		},
+		{
+			name:  "single-iface-connect-with-suffix",
+			line:  "Sat Aug  8 19:21:45 2026 daemon.notice hostapd: wlan0: AP-STA-CONNECTED 04:95:e6:76:55:a1 (wpa2_psk)",
+			want:  TypeConnected,
+			iface: "wlan0",
+		},
+		{
+			name:  "single-iface-disconnect-with-reason",
+			line:  "Sat Aug  8 19:21:45 2026 daemon.notice hostapd: wlan0: AP-STA-DISCONNECTED 04:95:e6:76:55:a1 reason=3",
+			want:  TypeDisconnected,
+			iface: "wlan0",
+		},
 	}
 	for _, c := range cases {
 		t.Run(c.name, func(t *testing.T) {