Переглянути джерело

Use iw dev for accurate SSID mapping

- iw dev returns exact SSID per interface
- Maps hostapd.wlan0-1 -> wlan0-1 -> iFilter-Q
- Maps hostapd.wlan0-2 -> wlan0-2 -> OpenWrt
- More reliable than UCI
Gogs 2 місяців тому
батько
коміт
e349a0427e
1 змінених файлів з 45 додано та 27 видалено
  1. 45 27
      package/src/client2server-minimal.lua

+ 45 - 27
package/src/client2server-minimal.lua

@@ -89,28 +89,39 @@ local function get_hostapd_interfaces()
     return interfaces
 end
 
--- Get SSID name for an interface via UCI
+-- Get SSID name for an interface via iw dev (most reliable)
 local function get_ssid_name(iface)
-    -- Parse all SSIDs from UCI and pick one based on interface
-    local f = io.popen("uci show wireless 2>/dev/null")
+    -- Map hostapd interface to wlan interface
+    local wlan_map = {
+        ["hostapd.wlan0-1"] = "wlan0-1",
+        ["hostapd.wlan0-2"] = "wlan0-2",
+    }
+    local wlan_iface = wlan_map[iface] or iface
+    
+    -- Use iw dev to get exact SSID
+    local f = io.popen("iw dev " .. wlan_iface .. " info 2>/dev/null | grep ssid")
+    if f then
+        local result = f:read("*a")
+        f:close()
+        local ssid = result:match("ssid%s+(.+)")
+        if ssid and ssid ~= "" then
+            return ssid:gsub("%s+$", "")  -- trim
+        end
+    end
+    
+    -- Fallback: try iw dev without specific interface
+    f = io.popen("iw dev 2>/dev/null")
     if f then
         local result = f:read("*a")
         f:close()
-        -- Extract all ssid values
-        local ssids = {}
-        for ssid in result:gmatch("ssid='([^']+)'") do
+        -- Extract SSIDs from output
+        for line in result:gmatch("[^
+]+") do
+            local ssid = line:match("ssid%s+(.+)")
             if ssid and ssid ~= "" then
-                table.insert(ssids, ssid)
+                return ssid
             end
         end
-        -- Map interface index to SSID
-        if iface == "hostapd.wlan0-1" and ssids[1] then
-            return ssids[1]
-        elseif iface == "hostapd.wlan0-2" and ssids[2] then
-            return ssids[2]
-        elseif ssids[1] then
-            return ssids[1]
-        end
     end
     return iface
 end
@@ -126,21 +137,28 @@ local function get_all_ssids()
     return ssids
 end
 
--- Get all SSID statuses via UCI
+-- Get all SSID statuses via iw
 local function get_all_ssids()
     local ssids = {}
-    local interfaces = get_hostapd_interfaces()
-    for _, iface in ipairs(interfaces) do
-        local ssid = get_ssid_name(iface)
-        -- Check if enabled via UCI
-        local f = io.popen("uci get wireless." .. iface .. ".disabled 2>/dev/null")
-        local enabled = true
-        if f then
-            local result = f:read("*a")
-            f:close()
-            enabled = (result:gsub("%s+$", "") ~= "1")
+    local f = io.popen("iw dev 2>/dev/null")
+    if f then
+        local result = f:read("*a")
+        f:close()
+        local current_iface = nil
+        for line in result:gmatch("[^
+]+") do
+            -- Detect interface from "Interface wlanX-1"
+            local iface = line:match("Interface%s+(wlan%d-%d)")
+            if iface then
+                current_iface = iface
+            end
+            -- Extract SSID
+            local ssid = line:match("ssid%s+(.+)")
+            if ssid and current_iface then
+                local hostapd_iface = "hostapd." .. current_iface
+                ssids[hostapd_iface] = { enabled = true, ssid = ssid }
+            end
         end
-        ssids[iface] = { enabled = enabled, ssid = ssid }
     end
     return ssids
 end