rule.lua 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. -- ------ extra functions ------ --
  2. function ruleCheck() -- determine if rules needs a proper protocol configured
  3. uci.cursor():foreach("mwan3", "rule",
  4. function (section)
  5. local sourcePort = ut.trim(sys.exec("uci -p /var/state get mwan3." .. section[".name"] .. ".src_port"))
  6. local destPort = ut.trim(sys.exec("uci -p /var/state get mwan3." .. section[".name"] .. ".dest_port"))
  7. if sourcePort ~= "" or destPort ~= "" then -- ports configured
  8. local protocol = ut.trim(sys.exec("uci -p /var/state get mwan3." .. section[".name"] .. ".proto"))
  9. if protocol == "" or protocol == "all" then -- no or improper protocol
  10. error_protocol_list = error_protocol_list .. section[".name"] .. " "
  11. end
  12. end
  13. end
  14. )
  15. end
  16. function ruleWarn() -- display warning messages at the top of the page
  17. if error_protocol_list ~= " " then
  18. return "<font color=\"ff0000\"><strong>" .. translate("WARNING: Some rules have a port configured with no or improper protocol specified! Please configure a specific protocol!") .. "</strong></font>"
  19. else
  20. return ""
  21. end
  22. end
  23. -- ------ rule configuration ------ --
  24. dsp = require "luci.dispatcher"
  25. sys = require "luci.sys"
  26. ut = require "luci.util"
  27. error_protocol_list = " "
  28. ruleCheck()
  29. m5 = Map("mwan3", translate("MWAN Rule Configuration"),
  30. ruleWarn())
  31. m5:append(Template("mwan/config_css"))
  32. mwan_rule = m5:section(TypedSection, "rule", translate("Traffic Rules"),
  33. translate("Rules specify which traffic will use a particular MWAN policy based on IP address, port or protocol<br />" ..
  34. "Rules are matched from top to bottom. Rules below a matching rule are ignored. Traffic not matching any rule is routed using the main routing table<br />" ..
  35. "Traffic destined for known (other than default) networks is handled by the main routing table. Traffic matching a rule, but all WAN interfaces for that policy are down will be blackholed<br />" ..
  36. "Names may contain characters A-Z, a-z, 0-9, _ and no spaces<br />" ..
  37. "Rules may not share the same name as configured interfaces, members or policies"))
  38. mwan_rule.addremove = true
  39. mwan_rule.anonymous = false
  40. mwan_rule.dynamic = false
  41. mwan_rule.sectionhead = translate("Rule")
  42. mwan_rule.sortable = true
  43. mwan_rule.template = "cbi/tblsection"
  44. mwan_rule.extedit = dsp.build_url("admin", "network", "mwan", "configuration", "rule", "%s")
  45. function mwan_rule.create(self, section)
  46. TypedSection.create(self, section)
  47. m5.uci:save("mwan3")
  48. luci.http.redirect(dsp.build_url("admin", "network", "mwan", "configuration", "rule", section))
  49. end
  50. src_ip = mwan_rule:option(DummyValue, "src_ip", translate("Source address"))
  51. src_ip.rawhtml = true
  52. function src_ip.cfgvalue(self, s)
  53. return self.map:get(s, "src_ip") or "&#8212;"
  54. end
  55. src_port = mwan_rule:option(DummyValue, "src_port", translate("Source port"))
  56. src_port.rawhtml = true
  57. function src_port.cfgvalue(self, s)
  58. return self.map:get(s, "src_port") or "&#8212;"
  59. end
  60. dest_ip = mwan_rule:option(DummyValue, "dest_ip", translate("Destination address"))
  61. dest_ip.rawhtml = true
  62. function dest_ip.cfgvalue(self, s)
  63. return self.map:get(s, "dest_ip") or "&#8212;"
  64. end
  65. dest_port = mwan_rule:option(DummyValue, "dest_port", translate("Destination port"))
  66. dest_port.rawhtml = true
  67. function dest_port.cfgvalue(self, s)
  68. return self.map:get(s, "dest_port") or "&#8212;"
  69. end
  70. proto = mwan_rule:option(DummyValue, "proto", translate("Protocol"))
  71. proto.rawhtml = true
  72. function proto.cfgvalue(self, s)
  73. return self.map:get(s, "proto") or "all"
  74. end
  75. sticky = mwan_rule:option(DummyValue, "sticky", translate("Sticky"))
  76. sticky.rawhtml = true
  77. function sticky.cfgvalue(self, s)
  78. if self.map:get(s, "sticky") == "1" then
  79. stickied = 1
  80. return translate("Yes")
  81. else
  82. stickied = nil
  83. return translate("No")
  84. end
  85. end
  86. timeout = mwan_rule:option(DummyValue, "timeout", translate("Sticky timeout"))
  87. timeout.rawhtml = true
  88. function timeout.cfgvalue(self, s)
  89. if stickied then
  90. local timeoutValue = self.map:get(s, "timeout")
  91. if timeoutValue then
  92. return timeoutValue .. "s"
  93. else
  94. return "600s"
  95. end
  96. else
  97. return "&#8212;"
  98. end
  99. end
  100. ipset = mwan_rule:option(DummyValue, "ipset", translate("IPset"))
  101. ipset.rawhtml = true
  102. function ipset.cfgvalue(self, s)
  103. return self.map:get(s, "ipset") or "&#8212;"
  104. end
  105. use_policy = mwan_rule:option(DummyValue, "use_policy", translate("Policy assigned"))
  106. use_policy.rawhtml = true
  107. function use_policy.cfgvalue(self, s)
  108. return self.map:get(s, "use_policy") or "&#8212;"
  109. end
  110. errors = mwan_rule:option(DummyValue, "errors", translate("Errors"))
  111. errors.rawhtml = true
  112. function errors.cfgvalue(self, s)
  113. if not string.find(error_protocol_list, " " .. s .. " ") then
  114. return ""
  115. else
  116. return "<span title=\"" .. translate("No protocol specified") .. "\"><img src=\"/luci-static/resources/cbi/reset.gif\" alt=\"error\"></img></span>"
  117. end
  118. end
  119. return m5