rule.lua 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. -- Copyright 2014 Aedan Renner <chipdankly@gmail.com>
  2. -- Copyright 2018 Florian Eckert <fe@dev.tdt.de>
  3. -- Licensed to the public under the GNU General Public License v2.
  4. dsp = require "luci.dispatcher"
  5. uci = require "uci"
  6. function ruleCheck()
  7. local rule_error = {}
  8. uci.cursor():foreach("mwan3", "rule",
  9. function (section)
  10. rule_error[section[".name"]] = false
  11. local uci = uci.cursor(nil, "/var/state")
  12. local sourcePort = uci:get("mwan3", section[".name"], "src_port")
  13. local destPort = uci:get("mwan3", section[".name"], "dest_port")
  14. if sourcePort ~= nil or destPort ~= nil then
  15. local protocol = uci:get("mwan3", section[".name"], "proto")
  16. if protocol == nil or protocol == "all" then
  17. rule_error[section[".name"]] = true
  18. end
  19. end
  20. end
  21. )
  22. return rule_error
  23. end
  24. function ruleWarn(rule_error)
  25. local warnings = ""
  26. for i, k in pairs(rule_error) do
  27. if rule_error[i] == true then
  28. warnings = warnings .. string.format("<strong>%s</strong><br />",
  29. translatef("WARNING: Rule %s have a port configured with no or improper protocol specified!", i)
  30. )
  31. end
  32. end
  33. return warnings
  34. end
  35. m5 = Map("mwan3", translate("MWAN - Rules"),
  36. ruleWarn(ruleCheck())
  37. )
  38. mwan_rule = m5:section(TypedSection, "rule", nil,
  39. translate("Rules specify which traffic will use a particular MWAN policy<br />" ..
  40. "Rules are based on IP address, port or protocol<br />" ..
  41. "Rules are matched from top to bottom<br />" ..
  42. "Rules below a matching rule are ignored<br />" ..
  43. "Traffic not matching any rule is routed using the main routing table<br />" ..
  44. "Traffic destined for known (other than default) networks is handled by the main routing table<br />" ..
  45. "Traffic matching a rule, but all WAN interfaces for that policy are down will be blackholed<br />" ..
  46. "Names may contain characters A-Z, a-z, 0-9, _ and no spaces<br />" ..
  47. "Rules may not share the same name as configured interfaces, members or policies"))
  48. mwan_rule.addremove = true
  49. mwan_rule.anonymous = false
  50. mwan_rule.dynamic = false
  51. mwan_rule.sectionhead = translate("Rule")
  52. mwan_rule.sortable = true
  53. mwan_rule.template = "cbi/tblsection"
  54. mwan_rule.extedit = dsp.build_url("admin", "network", "mwan", "rule", "%s")
  55. function mwan_rule.create(self, section)
  56. TypedSection.create(self, section)
  57. m5.uci:save("mwan3")
  58. luci.http.redirect(dsp.build_url("admin", "network", "mwan", "rule", section))
  59. end
  60. src_ip = mwan_rule:option(DummyValue, "src_ip", translate("Source address"))
  61. src_ip.rawhtml = true
  62. function src_ip.cfgvalue(self, s)
  63. return self.map:get(s, "src_ip") or "&#8212;"
  64. end
  65. src_port = mwan_rule:option(DummyValue, "src_port", translate("Source port"))
  66. src_port.rawhtml = true
  67. function src_port.cfgvalue(self, s)
  68. return self.map:get(s, "src_port") or "&#8212;"
  69. end
  70. dest_ip = mwan_rule:option(DummyValue, "dest_ip", translate("Destination address"))
  71. dest_ip.rawhtml = true
  72. function dest_ip.cfgvalue(self, s)
  73. return self.map:get(s, "dest_ip") or "&#8212;"
  74. end
  75. dest_port = mwan_rule:option(DummyValue, "dest_port", translate("Destination port"))
  76. dest_port.rawhtml = true
  77. function dest_port.cfgvalue(self, s)
  78. return self.map:get(s, "dest_port") or "&#8212;"
  79. end
  80. proto = mwan_rule:option(DummyValue, "proto", translate("Protocol"))
  81. proto.rawhtml = true
  82. function proto.cfgvalue(self, s)
  83. return self.map:get(s, "proto") or "all"
  84. end
  85. use_policy = mwan_rule:option(DummyValue, "use_policy", translate("Policy assigned"))
  86. use_policy.rawhtml = true
  87. function use_policy.cfgvalue(self, s)
  88. return self.map:get(s, "use_policy") or "&#8212;"
  89. end
  90. return m5