util.lua 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/usr/bin/lua
  2. local uci = require('luci.model.uci').cursor()
  3. local site = require 'gluon.site_config'
  4. local json = require 'luci.jsonc'
  5. local util = require("luci.util")
  6. function get_available_wifi_networks()
  7. local interfaces = util.split(util.trim(util.exec("iw dev | grep Interface | cut -d' ' -f2")))
  8. for _, ifname in ipairs(interfaces) do
  9. for line in io.popen(string.format("iw dev %s scan 2>/dev/null", ifname)):lines() do
  10. net = line:match(".+.freifunk.net.*")
  11. if net then
  12. netname = net:sub(8)
  13. return netname
  14. end
  15. end
  16. end
  17. return false
  18. end
  19. function get_update_hosts()
  20. local hosts = {}
  21. local branch = uci:get('autoupdater', 'settings', 'branch')
  22. local mirrors = uci:get_list('autoupdater', branch, 'mirror')
  23. while #mirrors > 0 do
  24. local m = table.remove(mirrors)
  25. mirror = m:match("://%[?([a-zA-Z0-9\:\.]+)%]?/")
  26. table.insert(hosts, 1, mirror)
  27. end
  28. return hosts
  29. end
  30. function get_site_macs()
  31. local macs = {}
  32. local interfaces = util.split(util.trim(util.exec(string.format("iw dev mesh0 scan | grep -B10 %s | grep BSS | cut -f2 -d' ' | cut -f1 -d'('", site.wifi24.ap.ssid))))
  33. for _, mac in ipairs(interfaces) do
  34. table.insert(macs, mac)
  35. end
  36. return macs
  37. end
  38. function is_in_fallback_mode()
  39. if uci:get('wireless', 'fallback', 'disabled') == '0' then
  40. return true
  41. end
  42. return false
  43. end
  44. function neighbours(iface)
  45. local stations = {}
  46. for k, v in pairs(iface.iw.assoclist(iface.ifname)) do
  47. table.insert(stations,k:lower())
  48. end
  49. return stations
  50. end
  51. function interfaces()
  52. local interfaces = {}
  53. for _, line in ipairs(util.split(util.exec('batctl if'))) do
  54. local ifname = line:match('^(.-): active')
  55. if ifname ~= nil then
  56. pcall(function()
  57. local address = util.trim(fs.readfile('/sys/class/net/' .. ifname .. '/address'))
  58. local wifitype = iwinfo.type(ifname)
  59. if wifitype ~= nil then
  60. interfaces[address] = { ifname = ifname, iw = iwinfo[wifitype] }
  61. end
  62. end)
  63. end
  64. end
  65. return interfaces
  66. end
  67. function get_wifi_neighbours()
  68. local wifi = {}
  69. for address, iface in pairs(interfaces()) do
  70. wifi[address] = { neighbours = neighbours(iface) }
  71. end
  72. if next(wifi) then
  73. return wifi
  74. end
  75. end