nft-qos.lua 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. -- Copyright 2018 Rosy Song <rosysong@rosinson.com>
  2. -- Licensed to the public under the Apache License 2.0.
  3. module("luci.controller.nft-qos", package.seeall)
  4. function index()
  5. if not nixio.fs.access("/etc/config/nft-qos") then
  6. return
  7. end
  8. local e
  9. e = entry({"admin", "status", "realtime", "rate"}, template("nft-qos/rate"), _("Rate"), 5)
  10. e.leaf = true
  11. e.acl_depends = { "luci-app-nft-qos" }
  12. e = entry({"admin", "status", "realtime", "rate_status"}, call("action_rate"))
  13. e.leaf = true
  14. e.acl_depends = { "luci-app-nft-qos" }
  15. e = entry({"admin", "services", "nft-qos"}, cbi("nft-qos/nft-qos"), _("QoS over Nftables"), 60)
  16. e.leaf = true
  17. e.acl_depends = { "luci-app-nft-qos" }
  18. end
  19. function _action_rate(rv, n)
  20. local c = nixio.fs.access("/proc/net/ipv6_route") and
  21. io.popen("nft list chain inet nft-qos-monitor " .. n .. " 2>/dev/null") or
  22. io.popen("nft list chain ip nft-qos-monitor " .. n .. " 2>/dev/null")
  23. if c then
  24. for l in c:lines() do
  25. local _, i, p, b = l:match(
  26. '^%s+ip ([^%s]+) ([^%s]+) counter packets (%d+) bytes (%d+)'
  27. )
  28. if i and p and b then
  29. -- handle expression
  30. rv[#rv + 1] = {
  31. rule = {
  32. family = "inet",
  33. table = "nft-qos-monitor",
  34. chain = n,
  35. handle = 0,
  36. expr = {
  37. { match = { right = i } },
  38. { counter = { packets = p, bytes = b } }
  39. }
  40. }
  41. }
  42. end
  43. end
  44. c:close()
  45. end
  46. end
  47. function action_rate()
  48. luci.http.prepare_content("application/json")
  49. local data = { nftables = {} }
  50. _action_rate(data.nftables, "upload")
  51. _action_rate(data.nftables, "download")
  52. luci.http.write_json(data)
  53. end