webadmin.lua 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. -- Copyright 2008 Steven Barth <steven@midlink.org>
  2. -- Copyright 2008-2015 Jo-Philipp Wich <jow@openwrt.org>
  3. -- Licensed to the public under the Apache License 2.0.
  4. module("luci.tools.webadmin", package.seeall)
  5. local util = require "luci.util"
  6. local uci = require "luci.model.uci"
  7. local ip = require "luci.ip"
  8. function byte_format(byte)
  9. local suff = {"B", "KB", "MB", "GB", "TB"}
  10. for i=1, 5 do
  11. if byte > 1024 and i < 5 then
  12. byte = byte / 1024
  13. else
  14. return string.format("%.2f %s", byte, suff[i])
  15. end
  16. end
  17. end
  18. function date_format(secs)
  19. local suff = {"min", "h", "d"}
  20. local mins = 0
  21. local hour = 0
  22. local days = 0
  23. secs = math.floor(secs)
  24. if secs > 60 then
  25. mins = math.floor(secs / 60)
  26. secs = secs % 60
  27. end
  28. if mins > 60 then
  29. hour = math.floor(mins / 60)
  30. mins = mins % 60
  31. end
  32. if hour > 24 then
  33. days = math.floor(hour / 24)
  34. hour = hour % 24
  35. end
  36. if days > 0 then
  37. return string.format("%.0fd %02.0fh %02.0fmin %02.0fs", days, hour, mins, secs)
  38. else
  39. return string.format("%02.0fh %02.0fmin %02.0fs", hour, mins, secs)
  40. end
  41. end
  42. function cbi_add_networks(field)
  43. uci.cursor():foreach("network", "interface",
  44. function (section)
  45. if section[".name"] ~= "loopback" then
  46. field:value(section[".name"])
  47. end
  48. end
  49. )
  50. field.titleref = luci.dispatcher.build_url("admin", "network", "network")
  51. end
  52. function cbi_add_knownips(field)
  53. local _, n
  54. for _, n in ipairs(ip.neighbors({ family = 4 })) do
  55. if n.dest then
  56. field:value(n.dest:string())
  57. end
  58. end
  59. end
  60. function firewall_find_zone(name)
  61. local find
  62. luci.model.uci.cursor():foreach("firewall", "zone",
  63. function (section)
  64. if section.name == name then
  65. find = section[".name"]
  66. end
  67. end
  68. )
  69. return find
  70. end
  71. function iface_get_network(iface)
  72. local link = ip.link(tostring(iface))
  73. if link.master then
  74. iface = link.master
  75. end
  76. local cur = uci.cursor()
  77. local dump = util.ubus("network.interface", "dump", { })
  78. if dump then
  79. local _, net
  80. for _, net in ipairs(dump.interface) do
  81. if net.l3_device == iface or net.device == iface then
  82. -- cross check with uci to filter out @name style aliases
  83. local uciname = cur:get("network", net.interface, "ifname")
  84. if type(uciname) == "string" and uciname:sub(1,1) ~= "@" or uciname then
  85. return net.interface
  86. end
  87. end
  88. end
  89. end
  90. end