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