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